#!/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
#SH if [ -s ${FIXofs}/${NET}.${RUN}.ctl ]
#SH then
#SH  . ${FIXofs}/${NET}.${RUN}.ctl
   
if [ -s ${CTL_FILE} ]
then
  . ${CTL_FILE}
else
  echo "${RUN} control file is not found"
  echo "please provide  ${RUN} control file of ${NET}.${RUN}.ctl in ${FIXofs}"
  msg="${RUN} control file is not found"
  ./postmsg "$jlogfile" "$msg"
  ./postmsg "$nosjlogfile" "$msg"
  msg="please provide  ${RUN} control file of ${NET}.${RUN}.ctl in ${FIXofs}"
  ./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"
. $USHnos/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`"

$USHnos/nos_ofs_nowcast_forecast.sh $runtype 
export err=$?

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

###  archive nowcast outputs
 $USHnos/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

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

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

echo "end of nos_ofs_nowcast_forecast.sh $runtype"

##  archive forecast outputs 
$USHnos/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
 if [ $envir = "dev" ]; then
#  # for development copy outputs to CO-OPS via sftp push 
   $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 "                                    "
###############################################################
