Skip to content
Snippets Groups Projects
simulator.php 6.84 KiB
Newer Older
Lucio Zambon's avatar
Lucio Zambon committed
<?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'];
Lucio Zambon's avatar
Lucio Zambon committed
	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";
Lucio Zambon's avatar
Lucio Zambon committed
	if ($machine=="esrf") $src = "srv-tango-ctrl-01:20000/sr/sim/esrf";
	if ($machine=="soleil") $src = "srv-tango-ctrl-01:20000/sr/sim/soleil";
Lucio Zambon's avatar
Lucio Zambon committed
	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);
			}
		}
Lucio Zambon's avatar
Lucio Zambon committed
		if ($machine=='soleil') {
Lucio Zambon's avatar
Lucio Zambon committed
			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') {
Lucio Zambon's avatar
Lucio Zambon committed
			$b = 0;
			$comp = $buf = array();
			foreach ($f as $k=>$v) {
Lucio Zambon's avatar
Lucio Zambon committed
				if ($v == 'BEND' || $v == 'B1H') {$b++; $comp[$b] = array(); $f[$k] = $v.'_S'.$b.'_1'; continue;}
Lucio Zambon's avatar
Lucio Zambon committed
				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];
			}
		}
Lucio Zambon's avatar
Lucio Zambon committed
		return $f;
	}
	if (isset($_REQUEST['src'])) {
		die(json_encode(fname($machine)));
Lucio Zambon's avatar
Lucio Zambon committed
	}
	if (isset($_REQUEST['spos'])) {
		die(file_get_contents("http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=$src/spos"));
	}
	if (isset($_REQUEST['angles'])) {
Lucio Zambon's avatar
Lucio Zambon committed
		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";
Lucio Zambon's avatar
Lucio Zambon committed
		}
Lucio Zambon's avatar
Lucio Zambon committed
		die(substr($lattice, 0, -2)."\n		]\n	}\n}\n");
Lucio Zambon's avatar
Lucio Zambon committed
	}
Lucio Zambon's avatar
Lucio Zambon committed
	$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;}
Lucio Zambon's avatar
Lucio Zambon committed
	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].']}}');
		}
	}
Lucio Zambon's avatar
Lucio Zambon committed
	$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.'}}');
?>