#!/bin/sh
#
#  UTILITY SCRIPT NAME :  mkgefs_wafs.sh
#               AUTHOR :  Boi Vuong
#         DATE WRITTEN :  10/17/2006
#
#  Abstract:  This utility script uses COPYGB to interpolate to WAFS
#             (grid 37-44) from the Ensemble model (Grid 3) and uses
#             MKWMOENS to add a WMO header.
#
#     Input:  1 argument is passed to this script.
#             1st argument is the grid ID - format of 2I
#
#

echo "------------------------------------------------------------"
echo "                 Ensemble WAFS processing                   "
echo "------------------------------------------------------------"
echo "History: FEB 2002 - First implementation of this new script."
echo "AUTHOR: Boi Vuong                                           "
echo "************************************************************"

########################################
msg="Begin job for $job"
postmsg "$jlogfile" "$msg"
########################################

gid="$1"
num=$#

if test $num -eq 1
then
   echo ""
   echo " Appropriate number of arguments were passed"
   echo ""
   set -x
   export EXECutil=${EXECutil:-/nwprod/util/exec}
   export PARMutil=${PARMutil:-/nwprod/util/parm}
   export envir=${envir:-prod}
   export cycle=${cycle:-t${cyc}z}
   export NET=${NET:-mrf}
   export COMIN=${COMIN:-/com/gens/prod/gefs.${PDY}/${cyc}/pgrba}
   export COMOUT=${COMOUT:-/com/gens/prod/gefs.${PDY}/${cyc}/wafs}
   export pcom=${pcom:-/pcom/ens}
   export SENDCOM=${SENDCOM:-NO}
   export SENDDBN=${SENDDBN:-NO}
else
   echo ""
   echo " Number of arguments were not passed "
   echo ""
   echo "   This script creates WAFS (grids 37-44)"
   echo "   from global ensemble data."
   echo ""
   echo "     Usage:  mkenswafs.sh \"37 38\" "
   echo ""
   exit 16
fi

if [[ ! -d $DATA ]]; then
  mkdir -p $DATA
fi

cd $DATA

set +x
echo " "
echo "#####################################"
echo "    Process GRIB ENS-WAFS PRODUCTS   "
echo "#####################################"
echo " "
set -x

for hr in 00 06 12 18 24 30 36 42 48 54 60 66 72 78 84  \
          90 96 102 108 114 120 126 132 138 144 150 156 \
          162 168 174 180 186 192;
do

   input_ens=all_ens.t${cyc}z.pgrbaf$hr
   input_ensi=all_ens.t${cyc}z.pgrbaif$hr
   $EXECutil/grbindex $input_ens $input_ensi
   for grid in $gid ; do
      $EXECutil/copygb -g${grid} -a $input_ens $input_ensi wafsens${grid}f$hr.${cycle}
      $EXECutil/grbindex wafsens${grid}f$hr.${cycle} wafsens${grid}if${hr}.${cycle}
      export pgm=mkwafsens
      . prep_step
      export XLFUNIT_11="wafsens${grid}f$hr.${cycle}"
      export XLFUNIT_31="wafsens${grid}if$hr.${cycle}"
      export XLFUNIT_51="wafs${grid}.${cycle}.ens${hr}"
      $EXECutil/mkwmoens < $PARMutil/grib_wafs${grid}.ens${hr} parm='KWBK'

      # Convert to GRIB2 format:
      export CNVGRIB=/nwprod/util/exec/cnvgrib
      export GRB2INDX=/nwprod/util/exec/grb2index
      $CNVGRIB -g12 -p40 wafs${grid}.${cycle}.ens${hr} wafs${grid}.${cycle}.ens${hr}.grib2
      $GRB2INDX wafs${grid}.${cycle}.ens${hr}.grib2 wafs${grid}.${cycle}.ens${hr}.grib2.idx

      if test $SENDCOM = 'YES'
      then
         cp wafs${grid}.${cycle}.ens${hr} $COMOUT/wafs${grid}.${cycle}.ens${hr}
         cp wafs${grid}.${cycle}.ens${hr}.grib2 $COMOUT/wafs${grid}.${cycle}.ens${hr}.grib2
         cp wafs${grid}.${cycle}.ens${hr}.grib2.idx $COMOUT/wafs${grid}.${cycle}.ens${hr}.grib2.idx
         if [[ "$grid" = "38" || "$grid" = "39" || "$grid" = "40" ]]
         then
	   cp wafs${grid}.${cycle}.ens${hr} $pcom/wafs${grid}.${cycle}.ens${hr}
        fi
      fi
   done
done



if [ "$SENDDBN" = "YES" ]
then
   for hr in 00 06 12 18 24 30 36 42 48 54 60 66 72 78 84  \
          90 96 102 108 114 120 126 132 138 144 150 156 \
          162 168 174 180 186 192;
   do
      for grid in $gid ; do
        $DBNROOT/bin/dbn_alert MODEL ENS_WAFS  $job $COMOUT/wafs${grid}.${cycle}.ens${hr}

        if [ $SENDDBN_GB2 = YES ]
        then

        $DBNROOT/bin/dbn_alert MODEL ENS_WAFS_GB2 $job $COMOUT/wafs${grid}.${cycle}.ens${hr}.grib2
        #$DBNROOT/bin/dbn_alert MODEL ENS_WAFS_GB2_WIDX $job $COMOUT/wafs${grid}.${cycle}.ens${hr}.grib2.idx

        fi

        if [[ "$grid" = "38" || "$grid" = "39" || "$grid" = "40" ]]
        then
           $DBNROOT/bin/dbn_alert GRIB_LOW ENS_XWAFS $job $pcom/wafs${grid}.${cycle}.ens${hr}
        fi
      done
   done 
fi

msg="HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"
