#!/bin/sh
set -xa
###############################################################################
#                                                                             #
# This is the actual forcast script for the global ens  wave model.           #
#                                                                             #
# For non-fatal errors output is witten to the $modIE.log (wave.log) file.    #
#                                                                             #
#                                                            December, 2003   #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation
#
  cd $DATA
  postmsg "$jlogfile" "HAS BEGUN on `hostname`"
#
  msg="Starting ens WAVE MODEL SCRIPT"
  postmsg "$jlogfile" "$msg"
#
  echo ' '
  echo '                      ******************************'
  echo '                      ***  ens WAVE MODEL SCRIPT ***'
  echo '                      ******************************'
  echo ' '
  echo "Starting at : `date`"
#
# --------------------------------------------------------------------------- #
# 1.  Set times
# 1.a Times
#
  export date=$PDY
  export YMDH=${PDY}${cyc}
#
  lsth=126
#  lsth=84
#
# 1.b Set all necessary times.
#     YMDH     :  nowcast time in yyyymmddhh format
#     time_beg :  begin time of run (6 hour hindcast)
#     time_now :  nowcast time (restart file time)
#     time_end :  ending time of run ($lsth hour forecast)
#     time_inb :  start time for nesting output
#
##  ymdh=`$utilexec/ndate -6 $YMDH`
  ymdh=$YMDH
  time_beg="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"
#
  time_now="`echo $YMDH | cut -c1-8` `echo $YMDH | cut -c9-10`0000"
#
  ymdh=`$utilexec/ndate $lsth $YMDH`
  time_end="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"
#
  echo ' '
  echo 'Times in wave model format :'
  echo '----------------------------'
  echo "   date / cycle  : $date $cycle"
  echo "   starting time : $time_beg"
  echo "   ending time   : $time_end"
  echo ' '
#
  time_inb=$time_beg
#
# --------------------------------------------------------------------------- #
# 2.  Get and prepare input and output files
#
  echo 'Preparing input files :'
  echo '-----------------------'
#
# 2.a Model definition file
#
  if [ ! -f mod_def.ww3 ]
  then
    echo "   Copy mod_def.ww3 from $FIXwave"
    cp $FIXwave/wave_$modID.moddef mod_def.ww3
  fi
#
  if [ -f mod_def.ww3 ]
  then
    echo "   mod_def.ww3 exists."
  else
    msg="ABNORMAL EXIT: NO MODEL DEFINITION FILE"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '********************************************** '
    echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
    echo '********************************************** '
    echo ' '
    echo "$modIE fcst $date $cycle : fixed file(s) missing." >> $wavelog
    echo $msg
    err=1;export err;err_chk
  fi
#
# 2.b Wind file
#
  if [ ! -f wind.ww3 ] 
  then
    echo "   Copy wind.ww3 from $COMIN/$modIE.$cycle.wind"
    cp $COMIN/$modIE.$cycle.wind wind.ww3
  fi
#
  if [ -f wind.ww3 ]
  then
    echo "   wind.ww3 exists."
  else
    msg="ABNORMAL EXIT: NO WIND FILE"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '********************************** '
    echo '*** FATAL ERROR : NO WIND FILE *** '
    echo '********************************** '
    echo ' '
    echo "$modIE fcst $date $cycle : wind file missing." >> $wavelog
    echo $msg
    err=2;export err;err_chk
  fi
#
# 2.c Ice file
#
  if [ ! -f ice.ww3 ] 
  then
    echo "   Copy ice.ww3 from $COMIN/$modID.$cycle.ice"
    cp $COMIN/$modID.$cycle.ice ice.ww3

  fi
#
  if [ -f ice.ww3 ]
  then
    echo "   ice.ww3 exists."
    ice_flag=T
  else
    echo "   ice.ww3 not found.                            **** WARNING **** "
    ice_flag=F
    echo "$modIE fcst $date $cycle : no ice file." >> $wavelog
    postmsg "$jlogfile" "NON-FATAL ERROR - ice.ww3 NOT FOUND."
  fi
#
# --------------------------------------------------------------------------- #
# 2.d Restart file
#
  time_ini="`echo $YMDH | cut -c1-8` `echo $YMDH | cut -c9-10`0000"
#
  if [ -f $COMIN/$modID.t${cyc}z.next_restart ] 
  then
    cp  $COMIN/$modID.t${cyc}z.next_restart  restart.ww3
    echo "   restart.ww3 copied ($COMIN/$modID.t${cyc}z.next_restart)."
  else
    msg="ABNORMAL EXIT: ERR in coping restart.ww3 file"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '**************************************************** '
    echo "*** ERR : No $modID.t${cyc}z.next_restart file *** "
    echo '**************************************************** '
    echo ' '
    echo "No $COMIN/$modID.t${cyc}z.next_restart " >> $wavelog
    err=5;export err;err_chk
  fi
#
  if [ ! -f restart.ww3 ] ; then
    msg="   restart.ww3 not found (COLD START).           **** WARNING **** "
    postmsg "$jlogfile" "$msg"
    echo "   restart.ww3 not found (COLD START).           **** WARNING **** "
    echo "$modIE fcst $date $cycle : cold start." >> $wavelog
    cold_start=yes
  fi
#
# 2.e Buoy location file
#
  if [ -f $FIXwave/wave_$modID.buoys ]
  then
    cp $FIXwave/wave_$modID.buoys buoy.loc
  fi
#
  if [ -f buoy.loc ]
  then
    echo "   buoy.loc copied ($FIXwave/wave_$modID.buoys)."
  else
    echo "   buoy.loc not found.                           **** WARNING **** "
    postmsg "$jlogfile" " **** WARNING **** buoy.loc NOT FOUND"
    touch buoy.loc
    echo "$modIE fcst $date $cycle : no buoy locations file." >> $wavelog
  fi
#
# 2.g Create model input file from template
#
  if [ -f $FIXwave/wavefcst.inp.tmpl ]
  then
    cp $FIXwave/wavefcst.inp.tmpl wavefcst.inp.tmpl
  fi
#
  if [ ! -f wavefcst.inp.tmpl ]
  then
    msg="ABNORMAL EXIT: NO TEMPLATE FOR INPUT FILE"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '************************************************ '
    echo '*** FATAL ERROR : NO TEMPLATE FOR INPUT FILE *** '
    echo '************************************************ '
    echo ' '
    echo "$modIE fcst $date $cycle : fixed file(s) missing." >> $wavelog
    echo $msg
    err=3;export err;err_chk
  fi
#
  sed -e "s/ICE_FLAG/$ice_flag/g" \
      -e "s/RUN_BEG/$time_ini/g" \
      -e "s/RUN_END/$time_end/g" \
      -e "s/OUT_BEG/$time_beg/g" \
      -e "s/OUT_END/$time_end/g" \
      -e "s/DTFLD/ 10800/g" \
      -e "s/FIELDS/F F T T T  T F T T F  T T T T F  F F F/g" \
      -e "s/DTPNT/ 10800/g" \
      -e "/BUOY_FILE/r buoy.loc" \
      -e "s/BUOY_FILE/DUMMY/g" \
      -e "s/DTTRK/    0/g" \
      -e "s/TRACK_FLAG/DUMMY/g" \
      -e "s/RST_TME/$time_now/g" \
      -e "s/BPT_BEG/$time_inb/g" \
      -e "s/BPT_END/$time_end/g" \
      -e "s/    1  /    0  /g"  \
      -e "s/DTBPT/    0/g" \
                                     wavefcst.inp.tmpl | \
  sed -n "/DUMMY/!p"               > wavefcst.inp
#
  rm -f wavefcst.inp.tmpl
#
# --------------------------------------------------------------------------- #
# 3.  Run model
#
  echo ' '
  echo 'Start the wave model :'
  echo '----------------------'
  echo ' '
  postmsg "$jlogfile" "Start the wave model."
#
  poe $EXECwave/wavefcst
  err=$?
#
  if [ "$err" != '0' ]
  then
    pgm=wave_fcst
    msg="ABNORMAL EXIT: ERROR IN wavefcst"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN wavefcst *** '
    echo '*************************************** '
    echo ' '
    echo "$modIE fcst $date $cycle : model crashed." >> $wavelog
    echo $msg
    err=4;export err pgm;err_chk
  fi
  postmsg "$jlogfile" "Wave Model completed."
#
  if [ "$cold_start" = 'no' ] && [ -n "`grep 'old start' log.ww3`" ]
  then
    echo "$modIE fcst $date $cycle : cold start (not found until section 3)." \
                                                                   >> $wavelog
    postmsg "$jlogfile" "COLD START (not found until section 3)."
  fi
#
# --------------------------------------------------------------------------- #
# 4.  Save and clean up files
# 4.a Send files to com if requested
#
  if [ "$SENDCOM" = 'YES' ]
  then
    echo ' '
    echo 'Saving output files :'
    echo '---------------------'
#
    echo "ncosp" > $COMOUT/where_waveens_ran.$cycle
#
    if [ -f log.ww3 ] 
    then
      echo "   Copying log.ww3      to $COMOUT/$modIE.$cycle.log"
               cp      log.ww3         $COMOUT/$modIE.$cycle.log
    fi
#
    if [ -f out_pnt.ww3 ] 
    then
      echo "   Copying out_pnt.ww3  to $COMOUT/$modIE.$cycle.outpnt"
               cp      out_pnt.ww3     $COMOUT/$modIE.$cycle.outpnt
    fi
#
    if [ -f out_grd.ww3 ] 
    then
      echo "   Copying out_grd.ww3  to $COMOUT/$modIE.$cycle.outgrd"
               cp      out_grd.ww3     $COMOUT/$modIE.$cycle.outgrd
    fi
#
  fi
#
# 4.b Clean up unwanted files
#
  rm -f wind.ww3 ice.ww3 restart*.ww3 nest*.ww3
  rm -f wavefcst.inp
#
# --------------------------------------------------------------------------- #
# 5.  Ending output
#
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo "                    *** End of $modIE forecast script ***"
#
  msg="End of NWW3$modIE WAVE MODEL SCRIPT"
  postmsg "$jlogfile" "$msg"
#
# End of script ------------------------------------------------------------- #
