#!/bin/sh
###############################################################################
#                                                                             #
# This script is the preprocessor for the regional NPH wave model that runs   #
# under the global  NWW3 wave model. It sets some shell script variables for  #
# export to child scripts and copies some generally used files to the work    #
# directory. After this the actual preprocessing is performed by the          #
# following child scripts :                                                   #
#                                                                             #
#  wavehur2p.sh   : make blended fields version 2-Pacific                     #
#                                                                             #
# Also used is the utililty script                                            #
#                                                                             #
#  wavestart.sh   : get time of most recent restart file.                     #
#                                                                             #
# Remarks :                                                                   #
# - For non-fatal errors output is witten to the wave.log file.               #
#                                                                             #
#                                                                 April 2003  #
#                                                                             #
###############################################################################
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA

  seton='-xa'
  setoff='+xa'
  set $seton

  msg="HAS BEGUN on `hostname`"
  postmsg "$jlogfile" "$msg"
  msg="Starting NPH PREPROCESSOR SCRIPT"
  postmsg "$jlogfile" "$msg"

  set $setoff
  echo ' '
  echo '                      *******************************'
  echo '                      *** NPH PREPROCESSOR SCRIPT ***'
  echo '                      *******************************'
  echo ' '
  echo "Starting at : `date`"
  set $seton

  export MP_PGMMODEL=mpmd
  export MP_CMDFILE=cmdfile

  lsth=126

  altID=enp

# 0.b Date and time stuff
#     The ending time of the run always is the $lsth hour forecast. The starting
#     time depends on availablility of restart files, and is obtained with
#     wavestart.sh
#
#     Make sure nback is set identically in the forecast script !!!
#     nback is the number of cycles (12-hour cycles) to look back.

  export date=$PDY
  export cycle=t${cyc}z
  export YMDH=${PDY}${cyc}

  export nback=10

  $USHwave/wavestart.sh

  ymdh_beg=`head wavestart.out | awk '{ print $1 }'`
  rm -f wavestart.out
  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"

  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 ' '

# --------------------------------------------------------------------------- #
# 1.  Get files that are used by most child scripts

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

# 1.a Model definition file

  if [ -f $FIXwave/wave_$modID.moddef ]
  then
    cp $FIXwave/wave_$modID.moddef mod_def.ww3
  fi

  if [ -f mod_def.ww3 ]
  then
    echo "   mod_def.ww3 copied ($FIXwave/wave_$modID.moddef)."
  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 $msg
    echo "$modID prep $date $cycle : fixed file(s) missing." >> $wavelog
    set $seton
    err=1;export err;err_chk
  fi

# 1.b Wind preprocessor template file

  if [ -f $FIXwave/waveprep.hur.tmpl ]
  then
    cp $FIXwave/waveprep.hur.tmpl waveprep.inp
  fi

  if [ -f waveprep.inp ]
  then
    echo "   waveprep.inp copied ($FIXwave/waveprep.hur.tmpl)."
  else
    msg="ABNORMAL EXIT: NO FILE waveprep.hur.tmpl"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '************************************** '
    echo '*** FATAL ERROR : NO TEMPLATE FILE *** '
    echo '************************************** '
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : template file missing." >> $wavelog
    set $seton
    err=2;export err;err_chk
  fi

# --------------------------------------------------------------------------- #
# 2.  Gather information from WNA and GFDL
# 2.a GFS preprocessing (take from $altID)

  if [ -f $COMIN/$altID.$cycle.wind ]
  then
    cp $COMIN/$altID.$cycle.wind wind.ww3
  fi

  if [ -f wind.ww3 ]
  then
    echo "   wind.ww3 copied ($COMIN/$altID.$cycle.wind)."
  else
    msg="ABNORMAL EXIT: NO SST FILE"
    postmsg "$jlogfile" "$msg"
    set $setoff
    echo ' '
    echo '******************************************'
    echo '*** FATAL ERROR : NO WNA GFS WIND FILE ***'
    echo '******************************************'
    echo ' '
    echo $msg
    echo "$modID prep $date $cycle : GFS wind file missing." >> $wavelog
    set $seton
    err=3;export err;err_chk
  fi

