#!/bin/csh -f
#+
#OSIRIS library of scripts
#
#NAME
#     osimgFilterInfo
#
#SYNOPSIS
#     
#
#DESCRIPTION
#     Get filter information for OSIRIS imager (OSIMG)
#
#OPTIONS
#     Several parameters are available including
#       -filtername  = translate passed argument(s) to filter
#       -whichwheel  = show in which wheel the filter resides
#       -position    = show in which position the filter resides
#       -waveband    = show waveband of filter (YJHK)
#       -bandwidth   = print bandpass of filter in microns
#       -centralwave = print central wavelength of filter in microns
#       -cuton       = print cuton wavelength of filter in microns
#       -cutoff      = print cutoff wavelegth of filter in microns
#       -trick       = print default trickDichroic for this filter
#       -keyword     = translate passed argument(s) to filter+pupil
#       -oneline     = print results in single line
#       -quiet       = suppress labels on output--will not print all by default
#
#EXAMPLES
#     1) osimgFilterInfo kp
#             Filter: Kp
#              Wheel: 2
#           Position: 4
#               Band: K
#      trickDichroic: hBand
#  CentralWavelength: 2.1444
#           Bandpass: 0.3071
#    CutOnWavelength: 1.9609
#   CutOffWavelength: 2.2680
#
#     2) osimgFilterInfo -oneline kp
#Filter: Kp Wheel: 2 Position: 4 Band: K trickDichroic: hBand CentralWavelength: 2.1444 Bandpass: 0.3071 CutOnWavelength: 1.9609 CutOffWavelength:  2.2680 
#
#     3) osimgFilterInfo -k kp
#Kp
#     4) osimgFilterInfo -k kp largehex
#Kp-LHex
# 
#     5) osimgFilterInfo -oneline -q kp
#
#
#     6) osimgFilterInfo -oneline -central -bandwidth kp
#CentralWavelength: 2.1444 Bandpass:  0.3071 
#     7) osimgFilterInfo -oneline -central -bandwidth -q kp
#2.1444 0.3071

#ENVIRONMENT VARIABLES
#     none
#
#FILES
#     /kroot/rel/default/data/osirisFilterSpecs.dat
#
#SERVERS & KEYWORDS
#     service = osiris
#          keywords: sfilter/ifilter, sscale
#     service = om5s and om6s
#          keywords: posname
#
#SCRIPTS CALLED
#     help, syncheck
#
#EXIT STATUS
#     0 - normal exit, no error
#     1 - script aborted by an interrupt
#     2 - PV lens requested with filter from wheel 2
#     3 - error parsing command line input
#     10 - error setting filter
#     11 - error setting spec scale
#
#SEE ALSO
#     ???
#-
#
# Modifcation History:
# 20180205 - jlyke: Original, adoptied from osirisSetupMech
# 20220207 - jlyke: Make exit status non-zero for bad input
# 20220209 - jlyke: Take image filter handling out of ifilt
#

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

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


