#!/bin/sh
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# This script is the ensemble postprocessor for the global ens wave model.  It +
# packs ensemble mean and spread in grib form.  It runs in serial mode and in  +
# its own directory. The output are copied to the output directory.            +
#                                                                              +
# For non-fatal errors output is witten to the ens.log file.                   +
#                                                                              +
# Feb, 2008                                                                    +
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# 0.  Preparations
# 0.a Basic modes of operation
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  set -x

  echo ' '
  echo '                   ******************************************'
  echo '                   *** WAVE ENSEMBLE POSTPROCESSOR SCRIPT ***'
  echo '                   ******************************************'
  echo ' '
  echo "Starting at : `date`"

  export MP_PGMMODEL=mpmd
  export MP_CMDFILE=cmdfile
#
# 0.b Date and time stuff
#
  export YMD=$PDY
  export YMDH=${PDY}${cyc}

  export grdID='glo_60m'
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# 1. Get files.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
# buoy input files exist?
#
  if [ -f $FIXwave/wave_ens_buoy.data ] ; then
    cp  $FIXwave/wave_ens_buoy.data  buoy_file.data
    echo " $FIXwave/wave_ens_buoy.data copied to buoy_file.data."
  else
    msg="ABNORMAL EXIT: ERR in coping ens_buoy_file.data."
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '***************************************************** '
    echo "*** ERR : No $FIXwave/wave_mens.data  copied. *** "
    echo '***************************************************** '
    echo ' '
    echo "$FIXwave/wave_ens_buoy.data  missing." >> $ensemb_log
    #set $seton
    err=1;export err;err_chk
  fi
#
  if [ -f $FIXwave/wave_ens_buoy.ratio ] ; then
    cp  $FIXwave/wave_ens_buoy.ratio  buoy_ratio.data
    echo " $FIXwave/wave_ens_buoy.ratio copied to buoy_ratio.data."
  else
    msg="ABNORMAL EXIT: ERR in coping ens_buoy_file.data."
    postmsg "$jlogfile" "$msg"
#    set $setoff
    echo ' '
    echo '***************************************************** '
    echo "*** ERR : No $FIXwave/wave_ens_buoy.ratio  copied.*** "
    echo '***************************************************** '
    echo ' '
    echo "$FIXwave/wave_ens_buoy.ratio  missing." >> $ensemb_log
    #set $seton
    err=1;export err;err_chk
  fi
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#

    end_hour='240'

    dh='6'

    export memb='cntl 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20'

  for me in $memb
  do
#
    if [ $me = 'cntl' ] ; then
      if [ -f $COMIN/$modID.$grdID.t${cyc}z.grib2 ] ; then
        cp  $COMIN/$modID.$grdID.t${cyc}z.grib2  $me.grib2
        echo " $COMIN/$modID.$grdID.t${cyc}z.grib2 copied to $me.grib2."
      else
        msg="ABNORMAL EXIT: ERR in coping $modID.$grdID.t${cyc}z.grib2."
        postmsg "$jlogfile" "$msg"
        echo '***************************************************** '
        echo "*** ERR : No $COMIN/nww3.$cycle.grib2 copied. *** "
        echo '***************************************************** '
        echo "$COMIN/$modID.$grdID.t${cyc}z.grib missing." >> $ensemb_log
        #set $seton
        err=1;export err;err_chk
      fi
    else
      if [ -f $COMIN/$ENS${me}.$grdID.t${cyc}z.grib2 ] ; then
        cp  $COMIN/$ENS${me}.$grdID.t${cyc}z.grib2  $me.grib2
        echo " $COMIN/$ENS${me}.$grdID.t${cyc}z.grib2 copied to $me.grib2."
      else
        msg="ABNORMAL EXIT: ERR in coping $ENS$me.$grdID.t${cyc}z.grib2."
        postmsg "$jlogfile" "$msg"
        echo ' '
        echo '******************************************************* '
        echo "*** ERR : No $COMIN/$ENS$me.$grdID.t${cyc}z.grib2 copied. *** "
        echo '******************************************************* '
        echo ' '
        echo "$COMIN/$ENS$me.$grdID.t${cyc}z.grib2 missing." >> $ensemb_log
        err=1;export err;err_chk
      fi
    fi
#
  done
#

# --------------------------------------------------------------------------- #
# 2.  Make command file(s)
# 2.a Command file set-up
#     The command file points to $nfile files named cmdfile.$ifile.
#     The actual work is distributed over these files.

  echo ' '
  echo 'Generate ensemble data :'
  echo '-----------------------'


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

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

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

  rm -f cmdfile
  rm -f cmdfile.*

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

  ifile=1

