#!/bin/sh
set +xa
echo "---------------------------------------------------------------"
echo "Script - SSTOI and NAM processing (previously J837)"
echo "---------------------------------------------------------------"
echo "History: Tuesday, May 04, 1993 - Implement SSTOI Analysis"
echo "Revised: Tuesday, August 29, 1995 - convert from CSS to FTA"
echo "Revised: Tue October 3, 1995 - added operational NAM SST script"
echo "Revised: Mon, 22 September, 1996 - Add OI SST processing."
echo "Revised: Wed, 26 October, 1996 - Add Satellite SST to OI processing."
echo "Revised: Tue, 18 March, 1997 - Add rmcomdir step"
echo "Revised: Tue, 17 June, 1997 - Add step to submit mkfossst.sh."
echo "Revised: 02/08/2000 - Initial implementation on the IBM SP "
echo "                      NAM SST script now in separate job"
echo "Revised: 06/28/2001 - Switch to NOAA-16 for satellite retrievals"
echo "                    - Remove loop for backup satellite source"
echo "                    - satdata vars optionally passed from J-job"
echo "                      (defaults here to sstnvh & noaa16)"
echo "                    - sat data processing code outputs data"
echo "                      for 3 sats for future upgrades but only"
echo "                      the used satellite gets saved to COMOUT"
echo "Revised: 05/10/2004 - Switch to NOAA-17 for satellite retrievals"
echo "                    - comment temporary transition block"
echo "---------------------------------------------------------------"
set -xa

###########################################################
# Processing/Flow of script
# 1) Copy this job's input files to the working directory
# 2) Run steps required for data preprocessing and  OI sst analysis
# 3) Save output files to $COMOUT
# 4) Interpolate 1-degre grid to 2.5 nh and sh degree grids
# 5) 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"

echo ${SAT_alg:=sstnvh}
echo ${SAT_id:=noaa17}

#############################################################
# cp OISST input files from prior 6 days to working directory
# checking yesterdays directory first, check others if necessary
#############################################################
set -x

for CDATE in $PDYm7 $PDYm6 $PDYm5 $PDYm4 $PDYm3 $PDYm2
do
   for CTYPE in ships dbuoy mbuoy  dsat.${SAT_alg}.${SAT_id}  nsat.${SAT_alg}.${SAT_id}  engice.grb
   do
      for SDIR in $COMINm1 $COMINm2 $COMINm3 $COMINm4 $COMINm5 $COMINm6 $COMINm7 
      do
         if [ -s $SDIR/sstoi_$CTYPE.$CDATE ]
         then
            cp $SDIR/sstoi_$CTYPE.$CDATE .
            break
         fi
      done
   done
done

#
# Dump insitu files that were not available in previous runs
#
export CHKPRT=true
export DUMP=/nwprod/ush/dumpjb
export FORM=ibm
#export LOUD=off  #defaults to off. commented so can now set in j-job

#  For each day, check for file for each type (ships, dbuoy, mbuoy)
# dump and process if missing or too small
#  always get bufr'd data for yesterday to pick up late obs.
for CDATE in $PDYm7 $PDYm6 $PDYm5 $PDYm4 $PDYm3 $PDYm2 $PDYm1
do
  for CTYPE in ships dbuoy mbuoy
  do

#check file existence/size
    if [ -s sstoi_${CTYPE}.$CDATE ];then
      filesize=`ls -l sstoi_${CTYPE}.$CDATE | awk '{print $5}' `
    else 
      filesize=0
    fi

    if  [ $CDATE = $PDYm1 -o $filesize -lt 60 -o $filesize -lt 60 ]
    then

      $DUMP $CDATE\12.0 11.99999 $CTYPE
      dumpstat=$?
      cat ${CTYPE}.out

      if [ $dumpstat -ne 0 ]
      then
         msg="data dump failed for $CTYPE $CDATE: $dumpstat"
         postmsg "$jlogfile" "$msg"
         export pgm=dump_${CTYPE}_$CDATE
         export err=3;$USHsstoi/sstoi_errwarn.sh
      else
        export pgm=sstoi_shp
        . prep_step

        #  run sstoishp on dumped bufr data
        echo $CDATE $CTYPE > sstoishp.input
        echo $CHKPRT     >> sstoishp.input

        export XLFUNIT_10="$CTYPE.$FORM"
        export XLFUNIT_31="$FIXsstoi/sstoi_lstags.halfdeg"
        export XLFUNIT_51="sstoi_$CTYPE.$CDATE"
 
        startmsg
        $EXECsstoi/sstoi_shp < sstoishp.input >> $pgmout 2>errfile
        export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk
      fi
    fi
  done
