#!/bin/sh
######################################################################
#  UTILITY SCRIPT NAME :  exgfs_grib_awips.sh.sm
#         DATE WRITTEN :  10/04/2004
#
#  Abstract:  This utility script produces the  GFS AWIPS GRIB
#
#     Input:  1 arguments are passed to this script.
#             1st argument - Forecast Hour - format of 2I
#
#####################################################################
echo "------------------------------------------------"
echo "JGFS_AWIPS_00/06/12/18 GFS postprocessing"
echo "------------------------------------------------"
echo "History: OCT 2004 - First implementation of this new script."
echo " "
#####################################################################
set +x
fcsthrs="$1"
num=$#

if test "$num" -ge 1
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECutil=${EXECutil:-/nwprod/util/exec}
   export PARMutil=${PARMutil:-/nwprod/util/parm}
   export envir=${envir:-prod}
   export jlogfile=${jlogfile:-jlogfile}
   export DBNALERT_TYPE=${DBNALERT_TYPE:-GRIB}
   export NET=${NET:-gfs}
   export COMIN=${COMIN:-/com/$NET/$envir/$NET.$PDY}
   export pcom=${pcom:-/pcom/$NET}
   export job=${job:-interactive}
   export pgmout=${pgmout:-OUTPUT.$$}
else
   echo ""
   echo "Usage: exgfs_grib_awips.sh.sm  \$fcsthrs "
   echo ""
   exit 16
fi

cd $DATA

set -x

###############################################
# Wait for the availability of the pgrb file
###############################################
icnt=1
while [ $icnt -lt 1000 ]
do
  if [ -s $COMIN/${RUN}.${cycle}.master.grbf$fcsthrs ]
  then
     break
  fi

  sleep 10
  icnt=$((icnt + 1))
  if [ $icnt -ge 180 ]
  then
    msg="ABORTING after 30 min of waiting for the pgrb file!"
    err_exit $msg
  fi
done

########################################
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

echo " ------------------------------------------"
echo " BEGIN MAKING GFS AWIPS PRODUCTS"
echo " ------------------------------------------"

set +x
echo " "
echo "#######################################"
echo " Process GRIB AWIP PRODUCTS (mkawipgrb)"
echo "#######################################"
echo " "
set -x

export PARMutil=/nwprod/util/parm
export GRID
if test $fcsthrs -le 120
then
#  for GRID in 201 202 203 213 225    ## Turn off low-resolution GFS FOS and AWIPS products
   for GRID in 213 225
   do
     sh $USHutil/mkawpgrb.sh  ${fcsthrs}
   done
fi

# GRID=211 out to 240 hours:
export GRID=211
sh $USHutil/mkawpgrb.sh  ${fcsthrs}

export PARMutil=/nwprod/util/parm

###############################################################
#    Process GFS GRIB2 AWIP PRODUCTS
###############################################################

export gfs_grb=$COMIN/gfs.t${cyc}z.master.grbf${fcsthrs}
export gfs_grbi=$COMIN/gfs.t${cyc}z.master.grbif${fcsthrs}

export GRID
for GRID in 201g2 213g2
do
   case $GRID in
      201g2)
           $EXECutil/copygb -g201 -i0 $gfs_grb  $gfs_grbi $DATA/tmp201
           $EXECutil/cnvgrib -g12 -p40 $DATA/tmp201  $COMOUT/gfs.t${cyc}z.awp201${fcsthrs}.grib2
            ;;
      213g2)
           $EXECutil/copygb -g213 -i0 $gfs_grb  $gfs_grbi $DATA/tmp213
           $EXECutil/cnvgrib -g12 -p40 $DATA/tmp213  $COMOUT/gfs.t${cyc}z.awp213${fcsthrs}.grib2
            ;;
   esac
   sh $USHutil/mkawpgrb.sh  ${fcsthrs}
done

export GRID
for GRID in 160 161 212 254
do
   if [ ${fcsthrs} = 192 ]
   then
    export input_grb=$COMIN/gfs.t${cyc}z.master.grbf${fcsthrs}
    export gfs_grb=gfs_input_to_awips.grbf${fcsthrs}
    export gfs_grbi=gfs_input_to_awips.grbif${fcsthrs}
    rm -f tmpfile* $gfs_grb $gfs_grbi
    cp $input_grb $gfs_grb
