# File   : orangAPIsetup.tcl
# Purpose: Initialize the TCL scripting environment for ORANG
# Author : Paulo Nunes
# Date   : Fri Aug 26  MET DST 2005
#
#
# define the procedures that are available to the TCL script developers
# based on scriptAPIsetup.tcl wrote by Nick Kornweibel


# include java
package require java


# init:
# Initialization procedure. Must be called at the beginning to set up the 
# environment: build the scripting box, initialize the data structures, etc. 
proc init {} {
    global scriptingBox
    set scriptingBox [java::call org.eso.ohs.phase2.orang.OrangScriptingBox getInstance]
 
}


proc getOBCount {}  {
	global scriptingBox
    return [$scriptingBox getNumberOfObservationBlocks]

}

# getOB:
# return the OB at the specified index (or null if the index is out of
# range). First index is 0.
# returned objects should be checked using evmValid

proc getOB {index} {
    global scriptingBox
    return [$scriptingBox getObSchedInfoAt $index]
}




# 
# objValid:
# Used to determine if an object returned by procedures is valid or not
# This should be used to check all object returned by evm procedures.

proc objValid in {
    if { [java::isnull $in] } {
        return 0
    } else {
        return 1
    }
}

# setOBRankScore:
#
# Sets the OB ranking score

proc setOBRankScore { index score } {
	global scriptingBox
    $scriptingBox setOBScore $index $score
}

proc getOBRankScore { index } {
	global scriptingBox
	return [$scriptingBox getOBScore $index]
}



proc getUserPriority ob {
    return [$ob getUserPriority]
}

proc getAirmass ob {
    return [$ob getAirmass]
}

## returns the dec in degrees
proc getDec ob {
    return [$ob getDec]
}

proc getDiffDec ob {
    return [$ob getDiffDec]
}

proc getDiffRA ob {
    return [$ob getDiffRA]
}

proc getFLI ob {
    return [$ob getFractionalLunarIllumination]
}

proc getMoonAngularDistance ob {
    return [$ob getMoonAngularDistance]
}

proc getPropDec ob {
    return [$ob getPropDec]
}

proc getPropRA ob {
    return [$ob getPropRA]
}

## returns the ra in degrees
proc getRa ob {
    return [$ob getRa]
}

proc getSeeing ob {
    return [$ob getSeeing]
}

proc getSkyTransparency ob {
    return [$ob getSkyTransparency]
}

proc getStrehlRatio ob {
    return [$ob getStrehlRatio]
}

proc getExecutionTime ob {
    return [$ob getExecutionTime]
}

proc getPeriod ob {
    return [$ob getPeriod]
}

proc getProgId ob {
    return [$ob getProgId]
}

proc getObsMode ob {
    return [$ob getObsMode]
}

proc getTelescope ob {
    return [$ob getTelescope]
}

proc getInstrument ob {
    return [$ob getInstrument]
}


proc getRankClass ob {
    return [$ob getRankClass]
}

proc getRank ob {
    return [$ob getRank]
}

proc getTimeCritical ob {

	java::try {	
    	set critical [$ob getTimeCritical]    	
	} catch {NullPointerException err} {	
    	java::throw [java::new org.eso.ohs.phase2.orang.OrangScriptException "Run properties not instantiated"]

	}

	
	return $critical
}

proc getChilean ob {
	return [$ob getChilean]
}

proc getPreImaging ob {
	return [$ob getPreImaging]
}

proc getRunStarted ob {
	return [$ob isRunStarted]
}

proc getCarryOver ob {
	return [$ob getCarryOver]
}

proc getLarge ob {
	return [$ob getLarge]
}

proc getSecondHalfTI ob {
	return [$ob getSecondHalfTimeInterval]
}

proc getLST ob {
	return [$ob getLST]
}

## calculates the hour angle of an OB based on the lst
proc getHourAngle ob {

	## get the RA
	set ra [getRa $ob]
	
	## convert the RA from degrees to hours
	set ra [expr $ra / 15]
	
	## get LST
	set lst [getLST $ob]
	
	##puts "RA=$ra"
	##puts "LST=$lst"
	
	set hourAngle  [expr $ra - $lst]
	
	if {$hourAngle < 0} {
		set hourAngle [expr 24.0 + $hourAngle ]
	}
	
	##format the hour angle to two decimal places	
	set start [string range [format "%+09d" [expr int($hourAngle*100)]] 0 6]
	set end [string range [format "%+09d" [expr int($hourAngle*100)]] 7 9]
	set hourAngle "$start.$end" 
	
	return $hourAngle
} 


## to be overriden
proc rank ob {}

## ranks all the 
proc rankall {} {

	set obcount [getOBCount]
	puts "TCL ranking: No OBs: $obcount"
		
    set i 0    
    while { $i < $obcount } {        
		set ob [getOB $i]		
		set valid [objValid $ob]		
		set score ""
		
		if ($valid) {
			
			set score [rank $ob]
			
	        setOBRankScore $i $score 									
			
		}  else {		
			puts "OB #$i not valid"
		}	
			
        incr i 1 
	}	
}



# end of file scriptAPIsetup.tcl
