#!/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:
#   dispVis inputFiles (blank separated list) maskFile  outputBaseName fringeSmooth delaySmooth remAve minopd maxopd
#
if ( $# < 8 ) then
   echo "   dispVis needs 8 input parameters "
   exit 1
endif
#
#override defaults if given
#
set remAve = ""
if ("$6" == "-removeAverage") set remAve = "$6" 
set minopd = "-minopd $7"
set maxopd = "-maxopd $8"

#
#echo "   compressing raw data"
$vltiCbin/oir1dCompressData "$1" $2 $3.compressed.fits 
if (0 == $status) then
#  echo "   compression complete"
else
   echo "   compression failed"
   exit
endif
echo "   computing fringes"
$vltiCbin/oirFormFringes $3.compressed.fits $3.fringes.fits -smooth $4 $remAve
if (0 == $status) then
#  echo "   fringing complete"
else
   echo "   fringing failed"
   exit
endif
#echo "   removing piezo OPD"
$vltiCbin/oirRotateInsOpd $3.fringes.fits $3.insopd.fits
if (0 == $status) then
#  echo "   insOpd complete"
else
   echo "   insOpd failed"
   exit
endif
#echo "   computing group delay "

$vltiCbin/oirGroupDelay  $3.insopd.fits $3.groupdelay.fits -smooth $5 $maxopd
if (0 == $status) then
#  echo "   group delay complete"
else
   echo "   group delay failed"
   exit
endif
#echo "   removing group delay and dispersion constant "
$vltiCbin/oirRotateGroupDelay $3.fringes.fits $3.groupdelay.fits $3.ungroupdelay.fits -smooth $5 -frames
if (0 == $status) then
#  echo "   ungroup delay complete"
else
   echo "   ungroup delay failed"
   exit
endif
#echo "   auto flagging data"
$vltiCbin/oirAutoFlag $3.fringes.fits $3.groupdelay.fits $3.flag.fits $minopd
if (0 == $status) then
#  echo "   autoflag complete"
else
   echo "   autoflag failed"
   exit
endif
echo "   compute average correlation"
$vltiCbin/oirAverageVis $3.ungroupdelay.fits $3.flag.fits $3.corr.fits
