<html>
<head>
<title>KATRIN Spectrometer and Detector Section Status Overview</title>
<style type="text/css">
	body      { font-family:Calibri,Arial; }
	a:link    { text-decoration:none; }
	a:visited { text-decoration:none; }
	a:hover   { text-decoration:none; }
	a:active  { text-decoration:none; }
	a:focus   { text-decoration:none; }
</style>

<?php
  // get client ip adress to check whether computer is on-site or not

  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
      $ip = $_SERVER['HTTP_CLIENT_IP'];
      } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
          $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
          } else { $ip = $_SERVER['REMOTE_ADDR']; }

$onsite = false;
$slowrefresh = false;
if (substr($ip, 0, 6) == "141.52") { echo '<meta http-equiv="refresh" content="60">'; $onsite = true; }
else { echo '<meta http-equiv="refresh" content="300">'; $slowrefresh= true; }

?>

</head>
<body>



<?php


	function placePic($posY, $posX, $pic, $size, $title)
	{
		echo '<div title="' . $title . '" style="position:absolute; top:' . $posY . 'px; left:' . $posX . 'px">';
		echo '<img src="pics/' . $pic . '" width="' . $size . '" height="' . $size . '">';
		echo '</div>';
	}
	

	function placePicWithLink($posY, $posX, $pic, $size, $title, $link)
	{
		echo '<a href="https://katrin.kit.edu/adei-katrin/#module=graph&db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&db_mask=all&experiment=-&window=86400&module=graph&virtual=srctree&srctree=' . $link . '" target="_blank">';
		placePic($posY, $posX, $pic, $size, $title);
		echo '</a>';
	}

	function placeText($posY, $posX, $text, $width, $size, $color)
	{
		echo '<div align="center" style="width:' . $width . 'px; position:absolute; top:' . $posY . 'px; left:' . $posX . 'px">';
		echo '<font size="' . $size . '" color="' . $color . '"><b>';
		echo $text;
		echo '</b></font></div>';
	}

	function placeTextWithLink($posY, $posX, $text, $width, $size, $color, $title, $link)
	{
		echo '<div title="' . $title . '" align="center" style="width:' . $width . 'px; position:absolute; top:' . $posY . 'px; left:' . $posX . 'px">';
		echo '<a href="https://katrin.kit.edu/adei-katrin/#module=graph&db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&db_mask=all&experiment=-&window=86400&module=graph&virtual=srctree&srctree=' . $link . '" target="_blank">';
		echo '<font size="' . $size . '" color="' . $color . '"><b>';
		echo $text;
		echo '</b></font></a></div>';
	}

	function placeTextWithWebsiteLink($posY, $posX, $text, $width, $size, $color, $title, $link)
	{
		echo '<div title="' . $title . '" align="center" style="width:' . $width . 'px; position:absolute; top:' . $posY . 'px; left:' . $posX . 'px">';
		echo '<a href='.$link.'>';
		echo '<font size="' . $size . '" color="' . $color . '"><b>';
		echo $text;
		echo '</b></font></a></div>';
	}
		
	function placeBox($posY, $posX, $text, $width, $size, $color)
	{
		echo '<div align="center" style="background-color:' . $color . '; border-style:solid; border-width:2px; border-color:#000000; width:' . $width . 'px; position:absolute; top:' . $posY . 'px; left:' . $posX . 'px">';
		echo '<font size="' . $size . '" color=#000000><b>';
		echo $text;
		echo '</b></font></div>';
	}

	function getValveStatus($value)
	{
		$value = decbin($value);
		$value = substr("00000000000", 0, 11 - strlen($value)) . $value;
		if (substr($value, 3, 1) == 1) return 1;
		else if (substr($value, 2, 1) == 1) return 0;
		else return -1;
	}

?>



