#!/bin/sh
set -xa
###############################################################################
#                                                                             #
# This script is the preprocessor for the global ens wave model.              #
# It sets some shell script variables for export to child scripts and         #
# copies the following files to the work directory,                           #
#                                                                             #
#   mod_def.ww3                                                               #
#   waveprep.gfs.tmpl                                                         #
###   ice.ww3                                                                   #
#   sst.ww3                                                                   #
###   restart.ww3                                                               #
#                                                                             #
# After this the actual preprocessing is performed by the following child     # 
# script:                                                                     #
#                                                                             #
#   wavesig.sh     : find and copy sigma files.                               #
#                                                                             #
# Remarks :                                                                   #
# - The sst script generates files that are needed in the wind script.        #
# - For non-fatal errors output is witten to the ${ENS}${ELEM}.log (wave.log) #
#   file.                                                                     #
#                                                                             #
#                                                            December, 2003   #
#                                                                             #
###############################################################################
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#  Preparations
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
#  Basic modes of operation
#
#  copy all needed files for prep step
  echo ' '
  echo '                      ********************************'
  echo '                      *** NWW3ens PREPROCESSOR SCRIPT ***'
  echo '                      ********************************'
  echo ' '
  echo "Starting at : `date`"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  export GRID="0.  358.75  288   -78.  78.  157"
  export MP_PGMMODEL=mpmd
#  lsth=84
  lsth=126
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Date and time stuff:
#   The starting time is given as in $PDY$cycle. 
#   The ending time of the run is always the $lsth hour forecast. 
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  ymd=$PDY
  ymdh=$PDY$cyc
  hh=$cyc
#
  echo "  ymdh=$ymdh"
  echo "  ymd=$ymd"
  echo "  hh=$hh"
  echo "  cycle=$cycle"
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  ymdh_beg=`echo $ymdh`
  time_beg="`echo $ymdh_beg | cut -c1-8` `echo $ymdh_beg | 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"
#
  echo ' '
  echo 'Times in wave model format :'
  echo '----------------------------'
  echo "   date / cycle  : $PDY $cyc"
  echo "   starting time : $time_beg"
  echo "   ending time   : $time_end"
  echo ' '
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  echo 'Preparing input files :'
  echo '-----------------------'
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Get files that are used by most child scripts
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Model definition file
#
  if [ -f $FIXwave/wave_$modID.moddef ] 
  then
    cp  $FIXwave/wave_$modID.moddef  mod_def.ww3
    echo "   mod_def.ww3 copied ($FIXwave/wave_$modID.moddef)."
  else
    msg="ABNORMAL EXIT: ERR in coping mod_def.ww3 file"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '*********************************** '
    echo '*** ERR : No mod_def.ww3 copied *** '
    echo '*********************************** '
    echo ' '
    echo "$FIXwave/wave_$modID.moddef missing." >> $wavelog
    err=1;export err;err_chk
  fi
#
# Wind preprocessor template file
#
  if [ -f $FIXwave/waveprep.gfs.tmpl ] 
  then
    cp  $FIXwave/waveprep.gfs.tmpl  .
    echo "   waveprep.gfs.tmpl copied ($FIXwave/waveprep.gfs.tmpl)."
  else
    msg="ABNORMAL EXIT: ERR in coping gfs.tmpl file"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '********************************** '
    echo '*** ERR : No waveprep.gfs.tmpl *** '
    echo '********************************** '
    echo ' '
    echo "$FIXwave/waveprep.gfs.tmpl missing." >> $wavelog
    err=2;export err;err_chk
  fi
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# copy ice file.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
##  if [ -f $COMIN/${modID}.$cycle.ice ] 
##  then
##    cp  $COMIN/${modID}.$cycle.ice  ice.ww3
##    echo "   ice.ww3 copied ($COMIN/${modID}.$cycle.ice)."
##  else
##    msg="ABNORMAL EXIT: ERR in coping ice.ww3 file"
##    postmsg "$jlogfile" "$msg"
##    echo ' '
##    echo '*************************************** '
##    echo "*** ERR : No ${modID}.$cycle.ice file *** "
##    echo '*************************************** '
##    echo ' '
##    echo "No $COMIN/${modID}.$cycle.ice " >> $wavelog
##    err=3;export err;err_chk
##  fi
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# copy sst file.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  if [ -f $COMIN/${modID}.$cycle.sst ] 
  then
    cp  $COMIN/${modID}.$cycle.sst  sst.ww3
    echo "   sst.ww3 copied ($COMIN/${modID}.$cycle.sst)."
##
## update backup sst.ww3, mainly for general run if no sst data.
##
##    echo $ymdh > ${HOMEwave}/ensdata_fd/sst.ymdh
##    cp  $COMIN/${modID}.$cycle.sst  ${HOMEwave}/ensdata_fd/sst.ww3
##
  else
    msg="ABNORMAL EXIT: ERR in coping sst.ww3 file"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '***************************************** '
    echo "*** ERR : No ${modID}.$cycle.sst file *** "
    echo '***************************************** '
    echo ' '
    echo "No $COMIN/${modID}.$cycle.sst " >> $wavelog
##    err=4;export err;err_chk
# Get the file from fix field
    if [ -f ${FIXwave}/sst.ww3 ]
    then
      cp  ${FIXwave}/sst.ww3  sst.ww3
#      echo "${FIXwave}/sst.ww3 copied " >> $wavelog

    else
      echo ' '
      echo "No ${FIXwave}/sst.ww3 either" >> $wavelog
      err=4;export err;err_chk
    fi
