#!/bin/sh
#-------- 09/29/2003 ------ BEGIN EXFOG SCRIPT -------------------------
 
set -x
cd $DATA

# ######################################################################
# start GBLVSBY processing
# modified: 20041027
# changes: 1) re-arranged script
#          2) added step to create northern hemispheric GRIB2 bulletins
#             for AWIPS
# ######################################################################

# **************************************************
#   PRODUCE GLOBAL VISIBILITY GUIDANCE
# **************************************************

for fhr in 00  03  06  09  12  15  18  21  24  27  30  33  36  39  42  45  48 \
           51  54  57  60  63  66  69  72  75  78  81  84  87  90  93  96  99 \
          102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 \
          153 156 159 162 165 168
do
  cp $COMIN/gfs.${cycle}.pgrbf${fhr} pgrbf${fhr}
  if [ -f $COMIN/gfs.${cycle}.pgrbif${fhr} ] ; then
    cp $COMIN/gfs.${cycle}.pgrbif${fhr} pgrbif${fhr}
  else
    ${utilexec}/grbindex pgrbf${fhr} pgrbif${fhr}
  fi
done

  cp $SSTIN/sst2dvar_grb sst2dvar_grb

echo $PDY
echo $cyc
echo $ypdy
  
echo $PDY > dtg.ft90
echo $cyc > dtg.ft91

echo " 1   1" > sf.ft92
 
export pgm="omb_gblvsby"; . ${DATA}/prep_step

iunit=10
junit=110
for fhr in 00  03  06  09  12  15  18  21  24  27  30  33  36  39  42  45  48 \
           51  54  57  60  63  66  69  72  75  78  81  84  87  90  93  96  99 \
          102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 \
          153 156 159 162 165 168
do
   iunit=`expr $iunit + 1 `
   export XLFUNIT_${iunit}="pgrbf${fhr}"
   ln -s pgrbf${fhr} fort.${iunit}

   junit=`expr $junit + 1 `
   export XLFUNIT_${junit}="pgrbif${fhr}"
   ln -s pgrbfi${fhr} fort.${junit}
done

export XLFRTEOPTS="unit_vars=yes"
export XLFUNIT_68="gvisg.dat"
export XLFUNIT_98="gvis.ft98"
export XLFUNIT_90="dtg.ft90"
export XLFUNIT_91="dtg.ft91"
export XLFUNIT_92="sf.ft92"
export XLFUNIT_93="sst2dvar_grb"
ln -s gvisg.dat fort.68
ln -s gvis.ft98 fort.98
ln -s dtg.ft90  fort.90
ln -s dtg.ft91  fort.91
ln -s sf.ft92   fort.92
ln -s sst2dvar_grb fort.93

startmsg
$EXEComb/omb_gblvsby >> $pgmout 2> errfile
export err=$?;err_chk
echo RG Done with model itself

# ********************************************************************
#   Create GRIB index file
# ********************************************************************

${utilexec}/grbindex gvisg.dat gvisgi.dat
head -1 gvisgi.dat

# *********************************************************************
#   Create grid 232 northern hemispheric 1x1 deg lon/lat grid GRIB file
# *********************************************************************

${utilexec}/copygb -g 232 -x gvisg.dat fvnhg.dat #please note change
${utilexec}/grbindex fvnhg.dat fvnhgi.dat

echo RG Done with copygb 232

# *************************************************************
# Build GRIB bulletins
# *************************************************************

parmf=/nwprod/util/parm # the parm file grib2_awpnhvis.232 comes in below

# *************************************************************
# Make grib2 conversion
# *************************************************************

${utilexec}/cnvgrib -g12 -p40 fvnhg.dat grib2

# *************************************************************
# Make grib2 index
# *************************************************************

${utilexec}/grb2index grib2 gribi2

echo RG done with cnvgrib grb2index

# *************************************************************
# Create data set to send to TOC/AWIPS
# *************************************************************

export XLFRTEOPTS="unit_vars=yes"
export FORT11="grib2"
export FORT31="gribi2"
export FORT51="xtrn.awpfog.t${cyc}"

ln -fs grib2                 fort.11
ln -fs gribi2                fort.31
ln -fs xtrn.awpfog.t${cyc}   fort.51

${utilexec}/tocgrib2 <$parmf/grib2_awpnhvis.232 1>> $pgmout 2>> errfile
echo RG done with tocgrib2

# *************************************************************
# Copy files to COM; note rearrangement of script
# *************************************************************

if test $SENDCOM = 'YES'
then
  echo "ncosp" > $COMOUT/where_gblvsby_ran.$cycle
  cp gvisg.dat $COMOUT/fog.${cycle}.gvisg
  cp gvisgi.dat $COMOUT/fog.${cycle}.gvisgi
  cp fvnhg.dat $COMOUT/fog.${cycle}.fvnhg
  cp fvnhgi.dat $COMOUT/fog.${cycle}.fvnhgi
  cp xtrn.awpfog.t${cyc} ${PCOMOUT}/xtrn.awpfog.t${cyc}
fi

################################
# Convert to grib2 format
################################
for fil in gvisg fvnhg
do
  $utilexec/cnvgrib -g12 -p40 $COMOUT/fog.${cycle}.${fil} $COMOUT/fog.${cycle}.${fil}.grib2
  $utilexec/wgrib2 $COMOUT/fog.${cycle}.${fil}.grib2 -s >$COMOUT/fog.${cycle}.${fil}.grib2.idx
done

# ******************************************
# Send files to ftpprd and pcom with alerts
# ******************************************

if test "$SENDDBN" = 'YES'
then
# add line to send xtrn.awpfog.t${cyc} to pcom and alert the TOC
  $DBNROOT/bin/dbn_alert GRIB_LOW OMBFOG $job /pcom/omb/xtrn.awpfog.t${cyc}
  $DBNROOT/bin/dbn_alert MODEL OMBFOG $job $COMOUT/fog.${cycle}.gvisg
  $DBNROOT/bin/dbn_alert MODEL OMBFOG $job $COMOUT/fog.${cycle}.fvnhg

  if [ $SENDDBN_GB2 = YES ]
  then

  $DBNROOT/bin/dbn_alert MODEL OMBFOG_GB2 $job $COMOUT/fog.${cycle}.gvisg.grib2
  $DBNROOT/bin/dbn_alert MODEL OMBFOG_GB2 $job $COMOUT/fog.${cycle}.fvnhg.grib2
 
  $DBNROOT/bin/dbn_alert MODEL OMBFOG_GB2_WIDX $job $COMOUT/fog.${cycle}.gvisg.grib2.idx
  $DBNROOT/bin/dbn_alert MODEL OMBFOG_GB2_WIDX $job $COMOUT/fog.${cycle}.fvnhg.grib2.idx

  fi

fi

set +x
echo " ***** PROCESSING COMPLETED NORMALLY *****"
echo " ***** PROCESSING COMPLETED NORMALLY *****"
echo " ***** PROCESSING COMPLETED NORMALLY *****"
set -x

# ---------------END OF EXFOG SCRIPT ----------------------------
