#!/bin/csh

# Set some parameters
set crifile=${PWD}/select.parsed

# Set base directories (run+prog)
set cdfdir=${PWD}
set tradir=${PWD}

# Write usage information
if ( ${#argv} ==  0 ) then
  echo " -------------------------------------------------------------------------------------" 
  echo "Usage: select <input> <output> <criterion> [optional flags]"
  echo 
  echo "Criterion structure: Command:Variable(VMode):Parameters:Times(TMode)"
  echo
  echo "     Command:    GT, LT, EQ, IN, OUT"
  echo "     Variable:   Name as in LSL header"
  echo "     VMode:      VALUE, MEAN, MAX, MIN, SUM"
  echo "     Parameters: Comma separated list"
  echo "     Times:      FIRST, LAST, Explicit values (separated with , or -)"
  echo "     TMode:      ALL, ANY, NONE"
  echo
  echo "Criterion examples:"
  echo
  echo "     GT:PV:2.:ALL            : PV at all times greater than 2"
  echo "     LT:PV:2.:FIRST          : PV at first time less than 2"
  echo "     IN:LAT:10,15:18,24      : Latitude within 10 and 15 N at time 18 and 24"
  echo "     EQ:LAT:10:18,24(ANY)    : Latitude equal to 10 N at time 18 or 24"
  echo "     EQ:LAT:10:18,24(NONE)   : Latitude equal to 10 N neither at time 18 or 24"
  echo "     OUT:LON:10,15:0-24(ALL) : Longitude outside 10 to 15 E for all times from 0 to 24"
  echo "     LT:PV(SUM):5:ALL        : Sum over all PV values less than 5 pvu"
  echo 
  echo "Logical operators examples:"
  echo
  echo "     GT:LAT:30:ALL & LT:LON:15:ALL : Logical AND"
  echo "     GT:LAT:30:ALL | LT:LON:15:ALL : Logical OR"
  echo 
  echo "Optional flags:"
  echo
  echo "     -noclean      : Keep temporary files  for debugging)"
  echo "     -boolean      : Output as boolean (0/1) list"
  echo "     -index        : Output as index (1...ntra) list"
  echo "     -startf       : Write onlay starting positions of selected trajectories"
  echo "     -regionf file : Name of the region file"
  echo "     -trigger      : Flag whether a trigger column should be added to the trajectory"
  echo "-------------------------------------------------------------------------------------"  
exit 1
endif

# Write list of special selection criteria
if ( "$1" == "-special" ) then
    grep '%)' ${LAGRANTO}/select/special.f
    exit 0
endif

# Write title
echo 
echo '========================================================='
echo '       *** START OF PREPROCESSOR SELECT ***              '
echo

# Save input arguments
set inpfile=$1
set outfile=$2
set crit="$3"
shift
shift
shift

# Handle optional arguments
echo
echo '---- OPTIONAL FLAGS -------------------------------------'
echo  

set noclean = 'false'
set format  = 'trajectory'
set regionf = 'regionf'
set trigger = 'nil'

while ( $#argv > 0 )

  switch ( $argv[1] )

   case -noclean
     set noclean = 'true'
     echo "noclean       -> true (user defined)"
   breaksw

   case -trigger
     set trigger = '-trigger'
     echo "trigger      ->  true (user defined)"
   breaksw

   case -boolean
     set format = 'boolean'
     echo "format       ->  boolean (user defined)"
   breaksw

   case -index
     set format = 'index'
     echo "format       ->  index (user defined)"
   breaksw

   case -count
     set format = 'count'
     echo "format       ->  count (user defined)"
   breaksw

   case -startf
     set format = 'startf'
     echo "format       ->  startf (user defined)"
   breaksw

   case -regionf
     set regionf = $argv[2]
     echo "regionf                -> ${regionf} (user defined)"
     shift;
   breaksw

  endsw
 
  shift;

end

# Decide whether <select> is a file or an explicit criterion
set flag_select = 'criterion'
set test = `echo ${crit} | grep ':' | wc -c`
if ( "${test}" == "0" ) then 
  set flag_select     = 'file'
  set flag_selectfile = $crit
  if ( -f ${flag_selectfile} ) then 
     set crit             = `cat ${flag_selectfile}`
  else
     echo " ERROR: criterion file ${flag_selectfile} is missing... Stop"
     exit 1
  endif
endif

# Get the start, end and reference date for the tracing
set ntra      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
set ntim      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
set ncol      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
set times     =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} times`
set vars      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} vars`

# Split the criterion into subunits (with Perl)
${LAGRANTO}/select/select.perl "${crit}" >! ${crifile} 
if ( "${status}" != "0" ) then
     echo "ERROR:  Parser <select> failed"
     exit 1
endif

# Write some status information
echo
echo '---- DIRECTORIES AND PROGRAMS ---------------------------'
echo    
echo "CDF directory         : ${cdfdir}"
echo "TRA directory         : ${tradir}"
echo "PROGRAM SELECT        : ${LAGRANTO}/select/select"
echo "PARSER                : ${LAGRANTO}/select/select.perl"
echo "CRITERION FILE        : ${crifile}"
echo
echo '---- INPUT PARAMETERS -----------------------------------'
echo    
echo "Input file            : ${inpfile}"
echo "Output file           : ${outfile}"
echo "Output format         : ${format}"
if ( "${flag_select}" == "criterion" ) then
    echo "Criterion             : ${crit}"
else
    echo "Criterion             : ${crit} (from file ${flag_selectfile})"
endif
echo
echo '---- INPUT FILE -----------------------------------------'
echo    
echo "# TRA                 : ${ntra}"
echo "# TIMES               : ${ntim}"
echo "# COLUMNS             : ${ncol}"
echo "Times                 : ${times}"
echo "Variables             : ${vars}"
echo
echo '---- PARSED CRITERION ------------------------------------'
echo 
cat ${crifile}

# Finish the preprocessor
echo 
echo '       *** END OF PREPROCESSOR SELECT ***              '
echo '========================================================='
echo

# Run the selection programme
echo \"${inpfile}\" >! select.param
echo \"${outfile}\" >> select.param
echo \"${format}\"  >> select.param
echo \"${crifile}\" >> select.param
echo ${ntra}        >> select.param
echo ${ntim}        >> select.param
echo ${ncol}        >> select.param
echo \"${regionf}\" >> select.param
echo \"${trigger}\" >> select.param

${LAGRANTO}/select/select

if ( "${status}" != "0" ) then
  echo "ERROR:  Program <select> failed"
  exit 1
endif

# Make clean
if ( "${noclean}" == "false" ) then
  \rm -f select.param
  \rm -f ${crifile}
endif

exit 0
