Newer
Older
<?php
if (!isset($_REQUEST['debug'])) header('Content-type: application/json; charset=utf-8');
$src = "srv-tango-sre-01.ecs.elettra.trieste.it:20000/sr/DIAGNOSTICS/bpmid_new";
$machine = empty($_REQUEST['machine'])? 'elettra': $_REQUEST['machine'];
if (strpos($machine, 'simulator_')!==false) {list($trash, $machine) = explode('_', $machine, 2);}
if ($machine=="elettra2") $src = "srv-tango-ctrl-01:20000/sr/sim/srElettra2_high_betax_long_straights";
if ($machine=="esrf") $src = "srv-tango-ctrl-01:20000/sr/sim/esrf";
if ($machine=="soleil") $src = "srv-tango-ctrl-01:20000/sr/sim/soleil";
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
function angles($machine) {
global $src;
$fname = fname($machine);
$f = json_decode(file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/bending_angle"), true);
$a = $n = array();
foreach ($f as $k=>$v) {
if ($v>0.005) {
$a[] = $v;
$n[] = $fname[$k];
}
}
return array($a, $n);
}
function angles2($machine) {
if ($machine=='esrf') {
$n = array();
for ($i=1; $i<=32; $i++) {$h1[] = "B1H_S{$i}_1"; $h2[] = "B2H_S{$i}_1"; $s1[] = "B1S_S{$i}_1"; $s2[] = "B2S_S{$i}_1";}
return array(array_fill(0, 64, 0.0923248)+array_fill(64, 64, 0.00585), array_merge($h1, $h2, $s1, $s2));
}
if ($machine=='soleil') {
$n = array();
for ($i=1; $i<=32; $i++) $n[] = "BEND_S{$i}_1";
return array(array_fill(0, 32, 0.196349540849362),$n);
}
}
function types($machine) {
global $src;
return json_decode(strtolower(file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/classname")), true);
}
function types2($machine, $f) {
if ($machine=='esrf') {
foreach ($f as $k=>$v) {
if (strpos($v, 'SD')===0) $f[$k] = 'drift';
else if (strpos($v, 'S')===0) $f[$k] = 'sextupole';
else if (strpos($v, 'Q')===0) $f[$k] = 'quadrupole';
else if (strpos($v, 'BPM')===0) $f[$k] = 'bpm';
else list($f[$k], $trash) = explode('_', strtolower($v), 2);
}
}
foreach ($f as $k=>$v) {
if (strpos($v, 'SD')===0) $f[$k] = 'drift';
else if (strpos($v, 'SkewQuad')===0) $f[$k] = 'quadrupole';
else if (strpos($v, 'S')===0) $f[$k] = 'sextupole';
else if (strpos($v, 'Q')===0) $f[$k] = 'quadrupole';
else if (strpos($v, 'BPM')===0) $f[$k] = 'bpm';
else list($f[$k], $trash) = explode('_', strtolower($v), 2);
}
}
return $f;
}
function fname($machine) {
global $src;
$f = json_decode(file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/fname"), true);
if ($machine=='soleil'|| $machine=='esrf') {
$b = 0;
$comp = $buf = array();
foreach ($f as $k=>$v) {
if ($v == 'BEND' || $v == 'B1H') {$b++; $comp[$b] = array(); $f[$k] = $v.'_S'.$b.'_1'; continue;}
if ($b<1) {$buf[] = $v; continue;}
if (!isset($comp[$b][$v])) $comp[$b][$v] = 1; else $comp[$b][$v]++;
$f[$k] = $v.'_S'.$b.'_'.$comp[$b][$v];
}
foreach ($buf as $k=>$v) {
if (!isset($comp[$b][$v])) $comp[$b][$v] = 1; else $comp[$b][$v]++;
$f[$k] = $v.'_S'.$b.'_'.$comp[$b][$v];
}
}
return $f;
}
if (isset($_REQUEST['src'])) {
die(json_encode(fname($machine)));
}
if (isset($_REQUEST['spos'])) {
die(file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/spos"));
}
if (isset($_REQUEST['angles'])) {
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
die(json_encode(angles($machine)));
}
if (isset($_REQUEST['angles2'])) {
die(json_encode(angles2($machine)));
}
if (isset($_REQUEST['types'])) {
die(json_encode(types($machine)));
}
if (isset($_REQUEST['types2'])) {
$fname = fname($machine);
die(json_encode(types($machine, $fname)));
}
if (isset($_REQUEST['lattice'])) {
$b = 0;
$a = $comp = $buf = $bpos = array();
$fname = fname($machine);
$types = types($machine);
$angles = angles($machine);
foreach ($angles[0] as $k=>$v) {$a[$angles[1][$k]] = $v;}
$pos = json_decode(file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/spos"));
$posx = $posz = $alpha = 0;
// die(json_encode($pos));
foreach ($fname as $k=>$v) {
// echo "$v<br>\n";
if (strpos($v, 'BEND')!==false || strpos($v, 'B1H')!==false || strpos($v, 'B2H')!==false || strpos($v, 'B1S')!==false || strpos($v, 'B2S')!==false) {
if ($b==0) {$posx += $pos[$k]; } else {
$posx += ($pos[$k] - $posb) * cos($alpha);
$posz += ($pos[$k] - $posb) * sin($alpha);
}
$posb = $pos[$k];
$alpha += $a[$v];
$b++;
$bpos[$b] = array($posx, $posz, $v);
$comp[$b] = array();
continue;
}
if ($b<1) {$buf[] = array($v, round(($pos[$k] - $posb)*1000), $types[$k]); continue;}
$comp[$b][] = array($v, round(($pos[$k] - $posb)*1000), $types[$k]);
}
foreach ($buf as $v) {$comp[$b][] = array($v[0], round($v[1] - $bpos[$b][0]*1000), $v[2]);}
// die(json_encode($bpos));
$offsetx = ($bpos[1][0] + $bpos[count($bpos)/2+1][0]) / 2;
$offsetz = ($bpos[1][1] + $bpos[count($bpos)/2+1][1]) / 2;
// die("$offsetx, $offsetz");
foreach ($bpos as $k=>$v) {$bpos[$k][0] = round(($v[0] - $offsetx)*1000); $bpos[$k][1] = round(($v[1] - $offsetz)*1000);}
$lattice = "{\n \"conf\": {\"modules\": [\"simulator\"]},\n \"sr\": {\n \"sections\": [\n";
foreach ($bpos as $k=>$v) {
$lattice .= " {\n \"start\": {\"x\": {$v[0]}, \"z\": {$v[1]}},
\"bending\": {\"type\": \"bending\", \"label\": \"show\", \"name\": \"{$v[2]}\"},
\"chamber\": {\"type\": \"chamber\"},
\"components\": [\n";
foreach ($comp[$k] as $i=>$v) {
$type = $v[2];
$comma = $i<count($comp[$k])-1? ',': '';
$lattice .= " {\"type\": \"$type\", \"position\": {$v[1]}, \"name\": \"{$v[0]}\"}$comma\n";
}
$lattice .= " ]\n },\n";
$xvar = 'x';
$yvar = 'y';
if (!empty($_REQUEST['param']) && in_array($_REQUEST['param'], array('beta', 'eta', 'mu', 'sigma'))) {$xvar = $_REQUEST['param'].'_'.$xvar; $yvar = $_REQUEST['param'].'_'.$yvar;}
if (file_exists("/run/panther/simulator.100.$machine.src")) {
list($timestamp, $s) = explode(':',file_get_contents("/run/panther/simulator.100.$machine.src"), 2);
$d = explode(',', $s);
$i = array_search("$src/$xvar", $d);
if ($i!==false) {
list($timestamp, $ds) = explode(':',file_get_contents("/run/panther/simulator.100.$machine"), 2);
$data = explode(';', $ds);
die('{"sr":{"Hor":['.$data[$i].'],"Ver":['.$data[$i+1].']}}');
}
}
$x = file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/$xvar");
$y = file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/$yvar");
// if ($yvar=='eta_y') {$valx = json_decode($x, true); foreach($valx as $i=>$v) {if ($i!=510) $valx[$i] = 0;} $x = json_encode($valx);}
if (isset($_REQUEST['debug'])) {echo "$xvar $yvar<br><br> $x<br><br>$y";}
die('{"sr":{"Hor":'.$x.',"Ver":'.$y.'}}');
?>