#!/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.                   +
#                                                                              +
# April, 2005                                                                  +
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# 0.  Preparations
# 0.a Basic modes of operation
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
  set -x

  echo ' '
  echo '                   ***************************************'
  echo '                   *** ens ensemb POSTPROCESSOR SCRIPT ***'
  echo '                   ***************************************'
  echo ' '
  echo "Starting at : `date`"
#
# 0.b Date and time stuff
#
  export YMD=$PDY
  export YMDH=${PDY}${cyc}
  export tcycz=t${cyc}z
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# 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_ens_buoy.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
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
#hour='0 6 12 18 24 30 36 42 48 54 60 66 72 78 84'
#  end_hour='84'
  end_hour='126'
  dh='6'
  memb='01 02 03 04 05 06 07 08 09 10 cn'
#para='WIND WDIR HTSGW WVDIR WVPER PERPW DIRPW'  
  para='WIND HTSGW PERPW'  
#Beaufort Wind Scale, from Force 2 t 10.  And corresponding wave heights.
  uscale='3.60 5.65 8.74 11.31 14.39 17.48 21.07 24.67'
  hscale='0.60 1.00 2.00  3.00  4.00  5.50  7.00  9.00'
##  tpscale='5.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0'
  tpscale='5.0 7.0 9.0 11.0 13.0 15.0 17.0 19.0'
  tmscale=''
  tsscale=''

  for me in $memb
  do
#
    if [ $me = 'cn' ] ; then
      if [ -f $COMIN_PROD/nww3.t${cyc}z.grib ] ; then
        cp  $COMIN_PROD/nww3.t${cyc}z.grib  $me.grib
        echo " $COMIN_PROD/nww3.t${cyc}z.grib copied to $me.grib."
      else
        msg="ABNORMAL EXIT: ERR in coping nww3.t${cyc}z.grib."
        postmsg "$jlogfile" "$msg"
#        set $setoff
        echo ' '
        echo '***************************************************** '
        echo "*** ERR : No $COMIN_PROD/nww3.$cycle.grib copied. *** "
        echo '***************************************************** '
        echo ' '
        echo "$COMIN_PROD/nww3.t${cyc}z.grib missing." >> $ensemb_log
        #set $seton
        err=1;export err;err_chk
      fi
    else
      if [ -f $COMIN/$ENS${me}.t${cyc}z.grib ] ; then
        cp  $COMIN/$ENS${me}.t${cyc}z.grib  $me.grib
        echo " $COMIN/$ENS${me}.t${cyc}z.grib copied to $me.grib."
      else
        msg="ABNORMAL EXIT: ERR in coping $ENS$me.t${cyc}z.grib."
        postmsg "$jlogfile" "$msg"
#        set $setoff
        echo ' '
        echo '******************************************************* '
        echo "*** ERR : No $COMIN/$ENS$me.t${cyc}z.grib copied. *** "
        echo '******************************************************* '
        echo ' '
        echo "$COMIN/$ENS$me.t${cyc}z.grib missing." >> $ensemb_log
        #set $seton
        err=1;export err;err_chk
      fi
    fi
#
  done
#
  rm -f mean.t${cyc}z.grib
#
# forecast hour loop.
#
  fhour=0
#
  while [ "$fhour" -le "$end_hour" ]
  do
#
    if [ $fhour -eq 0 ] ; then
      ihr='anl'
      hhh='000'
    elif [ $fhour -lt 10 ] ; then
      ihr=${fhour}hr
      hhh='00'$fhour
    elif [ $fhour -lt 100 ] ; then
      ihr=${fhour}hr
      hhh='0'$fhour
    elif [ $fhour -ge 100 ] ; then
      ihr=${fhour}hr
      hhh=$fhour
    fi
#
    for ip in $para
    do
#
      scale='     '
      case $ip in
        WIND)    scale=$uscale ; id_para='32'   ;;
        HTSGW)   scale=$hscale ; id_para='100'  ;;
        WVPER)   scale=$tmscale; id_para='103'  ;;
        PERPW)   scale=$tpscale; id_para='108'  ;;
        PERSW)   scale=$tsscale; id_para='110'  ;;
        *)       scale=$tsscale; id_para='   '  ;;
      esac
#
      rm -f fname_input data_* 
#
      echo $YMDH $hhh $ip $id_para  > fname_input
      echo $memb | wc -w            >> fname_input
      echo $memb                    >> fname_input
      echo $scale | wc -w           >> fname_input
      echo $scale                   >> fname_input
#
      export WGRIB=/nwprod/util/exec/wgrib
      for im in $memb
      do
        infile=$im.grib
        $WGRIB -s $infile | grep ":${ip}:sfc:${ihr}" | \
            $WGRIB -i $infile -o data_$im
        ok1=$?
        if [ $ok1 -ne 0 ] ; then
          echo " *** ERROR : ip=$ip, im=$im, ok1=$ok1"
          exit
        fi
        echo data_$im       >> fname_input
      done
#
# execute ensemb.x
#
      rm -f station_out mean_out spread_out probab_out test_out
#
      $EXECwave/wave_ensemble  <fname_input >>$pgmout 2>&1
#
##      cp station_out ${ip}.station.t${cyc}z.f$hhh
##      cp mean_out    ${ip}.mean.t${cyc}z.f$hhh
##      cp spread_out  ${ip}.spread.t${cyc}z.f$hhh
##      cp probab_out  ${ip}.probab.t${cyc}z.f$hhh
      cat ./station_out >> station.t${cyc}z.text
      cat ./mean_out   >> mean.t${cyc}z.grib
      cat ./spread_out >> spread.t${cyc}z.grib
      cat ./probab_out >> probab.t${cyc}z.grib
#
    done
    fhour=`expr $fhour + $dh`
  done
#
  cp  station.t${cyc}z.text $COMOUT/.
  cp  mean.t${cyc}z.grib    $COMOUT/.
  cp  spread.t${cyc}z.grib  $COMOUT/.
  cp  probab.t${cyc}z.grib  $COMOUT/.
#
  compress $COMOUT/station.t${cyc}z.text
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
# END
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --+ + + ++
#
