#!/bin/ksh

set -x
cd $DATA

#############################################################################
# Control script for RTOFS(Atlantic)-based Gulf Stream finder and Loop Current
# tracer 
# Steps are:
#  1) Get hourly surface temperature fields from RTOFS-Atlantic
#  2) Average them appropriately to make daily fields  
#  3) Get the navy north wall analysis for reference, if possible
#  4) If there is a north wall analysis to run with, do so, else use yesterday's
#       most fit genome
# Robert Grumbine 14 March 2006
#
# FEB 2007 - Cokley - Modified for production
# MAR 2009 - Magee  - Removed code to set julian date to 3 digit number since
#                     this is what's now returned by the %j option.
##############################################################################

jd=`date +"%j"`
#Grumbine Jan 22 2010
jdm=`expr $jd - 1`
if [ $jdm -eq 0 ] ; then
  jdm=365
fi
if [ $jdm -lt 100 -a $jdm -ge 10 ] ; then
  jdm=0$jdm
fi
if [ $jdm -lt 10 ] ; then
  jdm=00$jdm
fi

jdp=`expr $jd + 1`
if [ $jdp -eq 366 ] ; then
  jdp=001
fi
if [ $jdp -lt 100 -a $jdp -ge 10 ] ; then
  jdp=0$jdp
fi
if [ $jdp -lt 10 ] ; then
  jdp=00$jdp
fi

                                                                                
#Get the old genes if available:
if [ -f $COMINm1/rtofs_genes_in ] ; then
  cp $COMINm1/rtofs_genes_in .
fi

#########################################################
# Step 1: Get RTOFS-Atlantic hourly output files

cp ${COMINOFS}/ofs_atl.t00z.[nf]???.grb .

#########################################################
# Step 2: Get hours to work with and degrib appropriate 
# hourly files from hycom:

if [ -f nowcast ] ; then
  rm nowcast
fi

#Need the rtofs grid description
cp $FIXomb/ofs_atl.ncep1_12.regional.grid.a fort.061a
cp $FIXomb/ofs_atl.ncep1_12.regional.grid.b fort.61

for PARM in TMP SSHG
do
  for day in 1 2 3 4
  do
    if [ ! -f $PARM.day.$day ] ; then
      firsth=`expr $day \* 24 \- 12`
      hh=$firsth
      k=0
      while [ $k -lt 25 ]
      do
        if [ $hh -lt 100 ] ; then
          fn=ofs_atl.t00z.f0${hh}.grb
        else
          fn=ofs_atl.t00z.f${hh}.grb
        fi
        $EXECutil/wgrib $fn | grep $PARM | $EXECutil/wgrib -i $fn -nh \
                    -append -o $PARM.day.$day
        k=`expr $k + 1`
        hh=`expr $hh + 1`
      done
    fi
  done 


#Constructing average for nowcast:
  if [ ! -f $PARM.nowcast ] ; then
    hh=-12
    while [ $hh -le 12 ]
    do
  
      if [ $hh -lt 0 ] ; then
        fn=ofs_atl.t00z.n${hh}.grb
      elif [ $hh -eq 0 ] ; then
        fn=ofs_atl.t00z.n000.grb
      else 
        fn=ofs_atl.t00z.f0${hh}.grb
      fi
    
      $EXECutil/wgrib $fn | grep $PARM | $EXECutil/wgrib -i $fn -nh \
                    -append -o $PARM.nowcast 
    
      hh=`expr $hh + 1`
      if [ $hh -ge 0 -a $hh -lt 10 ] ; then
        hh=0$hh
      elif [ $hh -gt -10 -a $hh -lt 0 ] ; then
        hh=-0`echo $hh | cut -c2`
      fi 
    
    done
  fi

  for fn in $PARM.nowcast $PARM.day.1 $PARM.day.2 $PARM.day.3 $PARM.day.4
  do
    $EXEComb/rtofs_avg $fn avg.$fn >> $pgmout 2>> errfile
    export err=$?; err_chk
    echo $err
  done

done

#########################################################
# Step 3: Get a current Navy Gulf Stream File if possible:

if [ -f gs${jd}nw.sub ] ; then
  cp ${DCOM}/${PDY}/wtxtbul/gs${jd}nw.sub .
else
  cp ${DCOM}/${PDYm1}/wtxtbul/gs${jd}nw.sub .
fi
#Try again with yesterday -- Grumbine 2010 Jan 22
if [ ! -f gs${jd}nw.sub ] ; then
  if [ -f ${DCOM}/${PDYm1}/wtxtbul/gs${jd}nw.sub ] ; then
    cp ${DCOM}/${PDYm1}/wtxtbul/gs${jd}nw.sub .
  elif  [ -f ${DCOM}/${PDYm1}/wtxtbul/gs${jdm}nw.sub ] ; then
    cp ${DCOM}/${PDYm1}/wtxtbul/gs${jdm}nw.sub  gs${jd}nw.sub
  elif  [ -f ${DCOM}/${PDYm1}/wtxtbul/gs${jdp}nw.sub ] ; then
    cp ${DCOM}/${PDYm1}/wtxtbul/gs${jdp}nw.sub  gs${jd}nw.sub
  fi
fi

#Parse the Gulf Stream file if possible:
if [ -f gs${jd}nw.sub ] ; then
    $USHomb/gsf_parse.pl nout sout < gs${jd}nw.sub
    cp nout nout.$PDY
    cp sout sout.$PDY
fi

if [ ! -f nout.$PDY -o ! -f sout.$PDY ] ; then
  gs=no
else
  gs=yes
fi


#########################################################
# Step 4: Run Genetic finder if there is a navy file for comparison,
# Else, run the straightforward tracer off yesterday's genes

period=nowcast
if [ -f nout.$PDY ] ; then
   $EXEComb/rtofs_gsf avg.SSHG.$period avg.TMP.$period \
                    rtofs_genes_out rtofs_gsf_$period rtofs_lc_$period >> $pgmout 2>>errfile
   export err=$?; err_chk
   echo $err
   cp rtofs_genes_out rtofs_genes_in
  if test "$SENDCOM" = "YES"
    then
      cp rtofs_genes_in $COMOUT
  fi

else
    $EXEComb/rtofs_trace avg.SSHG.$period avg.TMP.$period \
             rtofs_genes_in rtofs_gsf_$period rtofs_lc_$period \
             rtofs_gsf_${period}.kml rtofs_lc_${period}.kml >> $pgmout 2>> errfile
    export err=$?; err_chk
    echo $err
fi

#Now do the forecast periods by rtofs_trace -- always works this way
for period in day.1 day.2 day.3 day.4
do
    $EXEComb/rtofs_trace avg.SSHG.$period avg.TMP.$period \
             rtofs_genes_in rtofs_gsf_$period rtofs_lc_$period \
             rtofs_gsf_${period}.kml rtofs_lc_${period}.kml >> $pgmout 2>> errfile
    export err=$?; err_chk
    echo $err
done

#########################################################
#Step 5 Distribute the output as appropriate:

if test "$SENDCOM" = "YES"
   then
     cp rtofs_gsf_* $COMOUT
     cp rtofs_lc_* $COMOUT
     cp rtofs_genes_in $COMOUT
fi

set +x
echo " ***** PROCESSING COMPLETED NORMALLY *****"
echo " ***** PROCESSING COMPLETED NORMALLY *****"
echo " ***** PROCESSING COMPLETED NORMALLY *****"