# If no arguments, report the filter
if ( $#argv == 0 ) then
  set posname5 = `show -s om5s -terse posname`
  set posname6 = `show -s om6s -terse posname`
  echo "$posname5 + $posname6"
  exit
endif


# Set up to trap control-C
onintr break

# Set default variable values
set fcmd = $0
set cmd = ${fcmd:t}
set cmdpre = ""
set cmdsuf = ""
set M = "i"
set mode = "imag"
set filter = ""
set pupil = ""
set pos5 = ""
set pos6 = ""
set pos = ""
# LAnn = 1
# sAnn = 4
# sHex = 7
# LHex = 10
# HAM  = 11
# Open = 14
set defaultpupilpos = 14
set filterspecs = /kroot/rel/default/data/osirisFilterSpecs.dat
set fw1list = ( jn1 jn2 jn3 hn1 hn2 hn3 hn4 ham feii hcont zn3)
set fw2list = (y j hbb kp hn5 kn1 kn2 kn3 kn4 kn5 brgamma kcont hei_b zbb)
set darklist = (drk dark d)
set filtlist = ($fw1list $fw2list $darklist)  
set pupillist = ( lann sann shex lhex ham open lens)

# output all info by default
set all
set wordy

set passedargs = ($argv)
# Check for more flags
set noglob
while ($#argv != 0)
  switch ($1)
   case -m:
    case -d*:
      set sim
      set cmdpre = "echo ${cmd} ${mode}: sim:"
      set cmdsuf = "-m"
      breaksw
    case -band:
      set waveband
      unset all
      breaksw
    case -bandw*:
    case -bandp*:
      set bandwidth
      unset all
      breaksw
    case -cent*:
    case -wav*:
    case -l*:
      set centralwave
      unset all
      breaksw
    case -cuton:
      set cuton
      unset all
      breaksw
    case -cutoff:
      set cutoff
      unset all
      breaksw
    case -f*:
      set filtername
      unset all
      breaksw
    case -k*:
      set keywordvalue
      set filtername
      unset all
      breaksw
    case -o*:
      set oneline
      breaksw
    case -pos*:
      set position
      breaksw
    case -q*:
      unset wordy
      unset all
      breaksw
    case -t*:
      set trick
      unset all
      breaksw
    case -wh*:
      set whichwheel
      breaksw
    default:
      set filter = $1
      if ($#argv > 1 ) then
        set pupil = $2
        shift
      endif
      breaksw
    endsw
    shift
end
unset noglob

# Check what was passed...
#echo "Passed args"
#echo $filter "$pupil"
if ($?all) then
  set filtername
  set whichwheel
  set position
  set waveband
  set bandwidth
  set centralwave
  set cuton
  set cutoff
  set trick
endif
set output = ()
set description = ()
# make things lower case for easier matching
set filter = `echo $filter | /usr/bin/tr "[A-Z]" "[a-z]"`
# also make "large" = "l" and "small" = "s"
set pupil = `echo "$pupil" | /usr/bin/tr "[A-Z]" "[a-z]" | sed -e 's/arge//g' | sed -e 's/mall//g'`

# parse the filter
switch ($filter)
  # wheel 1 (om5s)
  case jn1:
    set myfilt = Jn1
    set pos5 = 0
    set wheel = 1
    set band = J
    breaksw
  case jn2:
    set myfilt = Jn2
    set pos5 = 2
    set wheel = 1
    set band = J
    breaksw
  case jn3:
    set myfilt = Jn3
    set pos5 = 3
    set wheel = 1
    set band = J
    breaksw
  case hn1:
    set myfilt = Hn1
    set pos5 = 5
    set wheel = 1
    set band = H
    breaksw
  case hn2:
    set myfilt = Hn2
    set pos5 = 6
    set wheel = 1
    set band = H
    breaksw
  case hn3:
    set myfilt = Hn3
    set pos5 = 8
    set wheel = 1
    set band = H
    breaksw
  case hn4:
    set myfilt = Hn4
    set pos5 = 9
    set wheel = 1
    set band = H
    breaksw
  case ham:
    set myfilt = HAM
    set pos5 = 11
    set wheel = 1
    set band = None
    breaksw
  case fe*:
    set myfilt = FeII
    set pos5 = 12
    set wheel = 1
    set band = H
    breaksw
  case hc*:
    set myfilt = Hcont
    set pos5 = 13
    set wheel = 1
    set band = H
    breaksw
  case zn*:
    set myfilt = Zn3
    set pos5 = 15
    set wheel = 1
    set band = Z
    breaksw
  # wheel 2 (om6s)
  case y:
    set myfilt = Y
    set pos6 = 0
    set wheel = 2
    set band = Y
    breaksw
   case j:
    set myfilt = J
    set pos6 = 2
    set wheel = 2
    set band = J
    breaksw
  case hb*:
    set myfilt = Hbb
    set pos6 = 3
    set wheel = 2
    set band = H
    breaksw
  case kp:
    set myfilt = Kp
    set pos6 = 4
    set wheel = 2
    set band = K
    breaksw
  case hn5:
    set myfilt = Hn5
    set pos6 = 5
    set wheel = 2
    set band = H
    breaksw
  case kn1:
    set myfilt = Kn1
    set pos6 = 6
    set wheel = 2
    set band = K
    breaksw
  case kn2:
    set myfilt = Kn2
    set pos6 = 7
    set wheel = 2
    set band = K
    breaksw
   case kn3:
    set myfilt = Kn3
    set pos6 = 8
    set wheel = 2
    set band = K
    breaksw
  case kn4:
    set myfilt = Kn4
    set pos6 = 9
    set wheel = 2
    set band = K
    breaksw
  case kn5:
    set myfilt = Kn5
    set pos6 = 10
    set wheel = 2
    set band = K
    breaksw
  case brg*:
    set myfilt = BrGamma
    set pos6 = 11
    set wheel = 2
    set band = K
    breaksw
  case kco*:
    set myfilt = Kcont
    set pos6 = 12
    set wheel = 2
    set band = K
    breaksw
  case he*:
    set myfilt = HeI_B
    set pos6 = 13
    set wheel = 2
    set band = K
    breaksw
  case zb*:
    set myfilt = Zbb
    set pos6 = 15
    set wheel = 2
    set band = Z
    breaksw
  case d*:
    set myfilt = Drk
    set pos5 = None
    set pos6 = None
    set wheel = None
    set band = None
    breaksw
  default:
    set myfilt = Unknown
    set pos5 = None
    set pos6 = None
    set wheel = None
    set band = None
    breaksw
 endsw

# Now determine the proper pupil
# was a pupil passed?
if ("$pupil" != "" ) then
  switch ("$pupil")
    case la*:
      set pupil = LAnn
      set pos5 = 1
      breaksw
    case sa*:
      set pupil = sAnn
      set pos5 = 4
      breaksw
    case lh*:
      set pupil = LHex
      set pos5 = 10
      breaksw
    case sh*:
      set pupil = sHex
      set pos5 = 7
      breaksw
    case h*:
      set pupil = HAM
      set pos5 = 11
      breaksw
    case o*:
      # global server does not handle "Open"
      set pupil = ""
      set pos5 = 14
      breaksw
    case p*:
    case len*:
      if ( $wheel == 1 ) then
        set pupil = "PV_Lens"
        set pos6 = 14
      else
        echo "ERROR: PV_Lens is in wheel 2, filter must be from wheel 1:"
        echo "$fw1list"
        exit 2
      endif
      breaksw
    default: 
      if ( $?sim) then
        echo "INFO: Unrecognized pupil: "$pupil", using default"
      endif
      set pupil = ""
      breaksw
  endsw
endif

if ( $myfilt == "Unknown" ) then
  # did not parse the passed filter
  if ( $?sim) then
    echo "INFO: Unrecognized filter: "$filter", exiting"
  endif
  exit 1
else
  # build the output
  if ($?keywordvalue) then
    if ("$pupil" != "") then
      set myfilt = `echo "$myfilt-$pupil"`
    endif
  endif
  if ($?filtername) then
    set output = ( $output $myfilt )
    set description = ( $description "Filter: ")
  endif

  if ($?whichwheel) then
    set output = ( $output $wheel )
    set description = ( $description "Wheel: ")
  endif

  if ($?position) then
    switch ($wheel) 
      case 1:
        set position = $pos5
        breaksw
      case 2:
        set position = $pos6
        breaksw
      default:
        set position = "None"
        breaksw
    endsw
    set output = ($output $position)
    set description = ( $description "Position: ")
  endif

  if ($?waveband) then
    set output = ( $output $band )
    set description = ( $description "Band: ")
  endif

  if ($?trick) then
    # set default TRICK dichroic
    if ( $band == None ) then
      set trickDicroic = None
    else 
      if ( $band == K ) then
        set trickDichroic = hBand
      else
        set trickDichroic = kBand
      endif
    endif
    set output = ($output $trickDichroic )
    set description = ( $description "trickDichroic: ")
  endif

  # based upon the filter get some other filter info
  if ($?centralwave) then
    set centralwavelength = `awk '/^'$myfilt'/ {print $2}' $filterspecs`
    set output = ( $output $centralwavelength )
    set description = ( $description "CentralWavelength: ")
  endif
  if ($?bandwidth) then
    set width = `awk '/^'$myfilt'/ {print $3}' $filterspecs`
    set output = ( $output $width )
    set description = ( $description "Bandpass: ")
  endif
  if ($?cuton) then
    set cutonwave = `awk '/^'$myfilt'/ {print $4}' $filterspecs`
    set output = ( $output $cutonwave )
    set description = ( $description "CutOnWavelength: ")
  endif
  if ($?cutoff) then
    set cutoffwave = `awk '/^'$myfilt'/ {print $5}' $filterspecs`
    set output = ( $output $cutoffwave )
    set description = ( $description "CutOffWavelength: ")
  endif

  if ($#output == 1 ) then
    # assume quiet output
    unset wordy
  endif
  if ( $?wordy ) then
    if ( $?oneline ) then
      set idx = 1
      while ( $idx <= $#output ) 
        echo -n "$description[$idx] $output[$idx] "
        @ idx++
      end
      # end with newline
      echo ""
    else
      set idx = 1
      while ( $idx <= $#output ) 
        printf "%20s %s\n" $description[$idx] $output[$idx]
        @ idx++
      end
    endif     
  else
    echo $output
  endif
# if we have more than one param, print out labels...
goto done

break:
# Block of code to handle interrupts.
###modify this stuff for OSIRIS
exit 1

done:
# is there anything that needs to go here?
exit
