#!/bin/sh
######################################################################
#  UTILITY SCRIPT NAME :  exgfs_wafs_grib2.sh.sm
#         DATE WRITTEN :  07/15/2009
#
#  Abstract:  This utility script produces the WAFS GRIB2. The output 
#             GRIB files are posted on NCEP ftp server and the grib2 files
#             are pushed via dbnet to TOC to WAFS (ICSC).  
#             This is a joint project of WAFC London and WAFC Washington.
#
#             We are processing WFAS grib2 for fcsthrs from 06 - 36 
#             with 3-hour time increment.
#
#####################################################################
echo "-----------------------------------------------------"
echo "JGFS_WAFS_GRIB2 at 00Z/06Z/12Z/18Z GFS postprocessing"
echo "-----------------------------------------------------"
echo "History: AUGUST  2009 - First implementation of this new script."
echo " "
#####################################################################

cd $DATA

set -x

for fcsthrs in 06 09 12 15 18 21 24 27 30 33 36
do

##########################################################
# Wait for the availability of the gfs master pgrib 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 pgrib filei!"
      err_exit $msg
    fi
  done

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

  echo " ------------------------------------------"
  echo " BEGIN MAKING GFS WAFS GRIB2 PRODUCTS"
  echo " ------------------------------------------"

  set +x
  echo " "
  echo "#####################################"
  echo "      Process GRIB WAFS PRODUCTS     "
  echo " FORECAST HOURS 06 - 36."
  echo "#####################################"
  echo " "
  set -x
  cp $FIXgfs/awc_wafavn.cfg waf.cfg
  cp $COMIN/gfs.t${cyc}z.master.grbf${fcsthrs}  masterfilef${fcsthrs}
  count=1
  sleep 1
  while [[ $count -le 2 ]]; do
  ob=`ls -lrt $COMIN/gfs.t${cyc}z.master.grbf${fcsthrs} |awk '{print $5}'`
  cb=`ls -lrt masterfilef${fcsthrs} |awk '{print $5}'`
    if [ $ob -ne $cb ]; then
      echo "$COMIN/gfs.t${cyc}z.master.grbf${fcsthrs} not synced, $count attempt(s)"
      (( count = $count + 1 ))
      cp $COMIN/gfs.t${cyc}z.master.grbf${fcsthrs} masterfilef${fcsthrs}
      sleep 2
    else
      echo "$COMIN/gfs.t${cyc}z.master.grbf${fcsthrs}: $ob and masterfilef${fcsthrs}: $cb are size identical"
      break
    fi
  done
$utilexec/wgrib masterfilef${fcsthrs} | grep -F -f $PARMutil/wafs_gfsmaster.list | $utilexec/wgrib -i -grib -o tmpfile_gfsf${fcsthrs} masterfilef${fcsthrs}

  $EXECgfs/awc_wafavn -c waf.cfg -i masterfilef${fcsthrs} -o tmpfile_icaof${fcsthrs} icng tcld cat cb

  $utilexec/copygb  -g45 -i2 -x  tmpfile_gfsf${fcsthrs}  tmpfile_gfs_grb45f${fcsthrs}

  $utilexec/copygb  -g45 -i2 -x  tmpfile_icaof${fcsthrs} tmpfile_icao_grb45f${fcsthrs}

  cat tmpfile_gfs_grb45f${fcsthrs} tmpfile_icao_grb45f${fcsthrs} > gfs.t${cyc}z.wafs_grb45f${fcsthrs}

#  $utilexec/cnvgrib -g12 -p40 -nv  gfs.t${cyc}z.wafs_grb45f${fcsthrs}  gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2

  $utilexec/cnvgrib2_wafs -g12 -p40 -mastertable_ver_6 -nv  gfs.t${cyc}z.wafs_grb45f${fcsthrs}  gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2

  $utilexec/wgrib2 -s gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2 > gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2.idx
  
