#!/bin/sh

set -x

echo "*** Started script $0"

USAGE="Usage: get_altim.sh YYYYMMDD YYYYMMDD "

if test $# -eq 2
then
# first two args are the start and end dates
   startdate=$1
   enddate=$2
else
   echo
   exit -1
fi

# take care of vars not exported in
NDATE=${NDATE:-$utilexec/ndate}

#  -- additional data sources can be added here
# data sources

cd $DATA
mods_source='ngfoal njsnal envsal njsn2i'

gfo='ngfoal gfo'
jsn='njsnal jsn'
nvs='envsal env'
jsn='njsn2i jsn2'
# bufr_source can be defined by outside parameters
# for now set it to all sources
# bufr_source='$gfo' '$jsn' '$nvs'

# setup xlf environment
export XLFRTEOPTS="unit_vars=yes"
export XLFUNITS=0
unset `env | grep XLFUNIT | awk -F= '{print $1}'`

# 1) read in data from our source:
# - if mods, then use dumpmb to copy the raw data to local dir

$dumpmb $startdate  $enddate $mods_source

# 2) for each type and source, decode the altimetry data 
# - mods - use read_alps_mods

# for each type (gfo, jsn, ...)
for altimTYPE in 'ngfoal gfo' 'njsnal jsn' 'envsal env' 'njsn2i jsn2'
do
  aTYPE=`echo $altimTYPE | cut -d" " -f1`
  TYPE=`echo $altimTYPE | cut -d" " -f2`

   echo ALTIM Start processing for $TYPE .
      
# for each day incrementing
   ADATE=$startdate
   enddate=$enddate$cyc
   enddate=`$NDATE +24 $enddate | cut -c1-8`
   while [[ $ADATE < $enddate ]]
   do
      
     echo 	ALTIM Running decoder for $TYPE $ADATE .
# MODS --- read in the altimetry using read_alps_mods
# **** read_alps_mods ****
     data_available=0
     if test -f "$DATA/${aTYPE}.$ADATE"
     then
        data_available=1
        export XLFUNIT_11="$DATA/${aTYPE}.$ADATE"
        export XLFUNIT_61="$DATA/raw_$ADATE.${TYPE}_qc"
        export XLFUNIT_62="$DATA/raw_$ADATE.${TYPE}_or"
cat > read_alps_mods.in <<EOF
 $latobsmin $latobsmax $lonobsmin $lonobsmax
EOF
        $EXECofs/ofs_read_alps_mods >> $pgmout 2> errfile<read_alps_mods.in
        export err=$?; err_chk        
     fi

# 3) if able to decode any data, then 
# -- combine any original and quality reports, removing duplicates
# -- filter the data (grid it)

     if [ $data_available -eq 1 ]
     then
# **** combine ****
        export XLFUNIT_11="$DATA/raw_$ADATE.${TYPE}_qc"
        export XLFUNIT_12="$DATA/raw_$ADATE.${TYPE}_or"
        export XLFUNIT_63="$DATA/combined_$ADATE.${TYPE}"

        echo 	ALTIM Running combine for $TYPE $ADATE .
        $EXECofs/ofs_combine_ssh >> $pgmout 2> errfile
        export err=$?; err_chk
          
# **** lfilter ****
        if test -f "$DATA/combined_$ADATE.$TYPE" ; then

          export XLFUNIT_16="$DATA/combined_$ADATE.$TYPE"
          export XLFUNIT_60="$DATA/$ADATE.$TYPE"
	  
#          echo $latobsmin $latobsmax | $EXECofs/ofs_lfilter_ssh >> $pgmout 2> errfile
#          export err=$?; err_chk

          echo "${latobsmin}" > lfilter.in
          echo "${latobsmax}" >> lfilter.in
          echo "${dlat}" >> lfilter.in
          echo "${scl_lat}" >> lfilter.in
          
          $EXECofs/ofs_lfilter_ssh < lfilter.in >> $pgmout 2> errfile
          export err=$?; err_chk
          
          echo 	ALTIM Running lfilter for $TYPE $ADATE .
          cat $DATA/$ADATE.$TYPE >> $DATA/ssh.${cycle}.$TYPE
        fi

      fi
      ADATE=$ADATE$cyc
      ADATE=`$NDATE +24 $ADATE | cut -c1-8`
   done # ADATE<endate
done  # TYPE

echo "*** Finished script $0"

exit 0