<?php

	// ########## SLOW CONTROL CHANNELS ##########

	$adeiChannel = array(
		array(
			"aircoils__aircoils_rep__0__2",   //EMCS currents
			"aircoils__aircoils_rep__0__3",
			"aircoils__aircoils_rep__0__4",
			"aircoils__aircoils_rep__0__5",
			"aircoils__aircoils_rep__0__6",
			"aircoils__aircoils_rep__0__7",
			"aircoils__aircoils_rep__0__8",
			"aircoils__aircoils_rep__0__9",
			"aircoils__aircoils_rep__0__10",
			"aircoils__aircoils_rep__0__11",
			"aircoils__aircoils_rep__0__12",
			"aircoils__aircoils_rep__0__13",
			"aircoils__aircoils_rep__0__14",
			"aircoils__aircoils_rep__0__15",
			"aircoils__aircoils_rep__0__16",
			"aircoils__aircoils_rep__0__17"
		),
		array(
			"aircoils__aircoils_rep__0__34",   //EMCS Status
			"aircoils__aircoils_rep__0__35",
			"aircoils__aircoils_rep__0__36",
			"aircoils__aircoils_rep__0__37",
			"aircoils__aircoils_rep__0__38",
			"aircoils__aircoils_rep__0__39",
			"aircoils__aircoils_rep__0__40",
			"aircoils__aircoils_rep__0__41",
			"aircoils__aircoils_rep__0__42",
			"aircoils__aircoils_rep__0__43",
			"aircoils__aircoils_rep__0__44",
			"aircoils__aircoils_rep__0__45",
			"aircoils__aircoils_rep__0__46",
			"aircoils__aircoils_rep__0__47",
			"aircoils__aircoils_rep__0__48",
			"aircoils__aircoils_rep__0__49"
		),
		array(
			"aircoils__aircoils_rep__0__86",   // LEM Status
			"aircoils__aircoils_rep__0__87",
			"aircoils__aircoils_rep__0__88",
			"aircoils__aircoils_rep__0__89",
			"aircoils__aircoils_rep__0__90",
			"aircoils__aircoils_rep__0__91",
			"aircoils__aircoils_rep__0__92",
			"aircoils__aircoils_rep__0__93",
			"aircoils__aircoils_rep__0__94",
			"aircoils__aircoils_rep__0__95",
			"aircoils__aircoils_rep__0__96",
			"aircoils__aircoils_rep__0__97",
			"aircoils__aircoils_rep__0__98",
			"aircoils__aircoils_rep__0__99",
			"aircoils__aircoils_rep__0__100",
			"aircoils__aircoils_rep__0__101",
                        "aircoils__aircoils_rep__0__102"   // HV dipole trigger status
		),
		array(
			"aircoils__aircoils_rep__0__82",  //PS1 + PS2 current 
			"aircoils__aircoils_rep__0__83",
			"aircoils__aircoils_rep__0__84",  //PS1 + PS2 Status
			"aircoils__aircoils_rep__0__85"
		),
		array(
			"fpd__katrin_rep__0__153",
			"fpd__katrin_rep__0__158"
		),
	        // Baffle vacuum  (index 5, former egun part)
		array(
                        "csms__ControlSystem_MS__432VAK_Kryoleitung__9"
		),
		array(
			"fpd__katrin_rep__0__43",
			"fpd__katrin_rep__0__66",
			"fpd__katrin_rep__0__80",
			"fpd__katrin_rep__0__99",
			"fpd__katrin_rep__0__112",
			"fpd__katrin_rep__0__113",
			"fpd__katrin_rep__0__116",
			"fpd__katrin_rep__0__117",
			"fpd__katrin_rep__0__118",
			"fpd__katrin_rep__0__60",
			"fpd__katrin_rep__0__62",
			"fpd__katrin_rep__0__114",
			"fpd__katrin_rep__0__45"
		),
		array(
			"csms__ControlSystem_MS__432VAK_PumpPort2__33",
			"csms__ControlSystem_MS__432VAK_PumpPort2__6",
			"csms__ControlSystem_MS__432VAK_PumpPort2__12",
			"csms__ControlSystem_MS__432VAK_PumpPort2__18",
			"csms__ControlSystem_MS__432VAK_PumpPort2__57",
			"csms__ControlSystem_MS__432VAK_PumpPort2__60",
			"csms__ControlSystem_MS__432VAK_PumpPort2__63",
			"csms__ControlSystem_MS__432VAK_PumpPort2__36"
		),
		array(
			"csms__ControlSystem_MS__432VAK_PumpPort3__24",
			"csms__ControlSystem_MS__432VAK_PumpPort3__6",
			"csms__ControlSystem_MS__432VAK_PumpPort3__12",
			"csms__ControlSystem_MS__432VAK_PumpPort3__18",
			"csms__ControlSystem_MS__432VAK_PumpPort3__42",
			"csms__ControlSystem_MS__432VAK_PumpPort3__45",
			"csms__ControlSystem_MS__432VAK_PumpPort3__48"
		),
		array(
			"csms__ControlSystem_MS__432VAK_Tank__6",  // RPM 106
			"csms__ControlSystem_MS__432VAK_Tank__9",  // VAI 105
			"csms__ControlSystem_MS__432VAK_Tank__0",  // RPI 0096
			"csms__ControlSystem_MS__432VAK_Tank__15",  
			"csms__ControlSystem_MS__432VAK_Tank__27",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__33",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__30",
			"csms__ControlSystem_MS__432VAK_Tank__30"   
//                      "csms__ControlSystem_MS__432VAK_Tank__15",  
//			"csms__ControlSystem_MS__432VAK_Tank__27",
//			"csms__ControlSystem_MS__432VAK_Tank__30",
//			"csms__ControlSystem_MS__432VAK_Tank__33",
//			"csms__ControlSystem_MS__432VAK_Tank__39",
//			"csms__ControlSystem_MS__432VAK_Tank__45",
//			"csms__ControlSystem_MS__432VAK_Tank__48",
//			"csms__ControlSystem_MS__432VAK_Tank__36"
		),
                array(                                  //HV subsystem 
			"hv__katrin-hv_rep__0__39",    // MS vessel IST
			"hv__katrin-hv_rep__0__38",    // MS vessel SOLL
			"hv__katrin-hv_rep__0__87",    // MS IE Common electrode IST
			"hv__katrin-hv_rep__0__86",    // MS IE Common electrode SOLL
			"hv__katrin-hv_rep__0__71",    // MS IE dipole west IST
			"hv__katrin-hv_rep__0__70",    // MS IE dipole west SOLL
			"hv__katrin-hv_rep__0__79",    // MS IE dipole east IST
			"hv__katrin-hv_rep__0__78",    // MS IE dipole east SOLL
			"hv__katrin-hv_rep__0__63",
			"hv__katrin-hv_rep__0__62",
			"hv__katrin-hv_rep__0__55",
			"hv__katrin-hv_rep__0__54",
			"hv__katrin-hv_rep__0__132",   // dipole relay status 
			"hv__katrin-hv_rep__0__131",   // dipole relay status
                        "hv__katrin-hv_rep__0__95",    // PS vessel IST
			"hv__katrin-hv_rep__0__94",    // PS vessel SOLL
                        "hv__katrin-hv_rep__0__103",    // PS dipole west IST
			"hv__katrin-hv_rep__0__102",    // PS dipole west SOLL
                        "hv__katrin-hv_rep__0__111",    // PS dipole east IST
			"hv__katrin-hv_rep__0__110",    // PS dipole east SOLL
                        "hv__katrin-hv_rep__0__47",    // MS IE Precision Supply IST
			"hv__katrin-hv_rep__0__46",    // MS IE Precision Supply SOLL
		),
		array(
			"hv__katrin-hv_rep__1__3",
			"hv__katrin-hv_rep__1__10",
			"hv__katrin-hv_rep__1__17",
			"hv__katrin-hv_rep__1__24",
			"hv__katrin-hv_rep__1__31",
			"hv__katrin-hv_rep__1__38",
			"hv__katrin-hv_rep__1__45",
			"hv__katrin-hv_rep__1__52",
			"hv__katrin-hv_rep__1__59",
			"hv__katrin-hv_rep__1__66",
			"hv__katrin-hv_rep__1__73",
			"hv__katrin-hv_rep__1__80",
			"hv__katrin-hv_rep__1__87",
			"hv__katrin-hv_rep__1__94",
			"hv__katrin-hv_rep__1__101",
			"hv__katrin-hv_rep__1__108",
			"hv__katrin-hv_rep__1__115",
			"hv__katrin-hv_rep__1__122",
			"hv__katrin-hv_rep__1__129",
			"hv__katrin-hv_rep__1__136",
			"hv__katrin-hv_rep__1__143",
			"hv__katrin-hv_rep__1__150",
			"hv__katrin-hv_rep__1__157",
			"hv__katrin-hv_rep__1__164"
		),
		array(
			"hv__katrin-hv_rep__1__2",
			"hv__katrin-hv_rep__1__9",
			"hv__katrin-hv_rep__1__16",
			"hv__katrin-hv_rep__1__23",
			"hv__katrin-hv_rep__1__30",
			"hv__katrin-hv_rep__1__37",
			"hv__katrin-hv_rep__1__44",
			"hv__katrin-hv_rep__1__51",
			"hv__katrin-hv_rep__1__58",
			"hv__katrin-hv_rep__1__65",
			"hv__katrin-hv_rep__1__72",
			"hv__katrin-hv_rep__1__79",
			"hv__katrin-hv_rep__1__86",
			"hv__katrin-hv_rep__1__93",
			"hv__katrin-hv_rep__1__100",
			"hv__katrin-hv_rep__1__107",
			"hv__katrin-hv_rep__1__114",
			"hv__katrin-hv_rep__1__121",
			"hv__katrin-hv_rep__1__128",
			"hv__katrin-hv_rep__1__135",
			"hv__katrin-hv_rep__1__142",
			"hv__katrin-hv_rep__1__149",
			"hv__katrin-hv_rep__1__156",
			"hv__katrin-hv_rep__1__163"
		),
		array(
			"hv__katrin-hv_rep__2__3",
			"hv__katrin-hv_rep__2__10",
			"hv__katrin-hv_rep__2__17",
			"hv__katrin-hv_rep__2__24",
			"hv__katrin-hv_rep__2__31",
			"hv__katrin-hv_rep__2__38",
			"hv__katrin-hv_rep__2__45",
			"hv__katrin-hv_rep__2__52",
			"hv__katrin-hv_rep__2__59",
			"hv__katrin-hv_rep__2__66",
			"hv__katrin-hv_rep__2__73",
			"hv__katrin-hv_rep__2__80",
			"hv__katrin-hv_rep__2__87",
			"hv__katrin-hv_rep__2__94",
			"hv__katrin-hv_rep__2__101",
			"hv__katrin-hv_rep__2__108",
			"hv__katrin-hv_rep__2__115",
			"hv__katrin-hv_rep__2__122",
			"hv__katrin-hv_rep__2__129",
			"hv__katrin-hv_rep__2__136",
			"hv__katrin-hv_rep__2__143",
			"hv__katrin-hv_rep__2__150",
			"hv__katrin-hv_rep__2__157",
			"hv__katrin-hv_rep__2__164"
		),
		array(
			"hv__katrin-hv_rep__2__2",
			"hv__katrin-hv_rep__2__9",
			"hv__katrin-hv_rep__2__16",
			"hv__katrin-hv_rep__2__23",
			"hv__katrin-hv_rep__2__30",
			"hv__katrin-hv_rep__2__37",
			"hv__katrin-hv_rep__2__44",
			"hv__katrin-hv_rep__2__51",
			"hv__katrin-hv_rep__2__58",
			"hv__katrin-hv_rep__2__65",
			"hv__katrin-hv_rep__2__72",
			"hv__katrin-hv_rep__2__79",
			"hv__katrin-hv_rep__2__86",
			"hv__katrin-hv_rep__2__93",
			"hv__katrin-hv_rep__2__100",
			"hv__katrin-hv_rep__2__107",
			"hv__katrin-hv_rep__2__114",
			"hv__katrin-hv_rep__2__121",
			"hv__katrin-hv_rep__2__128",
			"hv__katrin-hv_rep__2__135",
			"hv__katrin-hv_rep__2__142",
			"hv__katrin-hv_rep__2__149",
			"hv__katrin-hv_rep__2__156",
			"hv__katrin-hv_rep__2__163"
		),
		array(
			"csms__ControlSystem_MS__430KRYO_Baffel__147",
			"csms__ControlSystem_MS__430KRYO_Baffel__159",
			"csms__ControlSystem_MS__430KRYO_Baffel__171",
			"csms__ControlSystem_MS__430KRYO_Baffel__183",
			"csms__ControlSystem_MS__430KRYO_Baffel__189",
			"csms__ControlSystem_MS__430KRYO_Baffel__195",
			"csms__ControlSystem_MS__430KRYO_Baffel__120",
			"csms__ControlSystem_MS__430KRYO_Baffel__123"
		),
		array(
			"precmm0__PrecMagnet__NorthRing__0",
			"precmm0__PrecMagnet__NorthRing__1",
			"precmm0__PrecMagnet__NorthRing__2",
			"precmm0__PrecMagnet__NorthRing__7",
			"precmm0__PrecMagnet__NorthRing__8",
			"precmm0__PrecMagnet__NorthRing__9",
			"precmm0__PrecMagnet__NorthRing__14",
			"precmm0__PrecMagnet__NorthRing__15",
			"precmm0__PrecMagnet__NorthRing__16",
			"precmm0__PrecMagnet__NorthRing__21",
			"precmm0__PrecMagnet__NorthRing__22",
			"precmm0__PrecMagnet__NorthRing__23"
		),
		array(
			"precmm0__PrecMagnet__MidRing__0",
			"precmm0__PrecMagnet__MidRing__1",
			"precmm0__PrecMagnet__MidRing__2",
			"precmm0__PrecMagnet__MidRing__7",
			"precmm0__PrecMagnet__MidRing__8",
			"precmm0__PrecMagnet__MidRing__9",
			"precmm0__PrecMagnet__MidRing__14",
			"precmm0__PrecMagnet__MidRing__15",
			"precmm0__PrecMagnet__MidRing__16",
			"precmm0__PrecMagnet__MidRing__21",
			"precmm0__PrecMagnet__MidRing__22",
			"precmm0__PrecMagnet__MidRing__23",
			"precmm0__PrecMagnet__MidRing__28",
			"precmm0__PrecMagnet__MidRing__29",
			"precmm0__PrecMagnet__MidRing__30",
			"precmm0__PrecMagnet__MidRing__35",
			"precmm0__PrecMagnet__MidRing__36",
			"precmm0__PrecMagnet__MidRing__37"
		),
		array(
			"precmm0__PrecMagnet__SouthRing__0",
			"precmm0__PrecMagnet__SouthRing__1",
			"precmm0__PrecMagnet__SouthRing__2",
			"precmm0__PrecMagnet__SouthRing__7",
			"precmm0__PrecMagnet__SouthRing__8",
			"precmm0__PrecMagnet__SouthRing__9",
			"precmm0__PrecMagnet__SouthRing__14",
			"precmm0__PrecMagnet__SouthRing__15",
			"precmm0__PrecMagnet__SouthRing__16",
			"precmm0__PrecMagnet__SouthRing__21",
			"precmm0__PrecMagnet__SouthRing__22",
			"precmm0__PrecMagnet__SouthRing__23"
		),
		array(
			"fpd__katrin_rep__0__198",
			"fpd__katrin_rep__0__199",
			"fpd__katrin_rep__0__200"
		),
		array(
			"csms__ControlSystem_MS__435HZ_Temperatur__27",
			"csms__ControlSystem_MS__435HZ_Temperatur__33",
			"csms__ControlSystem_MS__435HZ_Temperatur__42",
			"csms__ControlSystem_MS__435HZ_Temperatur__45"
		),
		array(
			"aircoils__aircoils_rep__1__2",
			"aircoils__aircoils_rep__1__3",
			"aircoils__aircoils_rep__1__4",
			"aircoils__aircoils_rep__1__5",
			"aircoils__aircoils_rep__1__6",
			"aircoils__aircoils_rep__1__7",
			"aircoils__aircoils_rep__1__8",
			"aircoils__aircoils_rep__1__9",
			"aircoils__aircoils_rep__1__10",
			"aircoils__aircoils_rep__1__11",
			"aircoils__aircoils_rep__1__12",
			"aircoils__aircoils_rep__1__13",
			"aircoils__aircoils_rep__1__14",
			"aircoils__aircoils_rep__1__15",
			"aircoils__aircoils_rep__1__16",
			"aircoils__aircoils_rep__1__17"
		),
		array(
			"aircoils__aircoils_rep__1__18",
			"aircoils__aircoils_rep__1__19",
			"aircoils__aircoils_rep__1__20",
			"aircoils__aircoils_rep__1__21",
			"aircoils__aircoils_rep__1__22",
			"aircoils__aircoils_rep__1__23",
			"aircoils__aircoils_rep__1__24",
			"aircoils__aircoils_rep__1__25",
			"aircoils__aircoils_rep__1__26",
			"aircoils__aircoils_rep__1__27",
			"aircoils__aircoils_rep__1__28",
			"aircoils__aircoils_rep__1__29",
			"aircoils__aircoils_rep__1__30",
			"aircoils__aircoils_rep__1__31",
			"aircoils__aircoils_rep__1__32",
			"aircoils__aircoils_rep__1__33"
		),
		array(
			"aircoils__aircoils_rep__1__57",
			"aircoils__aircoils_rep__1__58",
			"aircoils__aircoils_rep__1__43",
			"aircoils__aircoils_rep__1__44",
			"aircoils__aircoils_rep__1__45",
			"aircoils__aircoils_rep__1__46",
			"aircoils__aircoils_rep__1__47",
			"aircoils__aircoils_rep__1__48",
			"aircoils__aircoils_rep__1__49",
			"aircoils__aircoils_rep__1__50",
			"aircoils__aircoils_rep__1__51",
			"aircoils__aircoils_rep__1__52",
			"aircoils__aircoils_rep__1__53",
			"aircoils__aircoils_rep__1__54",
			"aircoils__aircoils_rep__1__55",
			"aircoils__aircoils_rep__1__56",
			"aircoils__aircoils_rep__1__38"
		),
                // CPS current (index 24)
                array(
                        "cscps__ControlSystem_CPS__320_MAG_Stromzufuhr__0"
                ) ,
                // PP2 TMP temps (index 25)
		array(
			"csms__ControlSystem_MS__435HZ_PumpPort2__78",
			"csms__ControlSystem_MS__435HZ_PumpPort2__81",
			"csms__ControlSystem_MS__435HZ_PumpPort2__84"
                ),
                // PP3 TMP temps (index 26)
		array(
			"csms__ControlSystem_MS__435HZ_PumpPort3__60",
			"csms__ControlSystem_MS__435HZ_PumpPort3__63",
			"csms__ControlSystem_MS__435HZ_PumpPort3__66"
                ),
                // PS vacuum system (index 27)
                array(
                        "csps__ControlSystem_PS__412VAK_PumpPort__27",  // CC1232
			"csps__ControlSystem_PS__412VAK_PumpPort__6",   // TMP 1122 speed
			"csps__ControlSystem_PS__412VAK_PumpPort__12",  // TMP 1123 speed
			"csps__ControlSystem_PS__412VAK_PumpPort__42",  // VAO 1313 ZUST
			"csps__ControlSystem_PS__412VAK_PumpPort__45",  // VAO 1314 ZUST
                        "csps__ControlSystem_PS__412VAK_PumpPort__21"   // Ex1221
                ),
                // MS PP1 vacuum (index 28)
		array(
			"csms__ControlSystem_MS__432VAK_PumpPort1__0"
                ),
                // PS Temperatures (index 29)
		array(
                         "csps__ControlSystem_PS__415HZ_Tank__78",
                         "csps__ControlSystem_PS__415HZ_Tank__84",
                         "csps__ControlSystem_PS__415HZ_Tank__90"
                ) ,
                // Voltage monitoring K35 etc. (index 30)
		array(
                         "hv__katrin-hv_rep__7__2",  //K35 divided voltage
                         "hv__katrin-hv_rep__7__5"  //K65 divided voltage
                ),
                // Interlock V4 and Detector Valve (index 31)
		array(
                         "csms__ControlSystem_MS__436HV_Interlook__6"  //CRT 5221 Freigabe 
                )
        );



	$username = 'katrin';
	$password = 'neutrino';
	$context = stream_context_create(array(
		'http' => array(
			'header'  => "Authorization: Basic " . base64_encode("$username:$password"), "Connection: close\r\n"
		)
	));

        $errorcount = 0;
        $errormessage = '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>';

	for ($id = 0; $id < sizeof($adeiChannel); $id++)
	{
		$adeiChannels = "";
		for ($id2 = 0; $id2 < sizeof($adeiChannel[$id]); $id2++)
		{
			$adeiChannels = $adeiChannels . $adeiChannel[$id][$id2] . ",";
		}
		$adeiChannels = substr($adeiChannels, 0, strlen($adeiChannels) - 1);

      		$fileip = file_get_contents("http://katrin.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=60&format=csv", false, $context);
      		//$fileip = file_get_contents("http://ipekatrinbackupserv1.ipe.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=60&format=csv", false, $context);
                //$fileip = file_get_contents("http://katrin.kit.edu/adei-katrin/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=10&format=csv", false, $context);


                //if ($id == 9) echo $fileip . "<br> <br>";  // echo debug info for one single group

                if (substr($fileip,0,5) == "ERROR")
                {$errorcount++; $errormessage = $errormessage . "Error loading ADEI channel " . $id . "<br>";}

		for ($id2 = sizeof($adeiChannel[$id]) - 1 ; $id2 > -1; $id2--)
		{
			$pos = strrpos($fileip, ",");
			$adeiValue[$id][$id2] = floatval(substr($fileip, $pos + 2));
			$fileip = substr($fileip, 0, $pos);
			//echo "<br>" . $fileip . "<br> <br>" . $id . ", " . $id2 . ", " . $adeiValue[$id][$id2] . "<br>";
		}
	}