# 2.b Check for hurricanes in domain

  set $setoff
  echo ' '
  echo 'Checking for hurricanes :'
  echo '-------------------------'
  set $seton

  rm -f name_file

  ymdh_hur=$YMDH

  while [ "$ymdh_hur" -ge "$ymdh_beg" ]
  do
    hdate=`echo $ymdh_hur | cut -c 1-8`
    hcyc="t`echo $ymdh_hur | cut -c 9-10`z"

    files=
    if [ -d $COMGFDL/hur.$ymdh_hur ]
    then
      cd $COMGFDL/hur.$ymdh_hur
      files=`ls *.gfdl_omb_ocean.tar 2> /dev/null | sort `
    fi

    if [ -n "$files" ]
    then
      for file in $files
      do
        stormID=`echo $file | sed 's/\./ /g' | awk '{ print $1 }'`
        lngth=`echo $stormID | wc -c`
        lngth=`expr $lngth - 1`
        basID=`echo $stormID | cut -c$lngth-$lngth`
        if [ "`echo $stormID | cut -c1-6`" != 'invest' ]
        then
          lngth=`expr $lngth - 3`
          stormID=`echo $stormID | cut -c1-$lngth`
        fi
        if [ "$basID" = 'e' ]
        then
          echo "   Found storm $stormID for $hdate $hcyc."
          echo $stormID >> $DATA/name_file
          touch $DATA/data.$ymdh_hur
        else
          echo "   Storm $stormID for $hdate $hcyc out of range."
        fi
      done
    else
      echo "   No storms for $hdate $hcyc."
    fi

    ymdh_hur=`$utilexec/ndate -6 $ymdh_hur`
  done

  cd $DATA

  if [ ! -f name_file ]
  then
    hur_names="GFDL: no data"
    set $setoff
    echo ' '
    echo "   $hur_names"
    echo ' '
    set $seton
  else

# SECTIONS 3 THROUGH 6 ONLY EXECUTED IN CASE OF GFDL DATA !!!!!!!!!!!!!!!!!!!!!

    hur_names="GFDL:"
    for stormID in `sort -u name_file`
    do
      hur_names="$hur_names $stormID"
    done
    set $setoff
    echo ' '
    echo "   $hur_names"
    set $seton

    rm -f name_file

# --------------------------------------------------------------------------- #
# 3.  We have hurricanes or TD's, make command file for wind generation
# 3.a Command file set-up
#     The command file points to $nfile files named cmdfile.$ifile.
#     The actual work is distributed over these files.

    ifile=1
    nfile=`echo $LOADL_PROCESSOR_LIST | wc -w | awk '{ print $1}'`
    iskip=1

    if [ "$nfile" -gt '1' ]
    then
      cmdtype='poe'
    else
      cmdtype='sh'
      nskip='-'
    fi

    set $setoff
    echo ' '
    echo 'Setting up parallel processing :'
    echo '--------------------------------'
    echo '   Making command file(s)'
    echo "   Set up command file structure (type = $cmdtype)."
    echo "      Number of command files  : $nfile"
    set $seton

    while [ "$ifile" -le "$nfile" ]
    do
      touch cmdfile.$ifile
      chmod 700 cmdfile.$ifile
      echo "cmdfile.$ifile" >> cmdfile
      ifile=`expr $ifile + 1`
    done

    ifile=1

