#  UTILITY SCRIPT NAME :  mkfossst
#               AUTHOR :  Joe Johnson 
#         DATE WRITTEN :  06/13/97
#             Modified :  07/16/97 added step SSTGRIDS which changes
#                         the PDS octet 9 from 11 (TMP) to 80 (WTMP)
#                         for FOS distribution.
#             Modified :  09/10/97 added steps to output SST 1-deg GRIB
#                         GRID 360x180 with WMO header HTXA99 for ECMWF.
#             Modified :  06/24/98 - Added copy of PGrbF00 and PGrbiF00
#                         files to current working directory.  Used by
#                         MKFOSSPC to create a bitmap for the 2x2 SST
#                         fields.
#
#  Abstract:  This utility script produces the SST 2.5x5, and
#             1.25x1.25 degree GRIB bulletins.  Also appends WMO
#             bulletin header on 360x180 1-deg SST grid.              
#
#             NOTE:  MKFOSSPC makes 2.5x5 deg bulletins
#                             makes 2x2 deg bulletins
#                    MAKEWAFS makes 1.25x1.25 deg bulletins
#
#     Input:  2 arguments are passed to this script.   
#             1st argument - Model - format of 3X in lower case (sst)
#             2nd argument - Forecast Hour - format of 2I (12)
#
echo "History: June 17,1997 - First implementation of this utility script"
#

set -xa
NET="$1"
hour="$2"
num=$#

if test "$num" -eq 2
then
   echo " Appropriate number of arguments were passed"
else
   echo " Two arguments are needed in order to execute"
   echo " this script.  You passed $num."
   exit 16
fi

if [ ! -f sstoigrb ]
then
   cp $COMOUT/sstoi_grb sstoigrb
fi
if [ ! -f sstoigrb.index ]
then
   cp $COMOUT/sstoi_grb.index sstoigrb.index
fi

## Process WAFS and FOS SST PRODUCTS
export pgm=sstoi_grids
. prep_step

export XLFUNIT_21="sstoigrb"
export XLFUNIT_22="sstoigrb.index"
export XLFUNIT_51="sstoi_regrid_grb"

startmsg
$EXECsstoi/sstoi_grids >> $pgmout 2>errfile
export err=$?;$USHsstoi/sstoi_errwarn.sh;export err=$?;err_chk

$EXECutil/grbindex sstoi_regrid_grb sstoi_regrid_grb.index

set +x
echo " ---------------------------------------------------"
echo "  Make FOS SST 2.5x5 GRIB Products "
echo "   and FOS SST 2x2 GRIB Products "
echo " ---------------------------------------------------"
set -x

#       Interpolate and copy grid 85 to grid 3 for input to mkfosspc 
#       and makewafs.  These programs need the standard 1 deg grid.

$EXECutil/copygb -g3 -k'4*-1 80' sstoi_regrid_grb sstoi_regrid_grb.index sst1deg

$EXECutil/grbindex sst1deg sst1deg_grb.index

#       Get PgrbF00 file.  Needed for the Land/Sea and Snow/Ice
#       fields used to produce a bitmap for the 2x2 degree SST

if [ -s $COMGDAS/gdas1.t12z.pgrbf00 ] && [ -s $COMGDAS/gdas1.t12z.pgrbif00 ]
then
  cp $COMGDAS/gdas1.t12z.pgrbf00 pgrbf00
  cp $COMGDAS/gdas1.t12z.pgrbif00 pgrbif00
else
  cp $COMGDAS/gdas1.t00z.pgrbf00 pgrbf00
  cp $COMGDAS/gdas1.t00z.pgrbif00 pgrbif00
fi

export pgm=mkfosspc
. prep_step

export XLFUNIT_11="sst1deg"
export XLFUNIT_31="sst1deg_grb.index"
export XLFUNIT_40="pgrbf00"
export XLFUNIT_41="pgrbif00"
export XLFUNIT_51="xtrn.fos${RUN}${hour}"

startmsg
$EXECutil/mkfosspc <$PARMutil/grib_fos${RUN}${hour} >> $pgmout 2>errfile
export err=$?;err_chk
 

#################################################################
if test "$SENDCOM" = 'YES'
then
  cp xtrn.fos${RUN}${hour} $pcom/xtrn.fos${RUN}${hour}.$job
fi

if test "$SENDDBN" = 'YES'
then
  $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $pcom/xtrn.fos${RUN}${hour}.$job
else
  echo "SENDDBN=$SENDDBN, files not posted to db_net."
fi
###################################################################

set +x
echo " ---------------------------------------------------"
echo "  Make SST WAFS 1.25x1.25 GRIB Products "
echo " ---------------------------------------------------"
set -x

export pgm=makewafs
. prep_step

export XLFUNIT_11="sst1deg"
export XLFUNIT_31="sst1deg_grb.index"
export XLFUNIT_51="xtrn.wfs${RUN}${hour}"
export XLFUNIT_53="wafs${RUN}${hour}"

startmsg
$EXECutil/makewafs < $PARMutil/grib_wfs${RUN}${hour} >>$pgmout 2>errfile
export err=$?; err_chk

cp wafs${RUN}${hour} com.wafs${RUN}${hour}

###################################################################

  if test "$SENDCOM" = 'YES'
  then
    cp xtrn.wfs${RUN}${hour} $pcom/xtrn.wfs${RUN}${hour}.$job
    cp com.wafs${RUN}${hour} $pcom/com.wafs${RUN}${hour}.$job
  fi

if test "$SENDDBN" = 'YES'
then
  $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $pcom/xtrn.wfs${RUN}${hour}.$job
else
  echo "SENDDBN=$SENDDBN, files not posted to db_net."
fi

###################################################################

set +x
echo " ---------------------------------------------------"
echo "  Make SST 1 degree GRIB Product for the ECMWF "
echo " ---------------------------------------------------"
set -x

# Add WMO Bulletin Header to 1 deg SST field for transmission to ECMWF.

export pgm=tocgrib
. prep_step

export XLFUNIT_11="sstoigrb"
export XLFUNIT_31="sstoigrb.index"
export XLFUNIT_51="xtrn.sstoi_ecmwf"

startmsg
$EXECutil/tocgrib < $PARMutil/grib_sstecmwf parm='KWBC' >> $pgmout 2>errfile
export err=$?;err_chk

#################################################################
if test "$SENDCOM" = 'YES'
then
  cp xtrn.sstoi_ecmwf $pcom/xtrn.sstoi_ecmwf.$job
fi

if test "$SENDDBN" = 'YES'
then
  $DBNROOT/bin/dbn_alert GRIB_LOW $NET $job $pcom/xtrn.sstoi_ecmwf.$job
else
  echo "SENDDBN=$SENDDBN, files not posted to db_net."
fi
###################################################################


msg="FOS and WAFS GRIB Processing ${hour} hour completed normally"
postmsg "$jlogfile" "$msg"

exit
