<?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"))); ?>