


<?php

// display network localization
if ($onsite) placeBox(25, 10, "Inside KIT CN network", 250, 2, "#DDDDDD");
if (substr($ip, 0, 7) == "134.107") placeBox(25, 10, "MPP Munich network", 250, 2, "#FFFFFF");
if (substr($ip, 0, 7) == "128.176") placeBox(25, 10, "WWU Muenster network", 250, 2, "#FFFFFF");

$local = false;
if (substr($ip, 0, 13) == "141.52.12.217") { placeBox(25, 10, "local dev computer", 250, 2, "#FFFFFF"); $local = true;}

// echo '<br>Requested from inside KIT CN network.<br>';

        // global variable for PS operation mode
        $PSmode = $adeiValue[32][0];
        $MSmode = $adeiValue[33][0];

        $tol = 0.5;

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

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

	// FPD gate valve
	$value = $adeiValue[36][2];
	$clearance = $adeiValue[31][0];
	$pic = "valve";
	if (($value == 1) && ($clearance == 1)) $pic = $pic . "On";
	else if (($value == 0) && ($clearance == 0)) $pic = $pic . "Off";
	else if (($value == 0) && ($clearance == 1)) $pic = $pic . "FREE";
	else if (($value == 1) && ($clearance == 0)) $pic = $pic . "Error";
	placePicWithLink(303, 1466, $pic . ".png", 30, "status FPD beam-line gate valve&#10;green: open, red: closed, grey: undefined", $adeiChannel[36][2].",".$adeiChannel[31][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 );

        $time_conversion_factor = 621355968000000000; // conversion for PCS7 .NET value;
        $timestamp = ($adeiValue[15][8] - $time_conversion_factor) / 10000000;
        echo '<br><font size="4" color="#000000">&nbsp;Baffle timestamp: ' . date("r", $timestamp ) . ' </font><br>';

        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]*0.77+0.83;
        $link = $adeiChannel[15][6];
        $text = sprintf('%.0F', $value) . " %";
        placeTextWithLink(590, 1395, $text, 70, 4, "#000000", "LN2 tank level", $link);	
 
        $value = $adeiValue[15][7]-1.86;
        $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+2) { $pic = $pic; $text = "OFF"; }
	else if ($value > 2E-5) { $pic = $pic . "Off"; $text = "high"; }
	else if ($value <= 1E-4 && $value >= 1.3333E-11) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
	else if ($value < 1.3333E-11) { $text = "LOW"; }
	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]);

        $time_conversion_factor = 621355968000000000; // conversion for PCS7 .NET value;
        $timestamp = ($adeiValue[7][8] - $time_conversion_factor) / 10000000;
        echo '<font size="4" color="#000000">&nbsp;Vacuum timestamp: ' . date("r", $timestamp ) . ' </font><br>';


	// 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.".png", 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 > 1E+2) { $pic = $pic; $text = "OFF"; }
    else if ($value > 2E-5) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1.1E-11) { $pic = $pic; $text = "LOW"; }
	else if ($value <= 2E-5 && $value > 1E-11) { $pic = "gaugeOn"; $text = sprintf('%.1E', $value);}
	placePicWithLink(230, 1220, $pic.".png", 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"; $color ="#107BC7"; $text = sprintf('%.1E', $value); }
	else if ($value >= 1E-6 && $value < 1e-1) { $pic = "gaugeOff"; $color = "FF0000"; $text = sprintf('%.1E', $value); }

	placePicWithLink(460, 1315, $pic . ".png", 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-4) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1E-4 && $value > 2E-12) { $pic = $pic . "On"; $text = sprintf('%.1E', $value); }
        placePicWithLink(370, 240, $pic . ".png", 30, "status cold cathode gauge CC1232&#10;green: on, red: off, grey: undefined", $link);
	placeTextWithLink(399, 220, $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-4) { $pic = $pic . "Off"; $text = "OFF"; }
	else if ($value <= 1E-4 && $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 . "Closed";
				}
				else if ($value > 240)
				{
					$value = $adeiValue[$pp + 7][$id + 4];
                                        //$pic = $pic . "On";
                                        if ($value == 1) $pic = $pic . "Standby";
					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, 157 + $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, 148 + $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(368-30, 1475, $pic . ".png", 30, "status FPD UHV cryo pump&#10;green: on, yellow: on but isolated, red: off, grey: undefined", $adeiChannel[6][3]);
	placeTextWithLink(368, 1455, $text, 70, 3, "#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(368-30, 1510, $pic . ".png", 30, "status FPD sources (electron and gamma source)&#10;green: retracted (out), red: inserted (in), grey: undefined", $link);
	placeTextWithLink(368, 1490, $text, 70, 3, "#000000", "status FPD sources (electron and gamma source)", $link);




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


	// LFCS

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

	$tol = 0.2;
	$text = "Unkn";
	$color = "#FFD800";
        $lfcs_array_offset = 2;
     
        $lfcs_config = -1;  // store config for emcs later in loop
	for ($pp = 0; $pp < sizeof($aircoil_settings); $pp++)
	{
		for ($id = 0; $id < 16; $id++)
		{
                        $tol = abs($tol);
			if ($id == 14) {$lfcs_array_offset = -13; }
			if ($id == 15) {$lfcs_array_offset = -15; }
			if (abs($adeiValue[0][$id + $lfcs_array_offset] - $aircoil_settings[$pp][$id + 1]) < $tol)
			{
			  if ($id == 13) { $text = $aircoil_settings[$pp][0]; $lfcs_config = $pp; $color = "#FF9300"; }
			  if ($id == 15) { $color = "#4CFF00"; }			}
			else break;
		}
		if ($lfcs_config > -1) break;
	}
	if ($text == "All Off") { $text = "OFF"; $color = "#FF0000"; }
	placeBox(-12, 384, $text, 82, 3, $color);

        // EMCS

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

	$tol = 0.2;
	$text = "???";
	$color = "#FFD800";
        $EMCShor = $aircoil_settings[$lfcs_config][15];
        $EMCSver = $aircoil_settings[$lfcs_config][16];

	if ($adeiValue[0][0] > $EMCSver - $tol && $adeiValue[0][0] < $EMCSver + $tol && $adeiValue[0][1] > $EMCShor - $tol && $adeiValue[0][1] < $EMCShor + $tol) { $text = "On OK"; $color = "#4CFF00"; }
	else if ($adeiValue[0][0] > $tol || $adeiValue[0][1] > $tol) { $text = "Unkn"; $color = "#FF9300"; }
        else { $text = "ERR ".$lfcs[$lfcs_config][16]; $color = "#FF0000"; }
	if ($adeiValue[0][0] < $tol && $adeiValue[0][1] < $tol && $adeiValue[0][0] > -$tol && $adeiValue[0][1] > -$tol) { $text = "OFF"; $color = "#FF0000"; }

        placeBox(-12, 128, $text, 67, 3, $color);


        // print current values 
	$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] for (".$lfcs[$lfcs_config][0].") setting.&#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]);

	}

	


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

