#!/bin/sh
###############################################################################
#                                                                             #
# This is the actual forcast script for the global multi grid wave model.     #
# It uses only a single ush script                                            #
#                                                                             #
#  multiwavestart.sh   : get initial time of most recent restart file(s)      #
#                                                                             #
# Remarks :                                                                   #
# - For errors output is witten to the wave.log file.                         #
# - This script runs multiple grid similtaneously. Optimization requires      #
#   assymetric loading of grids. NCO: PLEASE WORK WITH MMAB ON OPTIMIZATION   #
#   ON NEW HARDWARE>                                                          #
#                                                                             #
#  Update record :                                                            #
#                                                                             #
# - Origination:                                               05-Mar-2007    #
# - New mod_def file names, added output fields.               26-Apr-2006    #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
  cd $DATA
  seton='+xa'
  setoff='+xa'

  set $setoff
  echo ' '
  echo '                      ******************************'
  echo '                      *** MWW3 WAVE MODEL SCRIPT ***'
  echo '                      ******************************'
  echo '                          Global multi-grid model with GFS forcing'
  echo '                          Model identifier : multi_1p'
  echo ' '
  echo "Starting at : `date`"
  set $seton

# --------------------------------------------------------------------------- #
# 1.  Set times
# 1.a Cycle time info

  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_rst :  nowcast time (restart file time)
#     time_end :  ending time of run ($lsth hour forecast)

  grep 'off_hour=' $USHwave/multiwavestart.sh   > time_info
  grep 'stp_hour=' $USHwave/multiwavestart.sh  >> time_info
  . time_info ; rm -f time_info


  ymdh=`$utilexec/ndate -$off_hour $YMDH`
  time_rst="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"

  ymdh=`$utilexec/ndate -$stp_hour $ymdh`
  time_beg="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"

  ymdh_end=`$utilexec/ndate $lsth $YMDH`
  time_end="`echo $ymdh_end | cut -c1-8` `echo $ymdh_end | 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 "   restart time  : $time_rst"
  echo "   ending time   : $time_end"
  echo ' '
  set $seton

# 1.c Define all grids used

  ice='icean_5m'
  wind='gfs_30m'
  buoy="points"
  export grids='glo_30m ao_30m at_10m wc_10m ep_10m ak_10m at_4m wc_4m ak_4m'
  export int_grids='glo_30mext'

  set $setoff
  echo ' '
  echo 'Grid information  :'
  echo '-------------------'
  echo "   ice grid      : $ice"
  echo "   wind grid     : $wind"
  echo "   output points : ${modID}_$buoy"
  echo "   wave grids    : $grids"
  echo "   int. grids    : $int_grids"
  echo ' '
  set $seton

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

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

# 2.a Model definition files

  for grd in $ice $wind $buoy $grids $int_grids
  do
    if [ "$grd" = "$buoy" ]
    then
      grdID=${modID}_$grd
    else
      grdID=$grd
    fi

    echo "   Copy mod_def.$grd from $FIXwave/multiwave_$grdID.moddef"
    if [ -f $FIXwave/multiwave_$grdID.moddef ]
    then
      cp $FIXwave/multiwave_$grdID.moddef mod_def.$grd
    fi

    if [ ! -f mod_def.$grd ]
    then
      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
  done

# 2.b Wind info
# 2.b.1 Wind file

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

  if [ ! -f wind.$wind ]
  then
    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.b.2 Wind time info file

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

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

# 2.b.3 SST file

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

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

# 2.b.4 Wind preparation template file

  if [ -f $FIXwave/multiwaveprep.$wind.tmpl ]
  then
    cp $FIXwave/multiwaveprep.$wind.tmpl .
  fi

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

# 2.b.5 GRIB2 template file

#  if [ -f $FIXwave/multiwavegrib2_single.inp.tmpl ]
#  then
#    cp $FIXwave/multiwavegrib2_single.inp.tmpl .
#  fi

#  if [ ! -f multiwavegrib2_single.inp.tmpl ]
#  then
#    msg="ABNORMAL EXIT: NO GRIB2 TEMPLATE FILE"
#    postmsg "$jlogfile" "$msg"
#    set $setoff
#    echo ' '
#    echo '******************************************** '
#    echo '*** FATAL ERROR : NO GRIB2 TEMPLATE FILE *** '
#    echo '******************************************** '
#    echo ' '
#    echo "$modID fcst $date $cycle : grib2 template file missing." >> $wavelog
#    echo $msg
#    set $seton
#    err=3;export err;err_chk
#  fi

# 2.b.6 Grid Interpolation template files

#  for grd in $int_grids
#  do
#    if [ -f $FIXwave/${grd}_interp.inp.tmpl ]
#    then
#      cp $FIXwave/${grd}_interp.inp.tmpl .
#    fi

