#! /kroot/rel/default/bin/kpython3

### tz_convert.py
#   Written by Erin Redwing, 07/02/2019
#   Reads ddf file and outputs osirisTakeDataset command
#
#   Takes command line call in the form of:
#   python tz_convert.py obj filt
#   ex: python tz_convert.py Titan Kp

import sys
import os
import xml.etree.ElementTree as ET

def define_variables(ddf_filename):
    # parse ddf using ElementTree
    tree = ET.parse(ddf_filename)
    root = tree.getroot()
    variables_dict = {
            'dataset' : root[0].attrib['name'],
            'dataset_type' : root.attrib['type'],
            'obj' : root[0][0].text,
            'pa' : root[0][5].attrib['skyPA'],
            'aomode' : root[0].attrib['aomode'],
            'ifilter' : root[0][2][0].attrib['filter'],
            'mask' : root[0][2][0].attrib['mask'],
            'itime' : root[0][2][0].attrib['itime'],
            'icoadds' : root[0][2][0].attrib['coadds'],
            'irepeats' : root[0][2][0].attrib['repeats']
        }
    return variables_dict

def setMask(ddf_filename):
    tree = ET.parse(ddf_filename)
    root = tree.getroot()
    masks_dict = {
            'Open Circ' : '-Open',
            'Large Ann' : '-LAnn',
            'Small Ann' : '-sAnn',
            'Large Hex' : '-LHex',
            'Small Hex' : '-sHex',
            'HAM' : '-HAM'
        }
    mask_ddf = root[0][2][0].attrib['mask']
    return masks_dict[mask_ddf]

def otdStr(variables_dict, dither_str, mask):
    # strings for final command
    osd_str = 'osirisTakeDataset -d ' + variables_dict['dataset'] + ' -T ' + variables_dict['dataset_type']  + ' -o ' + variables_dict['obj']
    imager_str = ' -I \"Independent (Imager only)\" 1 ' + variables_dict['ifilter'] + mask + ' ' + variables_dict['irepeats'] + ' ' + variables_dict['itime'] + ' ' + variables_dict['icoadds']
    attrib_str = ' -P ' + variables_dict['pa'] + ' -A ' + variables_dict['aomode']
    cmd = osd_str + imager_str + attrib_str + dither_str
#    cmd = osd_str + imager_str + attrib_str + dither_str + ' -m ao tel'
    return cmd

def ditherStr(ddf_filename):
    # parse ddf using ElementTree
    tree = ET.parse(ddf_filename)
    root = tree.getroot()
    # if obj = Uranus, nod
#    if (root[0][0].text == 'Uranus'):
#        sframes = str(int(root[0][3].attrib['frames1']) + 1)
#    else: # else, box5 dither pattern
    sframes = root[0][3].attrib['frames1']
    coords = root[0][5].attrib['coords']
    dither_str = ' -D ' + sframes + ' ' + coords + ' '
    for i in range(int(sframes)):
        issky = int(root[0][5][i].attrib['sky'] == 'true')
        xoff = root[0][5][i].attrib['xOff']
        yoff = root[0][5][i].attrib['yOff']
        dither_str += str(issky) + ' ' + xoff + ' ' + yoff + ' '
    return dither_str

def main(ddf_filename):
    variables_dict = define_variables(ddf_filename)
    dither_str = ditherStr(ddf_filename)
    mask = setMask(ddf_filename)
    cmd = otdStr(variables_dict, dither_str, mask)
    print(cmd)

if __name__ == "__main__":
    # use obj and filter to read ddf file
    obj, filt = sys.argv[1], sys.argv[2]
    ddf_filename = '/kroot/rel/default' + '/data/tda/' + str(obj) + '_' + str(filt) + '.ddf'
    main(ddf_filename)
