#!/bin/sh
#---------------------------------------------------------------------------------------
#
# Script Name: exnos_glofs_forecast.sh.sms
#
# Prod Directory Location: /nwprod/scripts
# Dev Directory Location: /gpfs/d/marine/save/wx20gm/NCEPDEVEL/scripts
#
# Technical Contact(s): Name:  Greg Mott            Org:    NOS/CO-OPS
#                       Phone: 301-261-4500         E-Mail: greg.mott@noaa.gov
#                       Name:                       Org:   
#                       Phone:                      E-Mail:
#
# Abstract:
#                             This script is the main launch script that runs all 
#                             5 great lakes models. exported variables defined in the 
#                             j-job script will determine which lake this script runs for.  
#                             This script calls all scripts to get data, reformat data,  
#                             run the nowcast and forecast, and archive the data.  
#
# Usage:  Interactively:     (Ex. exnos_glofs_main.sh.sms)
#              Via cron:     (50 * * * * /gpfs/d/marine/save/wx20gm/NCEPDEVEL/scripts/launch_glofs.sh > 
#                             /gpfs/d/marine/save/wx20gm/NCEPDEVEL/output/nos_glofs_`date -u +\%Y\%m\%d\%H`_diagnostics.log 2>&1)
#
# Input Parameters:     (All input parameters, ie, exported variables are set in the j-job, see JNOS_GLOFS_FCST.sms in "jobs" directory)
#
# Language:  Bourne Shell Script
#
# Target Computer: mist/dew
#
# Estimated Execution Time: < 2 minutes nowcast only, < 10 minutes nowcast+forecast, for all five lakes. 
#
# Scripts/Programs Called:
#            Name                           Directory Location                              Description
#            nos_glofs_get_sfcmarobs.pl     /ush                                            script to call surface marine obs file for nowcast run 
#            nos_glofs_get_glsea.pl         /ush                                            script to call average water temperature file for nowcast run
#            nos_glofs_get_glavewl.pl       /ush                                            script to call average water level file for nowcast and forecast postprocessing  
#            nos_glofs_edit_sfcmarobs       /exec                                           fortran program to edit surface marine obs
#            nos_glofs_interp_sfcmarobs     /exec                                           fortran program to interpolate surface marine obs to the great lakes model grids
#            nos_l*ofs_forecast             /exec                                           fortran program to launch pom based nowcast/forecast system, one exec each lake
#            nos_glofs_hotstartcopy.sh      /ush                                            script to mv hotstart files to the input directory for the next nowcast/forecast run
#            nos_glofs_ndfd_reformat.sh     /ush                                            script to grab ndfd forecast data, and interpolate it onto the glofs model grids
#            nos_glofs_nam_reformat.sh      /ush                                            script to grab nam forecast data, and interpolate it onto the glofs model grids
#            nos_glofs_archive.sh           /ush                                            script to archive all netcdf model output files, and corms flags      i
#      
# Input Files:
#            Name                           Directory  Location                             Description
#            nos_$fcstsys_bath.dat          /fix                                            bathymetry file
#            nos_glofs_stations.lmd         /fix                                            observation stations file 
#            nos_$fcstsys_lat_lon.dat       /fix                                            lon-lat glofs model grid file 
#            nos_$fcstsys_stationlistfile.dat /fix                                          station list file for time series plotting
#            nos_glavewl.dat                /dcom/us007003/YYYYMMDDHH/wtxtbul/              average wl file for all five lakes, computed daily
#            get_glsea.txt                  /dcom/us007003/YYYYMMDDHH/wtxtbul/              average water temperature file for all five lakes, computed daily 
#            get_sfcmarobs.txt              /com/hourly/prod/hourly.YYYYMMDD/YYYYMMDDHHHH_sfcmarobs.txt  surface marine observation file for nowcast glofs runs
#            ********* $fcstsys is the variable for leofs, lmofs, lhofs, loofs, or lsofs, set in the j-job
#
# Output Files:
#            Name                           Directory Location                              Description
#            nos_l*ofs_YYYYMMDDHH_stations_nowcast.nc   /output                             netcdf model output field and station nowcast and forecast files 
#            nos_l*ofs_YYYYMMDDHH_fields_nowcast.nc     /output
#            nos_l*ofs_YYYYMMDDHH_stations_forecast.nc  /output
#            nos_l*ofs_YYYYMMDDHH_fields_forecast.nc    /output
#           
# Error Conditions:
#
# Author Name:                              Creation Date:
#
#
# Revisions:
#         Date                          Author                             Description
#
#
#
#
# Remarks:
#
#
# -----------------------------------------------------------------------------------------------------------------

