#!/bin/sh
###############################################################################
#                                                                             #
# This script proprocesses SST fields for the ocean wave models.              #
# It is run as a child scipt by the corresponding preprocessing script.       #
#                                                                             #
# Remarks :                                                                   #
# - Shell script variables controling time, directories etc. are set in the   #
#   mother script.                                                            #
# - This script runs in the work directory designated in the mother script,   #
#   under which it creates a directory 'sst'                                  #
# - See section 0.b for variables that need to be set.                        #
#                                                                             #
#                                                            April 27, 1999   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA
  seton='-xa'
  setoff='+xa'
  set $seton

  rm -rf sst
  mkdir sst
  cd sst
  ln -s ../postmsg .

# 0.b Define directories and the search path.
#     The tested variables should be exported by the postprocessor script.

  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '!         Make sst fields        |'
  echo '+--------------------------------+'
  echo "   Model ID        : $modID"
  echo ' '
  set $seton
  postmsg "$jlogfile" "Making SST Fields."

  if [ -z "$YMDH" ] || [ -z "$date" ] || [ -z "$cycle" ] || \
     [ -z "$COMOUT" ] || [ -z "$com" ] || [ -z "$EXECwave" ] || \
     [ -z "$modID" ] || [ -z "$SENDCOM" ] || [ -z "$COMGFS" ]
  then
    set $setoff
    echo ' '
    echo '**************************************************'
    echo '*** EXPORTED VARIABLES IN preprocessor NOT SET ***'
    echo '**************************************************'
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR - EXPORTED VARIABLES IN preprocessor NOT SET."
    exit 1
    set $seton
  fi

# --------------------------------------------------------------------------- #
# 1.  Get the necessary files
# 1.a Copy the sst data file

  file=$COMGFS/gfs.$cycle.sstgrb

  if [ -f $file ]
  then
    cp $file sst.grib
  fi

  if [ ! -f sst.grib ]
  then
    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** ERROR : NO SST FILE (GFS GRIB) *** '
    echo '************************************** '
    echo ' '
    set $seton
    postmsg "$jlogfile" "NON-FATAL ERROR - NO SST FILE (GFS GRIB)."
  else
    set $setoff
    echo "   sst.grib copied ($file)."
    set $seton

# --------------------------------------------------------------------------- #
# 2.  Process the GRIB packed SST file
# 2.a Make an index file

    set $setoff
    echo '   Making GRIB index file ...'
    set $setoff
    $utilexec/wgrib sst.grib > sst.index

# 2.b Extract data from GRIB file

    echo '   Extracting data from sst.grib ...'

    grep TMP sst.index | \
           $utilexec/wgrib sst.grib -i -o sst.raw 2>&1 > wgrib.out
    err=$?

    if [ "$err" != '0' ]
    then
      cat wgrib.out
      set $setoff
      echo ' '
      echo '************************* '
      echo '*** ERROR IN wgrib *** '
      echo '************************* '
      echo ' '
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR IN wgrib."
    else

# 2.c Convert to wave model grid

      set $setoff
      echo '   Convert to wave model grid ...'
      set $seton

      ln -s ../sst.ww3 .
      $EXECwave/wave_${modID}sst
      err=$?

      if [ "$err" != '0' ]
      then
        set $setoff
        echo ' '
        echo '***************************** '
        echo "*** ERROR IN wave_${modID}sst *** "
        echo '***************************** '
        echo ' '
        set $seton
        postmsg "$jlogfile" "NON-FATAL ERROR IN wave_${modID}sst"
      fi
    fi
  fi

# 2.d Clean up

  rm -f sst.grib
  rm -f sst.index
  rm -f sst.raw
  rm -f sst.ww3
  rm -f wgrib.out

# --------------------------------------------------------------------------- #
# 3.  Error fallback : copy file of previous cycle if necessary
# 3.a Get old file if none there

  if [ ! -f ../sst.ww3 ]
  then
    ymdh="`$utilexec/ndate -12 $YMDH`"
    pdate=`echo $ymdh | cut -c1-8`
    pcycle=t`echo $ymdh | cut -c9-10`z
    file=$com/wave.$pdate/$modID.$pcycle.sst

    if [ -f $file ]
    then
      cp $file ../sst.ww3
      set $setoff
      echo ' '
      echo '   SST data taken from previous run.'
      set $seton
      echo "$modID prep $date $cycle : SST taken from previous run" >> $wavelog
    fi
  fi

# 3.b exit with error code if still none there

  if [ ! -f ../sst.ww3 ]
  then
    set $setoff
    echo ' '
    echo '*************************************************** '
    echo '*** FATAL ERROR : NO SST FILE CREATED OR COPIED *** '
    echo '*************************************************** '
    echo ' '
    set $seton
    postmsg "$jlogfile" "FATAL ERROR - NO SST FILE CREATED OR COPIED."
    exit 2
  fi

# --------------------------------------------------------------------------- #
# 4.  Save the sst file for fallback option in section 3.

  if [ "$SENDCOM" = 'YES' ]
  then
    set $setoff
    echo ' '
    echo "   Saving sst.ww3 as $COMOUT/$modID.$cycle.sst."
    set $setoff
    cp ../sst.ww3 $COMOUT/$modID.$cycle.sst
  fi 

# --------------------------------------------------------------------------- #
# 5.  Clean up the directory

  set $setoff
  echo "   Removing work directory after success."
  set $seton

  cd ..
  rm -rf sst

  set $setoff
  echo ' '
  echo 'End of wavesst.sh at'
  date

# End of wavesst.sh --------------------------------------------------------- #
