#!/bin/csh -f
#+
# OSIRIS library of scripts
#
# NAME
#      osirisTint - set the exposure time
#
# SYNOPSIS
#     osirisTint spec|imag [x] 
#
# DESCRIPTION
#      sets the integration time to x seconds.
#      if the desired exposure time is less than 
#      the minimum exposure time, the time is set
#      to the minimum texp.
#
# ARGUMENTS
#     x - exptime in seconds.
#
# OPTIONS
#
# EXAMPLES
#    tint 5  - set integration time to 5 seconds
#    tint 0  - set exp to minimum integration time
#
# ENVIRONMENT VARIABLES
#
# FILES
#
# SERVERS & KEYWORDS
#    
#
#
# SCRIPTS CALLED
#
# EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - syncheck error
#     3 - error parsing command line input
#     other errors...
#
#
#
# Modification History:
# 2011apr27 - MK: Adapted for OSIRIS
# 2012may07 - MK/GW Modfied to supply the time in seconds
#------------------------------------------------------


# Boiler plate for "-h" support for command autohelp.

if ("$1" == "-h") then
    help $0
    exit $status
endif

# Boilerplate for syncheck.
# Note that the boiler plate should be transparent for all usages,
# but to make correct use of syncheck you will need to specify the
# correct pattern.

set noglob
set CheckStatus = `syncheck -command $0 $* -pattern {text float:0,2000}` 
unset noglob

if ("$CheckStatus" != "OK") then
    help $0
    exit 1
endif

# End of help/syncheck boiler plate.

set mode = $1
switch ($mode)
    case S*:
    case s*:
        set M = "s"
        set det = "spec"
        breaksw
    case I*:
    case i*:
        set M = "i"
        set det = "imag"
        breaksw
    default:
        osirisScriptMsg -T "${cmd} ${det}: Specified detector must be SPEC or IMAG 
- aborting."
        exit 1
        breaksw
endsw
shift

# Check integration time requested versus minimum calculated.
set ReqTime = $1

if ($ReqTime == "") then
    set showTime = `show -s o${M}ds -terse itime`
    calc "$showTime / 1000.0" 
else
    set MinTime = `osirisTmin $mode`
    # Convert the times to miliseconds
    set MinTime = `calc "1000.0 * $MinTime"` 
    set ReqTime = `calc "1000.0 * $ReqTime"` 
    # Round to integers
    set MinTime = `printf "%0.f " $MinTime`
    set ReqTime = `printf "%0.f " $ReqTime`
    if ( $ReqTime < $MinTime ) then
      modify -s o${M}ds itime = $MinTime
       echo "Request for integration less than the minimum ($MinTime msec)."
       echo "Integration time set to $MinTime msec."
    else
      modify -s o${M}ds itime = $ReqTime
    endif
endif
