#!/bin/sh
###############################################################################
#                                                                             #
# This script creates the sst the assimilation for OFS_atl model              #
#                                                                             #
#  ofs_sst_assim.sh : extract sst observations from mods                      #
#          output   : anomaly field  innovation and residual vectors.         #
#                                                                             #
###############################################################################

echo "*** Started script $0"

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

  srtdate=$1
  inova_in=$2
  field=sst

  msg="${field} analysis for $srtdate using background from $inova_in"
  postmsg  ${jlogfile} "${msg}"


  hostsp=`hostname -s`
  echo "sst assimilation has begun on $hostsp"

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

  sst_days=1
  pastdate=`${utilexec}/ndate -\`expr ${sst_days} \* 24 \` ${srtdate}00`
  pastdate=`echo $pastdate | cut -c1-8`


  mkdir -p $DATA/${field}
  export DATA=$DATA/${field}
  cd $DATA
  echo "Current working directory is now $DATA"

  ${utilscript}/setup.sh
# --------------------------------------------------------------------------- #
# 0.b  copy necessary files to work directory


# --------------------------------------------------------------------------- #
# 1. get sst observations from avhrr, transform to model coordinates and 
#    perform qc checks

export cycle=$cyc
###export dcom=/dcom/us007003

export mask=mask

# 1.a.  get sst observations from avhrr
#export datatype='sstns'
export datatype='sstnvh'

$USHofs/ofs_get_sstnvh.sh $pastdate $srtdate || exit -1

# output is avhrr.${field}.$srtdate.txt

# 1.b. run qc check on avhrr data
if [[ -f avhrr.${field}.${srtdate}.txt ]] ; then
    $USHofs/ofs_boxqc.sh sstAVHRR avhrr.${field}.${srtdate}.txt avhrr.${field}.${srtdate}.box.txt || echo "DANGER DANGER"
fi
touch avhrr.${field}.${srtdate}.box.txt

# --------------------------------------------------------------------------- #
# 2. get sst observations from goes, and run qc check

# 2.a.  get sst observations from goes
export datatype='gfohal'
$USHofs/ofs_get_sstgoes.sh $srtdate $srtdate

# 2.b.  run qc check on goes data
# check if anything decoded
if [[ -f goes.${field}.${srtdate}.txt ]] ; then
   $USHofs/ofs_boxqc.sh sstGOES goes.${field}.${srtdate}.txt goes.${field}.${srtdate}.box.txt || exit -1

#       run filter to bias GOES towrds AVHRR
   if [[ -s goes.${field}.${srtdate}.box.txt && -s  avhrr.${field}.${srtdate}.box.txt ]] ; then
       mv  goes.${field}.${srtdate}.box.txt  rough_goes.${field}.${srtdate}.box.txt
   $USHofs/ofs_bias.sh avhrr.${field}.${srtdate}.box.txt  rough_goes.${field}.${srtdate}.box.txt goes.${field}.${srtdate}.box.txt || exit -1
   fi

fi
touch goes.${field}.${srtdate}.box.txt

#--------------------------------------------------------------------------- #
# 3. get sst observations from in situ 
#export datatype='ships dbuoy mbuoy lcman metar '
#export datatype='bathy tesac trkob '
export datatype="all"
export level='0'

rm -rf situ.$field.$srtdate.box.txt
for platform in $datatype
do
  $USHofs/ofs_get_sstsitu.sh $pastdate $srtdate ${platform}
  if [[ -f situ.$field.$platform.$srtdate.$level ]] ; then
       $USHofs/ofs_boxqc.sh sstGOOFY situ.$field.$platform.$srtdate.$level situ.$field.$srtdate.$platform.box.txt
   fi
   cat situ.$field.$srtdate.$platform.box.txt >> situ.$field.$srtdate.box.txt
done

#--------------------------------------------------------------------------- #
# 4. combine data as dictated by the parm file

while read sourceline
do
  sstid=`echo $sourceline | cut -d" " -f1`
  sstswitch=`echo $sourceline | cut -d" " -f2`
  if [ $sstswitch -eq 0 ]
  then
     cat $sstid.$field.${srtdate}.box.txt >> ${field}.${srtdate}.test.txt
  else
     cat $sstid.$field.${srtdate}.box.txt >> ${field}.${srtdate}.txt
  fi
done < $PARMofs/${RUN}_${modID}.sst_data.txt

if [ -s ${field}.${srtdate}.txt ]
then

#--------------------------------------------------------------------------- #
# 5. generate innovation vector
# 5.a get files from FIX

  rm -rf   regional.grid.a regional.grid.b
  ln -s -f $FIXofs/${RUN}_${modID}.${gridres}.regional.grid.b regional.grid.b
  ln -s -f $FIXofs/${RUN}_${modID}.${gridres}.regional.grid.a regional.grid.a

# 5.b compute innovation vector

  export OBSFILE=${field}.${srtdate}.txt
  export MODFILE=$inova_in
  export num_records=10
  export label='temp     ='
  export fac_unit=1.0
  export fac_out=2.5
  export fac_err=1.2


  $USHofs/ofs_inova.sh

#--------------------------------------------------------------------------- #
# 6 compute anomaly field and residual vector

#6.1 set environmental variables

   export OBSFILE=${field}.${srtdate}.t${cyc}z.inova.txt
   export OBSFILE_OUT=${field}.${srtdate}.t${cyc}z.spread.txt
   export FLD_NAME=${field}.${srtdate}.t${cyc}z.spread.bin
   export fac_unit=1
   export record_no=4

#6.2 execute spread
   $USHofs/ofs_spread.sh

#6.3 post a status message
   msg="THE OFS_SST_ASSIM job  HAS ENDED NORMALLY."

else
#7. ABNORMAL EXIT
# ACTIONS:

# 7.1 post a diagnostic message.
   msg="No  data avaiable in the region  for date ${srtdate}"
   postmsg "$jlogfile" "$msg"

#7.2 output file is an  empty file
   touch    ${field}.${srtdate}.t${cyc}z.spread.bin

#7.3 post a status message
   msg="WARNING: THE OFS_SST_ASSIM job NOT BEEN COMPLETED ."       
fi
   postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################

echo "*** Finished script $0"
