#!/bin/sh
set +xa
echo "---------------------------------------------------------------"
echo "Script - SSTOIQD ANALYSIS processing (NCDC quarter-degree SST analysis)"
echo "---------------------------------------------------------------"
echo "History: October 2010 - Implement SSTOIQD Analysis"
echo "---------------------------------------------------------------"
set -xa

###########################################################
# Processing/Flow of script
# 1) Copy this job's input files to the working directory
# 2) Run step to generate analysis
# 3) Save sst analysis to $COMOUT
# 4) Run optional step to generate grib1 file and save to $COMOUT
# 5) Run optional step to generate grib2 file and save to $COMOUT
###########################################################

#############################################################
# 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"

AMSR_name=${AMSR_name:-amsre}
AVHRR_sats=${AVHRR_sats:-"noaa19 metop2"}

CREATE_GRIB1=${CREATE_GRIB1:-YES}
CREATE_GRIB2=${CREATE_GRIB2:-YES}

#Executables
OISSTX=${OISSTX:-$EXECsstoiqd/sstoiqd_oisst4}     # executable to compute OI SST analysis
GRIB1X=${GRIB1X:-$EXECsstoiqd/sstoiqd_gribsst}    # executable to convert SST output to grib1
GRIB2X=${GRIB2X:-$EXECsstoiqd/sstoiqd_grib2sst}   # executable to convert SST output to grib2


#############################################################
# cp input files to working directory
#############################################################

cp $COMIN/sstoiqd.guess.grb .
cp $COMIN/sstoiqd.buoy.sobs.$PDYm1.ieee .
cp $COMIN/sstoiqd.ice.sobs.$PDYm1.ieee .
cp $COMIN/sstoiqd.ship.cobs.$PDYm1.ieee .

for SATname in $AVHRR_sats $AMSR_name; do
  for typ in day nte; do
    sattyp=$SATname$typ
    cp $COMIN/sstoiqd.$sattyp.cobs.$PDYm1.ieee .
    cp $COMIN/sstoiqd.$sattyp.awts.$PDYm1.ieee .
  done
done

FMODE=${FMODE:-$FIXsstoiqd/sstoiqd_eot6.damp-zero.ev130.ano.dat}   # EOT modes file (filled)
MASK2D=${MASK2D:-$FIXsstoiqd/sstoiqd_lstags.twodeg.dat}      # EOT Land/SEA FILE
CLIM2D=${CLIM2D:-$FIXsstoiqd/sstoiqd_clim.71.00.gdat}        # EOT Climate FILE
VARMD=${VARMD:-$FIXsstoiqd/sstoiqd_var-mode}    # Variance of each modes
#
RESID=${RESID:-$FIXsstoiqd/sstoiqd_residual-stat-v2}
CORXY=${CORXY:-$FIXsstoiqd/sstoiqd_cor4sm-stat-v2}
FGVAR=${FGVAR:-$FIXsstoiqd/sstoiqd_path-incr-var-stat-v2}
RFAC=${RFAC:-$FIXsstoiqd/sstoiqd_error-cor-stat-v2}
MASK=${MASK:-$FIXsstoiqd/sstoiqd_quarter-mask-extend}
CLIM=${CLIM:-$FIXsstoiqd/sstoiqd_oiclm4.mon}

#
rmax=400.       # maximum x or y distance (km) over which obs are used.
nmax=22         # maximum number of obs are used. Must be <201
ifil=1          # number of times 1-2-1 smoothing is done; <1 no smoothing
rej=5.          # bias REJECT LIMIT for ABSOLUTE super obs increment
iwarn=20        # Percentage of satellite data: if lower warning message printed