set -x

export fcstsys=$1
export time_nowcastend=$PDY$cyc

#separate time_nowcastend into separate variables for sed
#substitution for forecast model control files
export ynet=`echo $time_nowcastend |cut -c1-4`
export mnet=`echo $time_nowcastend |cut -c5-6`
export dnet=`echo $time_nowcastend |cut -c7-8`

export time_forecastend=`$EXECutil/ndate +$forhrs $time_nowcastend`

cd $DATA/${fcstsys}

$USHutil/setup.sh

rm -rf ${fcstsys}.out

export pgm=nos_glofs_restart_${fcstsys}_grab
. ./prep_step

./startmsg

export RESTARTASK=restart_grab
$USHnos/nos_glofs_restart.sh 
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, restart file not found from last seven days in $nwges"
  msg="$pgm did not complete normally, restart file not found from last seven days in $nwges"
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

read time_hotstart < ${NET}_${fcstsys}_timehotstart.txt 
export time_hotstart

export nh=`$NHOUR $PDY$cyc $time_hotstart`

export yhst=`echo $time_hotstart |cut -c1-4`
export mhst=`echo $time_hotstart |cut -c5-6`
export dhst=`echo $time_hotstart |cut -c7-8`
export hhst=`echo $time_hotstart |cut -c9-10`

#compute 3 hours earlier from hotstart time to grab enough sfcmarobs data
export time_hotstartm3h=`$NDATE -3 $time_hotstart`

#compute day of year for nowcast and forecast control files
export dayn=`$JDAY $yhst$mhst$dhst |cut -c5-7`

####################################################################
# FCSTSYSLOG is the log file that CO-OPS uses to diagnostically 
# access ALL important aspects of GLOFS model run successes and/or
# failures, via simple flag creation.
###################################################################

export FCSTSYSLOG=$DATA/${fcstsys}/${fcstsys}.t${cyc}z.log

echo  "begin ncep $fcstsys model"                     >> $FCSTSYSLOG  
echo  "number hours ncep nowcast run = " $nh          >> $FCSTSYSLOG
echo  "ncep time_hotstartm3h"    $time_hotstartm3h    >> $FCSTSYSLOG 
echo  "ncep time_hotstart"       $time_hotstart       >> $FCSTSYSLOG 
echo  "ncep time_nowcastend"     $time_nowcastend     >> $FCSTSYSLOG 
echo  "ncep time_forecastend"    $time_forecastend    >> $FCSTSYSLOG
echo  "ncep day of year"         $dayn                >> $FCSTSYSLOG 

export pgm=static/dynamic_files_copied
. ./prep_step

# copy model specific static and control files to work directory 
cp $FIXnos/nos_${fcstsys}_bath.dat .
export err=$?; ./err_chk

cp $FIXnos/nos_glofs_stations.lmd .
export err=$?; ./err_chk

cp $FIXnos/nos_${fcstsys}_lat_lon.dat .
export err=$?; ./err_chk

cp $FIXnos/nos_${fcstsys}_stationlistfile.dat stationlistfile.dat
export err=$?; ./err_chk

cp $FIXnos/nos_${fcstsys}_edit_sfcmarobs.ctl .
export err=$?; ./err_chk

cat nos_${fcstsys}_edit_sfcmarobs.ctl | sed -e s/YYYY/$yhst/g -e s/MM/$mhst/g -e s/DD/$dhst/g -e s/HH/$hhst/g \
-e s/X/$nh/g > ${fcstsys}_edit_sfcmarobs.in
export err=$?; ./err_chk

cp $FIXnos/nos_${fcstsys}_interp_sfcmarobs.ctl .
export err=$?; ./err_chk

