#!/bin/sh
#  Script Name:  exnos_ofs_nowcast_forecast.sms.prod
#  Purpose:                                                                   #
#  This script is to make nowcast or forecast simulation after running:       #
#  nos_ofs_create_forcing_river.sh, nos_ofs_create_forcing_obc.sh             #
#  nos_ofs_create_forcing_met_nowcast.sh nos_ofs_create_forcing_met_forecast.sh
#  nos_ofs_reformat_roms_ctl_nowcast.sh nos_ofs_reformat_roms_ctl_forecast.sh 
#                                                                             #
#  Child scripts :                                                            #
#                                                                             #
#  The utililty script used:                                                  #
#                                                                             #
# Remarks :                                                                   #
# - For non-fatal errors output is written to the *.log file.                 #
# - NOTE TO NCO: this script is I/O limited. To get the script to run in      #
#                approximately 1:15, 1 node  and 12 processors are optimal    #
#                                                                             #
# Language:  C shell script
# Nowcast  
# Input:
#     nos.${RUN}.river.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.obc.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.met.nowcast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.init.nowcast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.hflux.nowcast.$yyyy$mm$dd.t${HH}z.nc
#     ${RUN}_roms_nowcast.in
#     nos.${RUN}.roms.tides.nc
# Output:
#     nos.${RUN}.stations.nowcast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.fields.nowcast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.fields.forecast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.rst.nowcast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.roms.nowcast.$yyyy$mm$dd.t${cyc}z.log
# Forecast  
# Input:
#     nos.${RUN}.river.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.obc.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.met.forecast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.rst.nowcast.$yyyy$mm$dd.t${cyc}z.nc
#     ${RUN}_roms_forecast.in
#     nos.${RUN}.roms.tides.nc
# Output:
#     nos.${RUN}.stations.forecast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.fields.forecast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.rst.forecast.$yyyy$mm$dd.t${cyc}z.nc
#     nos.${RUN}.roms.forecast.$yyyy$mm$dd.t${cyc}z.log
#
# Technical Contact:    Degui Cao           Org:  NOS/CO-OPS                  #
#                       Phone: 301-7132890 ext. 212                           #
#                       E-Mail: degui.cao@noaa.gov                            #
# Modification History:
#     Degui Cao     10/28/2010   
#        degui.cao@noaa.gov          301-7132890x212   
# 
#                                                                             #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

cd $DATA

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

set $setoff
echo ' '
echo '  		    ****************************************'
echo '  		    *** NOS OFS  NOWCAST/FORECAST SCRIPT ***'
echo '  		    ****************************************'
echo ' '
echo "Starting nos_ofs_nowcast_forecast.sh at : `date`"
set $seton

export MP_PGMMODEL=mpmd
export MP_CMDFILE=cmdfile

RUN=$1 
RUNTYPE=$2 

# --------------------------------------------------------------------------- #
# 1.  Get files that are used by most (child) scripts

echo "Preparing input files for ${RUN} $RUNTYPE "
echo '-----------------------'
set $seton

if [ $RUNTYPE == "NOWCAST" -o $RUNTYPE == "nowcast" ]
then

# 1.a RIVER FORCING FILE 

    if [ -s $DATA/$RIVER_FORCING_FILE ]
    then
      echo " $DATA/$RIVER_FORCING_FILE existed "
    elif [ -s $COMOUT/$RIVER_FORCING_FILE ]
    then
      ln -s $COMOUT/$RIVER_FORCING_FILE $RIVER_FORCING_FILE
    else  
      msg="ABNORMAL EXIT: NO RIVER FORCING FILE $RIVER_FORCING_FILE"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************** '
      echo '*** FATAL ERROR : NO $RIVER_FORCING_FILE   *** '
      echo '********************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi

# 1.b OBC FORCING FILE 

    if [ -f $DATA/$OBC_FORCING_FILE ]
    then
      echo "   $DATA/$OBC_FORCING_FILE existed "
    elif [ -s $COMOUT/$OBC_FORCING_FILE ]
    then
      ln -s $COMOUT/$OBC_FORCING_FILE $OBC_FORCING_FILE
    else
      msg="ABNORMAL EXIT: NO OBC FORCING FILE $OBC_FORCING_FILE"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************** '
      echo '*** FATAL ERROR : NO $OBC_FORCING_FILE   *** '
      echo '********************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi

# 1.c Meteorological Forcing For Nowcast 

    if [ -s $DATA/$MET_NETCDF_1_NOWCAST ]
    then
      echo "   $DATA/$MET_NETCDF_1_NOWCAST existed "
    elif [ -s $COMOUT/$MET_NETCDF_1_NOWCAST ]
    then
      ln -s $COMOUT/$MET_NETCDF_1_NOWCAST $MET_NETCDF_1_NOWCAST
    else
      msg="ABNORMAL EXIT: NO Meteorological Forcing For Nowcast $MET_NETCDF_1_NOWCAST"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************************** '
      echo '*** FATAL ERROR : NO $Meteorological Forcing For Nowcast   *** '
      echo '************************************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi
   
# 1.d Initial forcing For Nowcast
    if [ -f $DATA/$INI_FILE_ROMS_NOWCAST ]
    then
      echo "   $COMOUT/$INI_FILE_ROMS_NOWCAST existed "
    elif [ -s $COMOUT/$INI_FILE_ROMS_NOWCAST ]
    then
      ln -s $COMOUT/$INI_FILE_ROMS_NOWCAST $INI_FILE_ROMS_NOWCAST
    else
      msg="ABNORMAL EXIT: NO intial forcing file For Nowcast $INI_FILE_ROMS_NOWCAST"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************************** '
      echo '*** FATAL ERROR : NO intial forcing file For Nowcast   *** '
      echo '************************************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi
 

# 1.e HFlux For Nowcast
    if [ -f $DATA/$MET_NETCDF_2_NOWCAST ]
    then
      echo "   $DATA/$MET_NETCDF_2_NOWCAST existed "
    elif [ -s $COMOUT/$MET_NETCDF_2_NOWCAST ]
    then
      ln -s $COMOUT/$MET_NETCDF_2_NOWCAST $MET_NETCDF_2_NOWCAST
    fi
  

# 1.f Nowcast control file
    if [ -f $DATA/${RUN}_roms_nowcast.in ]
    then
      echo "   $DATA/${RUN}_roms_nowcast.in existed "
    elif [ -s $COMOUT/${RUN}_roms_nowcast.in ]
    then
      ln -s $COMOUT/${RUN}_roms_nowcast.in ${RUN}_roms_nowcast.in
    else
       msg="ABNORMAL EXIT: ROMS runtime input file for nowcast is not found"
       ./postmsg "$jlogfile" "$msg"
       ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************************** '
      echo '*** FATAL ERROR :    *** ROMS runtime input file for nowcast is not found'
      echo '************************************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi

#1.i Tide data 
    if [ -f $DATA/$HC_FILE_OFS ]
    then
      echo "   $DATA/$HC_FILE_OFS linked "
    elif [ -s $COMOUT/$HC_FILE_OFS ]
    then
      ln -s $COMOUT/$HC_FILE_OFS $HC_FILE_OFS
    fi
# --------------------------------------------------------------------------- #
# 2   Execute ocean model of ROMS; where ${RUN}_roms_nowcast.in is created by nos_ofs_reformat_roms_ctl.sh

    $EXECnos/${RUN}_roms_mpi ${RUN}_roms_nowcast.in >> ${ROMS_OUT_NOWCAST}
    export err=$?
    if [ $err -ne 0 ]
    then
      echo "Running ocean model ${RUN}_roms_mpi for $RUNTYPE did not complete normally"
      msg="Running ocean model ${RUN}_roms_mpi for $RUNTYPE did not complete normally"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      err_chk
    else
      echo "Running ocean model ${RUN}_roms_mpi for $RUNTYPE completed normally"
      msg="Running ocean model ${RUN}_roms_mpi for $RUNTYPE completed normally"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
    fi

    rm -f corms.now corms.fcst 
    if [ -s ${ROMS_OUT_NOWCAST} ]
    then
      grep "ROMS/TOMS - Blows up" ${ROMS_OUT_NOWCAST} > corms.now
      grep "Blowing-up" ${ROMS_OUT_NOWCAST} >> corms.now
      grep "Abnormal termination: BLOWUP" ${ROMS_OUT_NOWCAST} >> corms.now
    fi
    if [ -s  corms.now ]
    then
       echo "${RUN} NOWCAST RUN OF CYCLE t${HH}z ON $YYYY$MM$DD FAILED 00"  >> $CORMSLOG 
       echo "NOWCAST_RUN DONE 0"  >> $CORMSLOG
       export err=99; err_chk
    else
       echo "${RUN} NOWCAST RUN OF CYCLE t${HH}z ON $YYYY$MM$DD COMPLETED SUCCESSFULLY 100" >> $CORMSLOG
       echo "NOWCAST_RUN DONE 100"  >> $CORMSLOG
