#!/usr/bin/python

# autogen-steering-new-dfc -H 157.132.42.221 -F "80-01:80-11" -S SPSFB -T 250 --brightness 127 --width 10 --mask 0x3C0 --rate 9.5 --A0 128 --A1 128 --A2 128 --A3 128 --FADC 255 sps-ichub21 
# OLD: autogen-steering-new -H 157.132.42.221 -S SPS -F 21-11 -F 21-31 -T 500  sps-ichub21 sps-ithub01


"""
A script to generate the TestDAQ steering files.
$Id: autogen-steering,v 1.11 2005/04/22 04:03:51 krasberg Exp $
"""

import sys
# import math
from math import fmod

from steering_dfc import SteeringDeck
#from icecube.domtest.steering import SteeringDeck

from icecube.domtest.dor import Driver
from getopt import getopt
from xmlrpclib import ServerProxy
#from goto import goto, comefrom, label
#
# Default run parameters
#
host = 'localhost'
domname = ""
site = "SPSFB"
time = 200
brightness = "127";
width = "20";
mask = "0xFFF";
rate = "9.5";
A0 = "128";
A1 = "128";
A2 = "128";
A3 = "128";
FADC = "255";
RunTag = "";
TargetDOMs = "ALL";


opts, args = getopt(
    sys.argv[1:], 'H:F:S:T:h', [ 'dbhost=', 'help', 'brightness=', 'width=', 'mask=', 'rate=', 'A0=', 'A1=', 'A2=', 'A3=', 'FADC=', 'RunTag=', 'TargetDOMs=']
    )

for o, a in opts:
    if o == '--help' or o == '-h':
        print >>sys.stderr, """usage :: autogen-steering.py [ -hlst ] <hub-1> [ <hub-2> ... ]
    -H = <hostname>
       or
    --dbhost=<hostname> attach to database on remote host
    --brightness=<0-127> LED brightness
    --width=<10-?> LED pulse width in nanoseconds
    --mask=<0x000-0xFFF> FBLED mask
    --rate=<9.5Hz, 19Hz...> LED flashing rate, in multiples of 9.5Hz
    --A0[1,2,3]=,--FADC=<0-128> Number of digitization samples for ATWD channel 0,1,2,3 and FADC
    --RunTag=<the run tag, used to describe the run, will appear in the filename>
    --TargetDOMs=<comma-separated list of numbers of DOMs to be flashed.  If = ALL, flash all other DOMs  NOT YET IMPLEMENTED BELOW
"""
        sys.exit(1)
    elif o == '-H' or o == '--dbhost':
        host = a
    elif o == '-F':
        DOMsToFlash_str = a;     # Save the string so we can print it out in the filename below
        DOMsToFlash = a.split(':')  # Convert string in the form "29-1:29-2" into a list ['29-1','29-2']
    elif o == '-S':
        site = a
    elif o == '-T':
        time = int(a)
    elif o == '--brightness':
        brightness = int(a)
    elif o == '--width':
        width = int(a)*2;
    elif o == '--mask':
        mask = int(a,16)   # "mask" should be a hex number so convert it to decimal properly
    elif o == '--rate':
        BaseRate = 9.5;
        rate = float(a);
        rate = rate - fmod(rate,BaseRate); # Use nearest whole multiple of 9.5 for the rate.
    elif o == '--A0':
        A0 = int(a)
    elif o == '--A1':
        A1 = int(a)
    elif o == '--A2':
        A2 = int(a)
    elif o == '--A3':
        A3 = int(a)
    elif o == '--FADC':
        FADC = int(a)
    elif o == '--RunTag':
        RunTag = a
    elif o == '--TargetDOMs':
        TargetDOMs = a
hubs = args

print "Running with site = %s" % site;

#dfc DOMidToHub = {};
if len(hubs) == 0:
    # local connection
    d = Driver()
    d.scan()
    doms = d.discover_doms()
