#!/bin/sh
# #########################################################################
#  Script Name: exnos_ofs_prep.sh.sms 
#  Purpose:                                                                #
#  This is the main script is launch sripts to generating forcing files    #
# Location:   ~/jobs
# Technical Contact:    Aijun Zhang         Org:  NOS/CO-OPS
#                       Phone: 301-7132890 ext. 127
#                       E-Mail: aijun.zhang@noaa.gov
#
#  Usage: 
#
# Input Parameters:
#   OFS 
#
# Modification History:
#     Degui Cao     02/18/2010   
# #########################################################################

set -xa

echo "Start ${RUN} Preparation " > $CORMSLOG
#  Control Files For Model Run
if [ -s ${HOMEnos}/fix/nos_ofs/${NET}.${RUN}.ctl ]
then
 . ${HOMEnos}/fix/nos_ofs/${NET}.${RUN}.ctl
else
  echo "${RUN} control file is not found"
  echo "please provide  ${RUN} control file of ${NET}.${RUN}.ctl in ${FIXnos}"
  msg="${RUN} control file is not found"
  ./postmsg "$jlogfile" "$msg"
  ./postmsg "$nosjlogfile" "$msg"
  echo "${RUN} control file is not found"  >> $CORMSLOG
  err_chk
fi

 echo "run the launch script to set the NOS configuration"
 . $HOMEnos/ush/nos_ofs_launch.sh $OFS prep

 echo "Generating the meteorological forcingi for nowcast"
 $HOMEnos/ush/nos_ofs_create_forcing_met.sh nowcast
 export err=$?
 if [ $err -ne 0 ]
 then
    echo "Execution of $pgm did not complete normally, critical failure!"
    echo "Execution of $pgm did not complete normally, critical failure!" >> $CORMSLOG 
    msg=" Execution of $pgm did not complete normally, critical failure!"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
    err_chk
  else
    echo "Execution of $pgm completed normally" >> $CORMSLOG
    echo "Execution of $pgm completed normally"
    msg=" Execution of $pgm completed normally"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
  fi

  echo "Generating the river forcing"
  $HOMEnos/ush/nos_ofs_create_forcing_river.sh
  export err=$?
  if [ $err -ne 0 ]
  then
    echo "Execution of $pgm did not complete normally, critical failure!"
    echo "Execution of $pgm did not complete normally, critical failure!" >> $CORMSLOG 
    msg=" Execution of $pgm did not complete normally, critical failure!"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
    err_chk
  else
    echo "Execution of $pgm completed normally"
    echo "Execution of $pgm completed normally" >> $CORMSLOG
    msg=" Execution of $pgm completed normally"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
  fi

  echo "Generating the open boundary forcing"
  $HOMEnos/ush/nos_ofs_create_forcing_obc.sh
  export err=$?
  if [ $err -ne 0 ]
  then
    echo "Execution of $pgm did not complete normally, critical failure!"
    echo "Execution of $pgm did not complete normally, critical failure!" >> $CORMSLOG   
    msg=" Execution of $pgm did not complete normally, critical failure!"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
    err_chk
  else
    echo "Execution of $pgm completed normally"
    echo "Execution of $pgm completed normally" >> $CORMSLOG
    msg=" Execution of $pgm completed normally"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
  fi

  echo "Preparing ROMS Control File for nowcast"
  $HOMEnos/ush/nos_ofs_prep_roms_ctl.sh  $OFS nowcast
  export err=$?
  if [ $err -ne 0 ]
  then
    echo "Execution of $pgm did not complete normally, critical failure!"
    echo "Execution of $pgm did not complete normally, critical failure!" >> $CORMSLOG 
    msg=" Execution of $pgm did not complete normally, critical failure!"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
    err_chk
  else
    echo "Execution of $pgm completed normally"
    echo "Execution of $pgm completed normally" >> $CORMSLOG
    msg=" Execution of $pgm completed normally"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
  fi

  echo "Preparing ROMS Control File for forecast"
  $HOMEnos/ush/nos_ofs_prep_roms_ctl.sh  $OFS forecast
  export err=$?
  if [ $err -ne 0 ]
  then
    echo "Execution of $pgm did not complete normally, critical failure!"
    echo "Execution of $pgm did not complete normally, critical failure!" >> $CORMSLOG
    msg=" Execution of $pgm did not complete normally, critical failure!"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
    err_chk
  else
    echo "Execution of $pgm completed normally"
    echo "Execution of $pgm completed normally" >> $CORMSLOG
    msg=" Execution of $pgm completed normally"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
  fi

  echo "Generating the meteorological forcing for forecst"
  $HOMEnos/ush/nos_ofs_create_forcing_met.sh  forecast
  export err=$?
  if [ $err -ne 0 ]
  then
    echo "Execution of $pgm did not complete normally, critical failure!"
    echo "Execution of $pgm did not complete normally, critical failure!" >> $CORMSLOG
    msg=" Execution of $pgm did not complete normally, critical failure!"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
    err_chk
  else
    echo "Execution of $pgm completed normally"
    echo "Execution of $pgm completed normally" >> $CORMSLOG
    msg=" Execution of $pgm completed normally"
    ./postmsg "$jlogfile" "$msg"
    ./postmsg "$nosjlogfile" "$msg"
  fi

             echo "                         "
             echo "END OF PREP SUCCESSFULLY "
             echo "                         "
########################################################