FGSST=${FGSST:-sstoiqd.guess.grb}
OISST=${OISST:-sstoiqd.sst.ieee}
#
BUOYNSR=${BUOYNSR:-$FIXsstoiqd/sstoiqd_buoy4sm-nsr-stat-v2}
SHIPNSR=${SHIPNSR:-$FIXsstoiqd/sstoiqd_ship4sm-nsr-stat-v2}
SATNSRavhrrday=${SATNSRavhrrday:-$FIXsstoiqd/sstoiqd_day-path4sm-nsr-stat-v2}
SATNSRavhrrnte=${SATNSRavhrrnte:-$FIXsstoiqd/sstoiqd_nte-path4sm-nsr-stat-v2}
SATNSRamsrday=${SATNSRamsrday:-$FIXsstoiqd/sstoiqd_day-amsr4sm-nsr-stat-v2}
SATNSRamsrnte=${SATNSRamsrnte:-$FIXsstoiqd/sstoiqd_nte-amsr4sm-nsr-stat-v2}
ICENSR=${ICENSR:-$FIXsstoiqd/sstoiqd_cice4sm-nsr-stat-v2}
#

BUOYDAT=${BUOYDAT:-sstoiqd.buoy.sobs.$PDYm1.ieee}
SHIPDAT=${SHIPDAT:-sstoiqd.ship.cobs.$PDYm1.ieee}
ICEDAT=${ICEDAT:-sstoiqd.ice.sobs.$PDYm1.ieee}

# start analysis description title 
title="Daily-OI-V2, Interim, Data ("   # to be continued below

if [ -s $SHIPDAT ]; then
  filesize=`ls -l $SHIPDAT | awk '{print $5}' `
  [ $filesize -gt 100 ] && title="${title}ship"
fi
if [ -s $BUOYDAT ]; then
  filesize=`ls -l $BUOYDAT | awk '{print $5}' `
  [ $filesize -gt 100 ] && title="$title, buoy"
fi

> sat.list  # create empty file sat.list
> awts.list  # create empty file awts.list

nbias=0         # number of files to read of EOT bias estimates
nsat=0  #  initialize counter for independent sat instruments (with different bias characteristics)
for SATname in $AVHRR_sats; do
 for typ in day nte; do
   sattyp=$SATname$typ
   ANALwts=sstoiqd.$sattyp.awts.$PDYm1.ieee
   SAT=sstoiqd.$sattyp.cobs.$PDYm1.ieee
   eval SATNSR=\$SATNSRavhrr$typ
   if [ -s $SAT -a -s $ANALwts ]; then
      echo $ANALwts >> awts.list
      echo $SATNSR >> sat.list
      echo $SAT >> sat.list
      eval used_$SATname=YES
      avhrr_bias=YES
      nbias=`expr $nbias + 1`
    fi
  done
  eval chk=\$used_$SATname
  if [ "$chk" = YES ]; then
      title="$title, $SATname"
  fi
done
if [ "$avhrr_bias" = 'YES' ];then 
  nsat=`expr $nsat + 1`
fi

for SATname in $AMSR_name; do
 for typ in day nte; do
   sattyp=$SATname$typ
   ANALwts=sstoiqd.$sattyp.awts.$PDYm1.ieee
   SAT=sstoiqd.$sattyp.cobs.$PDYm1.ieee
   eval SATNSR=\$SATNSRamsr$typ
   if [ -s $SAT -a -s $ANALwts ]; then
      echo $ANALwts >> awts.list
      echo $SATNSR >> sat.list
      echo $SAT >> sat.list
      amsr_bias=YES
      nbias=`expr $nbias + 1`
    fi
  done
done
if [ "$amsr_bias" = 'YES' ]; then
 nsat=`expr $nsat + 1`
  title="$title, $AMSR_name"
fi
  
if [ -s $ICEDAT ]; then
  filesize=`ls -l $ICEDAT | awk '{print $5}' `
  [ $filesize -gt 100 ] && title="$title, NCEP-ice"
fi
title="${title})"


