#!/usr/bin/env tcsh 
# Copyright 2005, 2006 University of Leiden.
#
# This file is part of MIA+EWS.
#
# MIA+EWS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# MIA+EWS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MIA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#!/usr/bin/env tcsh 
#   Usage:
#   dispVisSP inputFiles (blank separated list) coordFile crossFile outputBaseName \
#  [-ndrop nDrop] [-smooth fringeSmooth] [-gsmooth delaySmooth] [-sigmaY sigmaY]
#  [-sky skyfiles]
# set defaults

set ndrop = 0
set fringesmooth = 50
set delaysmooth  = 6.0
set sigmaY       = 2.0
set sky          = ""

if ( $# < 4 ) then
   echo "   dispVisSP needs at least 4 input parameters"
   exit 1
endif
if ($# >= 6) then
   if ("$5" == "-ndrop") set ndrop = $6
   if ("$5" == "-smooth") set fringesmooth = $6
   if ("$5" == "-gsmooth") set delaysmooth = $6
   if ("$5" == "-sigmaY") set sigmaY = $6
   if ("$5" == "-sky") set sky = "$6"
endif
if ($# >= 8) then
   if ("$7" == "-ndrop") set ndrop = $8
   if ("$7" == "-smooth") set fringesmooth = $8
   if ("$7" == "-gsmooth") set delaysmooth = $8
   if ("$7" == "-sigmaY") set sigmaY = $8
   if ("$7" == "-sky") set sky = "$8"
endif
if ($# >= 10) then
   if ("$9" == "-ndrop") set ndrop = $10
   if ("$9" == "-smooth") set fringesmooth = $10
   if ("$9" == "-gsmooth") set delaysmooth = $10
   if ("$9" == "-sigmaY") set sigmaY = $10
   if ("$9" == "-sky") set sky = "$10"
endif
if ($# >= 12) then
   if ("$11" == "-ndrop") set ndrop = $12
   if ("$11" == "-smooth") set fringesmooth = $12
   if ("$11" == "-gsmooth") set delaysmooth = $12
   if ("$11" == "-sigmaY") set sigmaY = $12
   if ("$11" == "-sky") set sky = "$12"
endif
if ($# >= 14) then
   if ("$13" == "-ndrop") set ndrop = $14
   if ("$13" == "-smooth") set fringesmooth = $14
   if ("$13" == "-gsmooth") set delaysmooth = $14
   if ("$13" == "-sigmaY") set sigmaY = $14
   if ("$13" == "-sky") set sky = "$14"
endif
echo "Computing photometry and mask data: "
if ("$sky" == "") $vltiCbin/oirSciPhotometry  -data "$1" -ref $2 -cross $3 -out $4.photometry.fits -mask $4.maskfile.fits  -ndrop $ndrop -sigma $sigmaY 

if ("$sky" != "") $vltiCbin/oirSciPhotometry  -data "$1" -ref $2 -cross $3 -out $4.photometry.fits -mask $4.maskfile.fits  -ndrop $ndrop -sigma $sigmaY -sky "$sky"

if (0 == $status) then
  echo "Photometry complete"
else
   echo "Photometry failed"
   exit 1
endif
#
echo "Compressing Interferometry Data: "
$vltiCbin/oirCompressSPData -data "$1" -mask $4.maskfile.fits -out $4.compressed.fits \
   -ref $2 
if (0 == $status) then
  echo "Compression complete"
else
   echo "Compression failed"
   exit 1
endif
echo "Computing fringes:"
$vltiCbin/oirFormFringes $4.compressed.fits $4.fringes.fits $fringesmooth
if (0 == $status) then
  echo "Fringing complete"
else
   echo "Fringing failed"
   exit 1
endif
echo "Removing piezo OPD:"
$vltiCbin/oirRotateInsOpd $4.fringes.fits $4.insopd.fits
if (0 == $status) then
  echo "InsOpd complete"
else
   echo "InsOpd failed"
   exit 1
endif
echo "Computing group delay: "
$vltiCbin/oirGroupDelay  $4.insopd.fits $4.groupdelay.fits -s $delaysmooth
if (0 == $status) then
  echo "Group delay complete"
else
   echo "Group delay failed"
   exit 1
endif
echo "Removing group delay and dispersion constant: "
$vltiCbin/oirRotateGroupDelay $4.fringes.fits $4.groupdelay.fits $4.ungroupdelay.fits
if (0 == $status) then
  echo "Ungroup delay complete"
else
   echo "Ungroup delay failed"
   exit 1
endif
echo "Auto flagging data:"
$vltiCbin/oirAutoFlag $4.fringes.fits $4.groupdelay.fits $4.flag.fits
if (0 == $status) then
  echo "Autoflag complete"
else
   echo "Autoflag failed"
   exit 1
endif
echo "Compute average correlation: "
$vltiCbin/oirAverageVis $4.ungroupdelay.fits $4.flag.fits $4.corr.fits
if (0 == $status) then
  echo "DISPVISSP complete"
else
   echo "Autoflag failed"
endif
exit 0