##
  fi
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# copy restart file.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
##  if [ -f ${COMIN}/${modID}.$cycle.next_restart ] 
##  then
##    cp  ${COMIN}/${modID}.$cycle.next_restart  restart.ww3
##    echo "   restart.ww3 copied (${COMIN}/${modID}.$cycle.next_restart)."
##  else
##    msg="ABNORMAL EXIT: ERR in coping restart.ww3 file"
##    postmsg "$jlogfile" "$msg"
##    echo ' '
##    echo '**************************************************** '
##    echo "*** ERR : No ${modID}.$cycle.next_restart file *** "
##    echo '**************************************************** '
##    echo ' '
##    echo "No ${COMIN}/${modID}.$cycle.next_restart " >> $wavelog
##    err=5;export err;err_chk
##  fi
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# copy sigma wind files.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  fymdh0=${ymdh_beg}
  fhour=0
  fymdh=${fymdh0}
  while [ "$fhour" -le "$lsth" ]
  do 
    if [ "$fhour" -eq "0" ] 
    then
      sfHH="sanl"
      #sfHH="sf00"
    elif [ "$fhour" -lt "10" ] 
    then
      sfHH="sf0$fhour"
    else
      sfHH="sf$fhour"
    fi
#
    if [ -f $COMINens/$modIE.$cycle.$sfHH ] 
    then
      cp  $COMINens/$modIE.$cycle.$sfHH  swnd.$fymdh
      echo "   swnd.$fymdh copied ($COMINens/$modIE.$cycle.$sfHH)."
    elif [ -f $COMINens_init/$modIE.$cycle.$sfHH ] 
    then
      cp  $COMINens_init/$modIE.$cycle.$sfHH  swnd.$fymdh
      echo "   swnd.$fymdh copied ($COMINens/$modIE.$cycle.$sfHH)."
    else
      msg="ABNORMAL EXIT: ERR in coping $modIE file"
      postmsg "$jlogfile" "$msg"
      echo ' '
      echo '***************************************** '
      echo "*** ERR : No $COMINens/$modIE.$cycle.$sfHH file *** "
      echo '***************************************** '
      echo ' '
      echo "No $COMINens/$modIE.$cycle.$sfHH " >> $wavelog
      err=6;export err;err_chk
    fi
#
    fhour=`expr $fhour + 6`
    fymdh=`$utilexec/ndate $fhour $fymdh0`
  done
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Convert sigma files to final wind input file
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  echo ' '
  echo '   Extract wind fields from spectral files ...'
#
# Get into single file (${modID}gfs)
#
  echo "$time_beg $time_end" | $EXECwave/wave_${modID}gfs 
  err=$?
#
  if [ "$err" != '0' ] 
  then
    msg="ABNORMAL EXIT: ERROR IN ${modID}gfs"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '****************************************** '
    echo "*** FATAL ERROR : ERROR IN ${modID}gfs *** "
    echo '****************************************** '
    echo ' '
    echo "$modIE prep $ymd $cycle : error in ${modID}gfs." >> $wavelog
    err=7;export err;err_chk
  fi 
#
  if [ ! -f gfs.wind ] 
  then
    msg="ABNORMAL EXIT: FILE gfs.wind MISSING"
    postmsg "$jlogfile" "$msg"
    echo ' '
    cat ${modID}gfs.out
    echo ' '
    echo '****************************************'
    echo '*** FATAL ERROR : gfs.wind NOT FOUND ***'
    echo '****************************************'
    echo ' '
    echo "$modIE prep $ymd $cycle : gfs.wind missing." >> $wavelog
    err=8;export err;err_chk
  fi
#
  rm -f sst.ww3
  rm -f swnd.*
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Run waveprep
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  echo ' '
  echo '   Running wind fields through preprocessor.'
#
  sed "s/GRIDLAYOUT/$GRID/g" waveprep.gfs.tmpl > waveprep.inp
  rm -f waveprep.gfs.tmpl
#
  $EXECwave/waveprep
  err=$?
#
  if [ "$err" != '0' ] 
  then
    msg="ABNORMAL EXIT: ERROR IN waveprep"
    postmsg "$jlogfile" "$msg"
    echo ' '
    echo '*************************************** '
    echo '*** FATAL ERROR : ERROR IN waveprepens *** '
    echo '*************************************** '
    echo ' '
    echo "$modIE prep $ymd $cycle : error in waveprepens." >> $wavelog
    err=9;export err;err_chk
  fi
#
  if [ ! -f wind.ww3 ]
  then
    msg="ABNORMAL EXIT: FILE wind.ww3 MISSING"
    postmsg "$jlogfile" "$msg"
    echo ' '
    cat waveprep.out
    echo ' '
    echo '****************************************'
    echo '*** FATAL ERROR : wind.ww3 NOT FOUND ***'
    echo '****************************************'
    echo ' '
    echo "$modIE prep $ymd $cycle : wind.ww3 missing." >> $wavelog
    err=10;export err;err_chk
  fi
#
  rm -f gfs.wind
  rm -f waveprep.inp
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Output to /com
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  if [ "$SENDCOM" = 'YES' ] 
  then
    echo ' '
    echo "   Saving wind.ww3 as $COMOUT/$modIE.$cycle.wind"
    cp wind.ww3 $COMOUT/$modIE.$cycle.wind
  fi 
#
  chmod 664 $COMOUT/$modIE.$cycle.*
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Alert the wind file
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
 if [ $SENDDBN = YES ] ; then
    $DBNROOT/bin/dbn_alert MODEL WAVE_BINARY $job $COMOUT/$modIE.$cycle.wind
 fi
# 
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# Ending output
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo "                     *** End of $modIE preprocessor ***"
  echo ' '
#
  msg="$job completed normally"
  postmsg "$jlogfile" "$msg"
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# End of $modIE preprocessor script.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#