# Chuang: create a file in working dir without US unblended WAFS product for ftp server 

  $utilexec/wgrib2 gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2 | grep -v ":CAT" | grep -v ":CTP" | \
  grep -v ":ICIP" | grep -v ":ICAHT:cumulonimbus" | grep -v ":CBHE" | $utilexec/wgrib2 -i gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2 \
  -grib gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2
  $utilexec/wgrib2 -s gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2 > gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2.idx   

# Processing WAFS GRIB2 grid 45 for ISCS and WIFS

  pgm=tocgrib2
  export pgm;. prep_step
  startmsg

  export XLFUNIT_11=gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2
  export XLFUNIT_31=" "
  export XLFUNIT_51=grib2.t${cyc}z.wafs_grbf${fcsthrs}.45

  $utilexec/tocgrib2 <  $PARMutil/grib2_gfs_wafsf${fcsthrs}.45 >> $pgmout 2> errfile

  err=$?;export err ;err_chk
  echo " error from tocgrib=",$err

# Processing WAFS GRIB2 grid 45 (Icing, TB, CAT) for WIFS

   pgm=tocgrib2
   export pgm;. prep_step
   startmsg
 
   export XLFUNIT_11=gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2
   export XLFUNIT_31=" "
   export XLFUNIT_51=grib2.t${cyc}z.wafs_grb_wifsf${fcsthrs}.45
 
  $utilexec/tocgrib2 <  $PARMutil/grib2_gfs_wafs_wifs_f${fcsthrs}.45 >> $pgmout 2> errfile

   err=$?;export err ;err_chk
   echo " error from tocgrib=",$err


  if [ $SENDCOM = "YES" ] ; then

   ##############################
   # Post Files to COM
   ##############################

     mv gfs.t${cyc}z.wafs_grb45f${fcsthrs}  $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}
     mv gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2 $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2
     mv gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2.idx $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2.idx
     mv gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2 $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2
     mv gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2.idx $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2.idx
     

   ##############################
   # Post Files to PCOM
   ##############################

     mv grib2.t${cyc}z.wafs_grbf${fcsthrs}.45  $pcom/grib2.t${cyc}z.wafs_grbf${fcsthrs}.45
     mv grib2.t${cyc}z.wafs_grb_wifsf${fcsthrs}.45  $pcom/grib2.t${cyc}z.wafs_grb_wifsf${fcsthrs}.45
  fi

  ######################
  # Distribute Data
  ######################

  if [ $SENDDBN = "YES" ] ; then
#  
#   Distribute Data to NCEP FTP Server
#
#    touch $pcom/grib2.t${cyc}z.wafs_grbf${fcsthrs}.45
#    touch $pcom/grib2.t${cyc}z.wafs_grb_wifsf${fcsthrs}.45

#
#    Distribute Data to WOC
#
  
    # turning off delivery of US WAFS products because we're replacing them with US and UK blended products
    $DBNROOT/bin/dbn_alert MODEL GFS_WAFSA_GB2 $job $pcom/grib2.t${cyc}z.wafs_grb_wifsf${fcsthrs}.45
#   $DBNROOT/bin/dbn_alert MODEL GFS_WAFSA_GB2 $job $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2
#   $DBNROOT/bin/dbn_alert MODEL GFS_WAFSA_GB2_WIDX $job $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.grib2.idx

    # delivery of US WAFS products nouswafs.grib2 removing the icing, TB, and CAT fields
    $DBNROOT/bin/dbn_alert MODEL GFS_WAFSA_GB2 $job $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2
    $DBNROOT/bin/dbn_alert MODEL GFS_WAFSA_GB2_WIDX $job $COMOUT/gfs.t${cyc}z.wafs_grb45f${fcsthrs}.nouswafs.grib2.idx
#
#    Distribute Data to TOC TO WIFS FTP SERVER (AWC)
#
    $DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2.t${cyc}z.wafs_grbf${fcsthrs}.45
    # turning off delivery of US WAFS products because we're replacing them with US and UK blended products
    # this alert will be turned back on if bledning job fails
    #$DBNROOT/bin/dbn_alert NTC_LOW $NET $job   $pcom/grib2.t${cyc}z.wafs_grb_wifsf${fcsthrs}.45
#
  fi

done

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

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

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