#!/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
#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"
  echo "${RUN} control file is not found"  >> $CORMSLOG
  err_chk
fi

export pgm="$USHnos/nos_ofs_launch.sh $OFS prep"
echo "run the launch script to set the NOS configuration"
. $USHnos/nos_ofs_launch.sh $OFS prep
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

## For SELFE, reset base_date=time_hotstart  
#if [ ${OCEAN_MODEL} == "SELFE" -o ${OCEAN_MODEL} == "selfe" ]
#then
#     export BASE_DATE=$time_hotstart
#fi

echo "Generating the meteorological forcing for nowcast"

export pgm=nos_ofs_create_forcing_met.sh
. ./prep_step
$USHnos/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"
export pgm=nos_ofs_create_forcing_river.sh
. ./prep_step
$USHnos/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"
export pgm=nos_ofs_create_forcing_obc.sh
. ./prep_step
$USHnos/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 "Generating the meteorological forcing for forecst"
export pgm="nos_ofs_create_forcing_met.sh  forecast"
$USHnos/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

if [ ${OCEAN_MODEL} == "FVCOM" -o ${OCEAN_MODEL} == "fvcom" ]
then

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

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

elif [ ${OCEAN_MODEL} == "ROMS" -o ${OCEAN_MODEL} == "roms" ]
then
  echo "Preparing ROMS Control File for nowcast"
  export pgm=nos_ofs_prep_roms_ctl.sh
  . ./prep_step
  $USHnos/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"
  export pgm=nos_ofs_prep_roms_ctl.sh
  . ./prep_step
  $USHnos/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
elif [ ${OCEAN_MODEL} == "SELFE" -o ${OCEAN_MODEL} == "selfe" ]
then

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

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

fi
cp -p $jlogfile $COMOUT
	   echo "			  "
	   echo "END OF PREP SUCCESSFULLY "
	   echo "			  "

#exit
