Skip to content
Snippets Groups Projects
Commit b5ea043f authored by Lucio Zambon's avatar Lucio Zambon
Browse files

Update simulator.php

parent 968f988c
No related branches found
No related tags found
No related merge requests found
......@@ -2,16 +2,69 @@
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 ($machine=="elettra2") $src = "srv-tango-ctrl-01:20000/sr/sim/srElettra2_9_4_beta";
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";
if (isset($_REQUEST['src'])) {
$f = json_decode(file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/fname"), true);
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);
}
}
if ($machine=='soleil') {
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') {$b++; $comp[$b] = array(); $f[$k] = $v.'_S'.$b.'_1'; continue;}
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];
......@@ -21,50 +74,93 @@
$f[$k] = $v.'_S'.$b.'_'.$comp[$b][$v];
}
}
die(json_encode($f));
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'])) {
if ($machine=='esrf') die(json_encode(array(array_fill(0, 64, 0.0923248)+array_fill(64, 64, 0.00585))));
if ($machine=='soleil') {
$n = array();
for ($i=1; $i<=32; $i++) $n[] = "BEND_S$i";
die(json_encode(array(array_fill(0, 32, 0.196349540849362),$n)));
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";
}
die(substr($lattice, 0, -2)."\n ]\n }\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.'}}');
$url = "http://puma-01.elettra.eu/panther/updater.php?read&filename=";
if (strpos($_SERVER['HTTP_HOST'], 'puma-01')!==false) $url = "/run/panther/";
if ($machine != 'elettra' && $machine != 'fermi') die('Error, unsupported machine');
if (isset($_REQUEST['src'])) {
$confsrc = "$url$machine.100.bpm.src";
list($trash, $data) = explode(':', file_get_contents($confsrc), 2);
$data = explode(',', $data);
$a = array_merge(...json_decode($data, true));
die(json_encode($data));
}
$data = strtr(file_get_contents("$url$machine.100.bpm"), ['null'=>'0']);
if (!isset($_REQUEST['debug'])) while (strlen($data)>1 && !is_numeric(substr($data, -1))) $data = substr($data, 0, strlen($data)-1);
list($trash, $datasrc) = explode(':', $data,2);
if (isset($_REQUEST['debug'])) die("url: $url$machine.100.bpm<br><br>data:<br>$data<br><br>datasrc:<br>$datasrc<br>");
if (strpos($_SERVER['HTTP_HOST'], 'puma-01')!==false && $machine=='elettra') {
$d = explode(';', $datasrc);
die("{\"preinjector\":{\"Hor\":[".implode(',', array_slice($d, 0, 7)).'],"Ver":['.implode(',', array_slice($d, 7, 7))."]},\"bts\":{\"Hor\":[".implode(',', array_slice($d, 14, 10)).'],"Ver":['.implode(',', array_slice($d, 24, 10))."]},\"sr\":{\"Hor\":[".$d[34].'],"Ver":['.$d[35]."]}}");
}
if (strpos($_SERVER['HTTP_HOST'], 'puma-01')!==false && $machine=='fermi') {
$d = explode(';', $datasrc);
foreach ($d as $i=>$v) if ($v=='') $d[$i] = 'null';
die("{\"linac\":{\"Hor\":[".implode(',', array_slice($d, 0, 35)).'],"Ver":['.implode(',', array_slice($d, 35, 35))."]},\"fel1\":{\"Hor\":[".implode(',', array_slice($d, 70, 15)).'],"Ver":['.implode(',', array_slice($d, 85, 15))."]},\"fel2\":{\"Hor\":[".implode(',', array_slice($d, 100, 5)).'],"Ver":['.implode(',', array_slice($d, 105, 5))."]}}");
}
die(strtr(file_get_contents("$url$machine.100.bpm"), ['null'=>'0']));
?>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment