########################### EXVALRTGSST.SH.SMS ###############################
set +xa
echo "---------------------------------------------------------------"
echo "JVALRTGSST - SST2DVAR and NAM processing"
echo "---------------------------------------------------------------"
echo "History: Thursday, September 13, 2001 - Create separate script forvalidation of RTG_SST analysis"
echo "Revised: May 6, 2004 - Made changes for NOAA-17."
echo "Revised: Mar 2012 - Remove chmod 775 $COMOUT/* to protect restricted files."
echo "---------------------------------------------------------------"
set -xa

###########################################################
# Processing/Flow of script
# 1) Copy this job's input files to the working directory
# 2) Run steps required for validation of RTG_SST analysis
# 3) Save output files to $COMOUT
# 4) Remove files located in this job's working directory
###########################################################

#############################################################
# Ensure that this job is pointed to the production temporary
# working directory, which is where all of this job's input 
# and output files will be saved during execution
#############################################################
cd $DATA

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

set -x
cp $COMIN/sst2dvar_grb_0.5           sst2dvgrb0.5
cp $COMIN/sst2dvar_grb_0.5.index     sst2dvgrb0.5.index
cp $COMIN/sst2dvar_nsatn19_used.$PDY nsatn19c.realtime.used
cp $COMIN/sst2dvar_dsatn19_used.$PDY dsatn19c.realtime.used
cp $COMIN/sst2dvar_buoys_used.$PDY   buoy.realtime.used
cp $COMIN/sst2dvar_ships_used.$PDY   ship.realtime.used
cp $COMIN/sst2dvar_engice_used.$PDY  ice.realtime.used

##if [ -s $COMIN/sst2dvar_buoys_used.$PDY ]
##then
##   cp $COMIN/sst2dvar_buoys_used.$PDY buoy.realtime.used
##fi

echo $PDY > buoy_eval

sufstring="05 16 27 38 49"

for suf in $sufstring ; do