# 3.b Blended wind fields, hindcasts

    ymdh=$ymdh_beg

    while [ "$ymdh" -lt "$YMDH" ]
    do
      if [ -f data.$ymdh ]
      then
        fhs='0 1 2 3 4'
        rm -f data.$ymdh
      else
        fhs='0 3'
      fi
      for fh in $fhs
      do
        ymdh_hur=`$utilexec/ndate $fh $ymdh`
        echo "$USHwave/wavehur2p.sh $ymdh_hur > hur_$ymdh_hur.out 2>&1"  >> cmdfile.$ifile

        ifile=`expr $ifile + 1`
        if [ "$ifile" -gt "$nfile" ]
        then
          ifile=1
        fi
      done
      ymdh=`$utilexec/ndate 6 $ymdh`
    done

# 3.b Blended wind fields, forecasts

    ymdh=$YMDH
    if [ -f data.$ymdh ]
    then
      step=1
      rm -f data.$ymdh
    else
      step=3
    fi

    while [ "$ymdh" -le "$ymdh_end" ]
    do
      echo "$USHwave/wavehur2p.sh $ymdh > hur_$ymdh.out 2>&1"  >> cmdfile.$ifile
      ymdh=`$utilexec/ndate $step $ymdh`

      ifile=`expr $ifile + 1`
      if [ "$ifile" -gt "$nfile" ]
      then
        ifile=1
      fi
    done

# --------------------------------------------------------------------------- #
# 4   Execute command file
# 4.a Execution

    set $setoff
    echo "   Executing command file."
    set $seton

    if [ "$nfile" -gt '1' ]
    then
      poe
      exit=$?
    else
      cmdfile.1
      exit=$?
    fi

# 4.b Error trap on poe or shell

#    if [ "$exit" != '0' ]
#    then
#      msg="ABNORMAL EXIT: ERROR IN $cmdtype"
#      postmsg "$jlogfile" "$msg"
#      set $setoff
#      echo ' '
#      echo '**************************************** '
#      echo '*** FATAL ERROR : ERROR IN poe OR sh *** '
#      echo '**************************************** '
#      echo ' '
#      echo "$modID prep $date $cycle : error in $cmdtype." >> $wavelog
#      echo $msg
#      set $seton
#      err=3;export err;err_chk
#    fi

# 4.c Timing info on steps

    set $setoff
    echo ' '
    echo "   Ending times for sepate processors :"

    for file in `cat cmdfile`
    do
      if [ "`wc $file | awk '{ print $1 }'`" = '0' ]
      then
        echo "      $file : no commands in this file."
      else
        words="`tail -1 $file | wc | awk '{ print $2 }'`"
        wrdnr=`expr $words - 1`
        echo "{ print "'$'"$wrdnr }" > awkfile
        outfile="`tail -1 $file | awk -f awkfile`"
        rm -f awkfile
        echo "      $file : `tail -1 $outfile`"
      fi
    done
    set $seton

# --------------------------------------------------------------------------- #
# 5.  Check for errors

    set $setoff
    echo ' '
    echo '   Checking for errors.'
    set $seton

# 5.a Blended wind fields
#     We will go on if the number of errors in files is less
#     than err_max
#     Note that not all files need to be there ....

    err_max=0

    echo '      Blended wind fields :'
    set $seton

    ymdh=$ymdh_beg
    step=1
    nr_err=0

    while [ "$ymdh" -le "$ymdh_end" ]
    do
      if [ -d hur_${ymdh} ]
      then
        echo "         File for $ymdh : error in wavehur2p.sh"
        postmsg "$jlogfile" "    File for $ymdh : error in wavehur2p.sh"
        nr_err=`expr $nr_err + 1`
        rm -f ghur.$ymdh
      else
        if [ -f hur_${ymdh}.out ]
        then
          hurfile=`grep 'GFDL run(s)' hur_${ymdh}.out`
          if [ -z "$hurfile" ]
          then
            echo "         File for $ymdh : cannot identify source"
            nr_err=`expr $nr_err + 1`
            rm -f ghur.$ymdh
          else
            if [ ! -f ghur.$ymdh ]
            then
              echo "         File for $ymdh : file not found"
              nr_err=`expr $nr_err + 1`
            else
              echo "$hurfile"
              rm -f hur_${ymdh}.out
            fi
          fi
        fi
      fi
      ymdh=`$utilexec/ndate $step $ymdh`
    done

    if [ -f hur_*.out ]
    then
      set $setoff
      echo ' '
      echo '*********************************'
      echo '*** ERROR OUTPUT wavehur2p.sh ***'
      echo '*********************************'
      echo '            Possibly in multiple calls'
      echo "$modID prep $date $cycle : error in hurricane files." >> $wavelog
      for file in hur_*.out
      do
        echo ' '
        sed "s/^/$file : /g" $file
      done
      rm -f hur_*.out
      set $seton
      postmsg "$jlogfile" "NON-FATAL ERROR in wavehur2p.sh, possibly in multiple calls."
    fi

    if [ "$nr_err" -gt "$err_max" ]
    then
      msg="ABNORMAL EXIT: ERROR(S) IN BLENDED FILES"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '*********************************************** '
      echo '*** FATAL ERROR : ERROR(S) IN BLENDED FILES *** '
      echo '*********************************************** '
      echo ' '
      echo $msg
      echo "$modID prep $date $cycle : fatal error in hurricane files." >> $wavelog
      set $seton
      err=5;export err;err_chk
    fi

    rm -f cmdfile*