if (($MSmode == 5) or ($MSmode == 4))
  { // only display in MS operation mode High-Voltage or baking
    
    // load patch-panel configuration
    $handle = fopen("patchpanel/ringstoadei.txt", "r");
    
    $ringsToAdei = array();
    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)

    if ($MSmode == 5)
      { // only display in MS operation mode High-Voltage
    
	$time_conversion_factor = 2082844800;
	$timestamp = ($adeiValue[30][2] - $time_conversion_factor);
	echo '<font size="4" color="#000000">&nbsp;HV meas. timestamp: ' . date("r", $timestamp ) . ' </font><br>';
	
	// HV switch for K35
	$hv_switch1103_k35 = $adeiValue[35][2];
	if ($hv_switch1103_k35 == 1) $k35_text = "K35 @ MoS Voltage";
	else if ($hv_switch1103_k35 == 0) $k35_text = "K35 @ MS  Voltage";
	$k35_text = "JRL50 @ PS Tank";
	
	// voltage monitoring readout K35
	$color = "#000000";
	$factor_m = 5000.1021;
	$offset = 4.529e-6;
	$gain = 0.999997525656185;
	$value = ($adeiValue[30][0] - $offset) * $gain * $factor_m;
	//        $voltage_error = abs($value * 2e-3);
	$voltage_error = abs($value * 10e-6);
	if ($voltage_error*100 < 1) { $voltage_error = 0.2; }
	
	// voltage display K35
	$link = $adeiChannel[30][0];
	$text = "K35 divider readout 436-REU-0-0201-0001, precision voltage value, in 2019 used for JRL50 at PS";
	//placeText(385, 716, $k35_text, 130, 2, $color); // K35 voltage reading
	//placeTextWithLink(363, 696, sprintf('%.3F', $value ) . "(" . sprintf('%.0F', $voltage_error ) .") V" , 170, 4, $color, $text, $link);
	placeText(520, 136, $k35_text, 130, 2, $color); // K35 voltage reading
	// placeText(490, 136, "uncalibrated", 130, 2, "orange"); // K35 voltage reading
	placeTextWithLink(502, 116, sprintf('%.2F', $value ) . "(" . sprintf('%.0F', $voltage_error*100 ) .") V" , 170, 4, $color, $text, $link);
	
	// HV switch for K65
	$hv_switch1102_k65 = $adeiValue[35][1];
	$k65_text = "K35 @ MS Tank + IE";
	//$k65_text = "Fluke E @ Reference";
	//if ($hv_switch1102_k65 == 1) $k65_text = "K65 @ MoS Voltage";
	//if ($hv_switch1102_k65 == 0) $k65_text = "K65 @ MS  Voltage";
	
	// voltage monitoring readout K65
	$color = "#000000";
	$factor_m = 1972.449;
	$factor_m_rel_error = 0.01/$factor_m;
	$offset = 3.831e-06;
	$gain = 1.00000037602556;
	$value = ($adeiValue[30][1] - $offset) * $gain * $factor_m;
	$voltage_error = abs($value * $factor_m_rel_error * 1e3); // in mV
	if ($voltage_error < 1) { $voltage_error = 1; }
	
	// voltage display K65
	$link = $adeiChannel[30][1];
	$text = "K65 divider readout 436-REU-0-0301-0001, precision voltage value, in 2019 used for K35";
	//placeText(385, 916,
	placeTextWithLink(363, 796, sprintf('%.3F', $value ) . "(" . sprintf('%.0F', $voltage_error ) .") V" , 170, 4, $color, $text, $link);
	//        placeTextWithLink(363, 796, sprintf('%.6F', $value ) ." V" , 170, 4, $color, $text, $link);
	//        placeTextWithLink(363, 896,
	
	//display stability indicators
	//if ($local) {
	if (sizeof($flukeE) > 2) {
	  placeText(400, 811, $k65_text, 140, 2, $color); // K65 voltage reading
	  
	  if ($flukeE[0]['ppmdev'] <= 2e-6) $color1 = "lightgreen";
	  if ($flukeE[1]['ppmdev'] <= 2e-6) $color2 = "lightgreen";
	  if ($flukeE[2]['ppmdev'] <= 2e-6) $color3 = "lightgreen";
	  if ($flukeE[3]['ppmdev'] <= 2e-6) $color4 = "lightgreen";
	  
	  if ($flukeE[0]['ppmdev'] > 2e-6) $color1 = "orange";
	  if ($flukeE[1]['ppmdev'] > 2e-6) $color2 = "orange";
	  if ($flukeE[2]['ppmdev'] > 2e-6) $color3 = "orange";
	  if ($flukeE[3]['ppmdev'] > 2e-6) $color4 = "orange";
	  
	  if ($flukeE[0]['ppmdev'] > 5e-6) $color1 = "#FFA0A0";
	  if ($flukeE[1]['ppmdev'] > 5e-6) $color2 = "#FFA0A0";
	  if ($flukeE[2]['ppmdev'] > 5e-6) $color3 = "#FFA0A0";
	  if ($flukeE[3]['ppmdev'] > 5e-6) $color4 = "#FFA0A0";
	  
	  placeBox(384, 915, "10s", 35, 1, $color1);
	  placeBox(384, 880, "20s", 35, 1, $color2);
	  placeBox(384, 845, "1m", 35, 1, $color3);
	  placeBox(384, 810, "5m", 35, 1, $color4);
	  
	  echo '<font size="4" color="#000000">&nbsp;HV status timestamp: ' . $extraction_time_HV . ' </font><br>';
	}
	else placeText(385, 811, $k65_text, 140, 2, $color); // K65 voltage reading
      }
    
    //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
    
    // MS Tank from Precision HCP Supply or Standard HCN
    $text = "MS TANK";
    $ms_hcn_soll = $adeiValue[10][1];
    $ms_hcp_soll = $adeiValue[10][25];
    $HCP = false;
    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][24]; $adeiValue[10][1] = $adeiValue[10][25]; 
      $adeiChannel[10][0] = $adeiChannel[10][24]; $adeiChannel[10][1] = $adeiChannel[10][25]; }
    
    
    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, 300, 285, 300, 315 );
    $ringsPosX = array( 805, 805, 805, 805, 319, 1371, 0, 150, 160, 190, 220, 190 );
    $tolA = array ( 15, 2, 1, 1, 1, 1, 0, 250, 2, 2, 2, 2);
    
    // 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 HCN:436-EHV-0-1001-0002 HCP:436-EHV-0-1002-0002"; }
	else if ($pp == 1) { $text = "IE common offset potential, 436-EHV-0-1003-0002"; }
	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
	else if ($pp == 1) placeTextWithLink($ringsPosY[$pp], $ringsPosX[$pp], sprintf('%.2F V', -$value), 150, 5, $color, $text, $link);
	
	// IE dipoles
	else if ($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 )
if ($PSmode == 5)
  { // only display in PS operation mode High-Voltage
    
    
    placeText(265, 190, "PS TANK", 70, 2, "#000000"); // prespec tank
    placeText(330, 160, "PS IE", 120, 2, "#505050"); // prespec IE
    
    
    for ($pp = 7; $pp < 12; $pp++) // PS tank (channel 7 = array entry 14 + 15), IE west (8), IE east (9)
      {
	// color and text
	if ($pp == 7) { $text = "Primary PS tank potential of 416EHV1001"; $color = "#000000";}
	else if ($pp == 8) { $text = "relative voltage potential PS IE1 Ch1A of 416EHV1002"; $color = "#505050";}
	else if ($pp == 9) { $text = "relative voltage potential PS IE2 Ch1B of 416EHV1002"; $color = "#505050";}
	else if ($pp == 10) { $text = "relative voltage potential PS IE3 Ch2A of 416EHV1003"; $color = "#505050";}
	else if ($pp == 11) { $text = "relative voltage potential PS IE4 Ch2B of 416EHV1003"; $color = "#A0A0A0";}
	
	// setpoint mismatch 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 = $color;
	else
	  $color = "#FF0000";
	
	// show voltage
	
	$value = $adeiValue[10][2*$pp];
	$link = $adeiChannel[10][2*$pp];
	
	if ($pp == 7) placeTextWithLink($ringsPosY[$pp], $ringsPosX[$pp], sprintf('%.1F V', -$value), 150, 5, $color, $text, $link);
	else placeTextWithLink($ringsPosY[$pp], $ringsPosX[$pp], sprintf('%.1F V', -$value), 60, 2, $color, $text, $link);
      }
  }


       // ########## Split Ring Electrodes ##########

        $srPosX = array( 59, 284, 1420 );
        $srPosY = array( 402, 402, 370 );
        $srKATRINNumber = array( "418-EEL-3-2150", "418-EEL-3-4150", "438-EEL-3-5150" );

	// CPS-PS electrode 418-EEL-2150

        // $sr: 0 = cps-ps ; 1 = PS-MS ; 2 = MS-FPD
        for ($sr = 0; $sr < 3; $sr++)
	{ 
        $splitringON = $adeiValue[36][4*$sr + 3];
	$pic = "splitring";
        if ($splitringON = 1)
	  {
	    $color = "#000000";
	    $splitringVal = $adeiValue[36][4*$sr + 4];
	    $splitringPOL = $adeiValue[36][4*$sr + 5];
	    $splitringSet = $adeiValue[36][4*$sr + 6];
	    $link = $adeiChannel[36][$sr*4 + 4];

	    if ($splitringVal < 25) { $color="#FF0000"; $pic = $pic."Alarm";}
	    else {
	      if ($splitringPOL == 0) {$sign = "+"; $pic = $pic."On";}
	      if ($splitringPOL == 1) {$sign = "-"; $pic = $pic."Warn";}
	    }
	    
	    placePicWithLink($srPosY[$sr]-32,$srPosX[$sr]+16, $pic.".png", 30, "status of split ring electrode (green = polarity pos and OK, gray = off, red = low voltage alarm, orange = polarity neg warning", $link);
	    placeTextWithLink($srPosY[$sr],$srPosX[$sr], $sign.sprintf('%.0F V', $splitringVal), 60, 2, $color, "Voltage on split ring electrode ".$srKATRINNumber[$sr], $link);
	  }
	    else placePicWithLink($srPosY[$sr]-32,$srPosX[$sr]+16, $pic."Off.png", 30, "status of split ring electrode", $link);
	}


	// ########## 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, 1585, "newTemperature.png", 23, "temperature in KATRIN hall", $adeiChannel[19][1]);
	placeTextWithLink(-30-3, 1605, sprintf('%.1F &#176;C', $adeiValue[19][1]), 80, 4, "#000000", "temperature in KATRIN hall", $adeiChannel[19][1]);

	placePicWithLink(-2-3, 1585, "newPressure.png", 23, "atmospheric pressure in KATRIN hall", $adeiChannel[19][0]);
	placeTextWithLink(-2-3, 1605, 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, 1585, "newHumidity.png", 23, "relative humidity in KATRIN hall", $adeiChannel[19][2]);
	placeTextWithLink(26-3, 1605, sprintf('%.1F &#037;', $adeiValue[19][2]), 80, 4, "#000000", "relative humidity in KATRIN hall", $adeiChannel[19][2]);



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

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


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

        $value = $adeiValue[27][7];
        $posOK = $adeiValue[27][6];
        $link = $adeiChannel[27][6].",".$adeiChannel[27][7];
        if ($value < 1) 
	{
	  $pic = "bottle.png";
	  $text = "OFF";
	  $color = "#909090";
	}
	else if (($value >= 1) && ($value < 15))
	{
          $pic = "bottleBlinking.gif";
	  $text = "ON";
	  $color = "#000000";
	}
	else if ($value >= 15)
	{
	  $pic = "bottleOn.png";
	  $text = sprintf('%.1F', $value)." %";
	  $color = "#000000";
          if ($posOK == 1) placeBox(418, 107, "Pos OK", 40, 2, "#00FF00");
	  else placeBox(418, 107, "PosErr", 40, 2, "#FF0000");
	}
	placePicWithLink(370, 113 , $pic, 30, "status PS gas injection system&#10;gray: off, blinking: low, green: on&#10Should be off for nominal pressure measurements!!!", $link);
	placeTextWithLink(402, 99, $text, 60, 2, $color, "status gas injection system", $link);




      // ########## PS PCS7 operation mode ##########

    $OpModeText = "PS mode: ";
    $color = "#FFFFFF";
    if ($PSmode == 0) { $OpModeText = $OpModeText." commissioning"; $color="#FFA0A0"; }
    else if ($PSmode == 1) $OpModeText = $OpModeText.$PSmode." pump down";
    else if ($PSmode == 2) $OpModeText = $OpModeText.$PSmode." venting";
    else if ($PSmode == 3) $OpModeText = $OpModeText.$PSmode." pumping";
    else if ($PSmode == 4) $OpModeText = $OpModeText.$PSmode." baking";
    else if ($PSmode == 5) { $OpModeText = $OpModeText.$PSmode." high voltage"; $color="#B0FFB0"; }
    placeBox(50, 10, $OpModeText, 250, 2, $color);


       // ########## PS PCS7 operation mode ##########

    $OpModeText = "MS mode: ";
    $color = "#FFFFFF";
    if ($MSmode == 0) { $OpModeText = $OpModeText." commissioning"; $color="#FFA0A0"; }
    else if ($MSmode == 1) $OpModeText = $OpModeText.$PSmode." pump down";
    else if ($MSmode == 2) $OpModeText = $OpModeText.$PSmode." venting";
    else if ($MSmode == 3) $OpModeText = $OpModeText.$PSmode." pumping";
    else if ($MSmode == 4) $OpModeText = $OpModeText.$PSmode." baking";
    else if ($MSmode == 5) { $OpModeText = $OpModeText.$PSmode." high voltage"; $color="#B0FFB0"; }
    placeBox(75, 10, $OpModeText, 250, 2, $color);



	// ########## TLK Wall and LAUDA TEMPERATURE ##########



if ($PSmode == 5) 
    { // only display if PS operation mode is HV

	// mean of 4023, 4033, 4043
        $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 < 4000) placeTextWithLink(440, 145,  "T<sub>PS</sub> = " . sprintf('%.1F &#176;C', $value), 120, 4, $color, "PS mean temperature of 4023..33..43", $link);
    }

if ($PSmode != 5) 
    { // only display if PS operation mode is Baking

	$min = 18;
	$max = 30;

	// sensor 4100 ID [29][3]
        $link = $adeiChannel[29][3];
	$value = $adeiValue[29][3];
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 400) placeTextWithLink(120, 5, "T<sub>wall</sub> = " . sprintf('%.1F &#176;C', $value), 120, 4, $color, "TLK separation wall temperature of RTP4100", $link);

	$min = 15;
	// LAUDA System 5401 [34][0..1]
        $link = $adeiChannel[34][0].",".$adeiChannel[34][1];
	$value = $adeiValue[34][0];
	$max = $adeiValue[34][1]+1;
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 400) placeTextWithLink(460, 110, "T<sub>LAUDA</sub> = " . sprintf('%.1F &#176;C', $value), 180, 4, $color, "LAUDA System actual temperature value", $link);



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

	$min = 18;
	$max = 65;

	// mean of 4023, 4033, 4043
        $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 < 4000) placeTextWithLink(305, 185, sprintf('%.1F &#176;C', $value), 90, 4, $color, "PS mean temperature of 4023..33..43", $link);

	// mean of 3901, 3902, 3903, 3904
        $link = $adeiChannel[29][4] . "," . $adeiChannel[29][5] . "," . $adeiChannel[29][6] . "," . $adeiChannel[29][7];
	$value = ($adeiValue[29][4] + $adeiValue[29][5] + $adeiValue[29][6] + $adeiValue[29][7])/4.0;
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 4000) placeTextWithLink(295, 110, sprintf('%.1F &#176;C', $value), 90, 4, $color, "PS mean temperature of 3902..03..04 upper NEG port", $link);

	// mean of 3912, 3913, (3914 failed)
        $link = $adeiChannel[29][8] . "," . $adeiChannel[29][9] . "," . $adeiChannel[29][10] . "," . $adeiChannel[29][11];
	//	$value = ($adeiValue[29][8] + $adeiValue[29][9] + $adeiValue[29][10] + $adeiValue[29][11])/4.0;
	$value = ($adeiValue[29][8] + $adeiValue[29][9] + $adeiValue[29][10])/3.0;
	$color = "#000000";
	if ($value < $min) $color = "#0026FF";
	else if ($value > $max) $color = "#FF0000";
	if ($value < 4000) placeTextWithLink(315, 110, sprintf('%.1F &#176;C', $value), 90, 4, $color, "PS mean temperature of 3912..13..14 side NEG port", $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";
	// ########## STS ##########
	placeTextWithWebsiteLink(360, 5, "&larr;STS", 60, 5, $color, "Link to STS Status Overview", "https://status-sts.kaas.kit.edu/");

	// ########## WebTrium Link ##########
	placeTextWithWebsiteLink(-130, 1250, "&rarr; WebTrium", 155, 4, $color, "Link to WebTrium Overview", "https://webtrium.mpp.mpg.de");

	// ########## FPD Status ##########
	placeTextWithWebsiteLink(-100, 1250, "&rarr; FPD Status", 155, 4, $color, "Link to FPD Status Overview", "https://neutrino.ikp.kit.edu/fpdstatus");

	// ########## Sanshiro Katana Page ##########
	placeTextWithWebsiteLink(-70, 1250, "&rarr; Brew Kaffee Beans and Idle", 275, 4, $color, "Link to Brew Kaffee Beans and Idle page at UW/Seattle", "http://katana.npl.washington.edu/~sanshiro/");

	// ########## MoS und HV Status ##########
	placeTextWithWebsiteLink(-40, 1240, "&rarr; MoS and HV Status", 240, 4, $color, "Link to Monitor Spectrometer and High Voltage Status Overview", "https://bora-mos.kaas.kit.edu");

	// ########## Magnet Overview ##########
	placeTextWithWebsiteLink(-10, 1240, "&rarr; Magnet Control System", 270, 4, $color, "Link to Magnet Control System Status Page", "https://bora-mcs.kaas.kit.edu");


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


	// ########## HIGH VOLTAGE SWITCH ##########
        $value = $adeiValue[31][1];
	$pic = "switch";
	if ($value == 1) $pic = $pic . "Open";
	else if ($value == 0) $pic = $pic . "Closed";
	placePicWithLink(474, 440, $pic . ".png", 80, "status high-voltage ground switch", $adeiChannel[31][1]);

?>