# 2.b Ensemble Statistics
# 2.b.i Loop through the forecast hours

  fhour=0

  while [ "$fhour" -le "$end_hour" ]
  do

    if [ $fhour -eq 0 ] ; then
      ihr='anl'
      hhh='000'
    elif [ $fhour -lt 10 ] ; then
      ihr=${fhour}' hour'
      hhh='00'$fhour
    elif [ $fhour -lt 100 ] ; then
      ihr=${fhour}' hour'
      hhh='0'$fhour
    elif [ $fhour -ge 100 ] ; then
      ihr=${fhour}' hour'
      hhh=$fhour
    fi

    echo "$USHwave/multiwave_ensemble.sh $hhh '$ihr' > ens_$hhh.out 2>&1" \
                                           >> cmdfile.$ifile

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

    fhour=`expr $fhour + $dh`

  done

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

  echo "   Executing command file at `date`."

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

# 3.c Timing info on steps

  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

# --------------------------------------------------------------------------- #
# 4.  Check for errors

  echo ' '
  echo '   Checking for errors (error output concatenated below).'

  if [ -d ens_* ]
  then
    echo " Errors in creating ensemble statistics "
  else
    rm -f ens_*.out
    echo " Ensemble statistics successful "
  fi

  echo ' '

  if [ -f ens_*.out ]
  then
    echo '******************************************'
    echo '*** ERROR OUTPUT multiwave_ensemble.sh ***'
    echo '        (Possibly multiple calls)     '
    echo '******************************************'
    echo ' '
    echo "$modID ensemble $date $cycle : Error in generating ensemble statistics" >> $ensemb_log

    for file in ens_*.out
    do
      sed "s/^/$file : /g" $file
      rm -f $file
      echo ' '
    done
  fi
 
# --------------------------------------------------------------------------- #
# 5. Concatenate files
 
  rm -f mean.$cycle.grib
  rm -f spread.$cycle.grib
  rm -f probab.$cycle.grib

  fhour=0
  while [ "$fhour" -le "$end_hour" ]
  do
    if [ $fhour -eq 0 ] ; then
      hhh='000'
    elif [ $fhour -lt 10 ] ; then
      hhh='00'$fhour
    elif [ $fhour -lt 100 ] ; then
      hhh='0'$fhour
    elif [ $fhour -ge 100 ] ; then
      hhh=$fhour
    fi

    if [ -f station.$cycle.text.$hhh ]
    then
      cat station.$cycle.text.$hhh >> station.$cycle.text
      rm -f station.$cycle.text.$hhh
    else
      echo " Error: Could not find file station.$cycle.text.$hhh"
    fi

    if [ -f mean.$cycle.grib.$hhh ]
    then
      cat mean.$cycle.grib.$hhh >> mean.$cycle.grib
      rm -f mean.$cycle.grib.$hhh
    else
      echo " Error: Could not find file mean.$cycle.grib.$hhh"
    fi

    if [ -f spread.$cycle.grib.$hhh ]
    then
      cat spread.$cycle.grib.$hhh >> spread.$cycle.grib
      rm -f spread.$cycle.grib.$hhh
    else
      echo " Error: Could not find file spread.$cycle.grib.$hhh"
    fi

    if [ -f probab.$cycle.grib.$hhh ]
    then
      cat probab.$cycle.grib.$hhh >> probab.$cycle.grib
      rm -f probab.$cycle.grib.$hhh
    else
      echo " Error: Could not find file probab.$cycle.grib.$hhh"
    fi

    fhour=`expr $fhour + $dh`

  done

# --------------------------------------------------------------------------- #
# 6. Convert to grib2 and move files

   $utilexec/cnvgrib -g12 mean.t${cyc}z.grib mean.t${cyc}z.grib2
   $utilexec/cnvgrib -g12 spread.t${cyc}z.grib spread.t${cyc}z.grib2
   $utilexec/cnvgrib -g12 probab.t${cyc}z.grib probab.t${cyc}z.grib2

  if [ "$SENDCOM" = 'YES' ]
  then
    cp  mean.t${cyc}z.grib*    $COMOUT/.
    cp  spread.t${cyc}z.grib*  $COMOUT/.
    cp  probab.t${cyc}z.grib*  $COMOUT/.
    cp  station.t${cyc}z.text $COMOUT/.

    if [ $SENDDBN = YES ]; then
      $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2 $job $COMOUT/mean.t${cyc}z.grib2
      $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2 $job $COMOUT/spread.t${cyc}z.grib2
      $DBNROOT/bin/dbn_alert MODEL WAVE_GRIB_GB2 $job $COMOUT/probab.t${cyc}z.grib2
    fi

    compress $COMOUT/station.t${cyc}z.text
  fi

# --------------------------------------------------------------------------- #
# 7. Ending output

  echo ' '
  echo "Ending at : `date`"
  echo ' '
  echo '                 *** End of MENS ensemble postprocessor ***'
  echo ' '
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# END
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