else:
    doms = dict()
    # go through a remote proxy
    for h in hubs:
        d = ServerProxy('http://' + h + ':7501')
        d.scan()
        dh = d.discover_doms()
        for domid, domloc in dh.items():
            dh[domid] = [ h ] + domloc
        doms.update(dh)
#
#dfc: I think we can apply a list-of-DOMs-to-read-out mask here.
#
#dfc        DOMidToHub[domid] = h;

if (DOMsToFlash == ""):
#   goto .FlasherStuff
 if (site == "FAT"):
  print "starting up TimeResolution..."
  for atwdChip in (0, 1):
    for frequency in (1500, 15000):
        deck = SteeringDeck(host)
        deck.addTest('Time Resolution Test v1.2 (MK)',
                     doms,
                     executionTime=time,
                     samples=(32, 0, 0, 0),
                     pulserMode=4,
                     pulserFreq=frequency,
                     atwd=atwdChip,
                     filterWheel=1,
		     site=site,
                     )
        f = file("TimeResolution-ATWD%d-%dHz.xml" % (atwdChip, frequency), "wt")
        print >>f, deck
        f.close()

 if (site == "FAT"):
  print "starting up TimeResolution-LC..."
  for atwdChip in (0, 1):
   for frequency in (200, 15000):
     deck = SteeringDeck(host)
     deck.addTest('TimeResolution LC v1.0 (MK)',
                 doms,
                 executionTime=time,
                 samples=(32, 0, 0, 0),
                 pulserMode=4,
                 pulserFreq=frequency,
                 atwd=atwdChip,
                 filterWheel=1,
                 fadc=0,
                 trigMode=2,
		 localCoinc=800,
		 site=site,
                 )
     f = file("TimeResolution-LC-ATWD%d-%dHz.xml" % (atwdChip,frequency), "wt")
     print >>f, deck
     f.close()

        
# print "starting up OpticalSensitivity steering file creation..."
# for wavelen in range(320, 420, 20):
#    deck = SteeringDeck(host)
#    deck.addTest('Optical Sensitivity Test v1.0 (KDH)',
#                 doms,
#                 executionTime=200,
#                 samples=(0, 0, 0, 0),
#                 pulserMode=5,
#                 pulserFreq=100,
#                 filterWheel=3,
#                 monochromatorWavelen=wavelen
#                 )
#    f = file("OpticalSensitivity-%d.xml" % (wavelen), "wt")
#    print >>f, deck
#    f.close()

 if ((site == "FAT") | (site == "SPTS")):
  print "starting up Linearity-LC Big Pulse steering file creation..."
  for filter_pos in range(1,5):
    deck = SteeringDeck(host)
    deck.addTest('Non-linearity Test v2.0 (MK)',
                 doms,
                 executionTime=time,
                 samples=(128, 128, 128, 0),
                 fadc=255,
                 pulserMode=3,
                 pulserBrightness=4095,
                 pulserFreq=25,
                 filterWheel=filter_pos,
                 trigMode=2,
                 localCoinc=800,
		 site=site,
                 )
    f = file("Linearity-LC-BigPulse-Bright%d-FW%d.xml" % (4095,filter_pos), "wt")
    print >>f, deck
    f.close()

 if ((site == "FAT") | (site == "SPTS")):
  print "starting up Linearity-LC Big Pulse FW5 variable brightness steering file creation..."
  for pulser_brightness in (1024, 2048, 4095):
    deck = SteeringDeck(host)
    deck.addTest('Non-linearity Test v2.0 FW5 variable brightness (MK)',
                 doms,
                 executionTime=time,
                 samples=(128, 128, 128, 0),
                 fadc=255,
                 pulserMode=3,
                 pulserBrightness=pulser_brightness,
                 pulserFreq=25,
                 filterWheel=5,
                 trigMode=2,
                 localCoinc=800,
		 site=site,
                 )
    f = file("Linearity-LC-BigPulse-Bright%d-FW5.xml" % (pulser_brightness), "wt")
    print >>f, deck
    f.close()


 if ((site == "FAT") | (site == "SPTS")):
   print "starting up Linearity-LC Standard Pulse steering file creation"
   for filter_pos in range(1,6):
    for pulser_brightness in (2048, 4095):
