#/bin/sh
###############################################################################
#                                                                             #
# This script is the postprocessor for the ESTOFS that runs under the ADCIR   #
# model. It sets some shell script variables for export to child scripts      #
# and copies some generally used files to the work directory.                 #
# After this the actual preprocessing is performed by the following scripts:  #
#                                                                             #
#  estofs_harmonic_tidal_prediction.sh : Calculate harmonic tidal prediction  #
#  estofs_subtidal_water_level.sh      : Compute sub-tidal water level        #
#  estofs_grib2.sh                     : Create grib2 ndfd output             #
#  estofs_ndfd.sh                      : Interpolate into CONUS and Puerto    #
#                                        Rico grid                            #
# Remarks :                                                                   #
#                                                                             #
#                                                                 Dec, 2011   #
#                                                                             #
###############################################################################
# Start of exestofs_post.sh.sms script -------------------------------------- #
# 0.  Preparation
# 0.a Basic modes of operation

  cd $DATA

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

  msg="$job has begun on `hostname`"
  ./postmsg "$jlogfile" "$msg"

  wndh=3
  nowh=6
  lsth=180

# --------------------------------------------------------------------------- #
# 1.  Set times
# 1.a Set all necessary times
#     The ending time of the run always is the $lsth hour forecast. The starting
#     time depends on availablility of restart files, and is obtained with
#     estofs_restart.sh
#
#     Make sure nback is set identically in the forecast script !!!
#     nback is the number of cycles (6-hour cycles) to look back.
#
#     YMDH     :  current time cycle in yyyymmddhh format
#     time_beg :  begin time of run (normally, -6 hour hindcast or the most recent restart time)
#     time_rst :  nowcast time (restart file time, normally 3 hours increment)
#     time_now :  current time
#     time_end :  ending time of run ($lsth hour forecast)

  export date=$PDY
  export YMDH=${PDY}${cyc}
  export nback=20

  time_beg=`$NDATE -$nowh $YMDH`
  time_now=$YMDH
  time_end=`$NDATE $lsth $YMDH`

# --------------------------------------------------------------------------- #
# 2.  Get output files from ${COMIN}

  hdir=${COMIN}
  hfile=${RUN}.${modID}.${cycle}.fields.cwl.nc

  if [ -d $hdir ]
  then
    if [ -f $hdir/$hfile ]
    then
      cp $hdir/$hfile cwl.fort.63.nc
    fi
  fi

  if [ ! -f cwl.fort.63.nc ]
  then
    echo "cwl.fort.63.nc file did not existed"
  else
    echo "cwl.fort.63.nc file existed"
  fi
  
  idir=${COMIN}
  ifile=${RUN}.${modID}.${cycle}.fields.htp.nc

  if [ -d $idir ]
  then
    if [ -f $idir/$ifile ]
    then
      cp $idir/$ifile htp.fort.63.nc
    fi
  fi

  if [ ! -f htp.fort.63.nc ]
  then
    echo "htp.fort.63.nc file did not existed"
  else
    echo "htp.fort.63.nc file existed"
  fi

# --------------------------------------------------------------------------- #
# 3.  Execute estofs_subtidal_water_level.sh

  cp $FIXestofs/${RUN}_${modID}_grid ${RUN}.${modID}.fort.14
  $USHestofs/estofs_subtidal_water_level.sh 

# --------------------------------------------------------------------------- #
# 4.  Execute estofs_grib2.sh for CONUS grid

  $USHestofs/estofs_grib2.sh "conus" 

# --------------------------------------------------------------------------- #
# 5.  Execute estofs_grib2.sh for Puerto Rico grid

  $USHestofs/estofs_grib2.sh "puertori" 

# --------------------------------------------------------------------------- #
# 6.  Send output from estofs_grib2.sh to COM if requested

  if [ $SENDCOM = YES ]
  then
    for domain in conus puertori; do
      for fhr in `seq 0 180`; do
         if [ $fhr -lt 10 ]; then
            fhr=f00${fhr}
         elif [ $fhr -lt 100 ]; then
            fhr=f0${fhr}
         else
           fhr=f${fhr}
         fi
         cp ${RUN}.${modID}.${cycle}.${domain}.${fhr}.grib2 $COMOUT/