#############################################################
# generate SST analysis
#############################################################
#
#  1st OIsst4 input record is the title
#  2nd OIsst4 input record is the date
#  The 3rd OISST4 input record is 7 parameters:
#     $rmax $nmax $ifil $rej $nbias $nsat $iwarn
#  All remaining records are input file names which must be in order
#  NOTE: OISST4 can have a large number of input file data sets
#  Next nbias files follow: nbias may be 0
#     Each of these files is from the EOT bias correction with EOT bias error
#  Then in file namepairs until end of data:
#    Odd file name:  "SST noise to signal ratio (std dev)"
#    Even file name: "SST data file"
#

pgm=sstoiqd_oisst4
. prep_step

set +x
echo "  XLFUNIT_15  used by multiple files (see awts.list)"
echo "  XLFUNIT_11  used by multiple files (see nsr records in sat.list)"
echo "  XLFUNIT_12  used by multiple files (see cob records in sat.list)"
set -x

export XLFUNIT_31="$CORXY"
export XLFUNIT_38="$FGVAR"
export XLFUNIT_39="$RFAC"
export XLFUNIT_22="$FGSST"
export XLFUNIT_33="$MASK"
export XLFUNIT_34="$CLIM"
export XLFUNIT_30="$RESID"
export XLFUNIT_80="$OISST"

####  set up oiparm  (done in segments. content depends on satellite data used and available) ####
cat <<inxEOF> oiparm
$title
$PDYm1
$rmax $nmax $ifil $rej $nbias $nsat  $iwarn
inxEOF

cat awts.list >> oiparm

cat <<inxEOF>> oiparm
$BUOYNSR
$BUOYDAT
$SHIPNSR
$SHIPDAT
inxEOF

cat sat.list >> oiparm

cat <<inxEOF>> oiparm
$ICENSR
$ICEDAT
inxEOF
####  end set up oiparm  ####


startmsg
$OISSTX <oiparm >> $pgmout 2>errfile
export err=$?;err_chk
msg="Done computing sst analysis for $PDYm1"
postmsg "$jlogfile" "$msg"

if [ "$SENDCOM" = 'YES' ]; then
   cp $OISST $COMOUT/sstoiqd.sst.ieee
fi


#############################################################
# Create grib1 file  (optional)
#############################################################
if [ "$CREATE_GRIB1" = 'YES' ]; then
### convert to grib1
pgm=sstoiqd_gribsst
. prep_step

export XLFUNIT_20="$OISST"
export XLFUNIT_50=sstoiqd.sst.grb
startmsg
$GRIB1X >> $pgmout 2>errfile
export err=$?;err_chk
msg="Done converting sst to grib1"
postmsg "$jlogfile" "$msg"

$EXECutil/grbindex sstoiqd.sst.grb sstoiqd.sst.grb.index

  if [ "$SENDCOM" = 'YES' ]; then
     cp sstoiqd.sst.grb       $COMOUT/sstoiqd.sst.grb
     cp sstoiqd.sst.grb.index $COMOUT/sstoiqd.sst.grb.index
  fi

fi   # end CREATE_GRIB1


#############################################################
# Create grib2 file  (optional)
#############################################################
if [ "$CREATE_GRIB2" = 'YES' ]; then
pgm=sstoiqd_grib2sst
. prep_step

export XLFUNIT_20="$OISST"
export XLFUNIT_50=sstoiqd.sst.grib2
startmsg
$GRIB2X >> $pgmout 2>errfile
export err=$?;err_chk
msg="Done converting sst to grib2"
postmsg "$jlogfile" "$msg"

$EXECutil/wgrib2 -s sstoiqd.sst.grib2 > sstoiqd.sst.grib2.idx

if [ "$SENDCOM" = 'YES' ]; then
  cp sstoiqd.sst.grib2 $COMOUT/sstoiqd.sst.grib2
  cp sstoiqd.sst.grib2.idx $COMOUT/sstoiqd.sst.grib2.idx
fi

fi   # end CREATE_GRIB2


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

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

