#!/usr/bin/csh -f
#+
# osirisEndNight -- Copy needed calibration files to the user's data 
#                   directory and shutdown OSIRIS for the night
#
# Purpose:
#	Get the needed calibration files for data reduction
#       Put in blank filters
#
# Usage:
#	osirisEndNight
#	Run by observers at the end of the night
# 
# Arguments:
#	None
# 
# Output:
#	None
# 
# Restrictions:
#	Must be run as the osiris numbered account currently taking data
#       Requires "ospeclog and sym links in ~osiris##/osiris/
# 
# Exit values:
#	0 = normal completion
#	1 = wrong number of arguments
#       2 = not run as numbered account
#       3 = not run on default computer (osirisserver)
#
# Example:
#	1) Start the vncviewer in interactive mode:
#		osirisEndNight
#
#-
# Modification history:
#	2006 Sep 22	jlyke	Original
#       2007 May 30     jlyke   Will copy latest rect mat by default
#       2014 Mar 05     jlyke   Fix 2010 recmat issue, close GUIs
#       2016 Apr 21     jlyke   Update ospeclog handling for SPEC upgrade
#                               Relax user restrictions
#       2020 Jul 11     jlyke   Relax restriction on napili only
#       2020 Aug 25     jlyke   Make recmat copying optional, off for now
#
#-----------------------------------------------------------------------

# define initial values...
set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd"
# alias echo so we can ring the bell
alias echo /usr/bin/echo


# define defaults
set pid = $$
set computer = "napili"
set user = `whoami`
set count = 0
set rect_dir = "/osiris-calib/SPEC/rectification/"
set orig_dir = `pwd`
# flag whether to copy recmats to cal directory.  1=Yes, 0=No
set copy_rect = 0

# make sure we're running on napili
set mycomp = `hostname`
#if ( $mycomp != $computer ) then
#  echo ""
#  echo "\aWarning: $cmd must be run from $computer"
#  echo ""
#  exit 3
#endif

if ($copy_rect == 1 ) then
  # go to the raw data directory and get filters and scales
  cd ~/osiris/spec_raw
  set list = `ospeclog | awk '/^s/' | cut -c 76-82 | sort -u`
  # list is of the form: 
  # 020 Hn3 035 Kn1 035 Kn3 035 Kn5 050 Hbb
  set num = `math $#list / 2 | cut -f 1 -d .`

  while ( $#list > 0 )
    @ count++
    set filter = $list[2]
    set sscale = $list[1]
    set file = (`ls -1 $rect_dir | grep $filter | grep $sscale | tail -1`)
    if ( $#file == 1 ) then
	echo "copying $count of $num rectification matrices"
	if ( ! -e ~/osiris/spec_cal/$file ) then
	    cp $rect_dir$file ~/osiris/spec_cal/
	else
	    echo "$file has already been copied...skipping."
	endif
    else
	echo "\aWarning: there is more than one rectification matrix"
	echo "for filter $filter and sscale $sscale."
	echo "Not an emergency, but talk to your support astronomer."
    endif
    shift list
    shift list
  end

  echo "The following rectification matrices were copied:"
  ls ~/osiris/spec_cal/*.fits
endif

# now put in a dark filter for both the imager and spectrograph
if ( `osirisCheckServer osiris` == 1 ) then
    modify -s osiris sfilter=drk
    sleep 1
    modify -s osiris ifilter=drk
    sleep 1
else
    echo "The osiris global server seems unresponsive--skipping filter change"
endif

# create electronic logs for observers
echo "Creating imager log..."
cd ~/osiris/imag_raw
set file = "osiris_imager_log"
if ( ! -e $file ) then
    osimglog > $file
    echo `pwd`"/$file"
endif

echo "Creating spectrograph log..."
cd ~/osiris/spec_raw
set file = "osiris_spec_log"
if ( ! -e $file ) then
    ospeclog > $file
    echo `pwd`"/$file"
endif

echo "Shutting down user software..."
osirisStopGuis

echo ""
echo "$cmd complete"
echo ""

# reset original directory
cd $orig_dir

# reset echo
unalias echo

exit 0