cat nos_${fcstsys}_interp_sfcmarobs.ctl | sed -e s/YYYY/$yhst/g -e s/MM/$mhst/g -e s/DD/$dhst/g -e s/HH/$hhst/g  \
-e s/X/$nh/g > ${fcstsys}_interp_sfcmarobs.in
export err=$?; ./err_chk

cp $FIXnos/nos_${fcstsys}_nowcast.ctl .
export err=$?; ./err_chk

cat nos_${fcstsys}_nowcast.ctl | sed -e s/YYYY/$yhst/g -e s/JDN/$dayn/g \
   -e s/HH/$hhst/g -e s/X/$nh/g -e s/MM/$mhst/g -e s/DY/$dhst/g > ${fcstsys}_nowcast.in
export err=$?; ./err_chk

if [ $cyc -eq '00' -o $cyc -eq '06' -o $cyc -eq '12' -o $cyc -eq '18' ]
then
echo 'It is the correct time_hour for a forecast cycle, copy control files for forecast, UTC=' $cyc

cp $FIXnos/nos_${fcstsys}_forecast.ctl .
export err=$?; ./err_chk

cat nos_${fcstsys}_forecast.ctl | sed -e s/YYYY/$ynet/g -e s/JDN/$dayn/g \
   -e s/HH/$cyc/g -e s/MM/$mnet/g -e s/DY/$dnet/g -e s/FH/$forhrs/g > ${fcstsys}_forecast.in
export err=$?; ./err_chk

else
echo 'It is not the time_hour for a forecast cycle ....UTC=' $cyc
fi

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#   Module 1 Get data for  NOWCAST
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
export MODULE=get_data_now

export pgm=nos_glofs_get_sfcmarobs_${fcstsys}
. ./prep_step

./startmsg

$USHnos/nos_glofs_get_sfcmarobs.pl "$time_hotstartm3h" "$time_nowcastend" -b $fcstsys 
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, will use nam analysis, run in nos_glofs_backup.sh"
  msg="$pgm did not complete normally, will use nam analysis, run in nos_glofs_backup.sh"
  ./postmsg "$jlogfile" "$msg"
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

export pgm=nos_glofs_get_glsea_${fcstsys}
. ./prep_step

./startmsg

$USHnos/nos_glofs_get_glsea.pl "$time_hotstart" $fcstsys 
export err=$? 

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, will use last good glsea temp, run in nos_glofs_backup.sh"
  msg="$pgm did not complete normally, will use last good glsea temp, run in nos_glofs_backup.sh" 
  ./postmsg "$jlogfile" "$msg"
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
 ./postmsg "$jlogfile" "$msg"
fi

export pgm=nos_glofs_get_glavewl_${fcstsys}
. ./prep_step

./startmsg

$USHnos/nos_glofs_get_glavewl.pl "$time_retstart" $fcstsys
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, will use last good water level, run in nos_glofs_backup.sh"
  msg="$pgm did not complete normally, will use last good water level, run in nos_glofs_backup.sh"
  ./postmsg "$jlogfile" "$msg"
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

export pgm=nos_glofs_backup_${fcstsys}_get_data_now
. ./prep_step

./startmsg

$USHnos/nos_glofs_backup.sh
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, critical error!"
  msg="$pgm did not complete normally, critical error!"
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#   Module 2 Reformat data for NOWCAST
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
export MODULE=reformat_data_now

read EDITSFC < edit_sfcmarobs_test 

export pgm=nos_glofs_edit_sfcmarobs_${fcstsys}
. ./prep_step

./startmsg 
$EXECnos/nos_glofs_edit_sfcmarobs < ${fcstsys}_edit_sfcmarobs.in >> ${fcstsys}.out 2>>errfile 
export err=$?

if [ $err -ne 0 -a $EDITSFC == 'NAMNOW'  ]
then
  echo "$pgm did not complete normally, used nam analysis, run in nos_glofs_backup.sh"
  msg="$pgm did not complete normally, used nam analysis, run in nos_glofs_backup.sh" 
  ./postmsg "$jlogfile" "$msg" 
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi 

export pgm=nos_glofs_interp_sfcmarobs_${fcstsys}
. ./prep_step

./startmsg 
$EXECnos/nos_glofs_interp_sfcmarobs <${fcstsys}_interp_sfcmarobs.in  >> ${fcstsys}.out 2>>errfile 
export err=$?

