#!/bin/sh
###############################################################################
# Script ofs_ssh_assim.sh                                                     #
#                                                                             #
###############################################################################

# 0.  Preparations
# 0.a Basic modes of operation

echo "*** Started script $0"

  export FLD=ssh

  mkdir -p $DATA/ssh
  export DATA=$DATA/ssh
  cd $DATA
  echo "Current Dir is now $DATA"

  ${utilscript}/setup.sh

  seton='+xa'
  setoff='+xa'
  set $seton

  set $setoff
  echo ' '
  echo '                      *******************************'
  echo '                      *** SSH ASSIMILATION SCRIPT ***'
  echo '                      *******************************'
  echo ' '
  echo "Starting at : `date`"
  set $seton

# 0.b date and time stuff
# for ssh assimilation, pull data from today to today-$ssh_days


ssh_days=10

export cdate=$PDY

# Pull hourly SSH from today-1
export nowcast_date=$PDYm1

cdatem_ssh=`${utilexec}/ndate -\`expr ${ssh_days} \* 24 \` ${cdate}00`
cdatem_ssh=`echo $cdatem_ssh | cut -c1-8`

export exit_warning=0

export mask=mask_ssh

# --------------------------------------------------------------------------- #
# 1a.  get ssh observations from jsn and gfo

   export dcom=/dcom/us007003
   export srtdate=$cdate


   ${USHofs}/ofs_get_altim.sh $cdatem_ssh $cdate || exit $exit_warning

# --------------------------------------------------------------------------- #
# 2.  run box on each type

    ${USHofs}/ofs_boxqc.sh shaJASON ${FLD}.t${cyc}z.jsn altim.jsn.$cdate.box.txt || exit $exit_warning

    ${USHofs}/ofs_boxqc.sh shaGFO ${FLD}.t${cyc}z.gfo altim.gfo.$cdate.box.txt || exit $exit_warning
   
    ${USHofs}/ofs_boxqc.sh shaENV ${FLD}.t${cyc}z.env altim.env.$cdate.box.txt || exit $exit_warning

    ${USHofs}/ofs_boxqc.sh shaJASON2 ${FLD}.t${cyc}z.jsn2 altim.jsn2.$cdate.box.txt || exit $exit_warning
#--------------------------------------------------------------------------- #
# 3. combine data

   cat altim.jsn.$cdate.box.txt altim.jsn2.$cdate.box.txt altim.gfo.$cdate.box.txt altim.env.$cdate.box.txt> ${FLD}.$cdate.t${cyc}z.box.txt

# add pre-prepared observations
   if [ -s ${PARMofs}/${RUN}_${modID}.ssh_box_correction.txt ]
   then
       cat ${FLD}.$cdate.t${cyc}z.box.txt ${PARMofs}/${RUN}_${modID}.ssh_box_correction.txt > tmp.file.$$
       mv tmp.file.$$ ${FLD}.$cdate.t${cyc}z.box.txt
   fi

   if [ -s ${FLD}.$cdate.t${cyc}z.box.txt ]
   then

#--------------------------------------------------------------------------- #
# 4. compute sea surface height anomaly from averaged ssh and mean dynamic topography.

# 4.a. set grid  and depth files

        cp $FIXofs/${RUN}_${modID}.${gridres}.regional.grid.a regional.grid.a
        cp $FIXofs/${RUN}_${modID}.${gridres}.regional.grid.b regional.grid.b
        cp $FIXofs/${RUN}_${modID}.${gridres}.regional.depth.a regional.depth.a
        cp $FIXofs/${RUN}_${modID}.${gridres}.regional.depth.b regional.depth.b

# 4.b set mean dynamic topography

       cp $FIXofs/${RUN}_${modID}.A${gridsize}mdt.bin  mdt.bin

       cp $FIXofs/${RUN}_${modID}.A${gridsize}pscx.bin  pscx.bin

# 4.c gather sea surface height fields, and compute anomaly
#     and averaged bottom fields
        export anomaly_filename=${cdate}.${FLD}
        export label="adha     ="
        export anomaly_binfile=${FLD}.${srtdate}.t${cyc}z.adh.bin
        export bottom_binfile=${FLD}.${srtdate}.t${cyc}z.abot.bin
        ${USHofs}/ofs_adh.sh


#--------------------------------------------------------------------------- #
# 5. generate innovation vector
# 5.a link grid  and depth files: see 4.a

# 5.b set environment variables for inova
#      label:  see 4.c 

       export OBSFILE=${FLD}.${cdate}.t${cyc}z.box.txt
       export MODFILE=${anomaly_filename}
       export num_records=2

       export fac_unit=1.0
       export fac_out=2.3
       export fac_err=1.2

#5.c run inova

       ${USHofs}/ofs_inova.sh

#--------------------------------------------------------------------------- #
# 6. run spread (2dvar)

#6.a set environmental variables for spread

       export OBSFILE=${FLD}.${srtdate}.t${cyc}z.inova.txt
       if [ -s ${OBSFILE} ]
       then
       export OBSFILE_OUT=${FLD}.${srtdate}.t${cyc}z.spread.txt
       export FLD_NAME=${FLD}.${srtdate}.t${cyc}z.spread.bin
       export fac_unit=1
       export record_no=4

       ${USHofs}/ofs_spread.sh

       msg="THE OFS_SSH_ASSIM job  HAS ENDED NORMALLY."
       else
       msg="No altimeter data available for date ${srtdate}"
       postmsg "$jlogfile" "$msg"
       msg="WARNING: THE OFS_SSH_ASSIM job NOT BEEN COMPLETED ."       
       fi
   else
       msg="No altimeter data available for date ${srtdate}"
       postmsg "$jlogfile" "$msg"
       msg="WARNING: THE OFS_SSH_ASSIM job NOT BEEN COMPLETED ."       
   fi
   postmsg "$jlogfile" "$msg"

echo "*** Finished script $0"