##  create guess from previous analysis and climatology
  PREVSST=$COMINm1/sst2dvar_grb_verf${suf}_0.5
  if [ -s $PREVSST ]
  then
    cp $PREVSST prevanal_verf${suf}.grb
    err=$?
  else
    echo "Yesterday's analysis not available for first guess.  Check past days."
    err=1
    for SDIR in $COMINm2 $COMINm3 $COMINm4 $COMINm5 
    do
      PREVSST=$SDIR/sst2dvar_grb_verf${suf}_0.5
      if [ -s $PREVSST ]
      then
        cp $PREVSST prevanal_verf${suf}.grb
        err=$?
        warnSUBJ=JRTGSSTVAL
        warnBODY="Today is ${PDY}.  Guess field in test from ${PREVSST}."
        export warnSUBJ warnBODY
        break
      fi
    done
  fi

  if [ $err -ne 0 ]
  then
    echo #####################################################
    echo #####################################################
    echo   !!!!     NO PREVSST FILE   !!!!
    echo #####################################################
    echo #####################################################
    warnSUBJ=JRTGSSTVAL
    warnBODY="Todays date: ${PDY}. No GUESS. RTG_SST VALIDATION FAILED"
    export warnSUBJ warnBODY
    err_exit
    exit
  fi

  export pgm=sst2dvar_adjguess
  . prep_step

  export XLFUNIT_11="$FIXsst2dv/sst2dvar_aoi.61.90.clim.halfdeg"
  export XLFUNIT_12="prevanal_verf${suf}.grb"
  export XLFUNIT_13="buoy.realtime.used"
  export XLFUNIT_51="guess_verf${suf}"
  export XLFUNIT_52="climatology"

  startmsg
  $EXECsst2dv/sst2dvar_adjguess >> $pgmout 2>errfile
  export err=$?;$USHsst2dv/sst2dvar_errwarn.sh;export err=$?;err_chk

  export pgm=sst2dvar_verfbuoy
  . prep_step

  export XLFUNIT_11="buoy.realtime.used"
  export XLFUNIT_51="buoykeep${suf}.realtime"
  export XLFUNIT_52="buoydrop${suf}.realtime"

  startmsg

  suffix=$suf
  dropstring="REALTIME\nDROP "
  while [ ${#suffix} -gt 0 ] ; do
    sufdigit=$(echo $suffix | cut -c1)
    dropstring="${dropstring}////$sufdigit "
    suffix=$(echo $suffix | cut -c2-)
  done

  echo "$dropstring" | $EXECsst2dv/sst2dvar_verfbuoy >> $pgmout 2>errfile
  export err=$?;$USHsst2dv/sst2dvar_errwarn.sh;export err=$?;err_chk

## OI SST analysis
# read super observations and previous analysis (first guess)
# compute a new analysis using optimum interpolation (OI)
# and output the new SST OI analysis on a 1 degree grid

  export pgm=sst2dvar_rthranl3
  . prep_step
  
  export XLFUNIT_11="nsatn19c.realtime.used"
  export XLFUNIT_12="dsatn19c.realtime.used"
  export XLFUNIT_13="buoykeep${suf}.realtime"
  export XLFUNIT_14="ship.realtime.used"
  export XLFUNIT_15="ice.realtime.used"
  export XLFUNIT_16="guess_verf${suf}"
  export XLFUNIT_21="climatology"
  export XLFUNIT_31="$FIXsst2dv/sst2dvar_ls_nas.halfdeg.dat"
  export XLFUNIT_41="$FIXsst2dv/sst2dvar_stat0.5"
  export XLFUNIT_42="$FIXsst2dv/sst2dvar_IncrAmpGLB0.5"
  export XLFUNIT_43="$FIXsst2dv/sst2dvar_SSTgrdntScl.gr"
  export XLFUNIT_62="sst2dvgrb1.0_verf${suf}"
  export XLFUNIT_63="sst2dvgrb0.5_verf${suf}"
  export XLFUNIT_64="anomlygrb0.5_verf${suf}"
  export XLFUNIT_65="sst2dvgrb1.0_verf${suf}_awips"
  export XLFUNIT_71="nsatn19c.realtime.used_verf"
  export XLFUNIT_72="dsatn19c.realtime.used_verf"
  export XLFUNIT_73="buoy.realtime.used_verf"
  export XLFUNIT_74="ship.realtime.used_verf"
  export XLFUNIT_75="ice.realtime.used_verf"
  export XLFUNIT_87="weaver"

  cat <<"  end_ft05" > rthranl.input
   &CONTR2 LBUOY=T,LSHIP=T,LSTD14=T,LSTN14=T,LICE=T,
           LBUOYCHK=F,LSHIPCHK=F,LSTD14CHK=F,LSTN14CHK=F,LICECHK=F /
  end_ft05

  startmsg
  $EXECsst2dv/sst2dvar_rthranl3 < rthranl.input >> $pgmout 2>errfile
  export err=$?;$USHsst2dv/sst2dvar_errwarn.sh;export err=$?;err_chk

  $EXECutil/grbindex sst2dvgrb0.5_verf${suf} sst2dvgrb0.5_verf${suf}.index 
  $EXECutil/grbindex sst2dvgrb1.0_verf${suf} sst2dvgrb1.0_verf${suf}.index 
  $EXECutil/grbindex anomlygrb0.5_verf${suf} anomlygrb0.5_verf${suf}.index 

  export pgm=sst2dvar_evalregn
  . prep_step

  export XLFUNIT_11="buoydrop${suf}.realtime"
  export XLFUNIT_26="sst2dvgrb0.5_verf${suf}"
  export XLFUNIT_27="sst2dvgrb0.5_verf${suf}.index"
  export XLFUNIT_28="sst2dvgrb0.5"
  export XLFUNIT_29="sst2dvgrb0.5.index"
  export XLFUNIT_31="$FIXsst2dv/sst2dvar_ls_nas.halfdeg.dat"
  export XLFUNIT_51="region_eval${suf}"
  export XLFUNIT_52="buoy_eval${suf}"

  startmsg
  $EXECsst2dv/sst2dvar_evalregn < $PARMsst2dv/sst2dvar_buoyboxes.namelist >> $pgmout 2>errfile
  export err=$?;$USHsst2dv/sst2dvar_errwarn.sh;export err=$?;err_chk

  cat buoy_eval buoy_eval${suf} >> buoy_eval_temp
  mv buoy_eval_temp buoy_eval

  if test "$SENDCOM" = 'YES'
  then
    cp sst2dvgrb0.5_verf${suf} $COMOUT/sst2dvar_grb_verf${suf}_0.5
    cp sst2dvgrb0.5_verf${suf}.index \
       $COMOUT/sst2dvar_grb_verf${suf}_0.5.index
    cp buoydrop${suf}.realtime $COMOUT/sst2dvar_buoydrop${suf}.$PDY
    cp buoykeep${suf}.realtime $COMOUT/sst2dvar_buoykeep${suf}.$PDY
    cp region_eval${suf} $COMOUT/sst2dvar_region_eval${suf}.$PDY
    cp buoy_eval${suf}   $COMOUT/sst2dvar_buoy_eval${suf}.$PDY
  
  fi

done

export pgm=sst2dvar_evalavg
. prep_step

export XLFUNIT_11="nsatn19c.realtime.used"
export XLFUNIT_12="dsatn19c.realtime.used"
export XLFUNIT_13="buoy.realtime.used"
export XLFUNIT_14="ship.realtime.used"
export XLFUNIT_15="ice.realtime.used"

iunit=21
for suf in $sufstring ; do
  eval export XLFUNIT_$iunit="region_eval${suf}"
  iunit=$(($iunit+1))
done  
export XLFUNIT_$iunit
unset XLFUNIT_$iunit 

export XLFUNIT_51="region_wgtavg_eval"
export XLFUNIT_52="region_unwgtavg_eval"
export XLFUNIT_53="region_datacounts"

startmsg
$EXECsst2dv/sst2dvar_evalavg < $PARMsst2dv/sst2dvar_buoyboxes.namelist >> $pgmout 2>errfile
export err=$?;$USHsst2dv/sst2dvar_errwarn.sh;export err=$?;err_chk

export pgm=sst2dvar_evalavg
. prep_step

if test "$SENDCOM" = 'YES'
then
  cp buoy_eval            $COMOUT/sst2dvar_buoy_eval.$PDY
  cp region_wgtavg_eval   $COMOUT/sst2dvar_region_wgtavg_eval.$PDY
  cp region_unwgtavg_eval $COMOUT/sst2dvar_region_unwgtavg_eval.$PDY
  cp region_datacounts    $COMOUT/sst2dvar_region_datacounts.$PDY

fi

msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

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