#!/bin/sh

# ############################################################################
#  Script Name:  exnos_ofs_nowcast_forecast.sh.sms 
#  Purpose:                                                                   #
#  This is the main script is launch both nowcast and forecast simulations    #
# 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

#  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"
  msg="please provide  ${RUN} control file of ${NET}.${RUN}.ctl in ${FIXnos}"
  ./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 nowcast 

#####     Run nowcast simulation
 runtype='nowcast'

echo "     " >> $jlogfile 
echo "     " >> $nosjlogfile 
echo " Start $runtype " >> $jlogfile
echo " Start $runtype " >> $nosjlogfile
echo "Making $runtype at : `date`" >> $jlogfile
echo "Making $runtype at : `date`" >> $nosjlogfile
echo "Making $runtype at : `date`"

$HOMEnos/ush/nos_ofs_nowcast_forecast.sh $OFS $runtype 
export err=$?
if [ $err -ne 0 ]
then
  echo "nos_ofs_nowcast_forecast.sh $OFS $runtype did not complete normally"
  msg="nos_ofs_nowcast_forecast.sh $OFS $runtype did not complete normally"
  ./postmsg "$jlogfile" "$msg"
  ./postmsg "$nosjlogfile" "$msg"
  err_chk
else
  echo "nos_ofs_nowcast_forecast.sh $OFS $runtype completed normally"
  msg="nos_ofs_nowcast_forecast.sh $OFS $runtype  completed normally"
  ./postmsg "$jlogfile" "$msg"
  ./postmsg "$nosjlogfile" "$msg"
fi

###  archive nowcast outputs
 $HOMEnos/ush/nos_ofs_archive.sh $runtype
 export err=$?
 if [ $err -ne 0 ]
 then
   echo "nos_ofs_archive.sh $runtype did not complete normally"
   msg="nos_ofs_archive.sh $runtype did not complete normally"
   ./postmsg "$jlogfile" "$msg"
   ./postmsg "$nosjlogfile" "$msg"
   err_chk
 else
   echo "nos_ofs_archive.sh $runtype completed normally"
   msg="nos_ofs_archive.sh  $runtype completed normally"
   ./postmsg "$jlogfile" "$msg"
   ./postmsg "$nosjlogfile" "$msg"
 fi
#  # for development copy outputs to CO-OPS via sftp push
if [ $envir = "dev" ]; then
   $USHnos/nos_ofs_sftp.sh $runtype
fi

 echo "end of $runtype"

#####    Run forecast simulation
runtype='forecast'

echo "     " >> $jlogfile 
echo "     " >> $nosjlogfile 
echo " Start nos_ofs_nowcast_forecast.sh $runtype at : `date`" >> $jlogfile
echo " Start nos_ofs_nowcast_forecast.sh $runtype at : `date`" >> $nosjlogfile
echo "Running nos_ofs_nowcast_forecast.sh $runtype at : `date`" >> $jlogfile
echo "Running nos_ofs_nowcast_forecast.sh $runtype at : `date`" >> $nosjlogfile
echo " Start nos_ofs_nowcast_forecast.sh $runtype at : `date`" 

$HOMEnos/ush/nos_ofs_nowcast_forecast.sh $OFS $runtype 
export err=$?
if [ $err -ne 0 ]
then
  echo "nos_ofs_nowcast_forecast.sh $OFS $runtype did not complete normally"
  msg="nos_ofs_nowcast_forecast.sh $OFS $runtype did not complete normally"
  ./postmsg "$jlogfile" "$msg"
  ./postmsg "$nosjlogfile" "$msg"
  err_chk
else
  echo "nos_ofs_nowcast_forecast.sh $OFS $runtype completed normally"
  msg="nos_ofs_nowcast_forecast.sh $OFS $runtype  completed normally"
  ./postmsg "$jlogfile" "$msg"
  ./postmsg "$nosjlogfile" "$msg"
fi

echo "end of nos_ofs_nowcast_forecast.sh $runtype"

##  archive forecast outputs 
$HOMEnos/ush/nos_ofs_archive.sh $runtype 
 export err=$?
 if [ $err -ne 0 ]
 then
   echo "nos_ofs_archive.sh $OFS $runtype did not complete normally"
   msg="nos_ofs_archive.sh $OFS $runtype did not complete normally"
   ./postmsg "$jlogfile" "$msg"
   ./postmsg "$nosjlogfile" "$msg"
   err_chk
 else
   echo "nos_ofs_archive.sh $OFS $runtype completed normally"
   msg="nos_ofs_archive.sh $OFS $runtype  completed normally"
   ./postmsg "$jlogfile" "$msg"
   ./postmsg "$nosjlogfile" "$msg"
 fi
#  # for development copy outputs to CO-OPS via sftp push
if [ $envir = "dev" ]; then
   $USHnos/nos_ofs_sftp.sh $runtype
fi

if [ $SENDDBN = YES ]; then
  $DBNROOT/bin/dbn_alert MODEL $DBN_ALERT_TYPE_TEXT $job $nosjlogfile
fi

          echo "                                    "
          echo "END OF NOWCAST/FORECAST SUCCESSFULLY"
          echo "                                    "
###############################################################