# for atwdChip in (0, 1):
     deck = SteeringDeck(host)
     deck.addTest('LinearityLC v1.0 (MK)',
                 doms,
                 executionTime=time,
                 samples=(128, 128, 128, 0),
                 fadc=255,
#                 atwd=atwdChip,
                 pulserMode=1,
                 pulserFreq=25,
		 pulserBrightness=pulser_brightness,
                 filterWheel=filter_pos,
                 trigMode=2,
		 localCoinc=800,
		 site=site,
                 )
#    f = file("LinearityLC-%d-ATWD%d.xml" % (atwdChip), "wt")
     f = file("Linearity-LC-MarkerPulse-Bright%d-FW%d.xml" % (pulser_brightness,filter_pos), "wt")
     print >>f, deck
     f.close()


 if (site == "FAT"):
  print "starting up GainVsHV steering file creation..."
  for hv1 in range(1200, 1900, 100):
    for atwdChip in (0, 1):
        deck = SteeringDeck(host)
        deck.addTest('Gain vs. HV Test v1.0 (KDH)',
                     doms,
                     executionTime=time,
                     samples=(32, 32, 0, 0),
                     fadc=0,
                     atwd=atwdChip,
                     pulserMode=4,
                     pulserFreq=1000,
                     filterWheel=1,
                     commonHV=hv1,
		     site=site,
                     )
        f = file("GainVsHV-%d-ATWD%d.xml" % (hv1, atwdChip), "wt")
        print >>f, deck
        f.close()

 print "starting up PedestalPattern steering file creation..."
 for atwdChip in (0, 1):
    deck = SteeringDeck(host)
    deck.addTest('Pedestal Pattern Test v1.0 (KDH)',
                 doms,
                 executionTime=time,
                 samples=(128, 128, 128, 128),
                 fadc=255,
                 trigMode=1,
                 atwd=atwdChip,
                 pulserMode=0,
                 pulserFreq=10,
                 filterWheel=6,
                 commonHV=0,
		 site=site,
                 )
    f = file("PedestalPattern-ATWD%d.xml" % (atwdChip), "wt")
    print >>f, deck
    f.close()

 print "starting up DarkNoise steering file creation..."
 for atwdChip in (0, 1):
    deck = SteeringDeck(host)
    deck.addTest('Dark Noise Test v2.0 (MK)',
                 doms,
                 executionTime=time,
                 samples=(32, 0, 0, 0),
                 atwd=atwdChip,
                 fadc=0,
                 trigMode=2,
                 pulserMode=0,
                 pulserFreq=10,
                 filterWheel=6,
		 site=site,
                 hvsetting=1
                 )
    f = file("DarkNoise-ATWD%d-V2.xml" % (atwdChip), "wt")
    print >>f, deck
    f.close()

 print "starting up DarkNoise Full Readout steering file creation..."
 for atwdChip in (0, 1):
     deck = SteeringDeck(host)
     deck.addTest('Dark Noise Test Full Readout v1.0 (MK)',
                                           doms,
                                           executionTime=time,
                                           samples=(128, 128, 128, 0),
                                           atwd=atwdChip,
                                           fadc=255,
                                           trigMode=2,
                                           pulserMode=0,
                                           pulserFreq=10,
                                           filterWheel=6,
					   site=site,
                                           )
     f = file("DarkNoise-FullReadout-ATWD%d.xml" % (atwdChip), "wt")
     print >>f, deck
     f.close()
   
 print "starting up DarkNoise Multi-PE Full Readout steering file creation..."
 for atwdChip in (0, 1):
     deck = SteeringDeck(host)
     deck.addTest('Dark Noise MultiPE Full Readout v1.0 (MK)',
                                           doms,
                                           executionTime=time,
                                           samples=(128, 128, 128, 0),
                                           atwd=atwdChip,
                                           fadc=255,
                                           trigMode=2,
                                           pulserMode=0,
                                           pulserFreq=10,
                                           filterWheel=6,
					   site=site,
                                           spe=1,
                                           )
     f = file("DarkNoise-MultiPE-FullReadout-ATWD%d.xml" % (atwdChip), "wt")
     print >>f, deck
     f.close()

 if (site == "SPS"):
   print "starting up DarkNoise Multi-PE 5E6 Gain Full Readout steering file creation..."
   for atwdChip in (0, 1):
     deck = SteeringDeck(host)
     deck.addTest('Dark Noise MultiPE 5E6Gain Full Readout v1.0 (MK)',
                                           doms,
                                           executionTime=200,
                                           samples=(128, 128, 128, 0),
                                           atwd=atwdChip,
                                           fadc=255,
                                           trigMode=2,
                                           pulserMode=0,
                                           pulserFreq=10,
                                           filterWheel=6,
					   site=site,
                                           spe=800,
					   hvsetting=2,
                                           )
     f = file("DarkNoise-MultiPE-5E6Gain-FullReadout-ATWD%d.xml" % (atwdChip), "wt")
     print >>f, deck
     f.close()
   

                         
 print "starting up LocalCoincidence..."
 for atwdChip in (0, 1):
    deck = SteeringDeck(host)
    deck.addTest('Local Coincidence v3.0 (MK)',
                 doms,
                 executionTime=time,
                 samples=(128, 32, 32, 0),
                 samplesIT=(64, 64, 64, 0),
                 atwd=atwdChip,
                 fadc=56,
                 fadcIT=0,
                 trigMode=2,
		 localCoinc=800,
		 site=site,
                 )
    f = file("LC-ATWD%d-V2.xml" % (atwdChip), "wt")
    print >>f, deck
    f.close()

 print "starting up MultiPE LocalCoincidence..."
 for atwdChip in (0, 1):
    deck = SteeringDeck(host)
    deck.addTest('Local Coincidence v2.0 (MK)',
                 doms,
                 executionTime=time,
                 samples=(128, 32, 32, 0),
                 atwd=atwdChip,
                 fadc=56,
                 trigMode=2,
		 localCoinc=800,
		 site=site,
	 	 spe=850,
                 )
    f = file("LC-MultiPE-ATWD%d.xml" % (atwdChip), "wt")
    print >>f, deck
    f.close()