# save new nowcast restart file into archive directory for next cycle run
       if [ -f $DATA/$RST_OUT_ROMS_NOWCAST ]
       then
         cp -p $DATA/$RST_OUT_ROMS_NOWCAST $COMOUT/$RST_OUT_ROMS_NOWCAST
         echo "   $RST_OUT_ROMS_NOWCAST saved "
       fi
    fi
fi

#### FORECAST
 
if [ $RUNTYPE == "FORECAST" -o $RUNTYPE == "forecast" ]
then

# 1.a RIVER FORCING FILE 

    if [ -s $DATA/$RIVER_FORCING_FILE ]
    then
      echo " $DATA/$RIVER_FORCING_FILE existed "
    elif [ -s $COMOUT/$RIVER_FORCING_FILE ]
    then
      ln -s $COMOUT/$RIVER_FORCING_FILE $RIVER_FORCING_FILE
    else  
      msg="ABNORMAL EXIT: NO RIVER FORCING FILE $RIVER_FORCING_FILE"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************** '
      echo '*** FATAL ERROR : NO $RIVER_FORCING_FILE   *** '
      echo '********************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi

# 1.b OBC FORCING FILE 

    if [ -f $DATA/$OBC_FORCING_FILE ]
    then
      echo "   $DATA/$OBC_FORCING_FILE existed "
    elif [ -s $COMOUT/$OBC_FORCING_FILE ]
    then
      ln -s $COMOUT/$OBC_FORCING_FILE $OBC_FORCING_FILE
    else
      msg="ABNORMAL EXIT: NO OBC FORCING FILE $OBC_FORCING_FILE"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '********************************************** '
      echo '*** FATAL ERROR : NO $RIVER_FORCING_FILE   *** '
      echo '********************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi
# 1.a RIVER FORCING FILE 


# 1.c Meteorological Forcing For forecast 

    if [ -f $DATA/$MET_NETCDF_1_FORECAST ]
    then
      echo "   $DATA/$MET_NETCDF_1_FORECAST existed "
    elif [ -s $COMOUT/$MET_NETCDF_1_FORECAST ]
    then
      ln -s $COMOUT/$MET_NETCDF_1_FORECAST $MET_NETCDF_1_FORECAST
    else
      msg="ABNORMAL EXIT: NO Meteorological Forcing For forecast $MET_NETCDF_1_FORECAST"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************************** '
      echo '*** FATAL ERROR : NO $Meteorological Forcing For forecast   *** '
      echo '************************************************************** '
      echo ' '
      echo $msg
      set $seton
      err=1;export err;err_chk
    fi

# 1.g HFlux For Forecast
    if [ -s $DATA/$MET_NETCDF_2_FORECAST ]
    then
      echo "   $DATA/$MET_NETCDF_2_FORECAST existed "
    elif [ -s $COMOUT/$MET_NETCDF_2_FORECAST ]
    then
      ln -s $COMOUT/$MET_NETCDF_2_FORECAST $MET_NETCDF_2_FORECAST
    fi

# 1.h forecast cntl file
    if [ -f $DATA/${RUN}_roms_forecast.in ]
    then
      echo "   $DATA/${RUN}_roms_forecast.in existed "
    elif [ -s $COMOUT/${RUN}_roms_forecast.in ]
    then
      ln -s $COMOUT/${RUN}_roms_forecast.in ${RUN}_roms_forecast.in
    else
       msg="ABNORMAL EXIT: ROMS runtime input file for forecast is not found"
       ./postmsg "$jlogfile" "$msg"
       ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************************** '
      echo '*** FATAL ERROR :    *** ROMS runtime input file for forecast is not found'
      echo '************************************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi

#1.h Tide data 
    if [ -f $DATA/$HC_FILE_OFS ]
    then
      echo "   $DATA/$HC_FILE_OFS existed "
    elif [ -s $COMOUT/$HC_FILE_OFS ]
    then
      ln -s $COMOUT/$HC_FILE_OFS $HC_FILE_OFS
    else
       msg="ABNORMAL EXIT: Tide Constituent file for ROMS OBC is not found"
       ./postmsg "$jlogfile" "$msg"
       ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************************** '
      echo '*** FATAL ERROR :    *** Tide Constituent file for ROMS OBC is not found'
      echo '************************************************************** '
      echo ' '
      echo $msg
      set $seton
      touch err.${RUN}.$PDY1.t${HH}z
      err=1;export err;err_chk
    fi

#1.i Nowcast RST file 
    if [ -f $DATA/$RST_OUT_ROMS_NOWCAST ]
    then
      echo "   $DATA/$RST_OUT_ROMS_NOWCAST existed " 
    elif [ -s $COMOUT/$RST_OUT_ROMS_NOWCAST ]
    then
      ln -s $COMOUT/$RST_OUT_ROMS_NOWCAST $RST_OUT_ROMS_NOWCAST
    else
       msg="ABNORMAL EXIT: NO Restart file for Forecast"
       ./postmsg "$jlogfile" "$msg"
       ./postmsg "$nosjlogfile" "$msg"
      set $setoff
      echo ' '
      echo '************************************************************** '
      echo '*** FATAL ERROR : NO Restart file for Forecast *** '
      echo '************************************************************** '
      echo ' '
      echo $msg
      set $seton
      err=1;export err;err_chk
   fi

# --------------------------------------------------------------------------- #
# 2   Execute ocean model of ROMS; where ${RUN}_roms_forecast.in is created by nos_ofs_reformat_roms_ctl.sh

    $EXECnos/${RUN}_roms_mpi ${RUN}_roms_forecast.in >> ${ROMS_OUT_FORECAST}
    export err=$?
    if [ $err -ne 0 ]
    then
      echo "Running ocean model ${RUN}_roms_mpi for $RUNTYPE did not complete normally"
      msg="Running ocean model ${RUN}_roms_mpi for $RUNTYPE did not complete normally"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
      err_chk
    else
      echo "Running ocean model ${RUN}_roms_mpi completed normally"
      msg="Running ocean model ${RUN}_roms_mpi  completed normally"
      ./postmsg "$jlogfile" "$msg"
      ./postmsg "$nosjlogfile" "$msg"
    fi

    rm -f corms.fcst
    if [ -s ${ROMS_OUT_FORECAST} ]
    then
      grep "ROMS/TOMS - Blows up" ${ROMS_OUT_FORECAST} > corms.fcst
      grep "Blowing-up" ${ROMS_OUT_FORECAST} >> corms.fcst
      grep "Abnormal termination: BLOWUP" ${ROMS_OUT_FORECAST} >> corms.fcst
    fi
    if [ -s  corms.fcst ]
    then
      echo "${RUN} FORECAST RUN OF CYCLE t${HH}z ON $YYYY$MM$DD FAILED 00"  >> $CORMSLOG 
      echo "FORECAST_RUN DONE 0"  >> $CORMSLOG
      export err=99; err_chk
    else
      echo "${RUN} FORECAST RUN  OF CYCLE t${HH}z ON $YYYY$MM$DD COMPLETED SUCCESSFULLY 100" >> $CORMSLOG
      echo "FORECAST_RUN DONE 100"  >> $CORMSLOG
##    create a status file for CO-OPS side
      rm -f ${RUN}.status
      YYYY=`echo $time_nowcastend | cut -c1-4 `
        MM=`echo $time_nowcastend |cut -c5-6 `
        DD=`echo $time_nowcastend |cut -c7-8 `
        HH=`echo $time_nowcastend |cut -c9-10 `
       echo $YYYY$MM$DD$HH > ${RUN}.status
       cp ${RUN}.status $COMOUT/.

    fi

fi 


# --------------------------------------------------------------------------- #
# 4.  Ending output

  set $setoff
  echo ' '
  echo "Ending nos_ofs_nowcast_forecast.sh at : `date`"
  echo ' '
  echo '                     *** End of NOS OFS NOWCAST/FORECAST SIMULATIONS ***'
  echo ' '

# End of NOS OFS Nowcast script ------------------------------------------- #
