<?php $f = file('./lattice/Fermi_CM_coord.csv'); // extracted from //sincro-share/Private/FERMI_CM/Fermi_CM.xls $blmSrcs = array( 'http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-srf-01.fcs.elettra.trieste.it:20000/f/radiation_protection/blm_master_linac.01(BlmConfiguration)', 'http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-srf-01.fcs.elettra.trieste.it:20000/f/radiation_protection/blm_master_linac.02(BlmConfiguration)', 'http://puma-01.elettra.eu/rchan.php?json&valueOnly&src=srv-tango-srf-01.fcs.elettra.trieste.it:20000/f/radiation_protection/blm_master_uh.01(BlmConfiguration)' ); $blm = array(); foreach ($blmSrcs as $b) { $data = json_decode(file_get_contents($b), false); foreach ($data as $d) { $k = explode('/BLM_', strtoupper($d)); $end = substr($k[1], -2); $v = ($end=='_L' || $end=='_R')? substr($k[1], 0, -2): (($end=='DN' || $end=='UP')? substr($k[1], 0, -3): $k[1]); $blm[$k[1]] = $v; } } $components = array( "Q" => "quadrupolefermi", "CBPM" => "bpm", "BPM" => "bpm", /*"ACCT0" => "cavitylinac", "ACCT" => "cavitylinac", "ACCBT" => "cavitylinac", "ID" => "wiggler", "UND" => "undulator",*/ "CHV" => "correctorfermi", "CH" => "correctorfermi", "CV" => "correctorfermi" ); // ---------------------------------------------------------------- // 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($b,$a,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>"); } $bending = array(); $json = file('./fermi_lattice.json'); foreach ($json as $r) if (strpos($r, "bending")!==false) { $b = explode('"name": "', $r); $n = explode('"', $b[1]); $bending[$n[0]] = rtrim($r); } $buf = '{<br> "linac": [<br> {<br>'; $table = '<head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> </head > <table class="table table-hover table-striped"> '; $magnets = 0; foreach($f as $i=>$r) { if ($i<1) {continue;} $c = explode(';', strtr($r, array(','=>'.'))); $comp = explode('_', $c[0]); if ($i==2) {$x = round($c[2]); $z = -round($c[3]); $buf .= ' "start": {"x": '.$x.', "z": '.($z+60000).'},<br> "chamber": {"type": "chamber"},<br> "magnets": [<br>'; } if ($comp[0]=='B' && $c[1] != 'DRIFT') { $x = round($c[2]); $z = -round($c[3]); if ($magnets>0) $buf = substr($buf, 0, strlen($buf)-5).'<br>'; $buf .= ' ]<br> },<br> {<br> "start": {"x": '.$x.', "z": '.($z+60000).'},<br>'.$bending[$c[0]].'<br> "chamber": {"type": "chamber"},<br> "magnets": [<br>'; if (($i = array_search($c[0], $blm))!==false) {$buf .= ' {"type": "blm", "position": 100, "name": "BLM_'.$i.'"},<br>'; unset($blm[$i]);} if (($i = array_search($c[0], $blm))!==false) {$buf .= ' {"type": "blm", "position": 100, "name": "BLM_'.$i.'"},<br>'; unset($blm[$i]);} $magnets = 0; } else if ($comp[0]!='DR' && !empty($comp[0])) { $magnets++; $name = $c[0]; if (strpos($name, '(')!==false) {$n = explode('(', $c[0]); $name = trim($n[0]);} $buf .= ' {"type": "'.strtr($comp[0], $components).'", "position": '.round(sqrt(($x-$c[2])**2+($z+$c[3])**2)).', "name": "'.$c[0].'"},<br>'; if (($i = array_search($name, $blm))!==false) {$buf .= ' {"type": "blm", "position": '.round(sqrt(($x-$c[2])**2+($z+$c[3])**2)).', "name": "BLM_'.$i.'"},<br>'; unset($blm[$i]);} if (($i = array_search($name, $blm))!==false) {$buf .= ' {"type": "blm", "position": '.round(sqrt(($x-$c[2])**2+($z+$c[3])**2)).', "name": "BLM_'.$i.'"},<br>'; unset($blm[$i]);} } $table .= "<tr><td>{$comp[0]}</dt><td>{$c[1]}</dt><td>".round($c[2])."</dt><td>".round($c[3])."</dt><td>".round($c[6]*1000)."</dt><td>$r</td></tr>\n"; } $buf = substr($buf, 0, strlen($buf)-5).'<br> ]<br> }<br> ]<br>}<br>'; $table .= "</table>\n<br><br><br><br>"; if ($_REQUEST['json']=='diff') { $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(); } foreach ($blm as $k=>$v) { echo "$k $v ".(strpos($buf, $v)===false? "NOK": "ok")."<br>\n"; } if (isset($_REQUEST['table'])) echo "$table\n<br><br><br><br>"; echo "<pre>$buf</pre>"; ?>