/*
	// special case: temperature ramp speed (array 21) !!!!!!!!!!

	$id = 21;
	$adeiChannels = "";
	for ($id2 = 0; $id2 < sizeof($adeiChannel[$id]); $id2++)
	{
		$adeiChannels = $adeiChannels . $adeiChannel[$id][$id2] . ",";
	}
	$adeiChannels = substr($adeiChannels, 0, strlen($adeiChannels) - 1);

      	$fileip = file_get_contents("http://katrin.kit.edu/adei/services/getdata.php?db_server=virtual&db_name=srctree&db_group=-3&control_group=-3&virtual=srctree&srctree=" . $adeiChannels . "&window=" . (time() - 3600) . "-" . (time() - 3600 + 60) . "&format=csv", false, $context);

	for ($id2 = sizeof($adeiChannel[$id]) - 1 ; $id2 > -1; $id2--)
	{
		$pos = strrpos($fileip, ",");
		$adeiValue[$id][$id2 + 4] = floatval(substr($fileip, $pos + 2));
		$fileip = substr($fileip, 0, $pos);
	}

*/




	// ########## TIME STAMP ##########

	echo '<font size="6" color="#000000"><b>KATRIN Spectrometer and Detector Section Status Overview</b></font><br>';
	echo '<font size="5" color="#000000">' . date(DATE_RFC822) . '</font><br>';
        if ($errorcount >0) echo $errormessage;
?>



<div style="position:absolute; top:150px; left:0px">
<img src="pics/beamline_noMoS.png">

<div style="position:absolute; top:20px; left:315px">
<img src="pics/spectrometer.png" width="1130" height="596">
</div>

<div style="position:absolute; top:-140px; left:1800px">
<img src="pics/katrin_logo.jpg" width="100" height="100">
</div>

<a href="mailto:thuemmler@kit.edu">
<div title="send email to Thomas Thuemmler" style="position:absolute; top:-70px; left:8px">
<img src="pics/newEmail.png" width="23" height="23">
</div>
</a>