if [ $err -ne 0 -a $EDITSFC == 'NAMNOW' ]
then
  echo "$pgm did not complete normally, used nam analysis, run in nos_glofs_backup.sh"
  msg="$pgm did not complete normally, used nam analysis, run in nos_glofs_backup.sh"
  ./postmsg "$jlogfile" "$msg"
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi 

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#   Module 3 Run NOWCAST
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

export pgm=nos_${fcstsys}_nowcast
. ./prep_step

./startmsg 
$EXECnos/nos_${fcstsys}_forecast < ${fcstsys}_nowcast.in >>  ${fcstsys}.out 2>>errfile 
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, critical failure!"
  msg="$pgm did not complete normally, critical failure!"
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"

  mv runsuccess.txt ${fcstsys}_now_runsuccess.txt
## greps the runsuccessfile created at model run-time for "run success," otherwise model failure
 if grep "run success" ${fcstsys}_now_runsuccess.txt > /dev/null 2>&1
 then
  echo "NOWCAST DONE 100" >> $FCSTSYSLOG
 else
  echo "NOWCAST DONE 0" >> $FCSTSYSLOG
 fi
fi 

export pgm=nos_glofs_restart_${fcstsys}_copy
. ./prep_step

./startmsg

export RESTARTASK=restart_copy
$USHnos/nos_glofs_restart.sh pomgle.prst ${NET}_${fcstsys}_hotstart.dat time_hotstart.txt ${NET}_${fcstsys}_timehotstart.txt

export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, restart file not made, or not correct size, not copied to $nwges, critical failure"
  msg="$pgm did not complete normally, restart file not made, or not correct size, not copied to $nwges, critical failure" 
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#   MODULE 4 Get data and Reformat for FORECAST
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
export MODULE=reformat_data_for

if [ $cyc -eq '00' -o $cyc -eq '06' -o $cyc -eq '12' -o $cyc -eq '18' ]
then
echo 'It is the correct time_hour for a forecast cycle....UTC=' $cyc

export pgm=nos_glofs_reformat_ndfd_${fcstsys}_forecast
. ./prep_step

./startmsg

$USHnos/nos_glofs_reformat_ndfdfor.sh $time_nowcastend $time_forecastend
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, will use nam forecast, run in nos_glofs_backup.sh"
  msg="$pgm did not complete normally, will use nam forecast, run in nos_glofs_backup.sh"
  ./postmsg "$jlogfile" "$msg"
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

export pgm=nos_glofs_backup_${fcstsys}_reformat_data_for
. ./prep_step

./startmsg

$USHnos/nos_glofs_backup.sh
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, critical error!"
  msg="$pgm did not complete normally, critical error!"
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#   Module 5 Run FORECAST
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

export pgm=nos_${fcstsys}_forecast
. ./prep_step

./startmsg
$EXECnos/nos_${fcstsys}_forecast < ${fcstsys}_forecast.in >> ${fcstsys}.out 2>>errfile
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, critical failure"
  msg="$pgm did not complete normally, critical failure"
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
  mv runsuccess.txt ${fcstsys}_fore_runsuccess.txt
## greps the runsuccessfile created at model run-time for "run success," otherwise model failure
 if grep "run success" ${fcstsys}_fore_runsuccess.txt > /dev/null 2>&1
  then
  echo "FORECAST DONE 100" >> $FCSTSYSLOG
  else
  echo "FORECAST DONE 0" >> $FCSTSYSLOG
  fi
fi
else
    echo 'It is not the time_hour for a forecast cycle ....UTC=' $cyc
#   this line "greys" out corms flags not used by the nowcast
    cat $FIXnos/corms_raw_for_init.txt >> $FCSTSYSLOG 

fi

chmod 755 *

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#   Module 6 ARCHIVE NOWCAST/FORECAST
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

export pgm=nos_glofs_archive_${fcstsys}_output
. ./prep_step

./startmsg

$USHnos/nos_glofs_archive.sh
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, critical failure!"
  msg="$pgm did not complete normally, critical failure!"
  ./postmsg "$jlogfile" "$msg"
  ./err_chk
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi
