#!/bin/sh
###############################################################################
#                                                                             #
# This is the actual forcast script for the regional WNA wave model. It uses  #
# only a single ush script                                                    #
#                                                                             #
#    wavestart.sh   Determine the time for the most recent available restart  #
#                   file.                                                     #
#                                                                             #
# For non-fatal errors output is witten to the wave.log file.                 #
#                                                                             #
#                                                               August, 2002  #
# Copied from nwprod 03/12/03                                                 #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

  seton='-xa'
  setoff='+xa'
  postmsg "$jlogfile" "HAS BEGUN on `hostname`"

  msg="Starting WNA WAVE MODEL SCRIPT"
  postmsg "$jlogfile" "$msg"

  set $setoff
  echo ' '
  echo '                      *****************************'
  echo '                      *** WNA WAVE MODEL SCRIPT ***'
  echo '                      *****************************'
  echo ' '
  echo "Starting at : `date`"
  set $seton

# --------------------------------------------------------------------------- #
# 1.  Set times
# 1.a Times

  export date=$PDY
  export YMDH=${PDY}${cyc}

  lsth=180

# 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)

  ymdh=`$utilexec/ndate -6 $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"

  set $setoff
  echo ' '
  echo 'Times in wave model format :'
  echo '----------------------------'
  echo "   date / cycle  : $date $cycle"
  echo "   starting time : $time_beg"
  echo "   ending time   : $time_end"
  echo ' '

# --------------------------------------------------------------------------- #
# 2.  Get and prepare input and output files

  echo 'Preparing input files :'
  echo '-----------------------'
  set $seton

# 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"
    set $setoff
    echo ' '
    echo '********************************************** '
    echo '*** FATAL ERROR : NO MODEL DEFINITION FILE *** '
    echo '********************************************** '
    echo ' '
    echo "$modID fcst $date $cycle : fixed file(s) missing." >> $wavelog
    echo $msg
    set $seton
    err=1;export err;err_chk
  fi

# 2.b Wind file

  if [ ! -f wind.ww3 ]
  then
    echo "   Copy wind.ww3 from $COMIN/$modID.$cycle.wind"
    cp $COMIN/$modID.$cycle.wind wind.ww3
  fi

  if [ -f wind.ww3 ]
  then
    echo "   wind.ww3 exists."
  else
    msg="ABNORMAL EXIT: NO WIND FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '********************************** '
    echo '*** FATAL ERROR : NO WIND FILE *** '
    echo '********************************** '
    echo ' '
    echo "$modID fcst $date $cycle : wind file missing." >> $wavelog
    echo $msg
    set $seton
    err=2;export err;err_chk
  fi

# 2.c Boundary data file

  if [ ! -f nest.ww3 ]
  then
    echo "   Copy nest.ww3 from $COMIN/$modID.$cycle.nest"
    cp $COMIN/$modID.$cycle.nest nest.ww3
  fi

  if [ -f nest.ww3 ]
  then
    echo "   nest.ww3 exists."
  else
    msg="ABNORMAL EXIT: NO BOUNDARY DATA FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '*******************************************'
    echo '*** FATAL ERROR : NO BOUNDARY DATA FILE ***'
    echo '*******************************************'
    echo ' '
    echo "$modID fcst $date $cycle : nest file missing." >> $wavelog
    echo $msg
    set $seton
    err=3;export err;err_chk
  fi

# 2.d 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 "$modID fcst $date $cycle : no ice file." >> $wavelog
    postmsg "$jlogfile" "NON-FATAL ERROR - ice.ww3 NOT FOUND."
  fi

# --------------------------------------------------------------------------- #
# 2.e Restart file
#     Normally, we will pick up the file $modID.next_restart from the previous
#     cycle. If that file is not there, we try to find a restart form a 
#     cycle up to a maximum of nback cycles back. If there is no restart file
#     anywhere, a cold start is performed automatically.
#     The corresponding starting time is set in wavestart.sh
#
#     Make sure nback is set identically in the preprocessor !!!

  export nback=10

  $USHwave/wavestart.sh

  ymdh=`head wavestart.out | awk '{ print $1 }'`
  rm -f wavestart.out

  pdate=`echo $ymdh | cut -c 1-8`
  pcycle=t`echo $ymdh | cut -c 9-10`z
  time_ini="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"

   dir=$com/${RUN}.${pdate}
  file=$modID.${pcycle}.next_restart
  if [ -d $dir ]
  then
    if [ -f $dir/$file ]
    then
      cp  $dir/$file restart.ww3
    fi
  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 "$modID fcst $date $cycle : cold start." >> $wavelog
    cold_start=yes
  else
    echo "   restart.ww3 copied ($dir/$file)."
    if [ "$time_ini" != "$time_beg" ]
    then
      echo "$modID fcst $date $cycle : older restart used ($pdate $pcycle)." >> $wavelog
      postmsg "$jlogfile" "OLDER RESTART USED ($pdate $pcycle)."
    fi
    cold_start=no
  fi

# 2.f 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 "$modID 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"
    set $setoff
    echo ' '
    echo '************************************************ '
    echo '*** FATAL ERROR : NO TEMPLATE FOR INPUT FILE *** '
    echo '************************************************ '
    echo ' '
    echo "$modID fcst $date $cycle : fixed file(s) missing." >> $wavelog
    echo $msg
    set $seton
    err=4;export err;err_chk
  fi

  sed -e "s/ICE_FLAG/$ice_flag/g" \
      -e "s/DT0_FLAG/F/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/ 3600/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/ 3600/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_beg/g" \
      -e "s/BPT_END/$time_end/g" \
      -e "s/DTBPT/    0/g" \
                                     wavefcst.inp.tmpl | \
  sed -n "/DUMMY/!p"               > wavefcst.inp

  rm -f wavefcst.inp.tmpl

# --------------------------------------------------------------------------- #
# 3.  Run model

  set $setoff
  echo ' '
  echo 'Start the wave model :'
  echo '----------------------'
  echo ' '
  set $seton
  postmsg "$jlogfile" "Start the wave model."

  echo "Executing $EXECwave/wavefcst"
  poe $EXECwave/wavefcst
  err=$?

  if [ "$err" != '0' ]
  then
    pgm=wave_fcst
    msg="ABNORMAL EXIT: ERROR IN wavefcst"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN wavefcst *** '
    echo '*************************************** '
    echo ' '
    echo "$modID fcst $date $cycle : model crashed." >> $wavelog
    echo $msg
    set $seton
    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 "$modID 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
    set $setoff
    echo ' '
    echo 'Saving output files :'
    echo '---------------------'
    set $seton

    echo "ncosp" > $COMOUT/where_wavewna_ran.$cycle

    if [ -f log.ww3 ] ; then
      echo "   Copying log.ww3      to $COMOUT/$modID.$cycle.log"
               cp     log.ww3         $COMOUT/$modID.$cycle.log
    fi

    if [ -f out_pnt.ww3 ] ; then
      echo "   Copying out_pnt.ww3  to $COMOUT/$modID.$cycle.outpnt"
               cp     out_pnt.ww3     $COMOUT/$modID.$cycle.outpnt
    fi

    if [ -f out_grd.ww3 ] ; then
      echo "   Copying out_grd.ww3  to $COMOUT/$modID.$cycle.outgrd"
               cp     out_grd.ww3     $COMOUT/$modID.$cycle.outgrd
    fi

    if [ -f restart1.ww3 ] ; then
      echo "   Copying restart1.ww3 to $COMOUT/$modID.$cycle.next_restart"
               cp     restart1.ww3    $COMOUT/$modID.$cycle.next_restart
    fi

  fi

# 4.b Clean up unwanted files

  rm -f wind.ww3 ice.ww3 restart*.ww3 nest*.ww3
  rm -f wavefcst.inp

# --------------------------------------------------------------------------- #
# 5.  Alert the file(s)

 if [ $SENDDBN = YES ] ; then
    $DBNROOT/bin/dbn_alert MODEL WAVE_BINARY $job $COMOUT/$modID.$cycle.next_restart
 fi

# --------------------------------------------------------------------------- #
# 6.  Ending output

  set $setoff
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                    *** End of WNA forecast script ***'

  msg="End of WNA WAVE MODEL SCRIPT"
  postmsg "$jlogfile" "$msg"

# End of script ------------------------------------------------------------- #