<?php

        if ($onsite) echo '<br>Requested from inside KIT network.<br>';

        $tol = 0.5;

	// ########## VALVES ##########

	// V4
	$value = $adeiValue[31][0];
	$pic = "valve";
	if ($value == 1) $pic = $pic . "FREE";
	else if ($value == 0) $pic = $pic . "Off";
	placePicWithLink(303, 60, $pic . ".png", 30, "status CPS-PS gate vlave V4 &#10;purple: clearance to open, red: no clearance, grey: undefined", $adeiChannel[6][0]);
		
	// PS
	$value = 1; //getValveStatus($adeiValue[9][0]);
	$pic = "valve";
	if ($value == 1) $pic = $pic . "On";
	else if ($value == 0) $pic = $pic . "Off";
	placePicWithLink(303, 283, $pic . ".png", 30, "status PS beam-line gate valve&#10;green: open, red: closed, grey: undefined", $adeiChannel[9][0]);

	// FPD
	$value = $adeiValue[6][0];
	$pic = "valve";
	if ($value == 1) $pic = $pic . "On";
	else if ($value == 0) $pic = $pic . "Off";
	placePicWithLink(303, 1466, $pic . ".png", 30, "status FPD beam-line gate valve&#10;green: open, red: closed, grey: undefined", $adeiChannel[6][0]);



	// ########## MAGNETS ##########

	// CPS
	$NlCPS = 20550 / 0.9133;
	$BfieldCPS = 1.26e-6 * $adeiValue[24][0] * $NlCPS;	// mu0 * I * N/l
	if ($BfieldCPS < 0.1) { $value = "OFF"; $color = "#FF0000"; }
	else { $value = sprintf('%.1F T', $BfieldCPS); $color = "#000000"; }
	placeTextWithLink(240, 2, $value, 60, 5, $color, "field of CPS behind separation wall derived from current", $adeiChannel[24][0]);
	placeText(265, 2, "CPS", 60, 2, "#000000");

		
	// PS1
	if ($adeiValue[3][3] == 0) { $value = "LEM"; $color = "#FF0000"; }
	else { $value = sprintf('%.1F T', $adeiValue[3][1] / 157.0 * 4.5); $color = "#000000"; }
	placeTextWithLink(240, 66, $value, 70, 5, $color, "stand-alone center field PS1 magnet", $adeiChannel[3][1]);
	placeText(265, 66, "PS1", 70, 2, "#000000");

	// PS2
	if ($adeiValue[3][2] == 0) { $value = "LEM"; $color = "#FF0000"; }
	else { $value = sprintf('%.1F T', $adeiValue[3][0] / 157.0 * 4.5); $color = "#000000"; }
	placeTextWithLink(240, 274, $value, 70, 5, $color, "stand-alone center field PS2 magnet", $adeiChannel[3][0]);
	placeText(265, 274, "PS2", 70, 2, "#000000");

	// Pinch
	placeTextWithLink(240, 1420, sprintf('%.1F', $adeiValue[4][0] / 86.98 * 6.0) . " T", 70, 5, "#000000", "stand-alone center field PCH magnet", $adeiChannel[4][0]);
	placeText(265, 1420, "PCH", 70, 2, "#000000");

	// Detector
	placeTextWithLink(240, 1495, sprintf('%.1F', $adeiValue[4][1] / 93.59 * 6.0) . " T", 70, 5, "#000000", "stand-alone center field DET magnet", $adeiChannel[4][1]);
	placeText(265, 1495, "DET", 70, 2, "#000000");



	// ########## PAE ##########

	placeTextWithLink(303, 1553, sprintf('%.1F', $adeiValue[6][2]) . " kV", 90, 5, "#000000", "PAE potential", $adeiChannel[6][2]);
	placeText(328, 1553, "PAE", 90, 2, "#000000");



	// ########## PUMP PORTS ##########

	placeText(416, 1623, "PP2", 50, 2, "#000000");
	placeText(466, 1623, "PP1", 50, 2, "#000000");
	placeText(516, 1623, "PP3", 50, 2, "#000000");



	// ########## BAFFLE ##########

	$max = array( 90, 94, 88 );

        placeText(560, 1395, "LN<sub>2</sub> Tank", 130, 5, "#000000");
        $link = $adeiChannel[15][6] . "," . $adeiChannel[15][7];
        placePicWithLink(562,1360, "LN2tank.png", 60, "LN2 tank", $link);


        $value = $adeiValue[15][6];
        $link = $adeiChannel[15][6];
        $text = sprintf('%.0F', $value) . " %";
        placeTextWithLink(590, 1395, $text, 70, 4, "#000000", "LN2 tank level", $link);	
 
        $value = $adeiValue[15][7];
        $link = $adeiChannel[15][7];
        $text = sprintf('%.1F', $value) . " bar";
        placeTextWithLink(590, 1450, $text, 70, 4, "#000000", "LN2 tank pressure", $link);	

	for ($id = 0; $id < 3; $id++)
	{
		$value = $adeiValue[15][$id];
		$pic = "ice.png";
		$text = "---";

		if ($value < 800)
		{
			$text = sprintf('%.0F', $value) . " K";
			if ($value > $max[$id]) $pic = "iceOff.png";
			else
			{
				$value = $adeiValue[15][$id + 3];
				if ($value > 0.1 && $value < 7.0) $pic = "iceOn.png";
				else $pic = "iceBlink.gif";
			}
		}

		$pos = $id;
		if ($id == 0) $pos = 1;
		else if ($id == 1) $pos = 0;

		$link = $adeiChannel[15][$id] . "," . $adeiChannel[15][$id + 3];
		placePicWithLink(410 + $pos * 50, 1375, $pic, 30, "status baffle PP" . ($id + 1) . "&#10;blue: cooled, red: warmed, blinking: warming, grey: undefined", $link);
		placeTextWithLink(437 + $pos * 50, 1355, $text, 70, 4, "#000000", "temperature baffle PP" . ($id + 1), $link);	
	}



	// ########## PRESSURES ##########

	// CC2320
	$value = $adeiValue[7][0];
	$pic = "cc";
	$text = "---";
	if ($value > 1E-4) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1E-4 && $value > 1E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
	placePicWithLink(410, 1435, $pic . ".png", 30, "status cold cathode gauge CC2320&#10;green: on, red: off, grey: undefined", $adeiChannel[7][0]);
	placeTextWithLink(439, 1415, $text, 70, 4, "#000000", "pressure [mbar] cold cathode gauge CC2320", $adeiChannel[7][0]);

	// CC2345
	$value = $adeiValue[7][7];
	$pic = "cc";
	$text = "---";
	if ($value > 1E-4) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1E-4 && $value > 1E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
	placePicWithLink(427, 1482, $pic . ".png", 30, "status cold cathode gauge CC2345&#10;green: on, red: off, grey: undefined", $adeiChannel[7][7]);
	placeTextWithLink(406, 1462, $text, 70, 4, "#000000", "pressure [mbar] cold cathode gauge CC2345", $adeiChannel[7][7]);

	// Ex3110 
	$value = $adeiValue[8][0];
	$pic = "ex";
	$text = "---";
	if ($value > 2E-5) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 2E-5 && $value > 1E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
	placePicWithLink(510, 1435, $pic . ".png", 30, "status extractor ion gauge Ex3110&#10;green: on, red: off, grey: undefined", $adeiChannel[8][0]);
	placeTextWithLink(539, 1415, $text, 70, 4, "#000000", "pressure [mbar] extractor ion gauge Ex3110", $adeiChannel[8][0]);

	// Ex1110
	$value = $adeiValue[28][0];
        $link = $adeiChannel[28][0];
        $pic = "ex";
	$text = "---";
	if ($value > 2E-5) { $pic = $pic . "Off"; $text = "OFF"; }
	if ($value <= 2E-5 && $value > 1E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value);}
	placePicWithLink(460, 1435, $pic, 30, "status extractor ion gauge Ex1110&#10;green: off, blinking: on, grey: undefined", $link);
	placeTextWithLink(489, 1415, $text, 70, 4, "#000000", "pressure [mbar] extractor ion gauge Ex1110", $link);

	// Ex0096
	$value = $adeiValue[9][2];
        $link = $adeiChannel[9][2];
	$pic = "gauge";
	$text = "---";
        //$text = sprintf('%.1E', $value);
	if ($value > 2E-5) { $pic = $pic . "Off"; $text = "OFF"; }
	if ($value <= 1E-11) { $pic = $pic; $text = "LOW"; }
	if ($value <= 2E-5 && $value > 1E-11) { $pic = "gaugeOn.png"; $text = sprintf('%.1E', $value);}
	placePicWithLink(230, 1220, $pic, 30, "status stable ion gauge 0096&#10;green: off, blinking: on, grey: undefined", $link);
	placeTextWithLink(259, 1200, $text, 70, 4, "#000000", "pressure [mbar] stable ion gauge 0096", $link);

	// CC FPD
	$value = $adeiValue[6][1];
	$pic = "cc";
	$text = "---";
	if ($value > 1E-4) { $pic = $pic . "Off"; $text = "OFF"; }
	if ($value < 1E-11) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1E-4 && $value > 1E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
	placePicWithLink(188, 1475, $pic . ".png", 30, "status FPD cold cathode gauge&#10;green: on, red: off, grey: undefined", $adeiChannel[6][1]);
	placeTextWithLink(217, 1455, $text, 70, 4, "#000000", "pressure [mbar] FPD cold cathode gauge", $adeiChannel[6][1]);

	// Gauge Baffle
	$value = $adeiValue[5][0];
        $link = $adeiChannel[5][0];
        $color = "#000000";
        $pic = "gauge";
	$text = "---";
	if ($value > 1E-4) { $pic = $pic . "Off"; $text = "OFF"; }
        // standard cold blue "#0026FF"  ,  ice cold blue "#107BC7"
        if ($value < 2E-6 && $value > 1E-9) { $pic ="gaugeOn.png"; $color ="#107BC7"; $text = sprintf('%.1E', $value); }
	else if ($value >= 1E-6 && $value < 1e-1) { $pic = "gaugeOff.png"; $color = "FF0000"; $text = sprintf('%.1E', $value); }

	placePicWithLink(460, 1315, $pic, 30, "status baffle insulation vacuum gauge&#10;gray: off, green: on, red: too high&#10Should be < 5e-6 mbar for HV operation!!!", $link);
	placeTextWithLink(460+29, 1295, $text, 70, 4, $color, "pressure [mbar] baffle insulation vacuum gauge", $link);

	// PS CC1232
	$value = $adeiValue[27][0];
        $link = $adeiChannel[27][0];
	$pic = "cc";
	$text = "---";
	if ($value > 1E-5) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1E-5 && $value > 2E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
	placePicWithLink(370, 210, $pic . ".png", 30, "status cold cathode gauge CC1232&#10;green: on, red: off, grey: undefined", $link);
	placeTextWithLink(399, 190, $text, 70, 4, "#000000", "pressure [mbar] cold cathode gauge CC1232", $link);


	// PS Ex1221 
	$value = $adeiValue[27][5];
        $link = $adeiChannel[27][5];
	$pic = "ex";
	$text = "---";
	if ($value > 1E-5) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1E-5 && $value > 2E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
	placePicWithLink(188, 141, $pic . ".png", 30, "status extractor ion gauge Ex1221&#10;green: on, red: off, grey: undefined", $link);
	placeTextWithLink(217, 121, $text, 70, 4, "#000000", "pressure [mbar] extractor ion gauge Ex1221", $link);




	// ########## VACUUM PUMPS ##########

       // MS TMP Temperatures

        //T<sub>max</sub> 
	$value = max($adeiValue[25][0], $adeiValue[25][1], $adeiValue[25][2]);
        $link = $adeiChannel[25][0] . "," . $adeiChannel[25][1] . "," . $adeiChannel[25][2];
	$color = "#000000";
	if ($value < 10) $color = "#0026FF";
	else if ($value > 50) $color = "#FF0000";
	placeTextWithLink(435, 1625, sprintf('%.1F &#176;C', $value), 70, 3, $color, "TMP max temperature PP2", $link);

	$value = max($adeiValue[26][0], $adeiValue[26][1], $adeiValue[26][2]);
        $link = $adeiChannel[26][0] . "," . $adeiChannel[26][1] . "," . $adeiChannel[26][2];
	$color = "#000000";
	if ($value < 10) $color = "#0026FF";
	else if ($value > 50) $color = "#FF0000";
	placeTextWithLink(535, 1625, sprintf('%.1F &#176;C', $value), 70, 3, $color, "TMP max temperature PP3", $link);



	// TMPs (PP2 + PP3)

	for ($pp = 0; $pp < 2; $pp++)
	{
		for ($id = 0; $id < 3; $id++)
		{
			$value = $adeiValue[$pp + 7][$id + 1];
			$pic = "tmp";
			$text = "---";

			if ($value < 50)
			{
				$pic = $pic . "Off";
			}
			else if ($value >= 50 && $value < 500)
			{
				$text = sprintf('%.0F', $value);
				if ($value > 450)
				{
					$value = $adeiValue[$pp + 7][$id + 4];
                                        //$pic = $pic . "On";
                                        if ($value == 1) $pic = $pic . "On";
					else if ($value == 0) $pic = $pic . "Standby";
				}
			}

			placePicWithLink(410 + $pp * 100, 1528 + $id * 35, $pic . ".png", 30, "status turbo-molecular pump TMP" . ($pp + 2) . ($id + 4) . "20&#10;green: on, yellow: on but isolated, red: off, grey: undefined", $adeiChannel[$pp + 7][$id + 1]);
			placeTextWithLink(439 + $pp * 100, 1518 + $id * 35, $text, 50, 4, "#000000", "rotational speed [Hz] turbo-molecular pump TMP" . ($pp + 2) . ($id + 4) . "20", $adeiChannel[$pp + 7][$id + 1]);
		}
	}

       // PS TMPs

	for ($id = 0; $id < 2; $id++)
	{
                $value = $adeiValue[27][$id + 1];
                $link = $adeiChannel[27][$id + 1];
		$pic = "tmp";
		$text = "---";

		if ($value < 50)
		{
			$pic = $pic . "Off";
		}
		else if ($value >= 50 && $value < 700)
		{
			$text = sprintf('%.0F', $value);
			if ($value > 570)
			{
				$value = $adeiValue[27][$id + 3];
                                //$pic = $pic . "On";
                                if ($value == 1) $pic = $pic . "On";
				else if ($value == 0) $pic = $pic . "Standby";
			}
		}

		placePicWithLink(370, 123 + $id * 35, $pic . ".png", 30, "status turbo-molecular pump TMP" . (1123 - $id) . "&#10;green: on, yellow: on but isolated, red: off, grey: undefined", $link);
		placeTextWithLink(399, 114 + $id * 35, $text, 50, 4, "#000000", "rotational speed [Hz] turbo-molecular pump TMP" . (1123 - $id), $link);
	}


	// FPD CP

	$value = $adeiValue[6][3];
	$pic = "cp";
	$text = "---";

	if ($value > 20)
	{
		$pic = $pic . "Off";
	}
	else if ($value <= 20 && $value > 5)
	{
		$text = sprintf('%.0F K', $value);
		$value = $adeiValue[6][12];
		if ($value == 1) { $pic = $pic . "On"; }
		else if ($value == 0) { $pic = $pic . "Standby"; }
	}

	placePicWithLink(340, 1470, $pic . ".png", 30, "status FPD UHV cryo pump&#10;green: on, yellow: on but isolated, red: off, grey: undefined", $adeiChannel[6][3]);
	placeTextWithLink(367, 1450, $text, 70, 4, "#000000", "temperature FPD UHV cryo pump", $adeiChannel[6][3]);



	// ########## FPD SOURCES ##########

	$pic = "source";
	$text = "---";
	if ($adeiValue[6][9] == 1 && $adeiValue[6][10] == 1) { $pic = $pic . "On"; $text = "OUT"; }
	else if ($adeiValue[6][9] == 1 && $adeiValue[6][10] == 0) { $pic = $pic . "EIn"; $text = "IN"; }
	else if ($adeiValue[6][9] == 0 && $adeiValue[6][10] == 1) { $pic = $pic . "GIn"; $text = "IN"; }
	else if ($adeiValue[6][9] == 0 && $adeiValue[6][10] == 0) { $pic = $pic . "Off"; $text = "IN"; }
	$link = $adeiChannel[6][9] . "," . $adeiChannel[6][10];
	placePicWithLink(340, 1505, $pic . ".png", 30, "status FPD sources (electron and gamma source)&#10;green: retracted (out), red: inserted (in), grey: undefined", $link);
	placeTextWithLink(367, 1485, $text, 70, 4, "#000000", "status FPD sources (electron and gamma source)", $link);




	// ########## AIR COILS ##########

	$tol = 0.5;
	for ($coil = 0; $coil < 16; $coil++)
	{
		$value = $adeiValue[0][$coil];

		$color = "#000000";
		if (abs($value) < $adeiValue[21][$coil] - $tol || abs($value) > $adeiValue[21][$coil] + $tol) $color = "#FF0000";
		if ($value < -$tol && $adeiValue[22][$coil] != 1) $color = "#FF0000";
		if ($value > $tol && $adeiValue[22][$coil] != 0) $color = "#FF0000";

		if ($coil == 15) $value = -$value;  // coil 14 is inverted
		$value = sprintf('%.1F', $value);
		if ($value <= -100 || $value >= 100) $value = sprintf('%.0F', $value);
		if ($value >= -0.1 && $value <= 0.1) $value = sprintf('%.1F', 0.0); 

//		$status = $adeiValue[1][$coil];
                $status = 1;

//		$lem = $adeiValue[2][$coil];
                $lem = 1;

		if ($coil == 0) $pos = 270;
		else if ($coil == 1) $pos = 320;
		else if ($coil == 2) $pos = 521;
		else if ($coil == 15) $pos = 1167;
		else $pos = 609 + ($coil - 3) * 42.9;

		if ($status == 0 || $lem == 0) $color = "#FF0000";
		if ($status == 0) $value = "OVP";
		if ($lem == 0) $value = "LEM";

		$label = ($coil - 1);
		if ($coil == 0) $label = "VER";
		else if ($coil == 1) $label = "HOR";

                $color = "#006400";
                placeTextWithLink(-13, $pos, $value, 70, 4, $color, "air-coil current [A]&#10;OVP: over-voltage protection triggered, LEM: DCCT offline", $adeiChannel[0][$coil]);

		placeText(5, $pos, $label, 70, 2, "#000000");


		$color = "#000000";
		$label = "";
		if ($adeiValue[22][$coil] == 1 && $adeiValue[23][16] == 0) { $color = "#FF0000"; $label = "FLIP"; }
		if ($adeiValue[22][$coil] == 0 && $adeiValue[23][16] == 1 && $adeiValue[23][$coil] == 1) { $color = "#FF0000"; $label = "PULS"; }
		if ($adeiValue[22][$coil] == 1 && $adeiValue[23][16] == 1 && $adeiValue[23][$coil] == 1) { $color = "#FF0000"; $label = "PULS"; } // F+P

		placeTextWithLink(22 + 12, $pos, $label, 70, 2, $color, "air-coil status&#10;FLIP: flipped, PULS: pulsed, F+P: both", $adeiChannel[23][$coil]);

	}

	// EMCS

	placeText(-15, 200, "EMCS", 70, 5, "#000000");

	$tol = 0.5;
	$text = "???";
	$color = "#FFD800";
        $EMCSver = 46.46;
        $EMCShor = 7.1;
	if ($adeiValue[0][0] > $EMCSver - $tol && $adeiValue[0][0] < $EMCSver + $tol && $adeiValue[0][1] > $EMCShor - $tol && $adeiValue[0][1] < $EMCShor + $tol) { $text = "ON"; $color = "#4CFF00"; }
	else if ($adeiValue[0][0] < $tol && $adeiValue[0][1] < $tol && $adeiValue[0][0] > -$tol && $adeiValue[0][1] > -$tol) { $text = "OFF"; $color = "#FF0000"; }
	placeBox(-12, 160, $text, 35, 3, $color);


	// LFCS

        placeText(-15, 467, "LFCS", 70, 5, "#000000");

	$tol = 0.5;
	$text = "ON ???";
	$color = "#FFD800";
	$lfcs = array(    // current 14 is inverted, negative value in definition must be positive here
		array( "0.0", 0.000, 0.000, 0.000, 0.000, 0.000,0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000),
                array( "STD 20%", 32.3, 3.6, 4.8, 0.4, 27.4, 25.1, 2.5, 2.7, 34.9, 0.9, 22.4, 10.7, 11.4, 10.0 ),
                array( "2.7 70%", 0.0, 9.3, 1.5, 0.8, 12.9, 22.9, 36.7, 43.0, 14.7, 20.3, 9.2, 10.7, 11.0, 25.5 ),
                array( "2.7 100%", 38.09, 43.70, 24.34, 24.84, 44.50, 54.14, 19.45, 37.08, 47.81, 36.43, 39.24, 25.78, 40.46, 30.82 ),
                array( "1.0 100%", 30.0, 20.0, 25.0, 10.0, 0.0, 12.0, 20.0, 20.0, 12.0, 0.0, 5.0, 13.0, 20.0, 25.0),
                array( "1.0 70%", -24.4, -19.0, -21.4, -8.3, -1.8, 6.2, 21.5, 9.1, 11.5, 6.5, 4.4, -15.9, -25.0, 25.6),
                array( "10G 100%", 33.0, 159.0, 135.3, 78.8, 75.4, 116.7, 98.7, 86.3, 128.0, 57.4, 92.6, 150.7, 94.8, 33.7),
                array( "10G 70%", 31.4, 138.4, 112.2, 72.9, 110.8, 119.9, 96.6, 103.4, 140.0, 56.2, 86.0, 137.8, 110.1, 22.8),
                array( "2.0 70%", -11.9, -9.0, 5.8, -6.6, 15.3, 0.8, 34.1, 44.6, 5.5, 5.4, -8.6, 13.0, -10.0, 19.4),
                array( "9.7 70%", 28.4, 98.7, 93.0, 76.8, 95.6, 132.6, 103.4, 98.5, 149.1, 66.8, 84.4, 94.6, 95.0, 25.7),
                array( "14G 70%", 100, 94.8, 157.6, 143.9, 170, 130.1, 150.0, 150.0, 145, 162.5, 170, 100, 100, 0),
                array( "5/7 14G", 51.02, 48.37, 80.41, 73.42, 86.73, 66.38, 76.53, 76.53, 73.98, 82.91, 86.73, 51.02, 51.02, 0.00),
                array( "3.8G", 21.6, 21.3, 33.2, 7.4, 55.9, 32.0, 61.0, 56.8, 35.8, 36.0, 42.3, 33.9, 15.8, 45.2),
                array( "5G", 31.2, 45.8, 41.3, 33.7, 47.7, 53.0, 75.2, 65.6, 61.5, 35.0, 48.6, 49.9, 24.2, 32.3),
                array( "9G", 70.3, 84.0, 65.9, 106.4, 87.5, 142.3, 81.0, 103.1, 98.0, 112.7, 100.7, 90.1, 94.8, 54.0)
                );


	for ($pp = 0; $pp < 15; $pp++)
	{
		for ($id = 0; $id < 14; $id++)
		{
                        $tol = abs($tol);

                       //echo $pp . ", " . $id . ", " . $adeiValue[0][$id + 2] . ", " . $lfcs[$pp][$id + 1] .", ". $tol . "<br>";

                        //if ($adeiValue[0][$id + 2] > $lfcs[$pp][$id + 1] - $tol && $adeiValue[0][$id + 2] < $lfcs[$pp][$id + 1] + $tol)
			if (abs($adeiValue[0][$id + 2] - $lfcs[$pp][$id + 1]) < $tol)
			{
				if ($id == 13) { $text = $lfcs[$pp][0]; $color = "#4CFF00"; }
			}
			else break;
		}
	}
	if ($text == "0.0") { $text = "OFF"; $color = "#FF0000"; }
	placeBox(-12, 400, $text, 67, 3, $color);



	// ########## HIGH VOLTAGE ##########

	// load patch-panel configuration

	$handle = fopen("patchpanel/ringstoadei.txt", "r");
	if ($handle)
	{
		while (($buffer = fgets($handle, 4096)) !== false)
		{
			$buffer = trim($buffer);
			$ringsToAdei[count($ringsToAdei)] = $buffer;
		}
		fclose($handle);
	}

	// labels

	$posY = 12; // offset between labels and voltages (13)
	$posX = 6; // down/bottom (7)

        // voltage monitoring readout
        $color = "#000000";
        $factor_m = 1972.4531;
        $offset = 3.104e-6;
        $gain = 0.9999992578;
	$value = ($adeiValue[30][0] - $offset) * $gain * $factor_m;
        $voltage_error = abs($value * 2e-3);
        if ($voltage_error < 1) { $voltage_error = 1; }
        $link = $adeiChannel[30][0];
        $text = "K35 divider readout, precision value";
	placeText(385, 816, "K35 Voltage Reading", 130, 2, $color); // K35 voltage reading
        placeTextWithLink(363, 796, sprintf('%.3F', $value ) . "(" . sprintf('%.0F', $voltage_error ) .") V" , 170, 4, $color, $text, $link);

        //placeText(180, 845, "MS WEST IE", 70, 2,"#808080"); // west IE being displayed in loop below
        //placeText(450, 845, "MS EAST IE", 70, 2,"#808080"); // east IE
	placeText(265, 815, "MS IE COMMON", 130, 2, "#000000"); // mainspec IE Common
	placeText(265, 170, "PS TANK", 70, 2, "#000000"); // prespec tank
	placeText(305, 150, "PS CONES IE", 110, 2, "#505050"); // prespec west IE
	placeText(345, 150, "PS CENTER IE", 110, 2, "#505050"); // prespec east IE

        // MS Tank from Precision HCP Supply or Standard HCN
        $text = "MS TANK";
        $ms_hcn_soll = $adeiValue[10][1];
        $ms_hcp_soll = $adeiValue[10][21];
        if  ($ms_hcn_soll == 0) {
            if ($ms_hcp_soll == 0) { $text = $text . " OFF";}
            else { $text = $text . " HCP"; $HCP = true; }
        } else { $text = $text . " HCN"; $HCP = false; }
        placeText(70, 830, $text, 100, 2, "#000000"); // mainspec tank
        // switch values here between HCN and HCP in Value and Channel Array !!!
        if ($HCP) { $adeiValue[10][0] = -$adeiValue[10][20]; $adeiValue[10][1] = $adeiValue[10][21]; 
        $adeiChannel[10][0] = $adeiChannel[10][20]; $adeiChannel[10][1] = $adeiChannel[10][21]; }


	placeText(110-$posY, 845, "W7-11", 70, 2, "#000000"); // w7-11
	placeText(503+$posY+$posX, 845, "E7-11", 70, 2, "#000000"); // e7-11

	placeText(126-$posY, 600, "W6", 70, 2, "#000000"); // w6
	placeText(487+$posY+$posX, 600, "E6", 70, 2, "#000000"); // e6
	placeText(126-$posY, 1090, "W12", 70, 2, "#000000"); // w12
	placeText(487+$posY+$posX, 1090, "E12", 70, 2, "#000000"); // e12

	placeText(161-$posY, 516, "W5", 70, 2, "#000000"); // w5
	placeText(452+$posY+$posX, 516, "E5", 70, 2, "#000000"); // e5
	placeText(161-$posY, 1174, "W13", 70, 2, "#000000"); // w13
	placeText(452+$posY+$posX, 1174, "E13", 70, 2, "#000000"); // e13

	placeText(194-$posY, 434, "W4", 70, 2, "#000000"); // w4
	placeText(419+$posY+$posX, 434, "E4", 70, 2, "#000000"); // e4
	placeText(194-$posY, 1256, "W14", 70, 2, "#000000"); // w14
	placeText(419+$posY+$posX, 1256, "E14", 70, 2, "#000000"); // e14

	placeText(225-$posY, 385, "W3", 70, 2, "#000000"); // w3
	placeText(388+$posY+$posX, 385, "E3", 70, 2, "#000000"); // e3
	placeText(225-$posY, 1305, "W15", 70, 2, "#000000"); // w15
	placeText(388+$posY+$posX, 1305, "E15", 70, 2, "#000000"); // e15

	placeText(273-$posY, 352, "W2", 70, 2, "#000000"); // w2
	placeText(340+$posY+$posX, 352, "E2", 70, 2, "#000000"); // e2
	placeText(273-$posY, 1338, "W16", 70, 2, "#000000"); // w16
	placeText(340+$posY+$posX, 1338, "E16", 70, 2, "#000000"); // e16

	if ($adeiValue[10][8] > 10.0) placeText(309, 359, "APE", 70, 2, "#000000"); // ape south
	if ($adeiValue[10][10] > 10.0) placeText(309, 1331, "APE", 70, 2, "#000000"); // ape north

	// offset voltages

	$posY = 16; // offset between outer and inner voltages
	$ringsPosY = array( 273, 273+$posY, 225, 225+$posY, 194, 194+$posY, 161, 161+$posY, 126, 126+$posY, 110, 110+$posY, 126, 126+$posY, 161, 161+$posY, 194, 194+$posY, 225, 225+$posY, 273, 273+$posY, 340, 340-$posY, 388, 388-$posY, 419, 419-$posY, 452, 452-$posY, 487, 487-$posY, 503, 503-$posY, 487, 487-$posY, 452, 452-$posY, 419, 419-$posY, 388, 388-$posY, 340, 340-$posY );
	$ringsPosX = array( 352, 352, 385, 385, 434, 434, 516, 516, 600, 600, 845, 845, 1090, 1090, 1174, 1174, 1256, 1256, 1305, 1305, 1338, 1338, 352, 352, 385, 385, 434, 434, 516, 516, 600, 600, 845, 845, 1090, 1090, 1174, 1174, 1256, 1256, 1305, 1305, 1338, 1338 );
	$tol = 0.2;

	for ($pp = 0; $pp < 2; $pp++) // west (0), east (1)
	{
		for ($id = 0; $id < 22; $id++) // rings: o, i, o, i, ...
		{
			// show voltage if not shorted to IE
			if ($ringsToAdei[$id + 22*$pp] != "IE" && $ringsToAdei[$id + 22*$pp] != "")
			{
				// color
				if ( ($adeiValue[11 + 2*$pp][$ringsToAdei[$id + 22*$pp]] > $adeiValue[12 + 2*$pp][$ringsToAdei[$id + 22*$pp]] - $tol) && ($adeiValue[11 + 2*$pp][$ringsToAdei[$id + 22*$pp]] < $adeiValue[12 + 2*$pp][$ringsToAdei[$id + 22*$pp]] + $tol) )
					$color = "#000000";
				else
					$color = "#FF0000";

				// show voltage
				if ( ($id % 2 == 0) || ($id % 2 == 1 && $ringsToAdei[$id + 22*$pp] != $ringsToAdei[$id + 22*$pp - 1]) )
				{
					placeTextWithLink($ringsPosY[$id + 22*$pp], $ringsPosX[$id + 22*$pp], "+" . sprintf('%.1F', $adeiValue[11 + 2*$pp][$ringsToAdei[$id + 22*$pp]]), 70, 4, $color, "relative voltage offset", $adeiChannel[11 + 2*$pp][$ringsToAdei[$id + 22*$pp]]);
				}
			}
		}
	}

	// tank, ie, dipole, ape

	$ringsPosY = array( 45, 240, 155, 425, 306, 306, 0, 240, 280, 320 );
	$ringsPosX = array( 805, 805, 805, 805, 319, 1371, 0, 130, 130, 130 );
	$tolA = array ( 15, 2, 1, 1, 1, 1, 0, 250, 1, 1 );

        // MS voltages

	for ($pp = 0; $pp < 6; $pp++) // tank (0), ie (1), ie west (2), ie east (3), ape south (4), ape north (5)
	{
		// color
		if ( ($adeiValue[10][2*$pp] > $adeiValue[10][2*$pp + 1] - $tolA[$pp]) && ($adeiValue[10][2*$pp] < $adeiValue[10][2*$pp + 1] + $tolA[$pp]) )
			$color = "#000000";
		else
			$color = "#FF0000";

		// show voltage

		$value = $adeiValue[10][2*$pp];
                $link = $adeiChannel[10][2*$pp];

                $dipolestatus = $adeiValue[2][16];
                $dipolelink = $adeiChannel[2][16];

		if ($pp == 0) { $text = "absolute tank potential"; }
                else if ($pp == 1) { $text = "IE common offset potential"; }
		else if ($pp == 2) { $text = "relative voltage potential west IE"; }
		else if ($pp == 3) { $text = "relative voltage potential east IE"; }
		else if ($pp == 4) { $text = "relative voltage potential south APE"; }
		else if ($pp == 5) { $text = "relative voltage potential north APE"; }

		if ($pp == 2 || $pp == 3)
		{
			if ($dipolestatus == 1) // dipole enabled
			{
                                $color = "#990000";
                                $value = $adeiValue[10][2*$pp];
				if ($pp == 2)
                                {
                                    placeText($ringsPosY[$pp] + 40, $ringsPosX[$pp], "DIPOLE", 150, 4, "#CC0000" );
                                    
                                }
                                else if ($pp == 3)
                                {
                                    placeText($ringsPosY[$pp] + 40, $ringsPosX[$pp], "DIPOLE", 150, 4, "#CC0000");
                                    
                                }
                        }
			else
                        {
                                $value = $adeiValue[10][2*$pp]; // dipole disabled
                                $color = "#909090";
                        }

                        if ($pp == 2) placeText(180, 825, "MS WEST IE", 110, 2, $color); // west IE
                        else if ($pp == 3) placeText(450, 825, "MS EAST IE", 110, 2, $color); // east IE

			$link = $adeiChannel[10][2*$pp+1] . "," . $adeiChannel[10][2*$pp] . "," . $dipolelink;
		}

                // tank potential
                if ($pp == 0) placeTextWithLink($ringsPosY[$pp], $ringsPosX[$pp], sprintf('%.1F V', -$value), 150, 5, $color, $text, $link);

                // IE common and dipoles
                else if ($pp == 1 || $pp == 2 || $pp == 3) placeTextWithLink($ringsPosY[$pp], $ringsPosX[$pp], sprintf('%.2F V', -$value), 150, 5, $color, $text, $link);

                // APE
		else if ($pp == 4 || $pp == 5)
		{
			if ($adeiValue[10][2*$pp] > 10.0) { placeTextWithLink($ringsPosY[$pp], $ringsPosX[$pp] , sprintf('%.1F', $value), 70, 4, $color, $text, $link); }
		}
	}

        // PS voltages (add $pp < 10 for PS IE )

	for ($pp = 7; $pp < 10; $pp++) // PS tank (channel 7 = array entry 14 + 15), IE west (8), IE east (9)
	{
		// color
		if ( ($adeiValue[10][2*$pp] > $adeiValue[10][2*$pp + 1] - $tolA[$pp]) && ($adeiValue[10][2*$pp] < $adeiValue[10][2*$pp + 1] + $tolA[$pp]) )
			$color = "#000000";
		else
			$color = "#FF0000";

		// show voltage

		$value = $adeiValue[10][2*$pp];
		$link = $adeiChannel[10][2*$pp];

		if ($pp == 7) { $text = "absolute ps tank potential"; }
		else if ($pp == 8) { $text = "relative voltage potential PS IE west"; $color = "#505050";}
		else if ($pp == 9) { $text = "relative voltage potential PS IE west"; $color = "#505050";}

		placeTextWithLink($ringsPosY[$pp], $ringsPosX[$pp], sprintf('%.1F V', -$value), 150, 5, $color, $text, $link);
	}