# make 12 hr buket for awips at f192 because awips isn't able to change to read
# 6 hr bucket at f192, hoping to remove this when awips can take 6 hour bucket
    rm -f tmpfile
    $EXECutil/wgrib $input_grb | grep \
    ":kpds5=143:kpds6=1:kpds7=0" | $EXECutil/wgrib -i \
    $input_grb -grib -o tmpfile
    ln -sf tmpfile fort.11
    ln -sf tmpfile1 fort.51
    echo 180 | $EXECutil/overp1_grib

    rm -f tmpfile
    $EXECutil/wgrib $input_grb | grep \
    ":kpds5=142:kpds6=1:kpds7=0" | $EXECutil/wgrib -i \
    $input_grb -grib -o tmpfile
    ln -sf tmpfile fort.11
    ln -sf tmpfile2 fort.51
    echo 180 | $EXECutil/overp1_grib

    rm -f tmpfile
    $EXECutil/wgrib $input_grb | grep \
    ":kpds5=141:kpds6=1:kpds7=0" | $EXECutil/wgrib -i \
    $input_grb -grib -o tmpfile
    ln -sf tmpfile fort.11
    ln -sf tmpfile3 fort.51
    echo 180 | $EXECutil/overp1_grib
    
    rm -f tmpfile
    $EXECutil/wgrib $input_grb | grep \
    ":kpds5=140:kpds6=1:kpds7=0" | $EXECutil/wgrib -i \
    $input_grb -grib -o tmpfile
    ln -sf tmpfile fort.11
    ln -sf tmpfile4 fort.51
    echo 180 | $EXECutil/overp1_grib

    rm -f tmpfile
    $EXECutil/wgrib $input_grb | grep \
    ":kpds5=1:kpds6=232:kpds7=0" | $EXECutil/wgrib -i \
    $input_grb -grib -o tmpfile
    ln -sf tmpfile fort.11
    ln -sf tmpfile5 fort.51
    echo 180 | $EXECutil/overp1_grib

    rm -f tmpfile
    $EXECutil/wgrib $input_grb | grep \
    ":kpds5=1:kpds6=222:kpds7=0" | $EXECutil/wgrib -i \
    $input_grb -grib -o tmpfile
    ln -sf tmpfile fort.11
    ln -sf tmpfile6 fort.51
    echo 180 | $EXECutil/overp1_grib

    rm -f tmpfile
    $EXECutil/wgrib $input_grb | grep \
    ":kpds5=1:kpds6=212:kpds7=0" | $EXECutil/wgrib -i \
    $input_grb -grib -o tmpfile
    ln -sf tmpfile fort.11
    ln -sf tmpfile7 fort.51
    echo 180 | $EXECutil/overp1_grib

    cat tmpfile1 tmpfile2 tmpfile3 tmpfile4 tmpfile5 tmpfile6 tmpfile7>>$gfs_grb
    $EXECutil/grbindex $gfs_grb $gfs_grbi

fi
# end of making 12 hr bucket for awips for GFS F192

   $EXECutil/copygb -g${GRID} -i0 $gfs_grb  $gfs_grbi $DATA/tmp${GRID}
   $EXECutil/cnvgrib -g12 -p40 $DATA/tmp${GRID}  $COMOUT/gfs.t${cyc}z.awp${GRID}${fcsthrs}.grib2
   sh $USHutil/mkawpgrb.sh  ${fcsthrs}
done

if test $fcsthrs -eq 120
then

   set +x
   echo " "
   echo " ###############################################"
   echo "  Process PGRIB files from GFS products to create "
   echo "  grid 211 which contain surface fields. (00-120) "
   echo " ###############################################"
   echo " "
   set -x
   sh $USHutil/expgribf.sh

fi


cat $pgmout

###############################################################################
# GOOD RUN
set +x
echo "**************JOB EXGFS_GRIB_AWIPS.SH.SMS COMPLETED NORMALLY ON THE IBM"
echo "**************JOB EXGFS_GRIB_AWIPS.SH.SMS COMPLETED NORMALLY ON THE IBM"
echo "**************JOB EXGFS_GRIB_AWIPS.SH.SMS COMPLETED NORMALLY ON THE IBM"
set -x
###############################################################################

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

############## END OF SCRIPT #######################
