# File   : scriptAPIsetup.py
# Purpose: Initialize the EVM scripting environment for JPython.
# Author : Nick Kornweibel
# Date   : Tue Jun 19 13:00:43 MET DST 2001
#

# Import the scripting extension interface

import org.eso.ohs.scripting

# getScriptingBox
# Return the scriptingBox object

def getScriptingBox():
    return org.eso.ohs.scripting.ScriptingBox.getInstance()

# infoMsg
# Add string text to the verification report. The EVM does not process text
# in any way.

def infoMsg(msg):
    scriptingBox = getScriptingBox()
    scriptingBox.infoMessage(msg)

# warningMsg
# Report a warning. String text is added to the verification report prefixed
# by the string "warning"; the warning counter is incremented by one.

def warningMsg(msg):
    scriptingBox = getScriptingBox()
    scriptingBox.warningMessage(msg)

# errorMsg
# Report a verification error. String text is added to the verification
# report prefixed by "error"; the error counter is incremented by one

def errorMsg(msg):
    scriptingBox = getScriptingBox()
    scriptingBox.errorMessage(msg)

# headerMsg
# Over write the default report header with this header messages.
# Subsequent calls to headerMsg after the first will append to the
# report header.

def headerMsg(msg):
    scriptingBox = getScriptingBox()
    scriptingBox.headerMessage(msg)

# footerMsg
# Over write the default report footer with this footer messages.
# Subsequent calls to footerMsg after the first will append to the
# report footer.

def footerMsg(msg):
    scriptingBox = getScriptingBox()
    scriptingBox.footerMessage(msg)

# resetErrors:
# Tell the application that all errors so far reported are to be cleared.

def resetErrors():
    scriptingBox = getScriptingBox()
    scriptingBox.clearErrors()

# resetWarnings:
# Tell the application that all warnings so far reported are to be cleared.

def resetWarnings():
    scriptingBox = getScriptingBox()
    scriptingBox.clearWarnings()

# Test procedure for testing the API in the JPython script extension.

def evmTest():
    scriptingBox = getScriptingBox()
    apiVersion = scriptingBox.getVersion()
    infoMsg("Python API Generated Report")
    infoMsg("API Version: "+apiVersion)
    infoMsg("Test INFO message")
    warningMsg("Test WARNING message")
    errorMsg("Test ERROR message")
    resetWarnings()
    resetErrors()
    warningMsg("Should be the first WARNING message")
    errorMsg("Should be the first ERROR message")
    obcount = scriptingBox.getNumberOfObservationBlocks()
    obidx = 0
    while obidx < obcount:
        ob = scriptingBox.getObservationBlockAt(obidx)
        infoMsg("Name: "+ob.getName()+" Id: "+ob.getId())
        infoMsg("Type: "+ob.getType()+" Run Id: "+ob.getRunID())
        infoMsg("Inst: "+ob.getInstrumentName())
        infoMsg("Exp Time: "+str(ob.getExpTime()))
        infoMsg("Exec Time: "+str(ob.getExecutionTime()))

        # target
        targ = ob.getTarget()
        if targ == None:
            infoMsg("No Target")
        else:
            utils = scriptingBox.getUtils()
            infoMsg("Target Name: "+targ.getName()+" Target id: "+targ.getId())
            infoMsg("Target RA           : "+targ.getRightAscension())
            ramas = utils.getRAMilliarcseconds(targ.getRightAscension())
            infoMsg("Target RA (millarc) : "+str(ramas))
            infoMsg("Target Declination  : "+targ.getDeclination())
            decmas = utils.getDecMilliarcseconds(targ.getDeclination())
            infoMsg("Target Dec (millarc): "+str(decmas))
            infoMsg("Target Equinox      : "+targ.getEquinox())
            infoMsg("Target Epoch        : "+targ.getEpoch())

        # constraint set
        cs = ob.getConstraintSet()
        if cs == None:
            infoMsg("No Constraint Set")
        else:
            infoMsg("CS Name: "+cs.getName()+" CS id: "+cs.getId())
            infoMsg("Air Mass         : "+cs.getAirmass())
            infoMsg("Frac. Lunar Ill. : "+cs.getFractionalLunarIllumination())
            infoMsg("Sky Transp.      : "+cs.getSkyTransparency())
            infoMsg("Moon Angular Dis.: "+str(cs.getMoonAngularDistance()))
            infoMsg("Seeing           : "+cs.getSeeing())
            infoMsg("Tele. Temp       : "+str(cs.getTelTemp()))
            infoMsg("Water Vapour     : "+cs.getWaterVapour())
            infoMsg("Comments         : "+cs.getDescription())
            infoMsg("Strehl Ratio     : "+str(cs.getStrehlRatio()))

        # time intervals
        tiset = ob.getTimeIntervals()
        # if tiset == None or tiset.getNumberOfTimeIntervals() == 0:
        if tiset == None:
            infoMsg("No time intervals")
        else:
            if tiset.getNumberOfTimeIntervals() == 0:
                infoMsg("No time intervals in time interval set")
            else:
                tiidx = 0
                infoMsg("Time intervals: "
                        +str(tiset.getNumberOfTimeIntervals()))

                while tiidx < tiset.getNumberOfTimeIntervals():
                    ti = tiset.getTimeInterval(tiidx)
                    infoMsg("TI Name      : "+ti.getName()+" Ti id: "+ti.getId())
                    infoMsg("Start Time   : "+str(ti.getStartTime()))
                    infoMsg("End Time     : "+str(ti.getEndTime()))
                    infoMsg("Preference   : "+str(ti.getPreference()))
                    tiidx = tiidx + 1

        # templates
        tid = 0
        tcount = ob.getNumberOfODTemplates()
        infoMsg("OD Template count: "+str(tcount))
        while tid <= tcount:
            temp = ob.getTemplateAt(tid)
            if temp == None:
                infoMsg("Empty template at idx: "+str(tid))
            else:
                infoMsg("Template "+str(tid)+" name: "+temp.getName())
                infoMsg("Template "+str(tid)+" id: "+temp.getId())
                infoMsg("Template "+str(tid)+" type: "+temp.getTemplateType())
                infoMsg("Template "+str(tid)+" acq time: "+str(temp.getAcquisitionTime()))

                # get the INS.SLIT.NO paf-param from the INS.GBR.SETUP param
                pnm = "INS.GBR.SETUP"
                pfnm = "INS.SLIT.NO"
                utils = scriptingBox.getUtils()
                pafval = temp.getParameterValue(pnm)
                pafTable = utils.parsePAF(pafval)
                pvals = pafTable.getValuesForParam(pfnm)
                # we are only interested in the first match
                if pvals.getElementCount() > 0:
                    pval = pvals.getElementAt(0)
                else:
                    pval = ""
                infoMsg("PAF "+pnm+" "+pfnm+"="+pval)

                # loop through all params and print the names out
                pcount = temp.getNumberOfParameters()
                pidx = 0
                while pidx < pcount:
                    pname = temp.getParameterNameAt(pidx)
                    pval = temp.getParameterValueAt(pidx)
                    # only want to print short value strings ...
                    if len(pval) > 25:
                        pval = pval[0:20]+"..."
                    pidx = pidx + 1
                    infoMsg("Param "+str(pidx)+": "+pname+" "+pval)
            tid = tid + 1
        
     
        obidx = obidx + 1
# end of file