/*
	// voltage test

	$tol = 5.0;
	for ($pp = 0; $pp < 3; $pp++)
	{
		if ($pp == 0) $idMax = 2;
		else $idMax = 11;

		for ($id = 0; $id < $idMax; $id++)
		{
			if ( ($adeiValue[10 + $pp][$id] > $adeiValue[10 + $pp][$id + $idMax] - $tol) && ($adeiValue[10 + $pp][$id] < $adeiValue[10 + $pp][$id + $idMax] + $tol) )
				$adeiColor[10 + $pp][$id] = "#000000";
			else
				$adeiColor[10 + $pp][$id] = "#FF0000";
		}
	}

	// voltages

	placeTextWithLink(103, 805, sprintf('%.1F', -$adeiValue[10][0] - $adeiValue[10][1]) . " V", 150, 5, $adeiColor[10][1], "absolute IE potential", $adeiChannel[10][1]); // IE
	placeTextWithLink(53, 805, sprintf('%.1F', -$adeiValue[10][0]) . " V", 150, 5, $adeiColor[10][0], "absolute tank potential", $adeiChannel[10][0]); // tank

	placeTextWithLink(120, 590, "+" . sprintf('%.1F', $adeiValue[11][4]), 70, 4, $adeiColor[11][4], "relative voltage offset ring west-6", $adeiChannel[11][4]); // w6
	placeTextWithLink(490, 590, "+" . sprintf('%.1F', $adeiValue[12][4]), 70, 4, $adeiColor[12][4], "relative voltage offset ring east-6", $adeiChannel[12][4]); // e6
	placeTextWithLink(120, 1100, "+" . sprintf('%.1F', $adeiValue[11][6]), 70, 4, $adeiColor[11][6], "relative voltage offset ring west-12", $adeiChannel[11][6]); // w12
	placeTextWithLink(490, 1100, "+" . sprintf('%.1F', $adeiValue[12][6]), 70, 4, $adeiColor[12][6], "relative voltage offset ring east-12", $adeiChannel[12][6]); // e12

	placeTextWithLink(155, 506, "+" . sprintf('%.1F', $adeiValue[11][3]), 70, 4, $adeiColor[11][3], "relative voltage offset ring west-5", $adeiChannel[11][3]); // w5
	placeTextWithLink(455, 506, "+" . sprintf('%.1F', $adeiValue[12][3]), 70, 4, $adeiColor[12][3], "relative voltage offset ring east-5", $adeiChannel[12][3]); // e5
	placeTextWithLink(155, 1184, "+" . sprintf('%.1F', $adeiValue[11][7]), 70, 4, $adeiColor[11][7], "relative voltage offset ring west-13", $adeiChannel[11][7]); // w13
	placeTextWithLink(455, 1184, "+" . sprintf('%.1F', $adeiValue[12][7]), 70, 4, $adeiColor[12][7], "relative voltage offset ring east-13", $adeiChannel[12][7]); // e13

	placeTextWithLink(188, 424, "+" . sprintf('%.1F', $adeiValue[11][2]), 70, 4, $adeiColor[11][2], "relative voltage offset ring west-4", $adeiChannel[11][2]); // w4
	placeTextWithLink(422, 424, "+" . sprintf('%.1F', $adeiValue[12][2]), 70, 4, $adeiColor[12][2], "relative voltage offset ring east-4", $adeiChannel[12][2]); // e4
	placeTextWithLink(188, 1266, "+" . sprintf('%.1F', $adeiValue[11][8]), 70, 4, $adeiColor[11][8], "relative voltage offset ring west-14", $adeiChannel[11][8]); // w14
	placeTextWithLink(422, 1266, "+" . sprintf('%.1F', $adeiValue[12][8]), 70, 4, $adeiColor[12][8], "relative voltage offset ring east-14", $adeiChannel[12][8]); // e14

	placeTextWithLink(225, 385, "+" . sprintf('%.1F', $adeiValue[11][1]), 70, 4, $adeiColor[11][1], "relative voltage offset ring west-3", $adeiChannel[11][1]); // w3
	placeTextWithLink(385, 385, "+" . sprintf('%.1F', $adeiValue[12][1]), 70, 4, $adeiColor[12][1], "relative voltage offset ring east-3", $adeiChannel[12][1]); // e3
	placeTextWithLink(225, 1305, "+" . sprintf('%.1F', $adeiValue[11][9]), 70, 4, $adeiColor[11][9], "relative voltage offset ring west-15", $adeiChannel[11][9]); // w15
	placeTextWithLink(385, 1305, "+" . sprintf('%.1F', $adeiValue[12][9]), 70, 4, $adeiColor[12][9], "relative voltage offset ring east-15", $adeiChannel[12][9]); // e15

	placeTextWithLink(275, 350, "+" . sprintf('%.1F', $adeiValue[11][0]), 70, 4, $adeiColor[11][0], "relative voltage offset ring west-2", $adeiChannel[11][0]); // w2
	placeTextWithLink(335, 350, "+" . sprintf('%.1F', $adeiValue[12][0]), 70, 4, $adeiColor[12][0], "relative voltage offset ring east-2", $adeiChannel[12][0]); // e2
	placeTextWithLink(275, 1340, "+" . sprintf('%.1F', $adeiValue[11][10]), 70, 4, $adeiColor[11][10], "relative voltage offset ring west-16", $adeiChannel[11][10]); // w16
	placeTextWithLink(335, 1340, "+" . sprintf('%.1F', $adeiValue[12][10]), 70, 4, $adeiColor[12][10], "relative voltage offset ring east-16", $adeiChannel[12][10]); // e16

	$posY = 16; // offset between outer and inner voltages

	placeTextWithLink(126, 600, "+200.0", 70, 4, $adeiColor[11][4], "relative voltage offset ring west-6-outer", $adeiChannel[11][4]); // w6o
	placeTextWithLink(487, 600, "+200.0", 70, 4, $adeiColor[12][4], "relative voltage offset ring east-6-outer", $adeiChannel[12][4]); // e6o
	placeTextWithLink(126, 1090, "+200.0", 70, 4, $adeiColor[11][6], "relative voltage offset ring west-12-outer", $adeiChannel[11][6]); // w12o
	placeTextWithLink(487, 1090, "+200.0", 70, 4, $adeiColor[12][6], "relative voltage offset ring east-12-outer", $adeiChannel[12][6]); // e12o

	placeTextWithLink(126+$posY, 600, "+200.0", 70, 4, $adeiColor[11][4], "relative voltage offset ring west-6", $adeiChannel[11][4]); // w6i
	placeTextWithLink(487-$posY, 600, "+200.0", 70, 4, $adeiColor[12][4], "relative voltage offset ring east-6", $adeiChannel[12][4]); // e6i
	placeTextWithLink(126+$posY, 1090, "+200.0", 70, 4, $adeiColor[11][6], "relative voltage offset ring west-12", $adeiChannel[11][6]); // w12i
	placeTextWithLink(487-$posY, 1090, "+200.0", 70, 4, $adeiColor[12][6], "relative voltage offset ring east-12", $adeiChannel[12][6]); // e12i

	placeTextWithLink(161, 516, "+200.0", 70, 4, $adeiColor[11][3], "relative voltage offset ring west-5", $adeiChannel[11][3]); // w5o
	placeTextWithLink(452, 516, "+200.0", 70, 4, $adeiColor[12][3], "relative voltage offset ring east-5", $adeiChannel[12][3]); // e5o
	placeTextWithLink(161, 1174, "+200.0", 70, 4, $adeiColor[11][7], "relative voltage offset ring west-13", $adeiChannel[11][7]); // w13o
	placeTextWithLink(452, 1174, "+200.0", 70, 4, $adeiColor[12][7], "relative voltage offset ring east-13", $adeiChannel[12][7]); // e13o

	placeTextWithLink(161+$posY, 516, "+200.0", 70, 4, $adeiColor[11][3], "relative voltage offset ring west-5", $adeiChannel[11][3]); // w5i
	placeTextWithLink(452-$posY, 516, "+200.0", 70, 4, $adeiColor[12][3], "relative voltage offset ring east-5", $adeiChannel[12][3]); // e5i
	placeTextWithLink(161+$posY, 1174, "+200.0", 70, 4, $adeiColor[11][7], "relative voltage offset ring west-13", $adeiChannel[11][7]); // w13i
	placeTextWithLink(452-$posY, 1174, "+200.0", 70, 4, $adeiColor[12][7], "relative voltage offset ring east-13", $adeiChannel[12][7]); // e13i

	placeTextWithLink(194, 434, "+200.0", 70, 4, $adeiColor[11][2], "relative voltage offset ring west-4", $adeiChannel[11][2]); // w4o
	placeTextWithLink(419, 434, "+200.0", 70, 4, $adeiColor[12][2], "relative voltage offset ring east-4", $adeiChannel[12][2]); // e4o
	placeTextWithLink(194, 1256, "+200.0", 70, 4, $adeiColor[11][8], "relative voltage offset ring west-14", $adeiChannel[11][8]); // w14o
	placeTextWithLink(419, 1256, "+200.0", 70, 4, $adeiColor[12][8], "relative voltage offset ring east-14", $adeiChannel[12][8]); // e14o

	placeTextWithLink(194+$posY, 434, "+200.0", 70, 4, $adeiColor[11][2], "relative voltage offset ring west-4", $adeiChannel[11][2]); // w4i
	placeTextWithLink(419-$posY, 434, "+200.0", 70, 4, $adeiColor[12][2], "relative voltage offset ring east-4", $adeiChannel[12][2]); // e4i
	placeTextWithLink(194+$posY, 1256, "+200.0", 70, 4, $adeiColor[11][8], "relative voltage offset ring west-14", $adeiChannel[11][8]); // w14i
	placeTextWithLink(419-$posY, 1256, "+200.0", 70, 4, $adeiColor[12][8], "relative voltage offset ring east-14", $adeiChannel[12][8]); // e14i

	placeTextWithLink(225, 385, "+200.0", 70, 4, $adeiColor[11][1], "relative voltage offset ring west-3", $adeiChannel[11][1]); // w3o
	placeTextWithLink(388, 385, "+200.0", 70, 4, $adeiColor[12][1], "relative voltage offset ring east-3", $adeiChannel[12][1]); // e3o
	placeTextWithLink(225, 1305, "+200.0", 70, 4, $adeiColor[11][9], "relative voltage offset ring west-15", $adeiChannel[11][9]); // w15o
	placeTextWithLink(388, 1305, "+200.0", 70, 4, $adeiColor[12][9], "relative voltage offset ring east-15", $adeiChannel[12][9]); // e15o

	placeTextWithLink(225+$posY, 385, "+200.0", 70, 4, $adeiColor[11][1], "relative voltage offset ring west-3", $adeiChannel[11][1]); // w3i
	placeTextWithLink(388-$posY, 385, "+200.0", 70, 4, $adeiColor[12][1], "relative voltage offset ring east-3", $adeiChannel[12][1]); // e3i
	placeTextWithLink(225+$posY, 1305, "+200.0", 70, 4, $adeiColor[11][9], "relative voltage offset ring west-15", $adeiChannel[11][9]); // w15i
	placeTextWithLink(388-$posY, 1305, "+200.0", 70, 4, $adeiColor[12][9], "relative voltage offset ring east-15", $adeiChannel[12][9]); // e15i

	placeTextWithLink(273, 352, "+200.0", 70, 4, $adeiColor[11][0], "relative voltage offset ring west-2", $adeiChannel[11][0]); // w2o
	placeTextWithLink(340, 352, "+200.0", 70, 4, $adeiColor[12][0], "relative voltage offset ring east-2", $adeiChannel[12][0]); // e2o
	placeTextWithLink(273, 1338, "+200.0", 70, 4, $adeiColor[11][10], "relative voltage offset ring west-16", $adeiChannel[11][10]); // w16o
	placeTextWithLink(340, 1338, "+200.0", 70, 4, $adeiColor[12][10], "relative voltage offset ring east-16", $adeiChannel[12][10]); // e16o

	placeTextWithLink(273+$posY, 352, "+200.0", 70, 4, $adeiColor[11][0], "relative voltage offset ring west-2", $adeiChannel[11][0]); // w2i
	placeTextWithLink(340-$posY, 352, "+200.0", 70, 4, $adeiColor[12][0], "relative voltage offset ring east-2", $adeiChannel[12][0]); // e2i
	placeTextWithLink(273+$posY, 1338, "+200.0", 70, 4, $adeiColor[11][10], "relative voltage offset ring west-16", $adeiChannel[11][10]); // w16i
	placeTextWithLink(340-$posY, 1338, "+200.0", 70, 4, $adeiColor[12][10], "relative voltage offset ring east-16", $adeiChannel[12][10]); // e16i

	placeTextWithLink(306, 319, "-200.0", 70, 4, $adeiColor[12][10], "relative voltage offset ring east-16", $adeiChannel[12][10]); // apes
	placeTextWithLink(306, 1371, "-200.0", 70, 4, $adeiColor[11][10], "relative voltage offset ring west-16", $adeiChannel[11][10]); // apen
*/


	// ########## DETECTOR ##########

	// detector powered

	$lowVoltage = array( 12, 8, 6, 6, 120 );
	$link = "";
	$pic = "fpdOn";
	$text = "ON";
	for ($id = 0; $id < 5; $id++)
	{
		$value = $adeiValue[6][4 + $id];
		if (!($value < $lowVoltage[$id] + 0.2 && $value > $lowVoltage[$id] - 0.2))
		{
			$pic = "fpdOff";
			$text = "OFF";
		}
		$link = $link . $adeiChannel[6][4 + $id] . ",";
	}
	$link = substr($link, 0, strlen($link) - 1);
	placePicWithLink(200, 1600, $pic . ".png", 30, "status FPD power&#10;green: on, red: off, grey: undefined", $link);
	placeTextWithLink(227, 1580, $text, 70, 4, "#000000", "status FPD power", $link);

	// carousel

	$value = $adeiValue[6][11];
	$color = "#000000";

	if ($value < -95) $value = "---";
	else $value = sprintf('%.0F', $adeiValue[6][11]) . " &#176;C";

	placeTextWithLink(255, 1565, $value, 100, 4, $color, "temperature FPD carousel/electronics", $adeiChannel[6][11]);
	placeText(275, 1565, "carousel", 100, 2, "#000000");



	// ########## MAGNETOMETERS ##########

	$mag = array(4, 6, 4);

	for ($pp = 0; $pp < 3; $pp++)
	{
		$bx = 0;
		$by = 0;
		$bz = 0;
		$link = "";

		for ($id = 0; $id < $mag[$pp]; $id++)
		{
			$bx = $bx + $adeiValue[16 + $pp][3 * $id];
			$by = $by + $adeiValue[16 + $pp][3 * $id + 1];
			$bz = $bz + $adeiValue[16 + $pp][3 * $id + 2];
                        $link = $link . $adeiChannel[16 + $pp][3 * $id + 2] . ",";
//echo $bx . "  " . $by . "  " . $bz . "<br>";
		}

		$bx = $bx / $mag[$pp];
		$by = $by / $mag[$pp];
		$bz = $bz / $mag[$pp];
		$link = substr($link, 0, strlen($link) - 1);

		$value = sqrt($bx * $bx + $by * $by + $bz * $bz);
		
		$text = "averaged field precise magnetometers ";
		if ($pp == 0) $text = $text . "north ";
		else if ($pp == 1) $text = $text . "middle ";
		else if ($pp == 2) $text = $text . "south ";
		$text = $text . "ring";

		placeTextWithLink(553, 1015 - 170 * $pp, sprintf('%.1F G', $value), 70, 5, "#000000", $text, $link);
	}



	// dipole

	$pic = "status";
	if ($pulserId[0] == "dipole" && $daqElapsed[0] > 30)
	{
		if ($pulserCounts[0] > 0) $pic = $pic . "On";
		else if ($pulserCounts[0] == 0) $pic = $pic . "Off";
	}
	//placePic($posY + 34 + 25, $posX + 80, $pic . ".png", 17, "status dipole sync pulser&#10;green: on, red: off, grey: undefined");

	// egun

	$pic = "status";
	if ($pulserId[1] == "egun" && $daqElapsed[0] > 30)
	{
		if ($pulserCounts[1] > 0) $pic = $pic . "On";
		else if ($pulserCounts[1] == 0) $pic = $pic . "Off";
	}
	//placePic($posY + 34 + 50, $posX + 80, $pic . ".png", 17, "status egun sync pulser&#10;green: on, red: off, grey: undefined");

	// muon

