Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
$src = array(
'preinjector' => 'https://gitlab.elettra.eu/dt/machine/lattice/elettra/-/raw/main/ptblattice.m',
'booster'=> 'https://gitlab.elettra.eu/dt/machine/lattice/elettra/-/raw/main/booster_m.m',
'bts'=> 'https://gitlab.elettra.eu/dt/machine/lattice/elettra/-/raw/main/btslattice.m',
'sr'=> 'https://gitlab.elettra.eu/dt/machine/lattice/elettra/-/raw/main/elettra_strS.m'
);
$blmSrcs = array('sr'=>'http://pwma-dev.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/ecrada11/blmmaster/sr(BlmConfiguration)',
'booster'=>"http://pwma-dev.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/sa/radio_protection/blmmaster_b_a11.01(BlmConfiguration)",
'bts'=>"http://pwma-dev.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/sa/radio_protection/blmmaster_bts_a11.01(BlmConfiguration)",
'preinjector'=>"http://pwma-dev.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-sre-01.ecs.elettra.trieste.it:20000/sa/radio_protection/blmmaster_p_ptb_a11.01(BlmConfiguration)"
);
$blm = array('sr'=>array(),'booster'=>array(),'bts'=>array(),'preinjector'=>array());
foreach ($blmSrcs as $facility=>$blmSrc) {
$blms = json_decode(file_get_contents($blmSrc));
foreach ($blms as $b) {
$c = explode(':', $b);
$d = strtoupper(strtr(implode('_', array_slice(explode('_',$c[2]), 2)), array('chv'=>'ch')));
$blm[$facility][$d] = $c[2];
$n = explode('/', $c[2]);
if (isset($_REQUEST['blm']) && isset($_REQUEST['json'])) echo ' {"type": "blm", "position": xyz, "name": "'.strtoupper($n[2]).'"},<br>';
}
}
if (isset($_REQUEST['blm'])) {die("<pre>".print_r($blm, true)."</pre>\n");}
if ($_REQUEST['json']=='src') {header('Content-Type: application/json; charset=utf-8'); die(json_encode($src));}
if (isset($_REQUEST['src'])) {echo "<h2>elettra lattice sources</h2>"; foreach ($src as $s) {$a = explode('/', $s); echo "<a href='$s'>{$a[count($a)-1]}</a><br>";} exit();}
$prefix = array(
"AC" => "cavitylinac",
"B_" => "dipolebooster",
"SI" => "si",
"BP" => "bpm",
"CT" => "dcct",
"CA" => "cavity",
"CS" => "correctorbooster",
"CB" => "corrector",
"CH" => "corrector",
"CV" => "corrector",
"HC" => "corrector",
"VC" => "corrector",
"CM" => "cm",
"D_" => "drift",
"D1" => "drift",
"D2" => "drift",
"D3" => "drift",
"DK" => "drift",
"DB" => "drift",
"DC" => "drift",
"DF" => "drift",
"DM" => "drift",
"DS" => "drift",
"DP" => "drift",
"DT" => "drift",
"D0" => "drift",
"DL" => "drift",
"MO" => "drift",
"FL" => "flsc",
"ID" => "id",
"KI" => "kicker",
"KE" => "kicker",
"Q_" => "quadrupole",
"QB" => "quadrupolebooster",
"Q1" => "quadrupole",
"Q2" => "quadrupole",
"Q3" => "quadrupole",
"QD" => "quadrupole",
"QF" => "quadrupole",
"RF" => "rf",//"cavitylinac",
"SE" => "spte",
"SB" => "sextupolebooster",
"S1" => "sextupole",
"S2" => "sextupole",
"S3" => "sextupole",
"S4" => "sextupole",
"S5" => "sextupole",
"S6" => "sextupole",
"S7" => "sextupole",
"S8" => "sextupole",
"S9" => "sextupole",
"SF" => "sextupole",
"SD" => "sextupole",
"SR" => "srpm",
"SC" => "scraper",
"ST" => "strp",
"TA" => "tag",
"ZI" => "zigzag"
);
$latticeFile = empty($_REQUEST['lattice'])? './elettra_lattice.json': $_REQUEST['lattice'];
$json = file($latticeFile);
$buf = isset($_REQUEST['json'])? "<pre>{ <br>": "<table>\n";
// ----------------------------------------------------------------
// emit diff
function eval_diff($a, $b) {
$old_error_reporting = error_reporting(E_ALL);
require_once('../../lib/diff.php');
error_reporting($old_error_reporting);
$diff = new diff;
$text = $diff->inline($a,$b,2);
echo '<h3>Differences between JSON lattice and references</h3>'.count($diff->changes).' changes';
echo $text;
}
function component_diff() {
global $latticeFile;
echo '<h3>Supported components</h3><table>';
$json = json_decode(file_get_contents($latticeFile), true);
$comp = array();
foreach ($json as $devname=>$device) {
foreach ($device as $kseg=>$segment) {
foreach ($segment['magnets'] as $m) $comp[$m['type']] = true;
}
}
foreach ($comp as $component=>$v) {
if (strpos($component, '_FLSC')!==false) continue;
echo "<tr><td>$component </td><td> ".(file_exists("./components/{$component}.js")? "OK": "missing")."</td></tr>\n";
}
die("</table>");
}
// ------------------ Preinjector -------------------
$f = file($src['preinjector']);
$pos = 2200;
$skip = true;
$p = array();
$j = 0;
function initBranch() {
global $p, $j, $json;
if (empty($p)) $pbuf = ' "preinjector": [<br> {<br>'; else {$pbuf .= ' ]<br> },<br> {<br>';};
while (strpos($json[$j], '"start":')===false && $j<10000) {$j++;}
$pbuf .= $json[$j];
$j++;
if (strpos($json[$j], '"bending":')!==false) {$pbuf .= $json[$j]; $j++;}
if (strpos($json[$j], '"chamber":')!==false) {$pbuf .= $json[$j]; $j++;}
$pbuf .= ' "magnets": [<br>';
if (empty($p)) $pbuf .= ' {"type": "gun", "position": 0, "name": "Gun", "href": "https://puma-01.elettra.eu/spa/index.html?s=preinj_summary"},<br>';
$p[] = $pbuf;
}
initBranch();
foreach($f as $r) {
if (strpos($r, "marker('EXIT")!==false) {$skip = true; }
if (substr($r, 0, 2)=='B_') initBranch();
if (!$skip) $p[] = $r;
if (strpos($r, "marker('START")!==false) {$skip = false; }
}
// initBranch();
foreach($p as $r) {
if (strpos($r, '"start":')!==false) {$buf = substr($buf, 0, -5).'<br>'.$r; continue;}
$c = explode(',', $r);
if (count($c)<2) continue;
$nq = explode("'", $c[0]);
$n = explode("=", strtr($c[0],array(' '=>'','Q_'=>$nq[1].'_', 'P1_'=>'P1.', 'PTB1_'=>'PTB1.')));
$p = 0;
if (strpos($c[1], "'")===false) $p = $c[1]*1000.0;
$type = substr($n[0], 0, 2);
if (strpos($c[0], "drift")===false && !isset($prefix[$type])) $buf .= "<h3>$r</h3>\n";
$len = $p;
if ($prefix[$type] == "sextupole" && $len==230) $len = 0;
if ($prefix[$type] == "quadrupole" && $len==260) $len = 0;
if ($prefix[$type] == "quadrupole") $type = 'QB';
if ($type=='CV') continue;
if ($prefix[$type] == "corrector") $type = 'CS';
if (strpos($c[0], "ACCT")!==false) $len = $len.', "rotateY": "PI/2", "href": "https://puma-01.elettra.eu/spa/index.html?s=preinj_summary"';
if ((strpos($c[0], "drift")===false || strpos($c[0], "ACCT")!==false) && $type!='B_') $buf .= isset($_REQUEST['json'])? ' {"type": "'.$prefix[$type].'", "position": '.($pos+$p/2).($len>0? ', "length": '.$len:'').', "name": "'.$n[0].'"},<br>': "<tr><td>{$n[0]}</td><td>".($pos+$p/2)."</td><td>$p</td></tr>\n";
$pos += $p;
if ($type=='B_') {$pos = $p/2;}
}
$buf .= ' ]<br> }<br> ],<br>';
// ------------------ BOOSTER -------------------
$pos = 4800; //-2400000000000;
$booster = '';
$f = file($src['booster']);
$section = 28; $qnum = 1;
$j = 0;
foreach($f as $r) {
if (strpos($r, 'ring =')!==false || strpos($r, 'atringparam')!==false) continue;
if (strpos($r, '};')!==false) break;
$c = explode(',', $r);
if (strpos($r, 'atsbend(')!==false) {
$section++;
if ($section == 29) {$bbuf = "$booster ]<br> },<br>"; $booster = ''; $section = 1;} else $booster .= ' ]<br> },<br>';
$k = "B_B{$section}.1";
$pos += $c[1]*1000.0/2;
if (isset($_REQUEST['json'])) {while (strpos($json[$j], $k)===false && $j<10000) {$j++;} $booster .= ' {<br>'.$json[$j-1].$json[$j].$json[$j+1].' "magnets": [<br>';}
else $booster .= "</table>\nlength: $pos<h3>$k</h3>\n<table>\n";
$pos = $c[1]*1000.0/2;
if (isset($blm['booster'][$k.'_R'])) {
$blmfound = $blm['booster'][$k.'_R'];
$blmnames = explode('/', strtoupper($blm['booster'][$k.'_R']));
$booster .= ' {"type": "blm", "position": '.$pos.', "name": "'.$blmnames[2].'"},<br>';
// "<tr><td>$k.'_R'</td><td>0</td><td>$pos</td><td>$blmfound</td></tr>\n";
unset($blm['booster'][$k.'_R']);
$blmfound = $blm['booster'][$k.'_L'];
$blmnames = explode('/', strtoupper($blm['booster'][$k.'_L']));
$booster .= ' {"type": "blm", "position": '.$pos.', "name": "'.$blmnames[2].'"},<br>';
// "<tr><td>$k.'_L'</td><td>0</td><td>$pos</td><td>$blmfound</td></tr>\n";
unset($blm['booster'][$k.'_L']);
}
$qnum = 0;
}
else {
$n = explode("'", $c[0]);
$p = 0;
if (strpos($c[1], "'")===false) $p = $c[1]*1000.0;
$type = substr($n[1], 0, 2);
if (!isset($prefix[$type])) $booster .= "<h3>$r</h3>\n";
$len = $p;
if ($prefix[$type] == "sextupole" && $len==230) $len = 0;
if ($prefix[$type] == "quadrupole" && $len==260) $len = 0;
if ($prefix[$type] == "sextupole") $type = 'SB';
if ($prefix[$type] == "quadrupole") {$type = 'QB'; $qnum++;}
// if (strpos($r, "RF_B") $type = 'CB';
if (strpos($r, "atmarker('DCCT')")!==false) $type = 'CT';
if ($type=='CV') continue;
if ($prefix[$type] == "corrector") $type = 'CS';
$num = $type=='QB'? $qnum: 1;
$name = "{$n[1]}_B{$section}.$num";;
if (strpos($c[0], "drift")===false) $booster .= isset($_REQUEST['json'])? ' {"type": "'.$prefix[$type].'", "position": '.($pos+$p/2).($len>0? ', "length": '.$len:'').', "name": "'.$name.'"},<br>': "<tr><td>$name</td><td>".($pos+$p/2)."</td><td>$p</td></tr>\n";
$pos += $p;
}
}
$booster = strtr($booster.substr($bbuf,0,-5), array('},<br> ]'=>'}<br> ]', "},\n ]"=>"}\n ]"));
$buf .= (isset($_REQUEST['json'])? ' "booster": [<br>': "</table><br><br><br><br><br><h1>Booster</h1><table>\n").$booster."\n ],\n";
// ------------------ BTS -------------------
$f = file($src['bts']);
$start = false;
$btsbuf = isset($_REQUEST['json'])? ' "bts": [<br> {<br> "start": {"x": -3100, "z": -21558},<br> "magnets": [<br>': "<br><br><br><br><h1>BTS</h1><br>\n";
$j = 0;
$bending = array(
'BTS_B1' => 'B_BTS1.1',
'BTS_B2' => 'B_BTS1.2',
'BTS_B3' => 'B_BTS1.3',
'BTS_B4' => 'B_BTS2.1',
'BTS_B5' => 'B_BTS2.2',
'BTS_B6' => 'B_BTS2.3'
);
foreach($f as $r) {
if ($r{0}=='%') continue;
$eq = explode('=', $r);
$c = explode(',', $r);
if (strpos($r, "drift('BTS_D")!==false) {
$drift[trim($eq[0])] = $c[1]-0;
}
$Lquad = 0.495;
if (strpos($r, "quadrupole(")!==false) {
$quadrupole[trim($eq[0])] = $Lquad;
}
if (strpos($r, "START_BTS")!==false) {$start = true; $pos = 0; continue;}
if (strpos($r, "EXIT")!==false) {$start = false;}
if ($start) {
$sp = explode(' ', $r);
foreach ($sp as $comp) {
if (trim($comp) == '...') continue;
if (isset($drift[$comp])) $pos += $drift[$comp]*1000;
else {
if (strpos($comp, 'BTS_B')!==false && strpos($comp, 'BPM')===false) {
if (isset($_REQUEST['json'])) {while (strpos($json[$j], $bending[$comp])===false && $j<10000) {$j++;} $btsbuf .= ' ]<br> },<br> {<br>'.$json[$j-1].$json[$j].$json[$j+1].' "magnets": [<br>';}
// $btsbuf .= isset($_REQUEST['json'])? ' ]<br> "bending": {"type": "dipolebooster", "label": "show", "name": "'.trim($comp).'"},<br> "magnets": [<br>': "</table>\nlength: $pos<h3>$comp</h3>\n<table>\n";
$pos = $comp=='BTS_B6'? 1217: 737;
echo "{$bending[$comp]}_R<br>";
if (isset($blm['bts'][$bending[$comp].'_R'])) {
$blmfound = $blm['bts'][$bending[$comp].'_R'];
$blmnames = explode('/', strtoupper($blm['bts'][$bending[$comp].'_R']));
$btsbuf .= ' {"type": "blm", "position": '.$pos.', "name": "'.$blmnames[2].'"},<br>';
// "<tr><td>$comp.'_R'</td><td>0</td><td>$pos</td><td>$blmfound</td></tr>\n";
unset($blm['bts'][$bending[$comp].'_R']);
$blmfound = $blm['bts'][$bending[$comp].'_L'];
$blmnames = explode('/', strtoupper($blm['bts'][$bending[$comp].'_L']));
$btsbuf .= ' {"type": "blm", "position": '.$pos.', "name": "'.$blmnames[2].'"},<br>';
// "<tr><td>$comp.'_L'</td><td>0</td><td>$pos</td><td>$blmfound</td></tr>\n";
unset($blm['bts'][$bending[$comp].'_L']);
}
}
else {
$type = isset($quadrupole[$comp])? 'quadrupolebooster': (strpos($comp, 'BPM')!==false? 'bpm': $comp);
if ($comp=='CH' || $comp=='CV') $type = 'corrector';
$btsbuf .= isset($_REQUEST['json'])? ' {"type": "'.$type.'", "position": '.$pos.', "name": "'.$comp.'"},<br>': "<tr><td>{$comp}</td><td>".$pos."</td></tr>\n";
if (isset($blm['bts'][$comp.'_R'])) {
$blmfound = $blm['bts'][$comp.'_R'];
$blmnames = explode('/', strtoupper($blm['bts'][$comp.'_R']));
$btsbuf .= ' {"type": "blm", "position": '.$pos.', "name": "'.$blmnames[2].'"},<br>';
// "<tr><td>$comp.'_R'</td><td>0</td><td>$pos</td><td>$blmfound</td></tr>\n";
unset($blm['bts'][$comp.'_R']);
$blmfound = $blm['bts'][$comp.'_L'];
$blmnames = explode('/', strtoupper($blm['bts'][$comp.'_L']));
$btsbuf .= ' {"type": "blm", "position": '.$pos.', "name": "'.$blmnames[2].'"},<br>';
// "<tr><td>$comp.'_L'</td><td>0</td><td>$pos</td><td>$blmfound</td></tr>\n";
unset($blm['bts'][$comp.'_L']);
}
}
}
}
}
}
$btsbuf .= ' ]<br> },<br> {<br> "start": {"x": 38740, "z": -13180},<br> "bending": {"rotateY": "PI"},<br> "chamber": {"type": "chamber"},<br> "magnets": [<br> ]<br> }<br> ],<br>';
$buf .= strtr($btsbuf, array('},<br> ]'=>'}<br> ]'));
// ------------------ SR -------------------
$f = file($src['sr']);
$k = 'B_S12.1';
$pos = 6996;
$buf .= isset($_REQUEST['json'])? ' "sr": [<br>': "</table><br><br><br><br><br><h1>SR</h1><table>\n";
$srbuf = '';
foreach($f as $r) {
$c = explode(',', $r);
if (count($c)<2) continue;
if (strpos($r, 'atrbend(')!==false) {
$k = strtr($c[0], array("atrbend("=>'', "'"=>''));
$pos += $c[1]*1000.0/2;
if (strpos($r, "atrbend('B_S1.1")!==false) {$srtail = $srbuf; $srbuf = '';}
if (isset($_REQUEST['json'])) {$j=0; while (strpos($json[$j], trim($k))===false && $j<10000) {$j++;} $srbuf .= (strpos($r, "atrbend('B_S1.1")===false? ' ]<br> },<br>':'').' {<br>'.$json[$j-1].$json[$j].$json[$j+1].' "magnets": [<br>';}
else $srbuf .= isset($_REQUEST['json'])? ' ]<br> "bending": {"type": "dipole", "label": "show", "name": "'.trim($k).'"},<br> "magnets": [<br>': "</table>\nlength: $pos<h3>$k</h3>\n<table>\n";
$pos = $c[1]*1000.0/2;
}
else {
$n = explode("'", $c[0]);
$p = 0;
if (strpos($c[1], "'")===false) $p = $c[1]*1000.0;
$type = substr($n[1], 0, 2);
if (!isset($prefix[$type])) $srbuf .= "<h3>$r</h3>\n";
$len = $p;
if ($prefix[$type] == "sextupole" && $len==230) $len = 0;
if ($prefix[$type] == "quadrupole" && $len==260) $len = 0;
if ($type=='CV') continue;
$blmfound = '';
if (isset($blm['sr'][$n[1]])) {$blmfound = $blm['sr'][$n[1]]; $blmnames = explode('/', strtoupper($blm['sr'][$n[1]]));}
if (strpos($c[0], "drift")===false) $srbuf .= isset($_REQUEST['json'])?
' {"type": "'.$prefix[$type].'", "position": '.($pos+$p/2).($len>0? ', "length": '.$len:'').', "name": "'.$n[1].'"},<br>'.
(isset($blm['sr'][$n[1]])? ' {"type": "blm", "position": '.($pos+$p/2).', "name": "'.$blmnames[2].'"},<br>': ''):
"<tr><td>{$n[1]}</td><td>".($pos+$p/2)."</td><td>$p</td><td>$blmfound</td></tr>\n";
if (isset($blm['sr'][$n[1]])) {unset($blm['sr'][$n[1]]);}
$pos += $p;
}
}
$srbuf .= $srtail.(isset($_REQUEST['json'])? " ]<br> }<br> ]<br>}</pre>": "</table>\n");
$buf .= strtr($srbuf,array('},<br> ]'=>'}<br> ]', "},\n ]"=>"}\n ]"));
$envelope = $bpm = false;
if ($_REQUEST['json']=='diff') {
foreach ($json as $i=>$j) {
$json[$i] = rtrim($j);
if (strpos($j,'undulator')!==false || strpos($j,"wiggler")!==false) unset($json[$i]);
if (strpos($j,'envelope')!==false) {
if (strpos($j,'{')!==false && strpos($j,'}')===false) $envelope = true;
unset($json[$i]);
}
else if ($envelope) {
if (strpos($j,'}')!==false) $envelope = false;
unset($json[$i]);
}
if (strpos($j,'"bpm": ')!==false) {
if (strpos($j,'{')!==false && strpos($j,'}')===false) $bpm=3;
unset($json[$i]);
}
else if ($bpm>0) {
if (strpos($j,'}')!==false) $bpm--;
unset($json[$i]);
}
}
$b = explode("\n", strtr($buf, array('<br>'=>"\n", '<pre>'=>'', '</pre>'=>'', "},\n ]"=>"}\n ]")));
foreach ($b as $i=>$j) $b[$i] = rtrim($j);
eval_diff($json, $b);
component_diff();
}
$blmend = '';
if (isset($_REQUEST['blmend'])) die("<h2>BLM</h2><pre>".print_r($blm, true)."</pre>");
die(strtr($buf, array('<br>'=>"\n")));
?>