#!/usr/bin/python



"""
A script to generate the TestDAQ steering files.
$Id: autogen-steering,v 1.8 2004/10/12 19:26:43 kaeld Exp $
"""

import sys
from icecube.domtest.steering import SteeringDeck
from icecube.domtest.dor import Driver
from getopt import getopt
from xmlrpclib import ServerProxy

host = 'localhost'

opts, args = getopt(
    sys.argv[1:], 'H:h', [ 'dbhost=', 'help' ]
    )

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
"""
        sys.exit(1)
    elif o == '-H' or o == '--dbhost':
        host = a

hubs = args

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)

for atwdChip in (0, 1):
    for frequency in (1500, 15000):
        deck = SteeringDeck(host)
        deck.addTest('Time Resolution Test v1.2 (MK)',
                     doms,
                     executionTime=200,
                     samples=(32, 0, 0, 0),
                     pulserMode=4,
                     pulserFreq=frequency,
                     atwd=atwdChip,
                     filterWheel=1
                     )
        f = file("TimeResolution-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()

print "starting up Linearity steering file creation..."
for filter_pos in range(1,6):
    deck = SteeringDeck(host)
    deck.addTest('Non-linearity Test v1.0 (KDH)',
                 doms,
                 executionTime=200,
                 samples=(128, 128, 128, 0),
                 fadc=250,
                 pulserMode=1,
                 pulserFreq=10,
                 filterWheel=filter_pos
                 )
    f = file("Linearity-%d.xml" % (filter_pos), "wt")
    print >>f, deck
    f.close()

print "starting up GainVsHV steering file creation..."
for hv in range(1200, 1900, 100):
    for atwdChip in (0, 1):
        deck = SteeringDeck(host)
        deck.addTest('Gain vs. HV Test v1.0 (KDH)',
                     doms,
                     executionTime=200,
                     samples=(32, 32, 0, 0),
                     fadc=0,
                     atwd=atwdChip,
                     pulserMode=4,
                     pulserFreq=1000,
                     filterWheel=1,
                     commonHV=hv
                     )
        f = file("GainVsHV-%d-ATWD%d.xml" % (hv, 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=200,
                 samples=(128, 128, 128, 128),
                 fadc=255,
                 trigMode=1,
                 atwd=atwdChip,
                 pulserMode=0,
                 pulserFreq=10,
                 filterWheel=6,
                 commonHV=0
                 )
    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 v1.0 (KDH)',
                 doms,
                 executionTime=200,
                 samples=(32, 0, 0, 0),
                 atwd=atwdChip,
                 fadc=0,
                 trigMode=2,
                 pulserMode=0,
                 pulserFreq=10,
                 filterWheel=6
                 )
    f = file("DarkNoise-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 v1.0 (MK)',
                 doms,
                 executionTime=200,
                 samples=(128, 0, 0, 0),
                 atwd=atwdChip,
                 fadc=255,
                 trigMode=2,
		 localCoinc=800,
                 )
    f = file("LocalCoincidence-ATWD%d.xml" % (atwdChip), "wt")
    print >>f, deck
    f.close()


print "starting up Flasherboard steering file creation..."

domname = 'Parsley'
if (domname == 'Parsley'):
    domnumber = 39
if (domname == 'Indole'):
    domnumber = 38
if (domname == 'Lemur'):
    domnumber = 23
for FBbrightness in (64, 127):
    for FBwidth in (20 , 127):
      for FBmask in (0xFFF, 0x001, 0x008, 0x040, 0x200):
          FBrate=610
          atwdChip = 0
          deck = SteeringDeck(host)
          deck.addTest('Flasherboard v1.0 (MK)',
                 doms,
                 executionTime=200,
                 samples=(128, 128, 128, 0),
                 atwd=atwdChip,
                 fadc=255,
                 trigMode=2,
		 localCoinc=800,
                 domname=domname,
                 FBbrightness=FBbrightness,
                 FBwidth=FBwidth,
                 FBmask=FBmask,
                 FBrate=FBrate  )
          f = file("Flasherboard-%2.2d%s-Bright%3.3d-Width%3.3d-Mask%3.3X-Rate%4.4d-ATWD%d.xml" % (domnumber, 
            domname, FBbrightness, FBwidth, FBmask, FBrate, atwdChip), "wt")
          print >>f, deck
          f.close()