//	$pic = "status";
//	if ($pulserId[3] == "muon" && $daqElapsed[0] > 30)
//	{
//		if ($pulserCounts[3] > 0) $pic = $pic . "On";
//		else if ($pulserCounts[3] == 0) $pic = $pic . "Off";
//	}
//	placePic($posY + 34 + 75, $posX + 80, $pic . ".png", 17, "status muon sync pulser&#10;green: on, red: off, grey: undefined");



	// ########## HALL ##########

	placePicWithLink(-30-3, 1800, "newTemperature.png", 23, "temperature in KATRIN hall", $adeiChannel[19][1]);
	placeTextWithLink(-30-3, 1820, sprintf('%.1F &#176;C', $adeiValue[19][1]), 80, 4, "#000000", "temperature in KATRIN hall", $adeiChannel[19][1]);

	placePicWithLink(-2-3, 1800, "newPressure.png", 23, "atmospheric pressure in KATRIN hall", $adeiChannel[19][0]);
	placeTextWithLink(-2-3, 1820, sprintf('%.0F hPa', $adeiValue[19][0] * 0.0689475729 * 1000.0), 80, 4, "#000000", "atmospheric pressure in KATRIN hall", $adeiChannel[19][0]);
	//placeTextWithLink(-2-3, 1820, sprintf('%.3F bar', $adeiValue[19][0] * 0.0689475729), 80, 4, "#000000", "atmospheric pressure in KATRIN hall", $adeiChannel[19][0]);

	placePicWithLink(26-3, 1800, "newHumidity.png", 23, "relative humidity in KATRIN hall", $adeiChannel[19][2]);
	placeTextWithLink(26-3, 1820, sprintf('%.1F &#037;', $adeiValue[19][2]), 80, 4, "#000000", "relative humidity in KATRIN hall", $adeiChannel[19][2]);



	// ########## HIGH PRESSURE ##########

	if (getValveStatus($adeiValue[9][6]) == -1 && getValveStatus($adeiValue[9][7]) == 1 && getValveStatus($adeiValue[9][8]) == 1 && getValveStatus($adeiValue[9][9]) == 0)
	{
		$pic = "bottleBlinking.gif";
		$text = "ON";
		$color = "#000000";
		if ($adeiValue[9][4] != 64) $color = "#FF0000";
		placeTextWithLink(135, 1387, sprintf('%.1F &#037;', $adeiValue[9][5]), 70, 4, $color, "gas flow leak valve", $adeiChannel[9][5]);
		placeTextWithLink(160, 1387, sprintf('%.1E', $adeiValue[9][3]), 70, 4, $color, "pressure [mbar] MKS Baratron", $adeiChannel[9][3]);
	}
	else
	{
		if (getValveStatus($adeiValue[9][7]) == 1)
		{
			$pic = "bottleOn.png";
			$text = "OPEN";
		}
		else
		{
			$pic = "bottleOn.png";
			$text = "OFF";
		}
	}
	$link = $adeiChannel[9][3] . "," . $adeiChannel[9][4] . "," . $adeiChannel[9][5] . "," . $adeiChannel[9][6] . "," . $adeiChannel[9][7] . "," . $adeiChannel[9][8] . "," . $adeiChannel[9][9];
	placePicWithLink(135, 1358, $pic, 48, "status gas injection system&#10;green: off, blinking: on, grey: undefined&#10Should be off for nominal pressure measurements!!!", $link);
	placeTextWithLink(180, 1347, $text, 70, 4, "#000000", "status gas injection system", $link);


	// ########## PS VESSEL TEMPERATURES ##########

	$min = 18;
	$max = 30;

	// mean of 4020, 4030, 4040
        $link = $adeiChannel[29][0] . "," . $adeiChannel[29][1] . "," . $adeiChannel[29][2];
	$value = ($adeiValue[29][0] + $adeiValue[29][1] + $adeiValue[29][2])/3.0;
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 400) placeTextWithLink(303, 170, sprintf('%.1F &#176;C', $value), 90, 4, $color, "PS mean temperature of 4020..30..40", $link);



	// ########## MS VESSEL TEMPERATURES ##########

	$min = 14;
	$max = 24;

	// 0066
	$value = $adeiValue[20][0];
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 400) placeTextWithLink(110, 933, "T<sub>IE0066</sub> = " . sprintf('%.1F &#176;C', $value), 170, 4, $color, "IE temperature 0066", $adeiChannel[20][0]);

	// 0076
	$value = $adeiValue[20][1];
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 400) placeTextWithLink(110+20, 933, "T<sub>IE0076</sub> = " . sprintf('%.1F &#176;C', $value), 170, 4, $color, "IE temperature 0076", $adeiChannel[20][1]);

	// 0270
	$value = $adeiValue[20][2];
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 400) placeTextWithLink(307, 805, "T<sub>0270</sub> = " . sprintf('%.1F &#176;C', $value), 160, 4, $color, "tank temperature 0270", $adeiChannel[20][2]);

	// 0283
	$value = $adeiValue[20][3];
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 400) placeTextWithLink(307, 963, "T<sub>0283</sub> = " . sprintf('%.1F &#176;C', $value), 160, 4, $color, "tank temperature 0283", $adeiChannel[20][3]);