#####################################
# Alert estofs grib2 files
#####################################
         if [ $SENDDBN_GB2 = YES ]
         then
            $DBNROOT/bin/dbn_alert MODEL ESTOFS_GB2 $job $COMOUT/${RUN}.${modID}.${cycle}.${domain}.${fhr}.grib2
         fi
      done
    done
  fi

#######################
# Release GEMPAK jobs
#######################
$SMSBIN/setev grib2_ready

# --------------------------------------------------------------------------- #
# 7.  Execute estofs_ndfd.sh for CONUS grid

  $USHestofs/estofs_ndfd.sh "conus"

# --------------------------------------------------------------------------- #
# 8.  Execute estofs_ndfd.sh for Puerto Rico grid

  $USHestofs/estofs_ndfd.sh "puertori"

# --------------------------------------------------------------------------- #
# 9.  Process ESTOFS products with WMO headers

  pgm=tocgrib2
  export pgm;. prep_step
  startmsg

  for grid in conus puertori
  do
     for type in cwl htp swl 
     do   
        export XLFUNIT_11=${RUN}.${modID}.${cycle}.${grid}.${type}.grib2
        export XLFUNIT_31=" "
        export XLFUNIT_51=grib2_${RUN}.${modID}.${cycle}.${grid}.${type}

        ${EXECutil}/tocgrib2 <  $PARMutil/grib2_estofs_atl_${grid}_${type} >> $pgmout 2> errfile

        err=$?;export err ;err_chk
        echo " error from tocgrib2=",$err

     done
  done

# --------------------------------------------------------------------------- #
# 10. Save and clean up files
# 10.a Send files to com and pcom if requested

  if [ $SENDCOM = YES ]
  then
    cp ${RUN}.${modID}.${cycle}.conus.cwl.grib2               $COMOUT/.
    cp ${RUN}.${modID}.${cycle}.conus.htp.grib2               $COMOUT/.
    cp ${RUN}.${modID}.${cycle}.conus.swl.grib2               $COMOUT/.
    cp ${RUN}.${modID}.${cycle}.puertori.cwl.grib2               $COMOUT/.
    cp ${RUN}.${modID}.${cycle}.puertori.htp.grib2               $COMOUT/.
    cp ${RUN}.${modID}.${cycle}.puertori.swl.grib2               $COMOUT/.

  ############################
  # Send files to pcom
  ############################

# 10.b Send files to pcom if requested
#
    cp grib2_${RUN}.${modID}.${cycle}.conus.cwl               $pcom/.
    cp grib2_${RUN}.${modID}.${cycle}.conus.htp               $pcom/.
    cp grib2_${RUN}.${modID}.${cycle}.conus.swl               $pcom/.
    cp grib2_${RUN}.${modID}.${cycle}.puertori.cwl            $pcom/.
    cp grib2_${RUN}.${modID}.${cycle}.puertori.htp            $pcom/.
    cp grib2_${RUN}.${modID}.${cycle}.puertori.swl            $pcom/.
  fi
  
  ######################################
  # Distribute estofs products to AWIPS
  #####################################

# 10.c  Distribute ESTOFS products to AWIPS
#
  if [ $SENDDBN_PCOM = "YES" ] ; then
     $DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2_${RUN}.${modID}.${cycle}.conus.cwl
     $DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2_${RUN}.${modID}.${cycle}.conus.htp
     $DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2_${RUN}.${modID}.${cycle}.conus.swl
     $DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2_${RUN}.${modID}.${cycle}.puertori.cwl
     $DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2_${RUN}.${modID}.${cycle}.puertori.htp
     $DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2_${RUN}.${modID}.${cycle}.puertori.swl

  fi

  echo "ESTOFS postprocessor script for $modID completed normally"
  msg="ESTOFS postprocessor script for $modID completed normally"
  ./postmsg "$jlogfile" "$msg"

# End of exestofs_post.sh.sms script ---------------------------------------- #
