#! /bin/tcsh -f
# "@(#) $Id: ferosPrepRawData,v 1.3 2005/03/03 09:17:45 jpritcha Exp $"

set exstat=0
set guess=9857

set guessFileList="BLAZE.tbl ThAr50000.tbl ThAr9857_{{GORDER,INIT,ORDER,{LINE,WLC}{1,2}}.tbl,TEMPLATE.bdf}"

while ( $#argv )
  switch ( $1 )
  case -h:
    goto usage
  case -r:
    set rawDataDir=$2; shift; shift; breaksw
  case --mkLinks:
    set mkLinks; shift; breaksw
  case --DNmkLinks:
    set DNmkLinks; shift; breaksw
  case --noVLT:
    set noVLT; shift; breaksw
  default:
    set dateDir=$1; shift; breaksw
  endsw
end

if ( ! $?dateDir ) then
  echo "Error\!\!\! Must supply <dateDir>"
  @ exstat++
endif

set curwd=`pwd`

if ( ! $?rawDataDir ) then
  echo "Error\!\!\! Must supply <rawDataDir>"
  @ exstat = $exstat + 2
else
  cd /
  foreach posdir ( $rawDataDir $curwd/$rawDataDir )
    if ( -d $posdir ) set foundRawDataDir=$posdir
  end
  if ( ! $?foundRawDataDir ) then
    echo "Error\!\!\! Can't find $rawDataDir"
    @ exstat = $exstat + 4
  endif
endif

if ( ! $?noVLT ) then
  which gethead >& /dev/null
  if ( $status ) then
    echo "Error\!\!\! Can't find command gethead"
    echo " "
    echo "gethead is part of the wcs-tools package which can be downloaded from"
    echo "   http://tdc-www.harvard.edu/software/wcstools.html"
    @ exstat = $exstat + 8
  endif
endif

if ( $exstat ) goto usage

cd $curwd

if ( ! -d $dateDir ) mkdir -pv $dateDir
cd $dateDir
echo "Processing log files (if any)..."
ls $foundRawDataDir/FEROS*.log >& /dev/null
if ( ! $status ) then
  foreach logfile ( $foundRawDataDir/FEROS*.log )
    if ( -e $logfile:t ) then
## For BIAS generated images
      grep fero $logfile >> $logfile:t
## For FIERA generated images
      grep "FEROS_" $logfile >> $logfile:t
    else
      cat $logfile > $logfile:t
    endif
  end
endif
echo "Processing VLT compliant filename files (if any)..."
if ( ! $?noVLT ) then
## AstroCd raw data is NOT compressed...
## AstroCd raw data is sorted into date directories according to exposure
## start time
  ls $foundRawDataDir/FEROS.*.fits >& /dev/null
  if ( ! $status ) then
    foreach VLTfile ( $foundRawDataDir/FEROS.*.fits )
      cp -v $VLTfile $VLTfile:t
      chmod u+w $VLTfile:t
      set ORIGFILE="`gethead $VLTfile:t ORIGFILE`"
      set ISFIERA=0
      if ( "`gethead $VLTfile:t 'HIERARCH ESO DPR CATG'`" != "" ) set ISFIERA=1
      if ( ! $ISFIERA ) then
## If it is NOT a FIERA generated image then the ORIGFILE name will be of
## the feroNNNN.mt format
        ln -sv $VLTfile:t:s/.gz// $ORIGFILE
      endif
      if ( $?mkLinks || ( $ISFIERA && ! $?DNmkLinks ) ) then
        ferosMkferoNNNNLink -v -d . -i ${curwd}/.feroNNNN $VLTfile:t
      endif
    end
  endif
## ferosMkVACDs raw data is compressed...
## ferosMkVACDs raw data is sorted into date directories according to
## OB start time
  ls $foundRawDataDir/FEROS.*.fits.gz >& /dev/null
  if ( ! $status ) then
    foreach VLTfile ( $foundRawDataDir/FEROS.*.fits.gz )
      gzip -dcv $VLTfile > $VLTfile:t:s/.gz//
      set ORIGFILE="`gethead $VLTfile:t:s/.gz// ORIGFILE`"
      set ISFIERA=0
      if ( "`gethead $VLTfile:t:s/.gz// 'HIERARCH ESO DPR CATG'`" != "" ) set ISFIERA=1
      if ( ! $ISFIERA ) then
        ln -sv $VLTfile:t:s/.gz// $ORIGFILE
      endif
      if ( $?mkLinks || ( $ISFIERA && ! $?DNmkLinks ) ) then
        ferosMkferoNNNNLink -v -d . -i ${curwd}/.feroNNNN $VLTfile:t:s/.gz//
      endif
    end
  endif
endif
echo "Processing .mt filename files (if any)..."
ls $foundRawDataDir/fero????.mt.gz >& /dev/null
if ( ! $status ) then
  foreach MTfile (  $foundRawDataDir/fero????.mt.gz )
    gzip -dvc $MTfile > $MTfile:t:s/.gz//
  end
endif

exit

usage:
echo "Usage: ferosPrepRawData [-h] [--mkLinks|--DNmkLinks] [--noVLT] <dateDir> <-r <RawDataDir>>"
echo " "
echo "<dateDir>"
echo "   Directory to prepare"
echo "-r <RawDataDir>"
echo "   Directory with the raw data"
echo " "
echo "The FEROS MIDAS DRS expects file names of the type fero<NNNN>.mt, whereas"
echo "the FIERA CCD Controller creates files with name FEROS_ech_..."
echo "Therefore this script automatically detects images created with the FIERA CCD"
echo "Controller system and for these attempts to run the feroMkferoNNNNLink script"
echo "to create softlinks with the appropriate filename structure."
echo "This behaviour can be controlled manually using the following options:"
echo " "
echo "--mkLinks"
echo "   Force the use of the feroMkferoNNNNLink script to create softlinks."
echo "Should work even for BIAS created images."
echo "--DNmkLinks"
echo "   Prevent the use of the feroMkferoNNNNLink script even for FIERA images."
echo "Useful when recreating the data directory and you wish to remake the"
echo "previously created links using the feroNNNN.log file, i.e. use command:"
echo "[tcsh]       source feroNNNN.log"
exit $exstat