/*
	// 0066
	$value = $adeiValue[20][0] - $adeiValue[20][4];
	$color = "#000000";
	if ($value < 50) placeTextWithLink(110+16, 973-30, sprintf('(%.2F &#176;C/h)', $value), 130, 4, $color, "IE temperature 0066 (ramp speed)", $adeiChannel[20][0]);

	// 0076
	$value = $adeiValue[20][1] - $adeiValue[20][5];
	$color = "#000000";
	if ($value < 50) placeTextWithLink(126+24+16, 973-30, sprintf('(%.2F &#176;C/h)', $value), 130, 4, $color, "IE temperature 0076 (ramp speed)", $adeiChannel[20][1]);

	// 0270
	$value = $adeiValue[20][2] - $adeiValue[20][6];
	$color = "#000000";
	if ($value < 50) placeTextWithLink(307+16, 845-30, sprintf('(%.2F &#176;C/h)', $value), 130, 4, $color, "tank temperature 0270 (ramp speed)", $adeiChannel[20][2]);

	// 0283
	$value = $adeiValue[20][3] - $adeiValue[20][7];
	$color = "#000000";
	if ($value < 50) placeTextWithLink(307+16, 973-30, sprintf('(%.2F &#176;C/h)', $value), 130, 4, $color, "tank temperature 0283 (ramp speed)", $adeiChannel[20][3]);
*/

	$color = "#000000";
	// ########## 60s counter ##########
        $date= date("Ymdgis");
        if ($onsite) placePic(5, 5, "countdown60.gif?".$date, 100, "60s counter");


	// ########## STS ##########
	placeTextWithWebsiteLink(360, 5, "&larr;STS", 60, 5, $color, "Link to STS Status Overview", "../stsstatus");

	// ########## FPD Status ##########
	placeTextWithWebsiteLink(-10, 1450, "&rarr; FPD Status", 155, 4, $color, "Link to FPD Status Overview", "../fpdstatus");


	// ########## MoS und HV Status ##########
	placeTextWithWebsiteLink(20, 1440, "&rarr; MoS and HV Status", 240, 4, $color, "Link to Monitor Spectrometer and High Voltage Status Overview", "http://ipepc57.ipe.kit.edu:5858/mos/status");

	// ########## Run Summary ##########
	placeTextWithWebsiteLink(50, 1440, "&rarr; Run Summary Page", 240, 4, $color, "Link to Run Summary Page", "http://katana.npl.washington.edu/~sanshiro/kaffee/Krypton/");

	// ########## 10s readout link ##########