# --------------------------------------------------------------------------- #
# 6.  Combine individual fields and preprocess


    set $setoff
    echo ' '
    echo 'Combine individual blended wind fields :'
    echo '----------------------------------------'
    echo '   Combine into single file ...'
    set $seton

# 6.a Get into single file

    rm -f gfs.wind

    for field in `ls ghur.*`
    do
      cat $field >> gfs.wind
    done

    rm -f wind.ww3
    rm -f ghur.*

# 6.b Run waveprep

    set $setoff
    echo ' '
    echo '   Running wind fields through preprocessor.'
    set $seton

    $EXECwave/waveprep
    err=$?

    if [ "$err" != '0' ]
    then
      msg="ABNORMAL EXIT: ERROR IN waveprep"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      echo '*************************************** '
      echo '*** FATAL ERROR : ERROR IN waveprep *** '
      echo '*************************************** '
      echo ' '
      echo "$modID prep $date $cycle : error in waveprep." >> $wavelog
      set $seton
      err=8;export err;err_chk
    fi

    if [ ! -f wind.ww3 ]
    then
      msg="ABNORMAL EXIT: FILE wind.ww3 MISSING"
      postmsg "$jlogfile" "$msg"
      set $setoff
      echo ' '
      cat waveprep.out
      echo ' '
      echo '****************************************'
      echo '*** FATAL ERROR : wind.ww3 NOT FOUND ***'
      echo '****************************************'
      echo ' '
      echo "$modID prep $date $cycle : wind.ww3 missing." >> $wavelog
      set $seton
      err=9;export err;err_chk
    fi

    rm -f gfs.wind
    rm -f waveprep.inp

  fi

  rm -f mod_def.ww3
  rm -f *.tmpl

# --------------------------------------------------------------------------- #
# 6.  Output to /com

  if [ "$SENDCOM" = 'YES' ]
  then

    echo "$hur_names" > $COMOUT/what_wavenph_used.$cycle 

    set $setoff
    echo ' '
    echo "   Saving wind.ww3 as $COMOUT/$modID.$cycle.wind"
    set $seton
    cp wind.ww3 $COMOUT/$modID.$cycle.wind

    chmod 664 $COMOUT/$modID.$cycle.*
    chmod 664 $COMOUT/what_wavenph_used.$cycle

    $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB $job $COMOUT/what_wavenph_used.$cycle

  fi 

# --------------------------------------------------------------------------- #
# 7.  Alert the wind file

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

# --------------------------------------------------------------------------- #
# 8.  Ending output

  set $setoff
  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                     *** End of NPH preprocessor ***'
  echo ' '

  msg="$job completed normally"
  postmsg "$jlogfile" "$msg"

# End of NPH preprocessor script -------- ----------------------------------- #