#    if [ ! -f ${grd}_interp.inp.tmpl ]
#    then
#      msg="ABNORMAL EXIT: NO GRID INTERPOLATION TEMPLATE FILE"
#      postmsg "$jlogfile" "$msg"
#      set $setoff
#      echo ' '
#      echo '********************************************************* '
#      echo '*** FATAL ERROR : NO GRID INTERPOLATION TEMPLATE FILE *** ' 
#      echo '********************************************************* '
#      echo ' '
#      echo "$modID fcst $date $cycle : grid interpolation template file missing." >> $wavelog
#      echo $msg
#      set $seton
#      err=3;export err;err_chk
#    fi
#  done

# 2.c Ice file

  echo "   Copy ice.$ice from $COMIN/$ice.$cycle.ice"
  if [ -f $COMIN/$ice.$cycle.ice ]
  then
    cp $COMIN/$ice.$cycle.ice ice.$ice
  fi

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

# 2.d Restart file
#     Normally, we will pick up the file $modID.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 multiwavestart.sh
#
#     Make sure nback is set identically in the preprocessor !!!

  export nback=28

  $USHwave/multiwavestart.sh

  ymdh=`head multiwavestart.out | awk '{ print $1 }'`
  time_ini="`echo $ymdh | cut -c1-8` `echo $ymdh | cut -c9-10`0000"
  rm -f multiwavestart.out

  ymdh=`$utilexec/ndate $off_hour $ymdh`
  ymdh=`$utilexec/ndate $stp_hour $ymdh`
  rdate=`echo $ymdh | cut -c 1-8`
  rcycle=t`echo $ymdh | cut -c 9-10`z

  ymdh=`$utilexec/ndate $stp_hour $YMDH`
  ndate=`echo $ymdh | cut -c 1-8`
  ncycle=t`echo $ymdh | cut -c 9-10`z

  for grdID in $grids
  do
    echo "   Copy restart.$grdID from $com/$RUN.$rdate/$modID.$grdID.$rcycle.restart"
    if [ -f $com/$RUN.$rdate/$modID.$grdID.$rcycle.restart ]
    then
      cp $com/$RUN.$rdate/$modID.$grdID.$rcycle.restart restart.$grdID
    fi

    if [ ! -f restart.$grdID ]
    then
      msg="   restart.$grdID not found (COLD START).  **** WARNING **** "
      postmsg "$jlogfile" "$msg"
      echo "   restart.$grdID not found (COLD START).  **** WARNING **** "
      echo "$modID fcst $date $cycle : $grdID cold start." >> $wavelog
    else
      if [ "$time_ini" != "$time_beg" ]
      then
        echo "$modID fcst $date $cycle : older restart used ($rdate $rcycle)." >> $wavelog
        postmsg "$jlogfile" "OLDER RESTART USED ($grdID $rdate $rcycle)."
      fi
    fi

  done

# 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 "$modID fcst $date $cycle : no buoy locations file." >> $wavelog
  fi