done

#  save insitu files for tomorrows run
if test "$SENDCOM" = 'YES'
then
   for CDATE in $PDYm7 $PDYm6 $PDYm5 $PDYm4 $PDYm3 $PDYm2 $PDYm1
   do
      cp sstoi_ships.$CDATE $COMOUT
      cp sstoi_dbuoy.$CDATE $COMOUT
      cp sstoi_mbuoy.$CDATE $COMOUT
   done
fi


##  For each day, check for file for satellite data
# dump and process if missing
#-----------------------------------------------------------------
export DUMP=/nwprod/ush/dumpjb
export FORM=ibm
#export LOUD=off  #defaults to off. commented so can now set in j-job
export CHKPRT=true

#  For each day, check for file
#  if file does not exist or has is too small, get bufr'd data.
#  always get bufr'd data for yesterday to pick up late obs.

for CDATE in $PDYm7 $PDYm6 $PDYm5 $PDYm4 $PDYm3 $PDYm2 $PDYm1
do

#check file existence/size
    satfile=sstoi_dsat.${SAT_alg}.${SAT_id}.$CDATE
    if [ -s $satfile ];then
      filesize_dsat=`ls -l $satfile | awk '{print $5}' `
    else 
      filesize_dsat=0
    fi
    satfile=sstoi_nsat.${SAT_alg}.${SAT_id}.$CDATE
    if [ -s $satfile ];then
      filesize_nsat=`ls -l $satfile | awk '{print $5}' `
    else
      filesize_nsat=0
    fi


  if  [ $CDATE = $PDYm1 -o $filesize_dsat -lt 60 -o $filesize_nsat -lt 60 ]
  then

    CTYPE=$SAT_alg
    $DUMP $CDATE\12.0 11.99999 $CTYPE
    dumpstat=$?
    cat ${CTYPE}.out

    if [ $dumpstat -ne 0 ]
    then
       msg="data dump failed for $CTYPE $CDATE: $dumpstat"
       postmsg "$jlogfile" "$msg"
       export pgm=dump_${CTYPE}_$CDATE
       export err=3;$USHsstoi/sstoi_errwarn.sh
    else

       export pgm=sstoi_sat
       . prep_step

       echo $CDATE $SAT_alg > sstoisat.input
       echo $CHKPRT     >> sstoisat.input

       export XLFUNIT_20="$CTYPE.$FORM"
       export XLFUNIT_31="$FIXsstoi/sstoi_lstags.halfdeg"
       export XLFUNIT_51="sstoi_dsat.${SAT_alg}.noaa17.$CDATE"
       export XLFUNIT_52="sstoi_nsat.${SAT_alg}.noaa17.$CDATE"
       export XLFUNIT_61="sstoi_dsat.${SAT_alg}.noaa15.$CDATE"
       export XLFUNIT_62="sstoi_nsat.${SAT_alg}.noaa15.$CDATE"
       export XLFUNIT_71="sstoi_dsat.${SAT_alg}.noaa16.$CDATE"
       export XLFUNIT_72="sstoi_nsat.${SAT_alg}.noaa16.$CDATE"

       startmsg
       $EXECsstoi/sstoi_sat < sstoisat.input >> $pgmout 2>errfile
       export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk



#### THIS BLOCK IS TEMPORARY.  INCLUDED TO PRODUCE OLD FORMAT dsat AND nsat FILES FOR 
###    TRANSITION PERIOD IN CASE UNKNOWN USERS ACCESS THESE FILES.
##  TO BE DELETED AFTER DETERMINED TRANSITION PERIOD
##    kept simple, as requested, so no check for missing files is done.
##    any day processed by new sstoi_sat is processed here.
#########################################
#
#           export pgm=sstoi_sat.transition
#           . prep_step
#
#           echo $CDATE $SAT_alg > sstoisat.input
#           echo $CHKPRT     >> sstoisat.input
#
#           export XLFUNIT_20="$CTYPE.$FORM"
#           export XLFUNIT_31="$FIXsstoi/sstoi_lstags.halfdeg"
#           export XLFUNIT_51="sstoi_dsat.$CDATE"
#           export XLFUNIT_52="sstoi_nsat.$CDATE"
#
#           startmsg
#           $EXECsstoi/sstoi_sat.transition < sstoisat.input >> $pgmout 2>errfile
#           export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk
#           cp sstoi_dsat.$CDATE $COMOUT
#           cp sstoi_nsat.$CDATE $COMOUT
#
########################################
### END TEMPORARY BLOCK

    fi
  fi
done


#  save satellite archives for tomorrows run
if test "$SENDCOM" = 'YES'
then
   for CDATE in $PDYm7 $PDYm6 $PDYm5 $PDYm4 $PDYm3 $PDYm2 $PDYm1
   do
     cp sstoi_dsat.${SAT_alg}.${SAT_id}.$CDATE $COMOUT
     cp sstoi_nsat.${SAT_alg}.${SAT_id}.$CDATE $COMOUT
   done
fi


##  quality control ship and buoy data
export pgm=sstoi_insdt
. prep_step

export XLFUNIT_11="$FIXsstoi/sstoi_aoi.61.90.clim"
export XLFUNIT_12="$FIXsstoi/sstoi_stdev2d"
export XLFUNIT_21="sstoi_ships.$PDYm7"
export XLFUNIT_22="sstoi_ships.$PDYm6"
export XLFUNIT_23="sstoi_ships.$PDYm5"
export XLFUNIT_24="sstoi_ships.$PDYm4"
export XLFUNIT_25="sstoi_ships.$PDYm3"
export XLFUNIT_26="sstoi_ships.$PDYm2"
export XLFUNIT_27="sstoi_ships.$PDYm1"
export XLFUNIT_31="sstoi_dbuoy.$PDYm7"
export XLFUNIT_32="sstoi_dbuoy.$PDYm6"
export XLFUNIT_33="sstoi_dbuoy.$PDYm5"
export XLFUNIT_34="sstoi_dbuoy.$PDYm4"
export XLFUNIT_35="sstoi_dbuoy.$PDYm3"
export XLFUNIT_36="sstoi_dbuoy.$PDYm2"
export XLFUNIT_37="sstoi_dbuoy.$PDYm1"
export XLFUNIT_41="sstoi_mbuoy.$PDYm7"
export XLFUNIT_42="sstoi_mbuoy.$PDYm6"
export XLFUNIT_43="sstoi_mbuoy.$PDYm5"
export XLFUNIT_44="sstoi_mbuoy.$PDYm4"
export XLFUNIT_45="sstoi_mbuoy.$PDYm3"
export XLFUNIT_46="sstoi_mbuoy.$PDYm2"
export XLFUNIT_47="sstoi_mbuoy.$PDYm1"
export XLFUNIT_51="buoydat"
export XLFUNIT_52="shipdat"
export XLFUNIT_53="climdat"

iyrst=`echo "$PDYm7"|cut -c1-4`
imst=`echo "$PDYm7"|cut -c5-6`
idst=`echo "$PDYm7"|cut -c7-8`
cat <<end_ft05 > sstinsdt.input
$iyrst  $imst  $idst  7   iyrst,imst,idst,ndays  (format free)
$CHKPRT
end_ft05

startmsg
$EXECsstoi/sstoi_insdt < sstinsdt.input >> $pgmout 2>errfile
export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk

#  form 1-deg buoy, ship and ice super obs for oi analysis
# get ice files that were not available in previous runs from COM_ENGICE
#  copy all to COMOUT for tomorrows run if SENDCOM
for CDATE in $PDYm7 $PDYm6 $PDYm5 $PDYm4 $PDYm3 $PDYm2 $PDYm1
do
  if  [ ! -s sstoi_engice.grb.$CDATE ]
  then
    if [ -r ${COM_ENGICE}.${CDATE}/engice.t00z.grb ]
      then
      cp ${COM_ENGICE}.${CDATE}/engice.t00z.grb sstoi_engice.grb.$CDATE
      [ $SENDCOM = 'YES' ] && cp sstoi_engice.grb.$CDATE $COMOUT/sstoi_engice.grb.$CDATE
    fi
  else
    [ $SENDCOM = 'YES' ] && cp sstoi_engice.grb.$CDATE $COMOUT/sstoi_engice.grb.$CDATE
  fi
done

export pgm=sstoi_superins
. prep_step

export XLFUNIT_11="buoydat"
export XLFUNIT_12="shipdat"
export XLFUNIT_27="$FIXsstoi/seaice_newland"
export XLFUNIT_31="sstoi_engice.grb.$PDYm7"
export XLFUNIT_32="sstoi_engice.grb.$PDYm6"
export XLFUNIT_33="sstoi_engice.grb.$PDYm5"
export XLFUNIT_34="sstoi_engice.grb.$PDYm4"
export XLFUNIT_35="sstoi_engice.grb.$PDYm3"
export XLFUNIT_36="sstoi_engice.grb.$PDYm2"
export XLFUNIT_37="sstoi_engice.grb.$PDYm1"
export XLFUNIT_51="buoy.grid"
export XLFUNIT_52="ship.grid"
export XLFUNIT_53="ice.grid"

startmsg
$EXECsstoi/sstoi_superins >> $pgmout 2>errfile
export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk

if [ `grep -c 'NO ICE' $pgmout` -ne 0 ];then
  msg="Not all ice fields available"
  postmsg "$jlogfile" "$msg"
  export pgm=sstoi_superins.sub_getice
  export err=2;$USHsstoi/sstoi_errwarn.sh
fi

##  form 1-deg day and night satellite super obs for oi analysis
export pgm=sstoi_supersat
. prep_step

export XLFUNIT_21="buoy.grid"
export XLFUNIT_22="climdat"
export XLFUNIT_23="$FIXsstoi/sstoi_aoi.61.90.clim"
export XLFUNIT_24="$FIXsstoi/sstoi_stdev2d"
export XLFUNIT_31="sstoi_dsat.${SAT_alg}.${SAT_id}.$PDYm7"
export XLFUNIT_32="sstoi_dsat.${SAT_alg}.${SAT_id}.$PDYm6"
export XLFUNIT_33="sstoi_dsat.${SAT_alg}.${SAT_id}.$PDYm5"
export XLFUNIT_34="sstoi_dsat.${SAT_alg}.${SAT_id}.$PDYm4"
export XLFUNIT_35="sstoi_dsat.${SAT_alg}.${SAT_id}.$PDYm3"
export XLFUNIT_36="sstoi_dsat.${SAT_alg}.${SAT_id}.$PDYm2"
export XLFUNIT_37="sstoi_dsat.${SAT_alg}.${SAT_id}.$PDYm1"
export XLFUNIT_41="sstoi_nsat.${SAT_alg}.${SAT_id}.$PDYm7"
export XLFUNIT_42="sstoi_nsat.${SAT_alg}.${SAT_id}.$PDYm6"
export XLFUNIT_43="sstoi_nsat.${SAT_alg}.${SAT_id}.$PDYm5"
export XLFUNIT_44="sstoi_nsat.${SAT_alg}.${SAT_id}.$PDYm4"
export XLFUNIT_45="sstoi_nsat.${SAT_alg}.${SAT_id}.$PDYm3"
export XLFUNIT_46="sstoi_nsat.${SAT_alg}.${SAT_id}.$PDYm2"
export XLFUNIT_47="sstoi_nsat.${SAT_alg}.${SAT_id}.$PDYm1"
export XLFUNIT_51="dsat.grid"
export XLFUNIT_52="nsat.grid"


startmsg
$EXECsstoi/sstoi_supersat >> $pgmout 2>errfile
export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk

##  create guess from previous analysis and climatology
PREVSST=${COMINm1}/sstoi_grb
if [ -s $PREVSST ]
then
  cp $PREVSST prevanal.grb
  err=$?