if ($ip == "141.52.12.164") placeTextWithWebsiteLink(80, 1475, "&rarr; Switch to quick refresh", 195, 4, $color, "Link to 10s readout page", "./index_q.php");
if ($ip == "141.52.16.94") placeTextWithWebsiteLink(80, 1475, "&rarr; Switch to quick refresh", 195, 4, $color, "Link to 10s readout page", "./index_q.php");


        // ########## PS ##########
//	placeText(100, 170, "PS", 60, 30, $color);


	// ########## HIGH VOLTAGE SWITCH ##########
/*
	$value = $adeiValue[10][4]; // HAS CHANGED!!!!!
	$pic = "switch";
	if ($value == 1) $pic = $pic . "Open";
	else if ($value == 0) $pic = $pic . "Closed";
	placePicWithLink(498, 501, $pic . ".png", 80, "status high-voltage ground switch", $adeiChannel[10][4]);
*/
?>



<!--
<div style="position:absolute; top:100px; left:100px">
    <canvas id="myCanvas" width="578" height="200"></canvas>
    <script>
      var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');

      context.beginPath();
      context.moveTo(100, 150);
      context.lineTo(450, 50);
      context.lineTo(250, 250);
      context.stroke();
    </script>
</div>
-->

</div>



</body>
</html>
