#!/bin/sh
#######################################################################
# This script save the output files to /com or /nwges area
#######################################################################

set -x

cd $DATA/$fcstsys

case "$MODULE"
in
  "get_data_now")

## contingency plan for average water level file
if [ -s nos_glavewl.dat ]
then
#separate average water for each lake into individual files for netcdf subroutine read in
grep $fcstsys nos_glavewl.dat | awk '{print $1}' > $fcstsys'_avewl.dat'
echo 'FOUND AVERAGE WATER LEVEL FILE!'
echo 'SEPARATED AVERAGE WATER LEVEL FILE INTO SEPARATE LAKES!'

export pgm=separate/copy_average_${fcstsys}_water_level_file_into_separate_lakes
. ./prep_step

cp nos_glavewl.dat $GESDIR/${NET}_${fcstsys}_glavewl.dat
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, use last good file"
  msg="$pgm did not complete normally, use last good file"
  ./postmsg "$jlogfile" "$msg"
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
  echo DONE GLAVEWL 100 >> $FCSTSYSLOG
fi

else

echo 'DID NOT FIND AVERAGE WATER LEVEL FILE!'
echo 'COPY FILE FROM LAST GOOD RETURN FROM NWGES!'

export pgm=separate/copy_average_${fcstsys}_water_level_file_into_separate_lakes_last_good_run

. ./prep_step

./startmsg

cp $GESDIR/${NET}_${fcstsys}_glavewl.dat nos_glavewl.dat
#separate average water for each lake into individual files for netcdf subroutine read in
grep $fcstsys nos_glavewl.dat | awk '{print $1}' > $fcstsys'_avewl.dat'
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
  echo DONE GLAVEWL 0 >> $FCSTSYSLOG
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
  echo DONE GLAVEWL 0 >> $FCSTSYSLOG
fi

fi

## contingency plan for average lake temperature file 

if [ -s get_glsea.txt ]
then
      
echo 'FOUND AVERAGE LAKE TEMPERATURE FILE!'

export pgm=copy_${fcstsys}_average_lake_temp_file
. ./prep_step

./startmsg

cp get_glsea.txt $GESDIR/${NET}_${fcstsys}_get_glsea.txt
export err=$?

if [ $err -ne 0 ]
then
  echo "$pgm did not complete normally, use last good file"
  msg="$pgm did not complete normally, use last good file"
  ./postmsg "$jlogfile" "$msg"
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
  echo GET GLSEA 100 >> $FCSTSYSLOG
fi

else

echo 'DID NOT FIND AVERAGE LAKE TEMPERATURE FILE!'
echo 'COPY FILE FROM LAST GOOD RETURN!'

export pgm=copy_${fcstsys}_average_lake_temp_file_from_previous_good_run
. ./prep_step

./startmsg

cp $GESDIR/${NET}_${fcstsys}_get_glsea.txt get_glsea.txt 
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
  echo DONE GLAVEWL 0 >> $FCSTSYSLOG
else
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
  echo GET GLSEA 0 >> $FCSTSYSLOG
fi

fi

## contingency plan for surfacemarobs file obs data for running nowcast 
  
if [ -s get_sfcmarobs.txt ]
then
echo 'SURFACEMAROBS RETURNED SUCCESSFULLY!'
echo 'NONAMNOW' > edit_sfcmarobs_test
echo DONE SFCMAROBS 100 >> $FCSTSYSLOG
else
echo 'USING NAM ANALYSIS IN PLACE OF SURFACEMAROBS!'

## grab NAM data to run nowcast in the absence of surfacemarobs data
#remove forecast files from potential previous now or forecast run
      
rm uwind_for.txt vwind_for.txt atemp_for.txt

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

./startmsg

$USHnos/nos_glofs_reformat_namnow.sh $time_hotstart $time_nowcastend
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 'NAMNOW' > edit_sfcmarobs_test
  echo "$pgm completed normally"
  msg="$pgm completed normally"
  ./postmsg "$jlogfile" "$msg"
fi

if [ -s uwind_for.txt ]
then
cp uwind_for.txt uwind.txt
cp vwind_for.txt vwind.txt
cp atemp_for.txt atemp.txt
cp dtemp_for.txt dtemp.txt
cp cldcv_for.txt cldcv.txt

echo 'INTERPOLATED NAM IN PLACE OF SURFACEMAROBS SUCCESSFUL!!!!'
fi

echo DONE SFCMAROBS  0 >> $FCSTSYSLOG
fi

;;

"reformat_data_for")

## contingency plan for ndfd forecast files
read FORCING < forforcing

if [ $FORCING == 'NDFD' ]
then
if [ -s uwind_for.txt ]
then
echo "REFORMAT WFO 100" >> $FCSTSYSLOG
else
echo "REFORMAT WFO 0" >> $FCSTSYSLOG
fi

elif [ $FORCING == 'NAM' ]
then

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

./startmsg

$USHnos/nos_glofs_reformat_namfor.sh $time_nowcastend $time_forecastend
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

if [ -s uwind_for.txt ]
then
echo "REFORMAT NAM 100" >> $FCSTSYSLOG
else
echo "REFORMAT NAM 0" >> $FCSTSYSLOG
fi

fi
;;

esac