#label .FlasherStuff
if (site == "SPSFB"):
# else:
 print "starting up Flasherboard steering file creation..."
 position = '';
 if ((domname == 'Bat') | (domname == '21-01')):
  domname = 'Bat';
  position = '21-01';
 if ((domname == 'Conchiglie') | (domname == '21-02')):
  domname = 'Conchiglie';
  position = '21-02';
 if ((domname == 'Summer_ale') | (domname == '21-03')):
  domname = 'Summer_ale';
  position = '21-03';
 if ((domname == 'Topi') | (domname == '21-04')):
  domname = 'Topi';
  position = '21-04';
 if ((domname == 'Dubbel') | (domname == '21-05')):
  domname = 'Dubbel';
  position = '21-05';
 if ((domname == 'Squirrel') | (domname == '21-06')):
  domname = 'Squirrel';
  position = '21-06';
 if ((domname == 'Trappist') | (domname == '21-07')):
  domname = 'Trappist';
  position = '21-07';
 if ((domname == 'Rhinoceros') | (domname == '21-08')):
  domname = 'Rhinoceros';
  position = '21-08';
 if ((domname == 'Erik_the_Red') | (domname == '21-09')):
  domname = 'Erik_the_Red';
  position = '21-09';
 if ((domname == 'Ragnar_Lodbrok') | (domname == '21-10')):
  domname = 'Ragnar_Lodbrok';
  position = '21-10';
 if ((domname == 'Erik_Segersaell') | (domname == '21-11')):
  domname = 'Erik_Segersaell';
  position = '21-11';
 if ((domname == 'Sverker') | (domname == '21-12')):
  domname = 'Sverker';
  position = '21-12';
 if ((domname == 'Wickueler') | (domname == '21-13')):
  domname = 'Wickueler';
  position = '21-13';
 if ((domname == 'Reissdorf_Koelsch') | (domname == '21-14')):
  domname = 'Reissdorf_Koelsch';
  position = '21-14';
 if ((domname == 'Pilsener_Urquell') | (domname == '21-15')):
  domname = 'Pilsener_Urquell';
  position = '21-15';
 if ((domname == 'Radeberger_Pilsener') | (domname == '21-16')):
  domname = 'Radeberger_Pilsener';
  position = '21-16';
 if ((domname == 'Wittinger') | (domname == '21-17')):
  domname = 'Wittinger';
  position = '21-17';
 if ((domname == 'Veltins') | (domname == '21-18')):
  domname = 'Veltins';
  position = '21-18';
 if ((domname == 'Rhenenia_Alt') | (domname == '21-19')):
  domname = 'Rhenenia_Alt';
  position = '21-19';
 if ((domname == 'Schneider_Weisse') | (domname == '21-20')):
  domname = 'Schneider_Weisse';
  position = '21-20';
 if ((domname == 'Antelope') | (domname == '21-21')):
  domname = 'Antelope';
  position = '21-21';
 if ((domname == 'Wombat') | (domname == '21-22')):
  domname = 'Wombat';
  position = '21-22';
 if ((domname == 'Lemur') | (domname == '21-23')):
  domname = 'Lemur';
  position = '21-23';
 if ((domname == 'Porcupine') | (domname == '21-24')):
  domname = 'Porcupine';
  position = '21-24';
 if ((domname == 'Gnu') | (domname == '21-25')):
  domname = 'Gnu';
  position = '21-25';
 if ((domname == 'Mouse') | (domname == '21-26')):
  domname = 'Mouse';
  position = '21-26';
 if ((domname == 'Dolphin') | (domname == '21-27')):
  domname = 'Dolphin';
  position = '21-27';
 if ((domname == 'Monkey') | (domname == '21-28')):
  domname = 'Monkey';
  position = '21-28';
 if ((domname == 'Syrah') | (domname == '21-29')):
  domname = 'Syrah';
  position = '21-29';
 if ((domname == 'Phenol') | (domname == '21-30')):
  domname = 'Phenol';
  position = '21-30';
 if ((domname == 'Dimethylsulfoxide') | (domname == '21-31')):
  domname = 'Dimethylsulfoxide';
  position = '21-31';
 if ((domname == 'Cholesterol') | (domname == '21-32')):
  domname = 'Cholesterol';
  position = '21-32';
 if ((domname == 'Oatmeal_stout') | (domname == '21-33')):
  domname = 'Oatmeal_stout';
  position = '21-33';
 if ((domname == 'Lima_bean') | (domname == '21-34')):
  domname = 'Lima_bean';
  position = '21-34';
 if ((domname == 'Dimethylamine') | (domname == '21-35')):
  domname = 'Dimethylamine';
  position = '21-35';
 if ((domname == 'Glycerol') | (domname == '21-36')):
  domname = 'Glycerol';
  position = '21-36';
 if ((domname == 'Anthracene') | (domname == '21-37')):
  domname = 'Anthracene';
  position = '21-37';
 if ((domname == 'Indole') | (domname == '21-38')):
  domname = 'Indole';
  position = '21-38';
 if ((domname == 'Parsley') | (domname == '21-39')):
  domname = 'Parsley';
  position = '21-39';
 if ((domname == 'Diethyl_ether') | (domname == '21-40')):
  domname = 'Diethyl_ether';
  position = '21-40';
 if ((domname == 'Cayenne_pepper') | (domname == '21-41')):
  domname = 'Cayenne_pepper';
  position = '21-41';
 if ((domname == 'Porter') | (domname == '21-42')):
  domname = 'Porter';
  position = '21-42';
 if ((domname == 'Chardonnay') | (domname == '21-43')):
  domname = 'Chardonnay';
  position = '21-43';
 if ((domname == 'Napthalene') | (domname == '21-44')):
  domname = 'Napthalene';
  position = '21-44';
 if ((domname == 'Hamachi') | (domname == '21-45')):
  domname = 'Hamachi';
  position = '21-45';
 if ((domname == 'Tetrahydrofuran') | (domname == '21-46')):
  domname = 'Tetrahydrofuran';
  position = '21-46';
 if ((domname == 'Avocado') | (domname == '21-47')):
  domname = 'Avocado';
  position = '21-47';
 if ((domname == 'Riesling') | (domname == '21-48')):
  domname = 'Riesling';
  position = '21-48';
 if ((domname == 'Suzuki') | (domname == '21-49')):
  domname = 'Suzuki';
  position = '21-49';
 if ((domname == 'Loquat') | (domname == '21-50')):
  domname = 'Loquat';
  position = '21-50';
 if ((domname == 'Argon') | (domname == '21-51')):
  domname = 'Argon';
  position = '21-51';
 if ((domname == 'Nitrogen') | (domname == '21-52')):
  domname = 'Nitrogen';
  position = '21-52';
 if ((domname == 'Sulfur') | (domname == '21-53')):
  domname = 'Sulfur';
  position = '21-53';
 if ((domname == 'Fluorine') | (domname == '21-54')):
  domname = 'Fluorine';
  position = '21-54';
 if ((domname == 'Vanadium') | (domname == '21-55')):
  domname = 'Vanadium';
  position = '21-55';
 if ((domname == 'Oxygen') | (domname == '21-56')):
  domname = 'Oxygen';
  position = '21-56';
 if ((domname == 'Beryllium') | (domname == '21-57')):
  domname = 'Beryllium';
  position = '21-57';
 if ((domname == 'Sodium') | (domname == '21-58')):
  domname = 'Sodium';
  position = '21-58';
 if ((domname == 'Ebi') | (domname == '21-59')):
  domname = 'Ebi';
  position = '21-59';
 if ((domname == 'Bucatini') | (domname == '21-60')):
  domname = 'Bucatini';
  position = '21-60';
 if ((domname == 'Ziti') | (domname == '21-61')):
  domname = 'Ziti';
  position = '21-61';
 if ((domname == 'Uni') | (domname == '21-62')):
  domname = 'Uni';
  position = '21-62';
 if ((domname == 'Brown_ale') | (domname == '21-63')):
  domname = 'Brown_ale';
  position = '21-63';
 if ((domname == 'Fig') | (domname == '21-64')):
  domname = 'Fig';
  position = '21-64';
 if ((domname == 'Cerrano_pepper') | (domname == '29-61')):
  domname = 'Cerrano_pepper';
  position = '29-61';
 if ((domname == 'Helium') | (domname == '29-62')):
  domname = 'Helium';
  position = '29-62';
 if ((domname == 'Tako') | (domname == '29-63')):
  domname = 'Tako';
  position = '29-63';
 if ((domname == 'Manicotti') | (domname == '29-64')):
  domname = 'Manicotti';
  position = '29-64';
 if ((domname == 'Akagai') | (domname == '30-61')):
  domname = 'Akagai';
  position = '30-61';
 if ((domname == 'Lithium') | (domname == '30-62')):
  domname = 'Lithium';
  position = '30-62';
 if ((domname == 'Carbon') | (domname == '30-63')):
  domname = 'Carbon';
  position = '30-63';
 if ((domname == 'Black_bean') | (domname == '30-64')):
  domname = 'Black_bean';
  position = '30-64';
 if ((domname == 'Hydrogen') | (domname == '39-61')):
  domname = 'Hydrogen';
  position = '39-61';
 if ((domname == 'Unagi') | (domname == '39-62')):
  domname = 'Unagi';
  position = '39-62';
 if ((domname == 'Tsubugai') | (domname == '39-63')):
  domname = 'Tsubugai';
  position = '39-63';
 if ((domname == 'Tofe') | (domname == '39-64')):
  domname = 'Tofe';
  position = '39-64';



ATWD0Samples = int(A0);
ATWD1Samples = int(A1);
ATWD2Samples = int(A2);
ATWD3Samples = int(A3);
FADCSamples  = int(FADC);
atwdChip = 0
print DOMsToFlash;
deck = SteeringDeck(host)
deck.addTest('Flasherboard v3.0 (MK)',
             doms,
             executionTime=time,
             samples=(ATWD0Samples,ATWD1Samples,ATWD2Samples,ATWD3Samples),
             atwd=atwdChip,
             fadc=FADCSamples,
             trigMode=2,
             localCoinc=800,
             domnames=DOMsToFlash,
             FBbrightness=int(brightness),
             FBwidth=int(width),
             FBmask=mask,
             FBrate=rate,
             site=site,
             )
#
# Format the list of domnames into a string, i.e., go from
# ["25-1","25-2"] to "25-1:25-2"
#
#for domname in DOMsToFlash:
#    DOMsToFlash_str += str(domname);
#    DOMsToFlash_str += ":";
#DOMsToFlash_str = DOMsToFlash_str.rstrip(':');


xmlFilename = "Flasherboard-LC-%s-%s-Bright%3.3d-Width%3.3d-Mask%3.3X-Rate%4.4d-ATWD%d.xml" % (DOMsToFlash_str, RunTag, int(brightness), int(width), mask, int(rate), atwdChip)
f = file(xmlFilename, "wt");

print >>f, deck;

f.close();

#
# March through the xml file, remembering the numbers of the lines
# L_a,L_b that bracket each <dom...> <\dom> xml element.
# Extract dom_number from the comment line inserted earlier by this script.
# Build a dictionary with key=dom_number and values=L_a,L_b
# Finally, write out a file with a name that includes the DOMName.
#
#xmlFileLines = open(xmlFilename).read();
#LineNum = 0;
#LineNumA = -1;
#LineNumB = -1;
#for (line in xmlFileLines):
#    if ("<domConfiguration" in line):
#        LastLineInPreamble = LineNum;
#    if ("<dom domId" in line):
#        LineNumA = LineNum
#    if ("DOMPosition:" in Line):
#        DOMPosition = DFC???
#    if ("<\dom" in line):
#        LineNumB = LineNum
#    if ("</domConfiguration" in line):
#        FirstLineInPostamble = LineNum;
#        LastLineInPostamble = len(xmlFileLines)-1;
#    LineNum += 1;
#    if (LineNumA >= 0 and LineNumB >=0):
#        DOMPositionToLinesAB[DOMPosition] = (LineNumA,LineNumB);
#        LineNumA = -1;
#        LineNumB = -1;
#
#DOMPositionToLinesAB.sort().keys;
#
#LineNum = 0;
#for (LineNum in range 1..len(xmlFileLines)-1):
#    if (LineNum <= LastLineInPreamble):
#        print line;
#    for sorted keys:
#        LineNumA = DOMPositionToLinesAB[key](0)
#        LineNumB = DOMPositionToLinesAB[key](1)
#        if (LineNumA <= LineNum and LineNum <= LineNumB):
    
        



# Add "useless" run to reset DOMs for some reason
#
FBbrightness = 127;
FBwidth = 20;
FBmask = 0xFFF;
FBrate=10
atwdChip = 0
deck = SteeringDeck(host)
deck.addTest('Flasherboard v3.0 (MK)',
                 doms,
                 executionTime=10,
                 samples=(128, 128, 128, 0),
                 atwd=atwdChip,
                 fadc=255,
                 trigMode=2,
		 localCoinc=800,
                 domnames=DOMsToFlash,
                 FBbrightness=FBbrightness,
                 FBwidth=FBwidth,
                 FBmask=FBmask,
                 FBrate=FBrate,
		 site=site,
		 )
f = file("Flasherboard-LC-%s-%s-AAA-Useless-Primer-Run-To-Avoid-HV-Interlock.xml" % ( 
            DOMsToFlash_str,RunTag), "wt")
print >>f, deck
f.close()

#
# Sort final xml file by 21-01,21-02...
#