# 2.f Create model input file from template

  if [ -f $FIXwave/multiwavefcst.$modID.tmpl ]
  then
    cp $FIXwave/multiwavefcst.$modID.tmpl multiwavefcst.inp.tmpl
  fi

  if [ ! -f multiwavefcst.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

#     -e "s/FIELDS/F F T T T  T F T T F  T T T T T  T F T F F  T T F F F  F F F F F  F/g" \
  sed -e "s/RUN_BEG/$time_ini/g" \
      -e "s/RUN_END/$time_end/g" \
      -e "s/OUT_BEG/$time_ini/g" \
      -e "s/OUT_END/$time_end/g" \
      -e "s/DTFLD/ 3600/g" \
      -e "s/FIELDS/F F T F F  T F T T F  T T T T T  T F T F F  F F F F F  F F F F F  F/g" \
      -e "s/DTPNT/ 3600/g" \
      -e "/BUOY_FILE/r buoy.loc" \
      -e "s/BUOY_FILE/DUMMY/g" \
      -e "s/RST_TME/$time_rst/g" \
                                     multiwavefcst.inp.tmpl | \
  sed -n "/DUMMY/!p"               > multiwavefcst.inp


  rm -f multiwavefcst.inp.tmpl buoy.loc

# --------------------------------------------------------------------------- #
# 3.  Run the model
# 3.a Check last wind forcing time step and start the gfs
#     incremental script 

  ln -sf times.$wind times.inp 

  date1=`tail -n 1 times.$wind | cut -d ' ' -f 2` 
  date2=`tail -n 1 times.$wind | cut -d ' ' -f 3` 
  ymdh_gfsl="$date1`echo $date2 | cut -c1-2`"

  if [ "$ymdh_gfsl" -lt "$ymdh_end" ]
  then
    ymdh=`$utilexec/ndate 3 $ymdh_gfsl`
    set $setoff
    echo ' '
    echo " Wind data record available till : $ymdh_gfsl"
    echo " Model end time                  : $ymdh_end"
    echo ' Updating wind data ...'
    echo ' '
    set $seton
    ln -sf sst.$wind sst.ww3
    $USHwave/multiwavegfs_inc.sh $ymdh $ymdh_end > gfsinc.out 2>&1  &
  fi

# 3.b Clean up directory of all tag files and start grib2 script

#  rm -f gribdone*
#  rm -f Field_done*
#  rm -f grib2.$cycle.complete

#  export dtgrib=10800
#  export gribflags='F F T F F  T F F F F  T T F F T  T F T F F  F F F F F  F F F F F  F'

#  set $setoff
#  echo ' '
#  echo " Launching grib2 generation script ..."
#  $USHwave/multiwavegrib2_sbs.sh $ymdh_end > grib2sbs.out 2>&1 &

# 3.c Run the wave model

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

  poe $EXECwave/multiwavefcst_sbs
  err=$?

  if [ "$err" != '0' ]
  then
    pgm=wave_fcst
    msg="ABNORMAL EXIT: ERROR IN multiwavefcst"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** FATAL ERROR : ERROR IN multiwavefcst *** '
    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."

# 3.c Check to see if gfs incremental script ended cleanly

  if [ -f $wind.$cycle.complete ]
  then
    set $setoff
    echo ' '
    echo "Wind data extracted succesfully"
    echo ' '
    set $seton
  else
    msg="Possible error in wind data extraction : ERROR IN multiwavegfs_inc.sh"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** ERROR : ERROR IN multiwavegfs_inc.sh *** '
    echo '******************************************** '
    echo ' '
    echo "$modID fcst $date $cycle : problems in gfs data extraction during model run" >> $wavelog
    echo $msg
    set $seton
    sed "s/^/gfsinc.out : /g" gfsinc.out
  fi

# 3.d Wait a while to see if grib2 generation completes cleanly
# How long you wait can be set by grib2_cycle which is multiple of minutes

  grib2_cycle=20
  grib2_inc=1

  while [ "$grib2_inc" -le "$grib2_cycle" ]
  do
    if [ -f "grib2.$cycle.complete" ]
    then
      break
    else
      sleep 60
      grib2_inc=`expr $grib2_inc + 1`
    fi
  done

# remove any remaining grib2 lock files after wait time. This 
# should force a stop in the code for grib2 run

  rm -f grib2.*.lock

  if [ -f grib2.$cycle.complete ]
  then
    set $setoff
    echo ' '
    echo "Grib2 data extracted succesfully"
    echo ' '
    set $seton
  else
    msg="Possible error in grib2 data extraction : ERROR IN exwavemulti_1p_grib2.sh.sms (see corrspnd err file)"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '**************************************************** '
    echo '*** ERROR : ERROR IN exwavemulti_1p_grib2.sh.sms *** '
    echo '**************************************************** '
    echo ' '
    echo "$modID fcst $date $cycle : problems in grib2 data extraction during model run" >> $wavelog
    echo $msg
    set $seton
  fi

  rm -f mod_def.* ice.* multiwavefcst_sbs.inp
  rm -f test*.mww3
  for grdID in $grids
  do
    rm -f Field_done*.$grdID
  done

# --------------------------------------------------------------------------- #
# 4.  Save and clean up files
# 4.a Send files to com if requested

  COMRST=${com}/${RUN}.${ndate}

  if [ "$SENDCOM" = 'YES' ]
  then
    set $setoff
    echo ' '
    echo 'Saving output files :'
    echo '---------------------'
    set $seton

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

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

    if [ -f wind.$wind ] ; then
      echo "   Copying wind.$wind      to $COMOUT/$wind.$cycle.wind"
               mv      wind.$wind         $COMOUT/$wind.$cycle.wind
               mv      times.$wind        $COMOUT/times.$cycle.$wind
    fi

    if [ -f grib2.$cycle.complete ] ; then
      echo "   Copying grib2.$cycle.complete  to $COMOUT/$modID.$cycle.grib2done"
               mv      grib2.$cycle.complete    $COMOUT/$modID.$cycle.grib2done
    fi

    for grdID in $grids
    do

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

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

      if [ -f restart1.$grdID ] ; then
        echo "   Copying restart1.$grdID to $COMRST/$modID.$grdID.$ncycle.restart"
                 mv      restart1.$grdID    $COMRST/$modID.$grdID.$ncycle.restart
      fi

    done

  fi

# 4.b Set up runID and "lock" file

    echo "`hostname -s`" > $COMOUT/where_wave_${modID}_ran.$cycle

# --------------------------------------------------------------------------- #
# 5.  Ending output

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

  msg="Step $step of $job completed normally"
  postmsg "$jlogfile" "$msg"

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