#!/bin/csh -f
#+
# OSIRIS library of scripts
#
# NAME
#     osirisTmin - computes minimum integration time
#
# SYNOPSIS
#     osirisTmin spec|imag
#
# DESCRIPTION
#     Computes the minimum integration time for the current setup. 
#     The integration time is calculated in microseconds and then 
#     converted to seconds at the end of the script.
#     
#
# ARGUMENTS
#     spec|imag - either the SPEC or IMAG detector
#
# OPTIONS
#
# EXAMPLES
#     tmin
#
# ENVIRONMENT VARIABLES
#
# FILES
#
# SERVERS & KEYWORDS
#     osiris: sampmode, numreads, numgroups, endx, endy, startx, starty, 
#
# 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...
#
# Used by tint, sampmode
#
# Modification History:
# 2017jul 24 - jlyke: Adapted from MOSFIRE tmin for OSIRIS
#
#------------------------------------------------------


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

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

if ( $#argv < 1 ) then
  help $0
  exit 1
endif
# End of help/syncheck boiler plate.
set fastspec = 1 # if 1, assume full frame, else calculate via keywords
set fastimag = 0 # if 1, assume full frame, else calculate via keywords

set keylist = (sampmode numreads groups endy endx starty startx)

# Decide if we should use SPEC or IMAG
set mode = $1
switch ($mode)
    case S*:
    case s*:
        if ( $fastspec == 1 ) then        
          set fast = 1
          set keylist = (sampmode numreads groups)
        endif
        set M = "s"
        set mode = "spec"
	set full = 2048
	set chan = 32
	set refcol = 8
	set refrow = 1
        breaksw
    case I*:
    case i*:
        if ( $fastimag == 1 ) then        
          set fast = 1
          set keylist = (sampmode numreads groups)
        endif
        set M = "i"
        set mode = "imag"
	set full = 2048
	set chan = 32
	set refcol = 8
	set refrow = 1
        breaksw
    default:
        osirisScriptMsg -T "${cmd} ${mode}: Specified mode must be SPEC or IMAG 
- aborting."
        exit 1
        breaksw
endsw
shift

# minumum sub array configuration to improve read time.
set subsizemin = ` calc "($full / $chan + $refcol ) x ( $full + $refrow )" `
#units are microseconds
set tpix    = 10.0 

# Read in various keywords.
set keyarr = (`show -s o${M}ds -terse $keylist`)
if ($?fast) then
  set sampmode     = `echo $keyarr[1]`
  set nreads       = `echo $keyarr[2]`
  set ngroups      = `echo $keyarr[3]`
  set nrows        = $full
  set ncols        = $full
else
  set sampmode     = `echo $keyarr[1]`
  set nreads       = `echo $keyarr[2]`
  set ngroups      = `echo $keyarr[3]`
  set endrows     = `echo $keyarr[4]`
  set endcols     = `echo $keyarr[5]`
  set startrows   = `echo $keyarr[6]`
  set startcols   = `echo $keyarr[7]`
  # set number of rows and columns
  set nrows = `calc  $endrows - $startrows  +1`
  set ncols = `calc  $endcols - $startcols  +1`
endif


# calculate the current sub size 
set subsize = `calc "( $ncols + $refcol ) x ( $nrows + $refrow )" `  

set subarrayflag = ` calc $subsizemin - $subsize `
#set subarrayflag = `round $subarrayflag` 
set subarrayflag = `printf "%0.f" $subarrayflag`

if ( $subarrayflag <= 0 ) then
  set tframe = ` calc $subsizemin x $tpix  `
else
  set tframe = ` calc "($ncols + $refcol ) x ( $nrows +$refrow ) x $tpix " `
endif

# tframe for full frame is 1475280.  Itime is integer ms and must be
# at least 1476 ms if G>1 
switch ($sampmode)
case 1:
   #single readout mode. In this mode integration time may be zero.
   # this is why G = 1.
   set R = 1
   set G = 1
   set fudge = 0.0
  breaksw
case 2:
   # CDS
   set R = 1
   set G = 2
   set fudge = 0.5
  breaksw
case 3:
   # MCDS
   set R = $nreads
   set G = 2
   set fudge = 0.5
  breaksw
case 4:
   # UTR
   set R = $nreads
   set G = $ngroups
   set fudge = 0.5
endsw

#set the minumum exposure time
set tmin = `calc " $R x ( $G - 1 ) x $tframe " `

set tmin = `calc $tmin / 1000 + $fudge`

# mosfire-style calc int part floors tmin to 1475 ms...no good
# for full frames...may have to play with subarrays
#set tmin = `calc "int($tmin)"`
#set tmin = `round $tmin`
set tmin = `printf "%0.f" $tmin`

set tmin = `calc "$tmin / 1000"`

#minimum time is in seconds
echo $tmin