else
  echo "Yesterday's analysis not available for first guess.  Check past days."
  err=1
  for CDATE in $PDYm2 $PDYm3 $PDYm4 $PDYm5 $PDYm6 $PDYm7
  do
    PREVSST=${com}/${RUN}.${CDATE}/sstoi_grb
    if [ -s $PREVSST ]
    then
      cp $PREVSST prevanal.grb
      err=$?
      warnSUBJ=J837
      warnBODY="Today is ${PDY}.  Guess field in oper from ${PREVSST}."
      export warnSUBJ warnBODY
      break
    fi
  done
fi

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

export pgm=sstoi_adjguess
. prep_step

export XLFUNIT_11="$FIXsstoi/sstoi_aoi.61.90.clim"
export XLFUNIT_12="prevanal.grb"
export XLFUNIT_13="buoy.grid"
export XLFUNIT_51="guess"

startmsg
$EXECsstoi/sstoi_adjguess >> $pgmout 2>errfile
export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk

##  correct day and night satellite biases
export pgm=sstoi_biascor
. prep_step

export XLFUNIT_11="ship.grid"
export XLFUNIT_12="buoy.grid"
export XLFUNIT_13="ice.grid"
export XLFUNIT_14="dsat.grid"
export XLFUNIT_15="nsat.grid"
export XLFUNIT_31="$FIXsstoi/sstoi_ls_nas.dat"
export XLFUNIT_41="guess"
export XLFUNIT_51="dsat.cgrid"
export XLFUNIT_52="nsat.cgrid"
export XLFUNIT_87="weaver"

startmsg
$EXECsstoi/sstoi_biascor >> $pgmout 2>errfile
export err=$?;$USHsstoi/sstoi_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=sstoi_anl
. prep_step

export XLFUNIT_11="nsat.cgrid"
export XLFUNIT_12="dsat.cgrid"
export XLFUNIT_13="buoy.grid"
export XLFUNIT_14="ship.grid"
export XLFUNIT_15="ice.grid"
export XLFUNIT_16="guess"
export XLFUNIT_31="$FIXsstoi/sstoi_ls_nas.dat"
export XLFUNIT_41="$FIXsstoi/sstoi_oi.stat"
export XLFUNIT_53="sstoianal"
export XLFUNIT_63="sstoigrb"
export XLFUNIT_87="weaver"

startmsg
$EXECsstoi/sstoi_anl < $PARMsstoi/sstoi_parm >> $pgmout 2>errfile
export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk

$EXECutil/grbindex sstoigrb sstoigrb.index 

#######################################################
# Convert to GRIB2
#######################################################

/nwprod/util/exec/cnvgrib -g12 -p40 sstoigrb sstoigrb.grib2
/nwprod/util/exec/wgrib2 -s sstoigrb.grib2 > sstoigrb.grib2.idx

if test "$SENDCOM" = 'YES'
then
   cp sstoianal      $COMOUT/sstoi_anal
   cp sstoigrb       $COMOUT/sstoi_grb
   cp sstoigrb.index $COMOUT/sstoi_grb.index

   cp sstoigrb.grib2 $COMOUT/sstoi_grb.grib2
   cp sstoigrb.grib2.idx $COMOUT/sstoi_grb.grib2.idx
fi

if test "$SENDDBN" = 'YES'
then
   $DBNROOT/bin/dbn_alert MODEL SSTOI_GRIB $job $COMOUT/sstoi_grb
fi

if test "$SENDDBN_GB2" = 'YES'
then
   $DBNROOT/bin/dbn_alert MODEL SSTOI_GRIB_GB2 $job $COMOUT/sstoi_grb.grib2
   $DBNROOT/bin/dbn_alert MODEL SSTOI_GRIB_GB2_WIDX $job $COMOUT/sstoi_grb.grib2.idx
fi

set +x
echo " "
echo "######################################################"
echo " Utility Script to Process WAFS and FOS SST PRODUCTS "
echo "######################################################"
echo " "
set -x

hour=00
sh $USHutil/mkfossst.sh $NET $hour

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

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