#!/bin/ksh
# -- 09/08/02 -------- BEGIN EXSICE SCRIPT ----------------------------

set -x
cd $DATA

# ######################################################################
# start SICE processing
# modified: 20030929
# changes: 1) new output file added at 3-h intervals out to 168 hours
#             sice.$cycle.siceg for new web page and NAWIPS
#          2) runs on all 4 model cycles
#          3) runs all year
# modified: 20041027
# changes: 1) re-arranged script
#          2) added step to create northern hemispheric GRIB2 bulletins
#             for AWIPS
#    History: OCT 2005  Lilly -- Converted fax graphics to T4 format
#                                and stopped writing to the stat file.
#    History: JUN 2006  Lilly -- Removed the legacy fax graphics for
#                                Vessel Icing.
#
# ######################################################################
 
# **************************************************
#   PRODUCE VESSEL ICING 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 .

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

export pgm="omb_gblsice"; . prep_step

iunit=94
junit=194
for hh 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$hh"
   junit=`expr $junit + 1 `
   export XLFUNIT_${junit}="pgrbif$hh"
done

export XLFRTEOPTS="unit_vars=yes"
export XLFUNIT_52="siceg"
export XLFUNIT_90="dtg.ft90"
export XLFUNIT_91="dtg.ft91"
export XLFUNIT_92="sst2dvar_grb"

startmsg
$EXEComb/omb_gblsice >> $pgmout 2>> errfile
export err=$?; err_chk
echo $err

# ****************************************
# Make grib index of the global ice file 
# ****************************************

${utilexec}/grbindex siceg sicegi

# ****************************************
# Make Alaska files
# ****************************************

export XLFRTEOPTS="unit_vars=yes"
export XLFUNIT_11="siceg"
export XLFUNIT_12="sicegi"
export XLFUNIT_53="siceg_akw"
export XLFUNIT_90="dtg.ft90"
export XLFUNIT_91="dtg.ft91"

startmsg
$EXEComb/omb_aksice >> $pgmout 2>> errfile
export err=$?; err_chk
echo $err

# ****************************************
# Make grib indices of output files
# ****************************************

${utilexec}/grbindex siceg_akw sicegi_akw

# *************************************************************
# Make GRIB file with northern hemispheric 1x1 deg lon/lat grid
# *************************************************************

${utilexec}/copygb -g 232 -x siceg siceg_1x1.all
${utilexec}/grbindex siceg_1x1.all sicegi_1x1.all

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

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

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

${utilexec}/cnvgrib -g12 -p40 siceg_1x1.all grib2

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

${utilexec}/grb2index grib2 gribi2

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

export XLFRTEOPTS="unit_vars=yes"

export XLFUNIT_11="grib2"
export XLFUNIT_31="gribi2"
export XLFUNIT_51="xtrn.awpsice.t${cyc}"

${utilexec}/tocgrib2 <$parmf/grib2_awpnsice.232 1>> $pgmout 2>> errfile

if test $SENDCOM = 'YES'
then
   echo "ncosp" > $COMOUT/where_sice_ran.$cycle
   cp siceg $COMOUT/sice.$cycle.siceg
   cp siceg_1x1.all $COMOUT/sice.$cycle.siceg_1x1.all
   cp siceg_akw $COMOUT/sice.$cycle.siceg_akw

   cp sicegi $COMOUT/sice.$cycle.sicegi
   cp sicegi_1x1.all $COMOUT/sice.$cycle.sicegi_1x1.all
   cp sicegi_akw $COMOUT/sice.$cycle.sicegi_akw

   cp xtrn.awpsice.t${cyc} $pcom/xtrn.awpsice.t${cyc} 

fi

#################################
# Convert to grib2 format
#################################
for fil in siceg siceg_1x1.all siceg_akw
do
  $utilexec/cnvgrib -g12 -p40 $COMOUT/sice.$cycle.${fil} $COMOUT/sice.$cycle.${fil}.grib2
  $utilexec/wgrib2 $COMOUT/sice.$cycle.${fil}.grib2 -s > $COMOUT/sice.$cycle.${fil}.grib2.idx
done

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

if test "$SENDDBN" = 'YES'
then
# add line to send xtrn.awpsice.t${cyc} to pcom and alert the TOC
  $DBNROOT/bin/dbn_alert GRIB_LOW OMBSICE $job $pcom/xtrn.awpsice.t${cyc}
  $DBNROOT/bin/dbn_alert MODEL OMBSICE $job $COMOUT/sice.$cycle.siceg
  $DBNROOT/bin/dbn_alert MODEL OMBSICE $job $COMOUT/sice.$cycle.siceg_1x1.all
  $DBNROOT/bin/dbn_alert MODEL OMBSICE $job $COMOUT/sice.$cycle.siceg_akw

  if [ $SENDDBN_GB2 = YES ]
  then

  $DBNROOT/bin/dbn_alert MODEL OMBSICE_GB2 $job $COMOUT/sice.$cycle.siceg.grib2
  $DBNROOT/bin/dbn_alert MODEL OMBSICE_GB2 $job $COMOUT/sice.$cycle.siceg_1x1.all.grib2
  $DBNROOT/bin/dbn_alert MODEL OMBSICE_GB2 $job $COMOUT/sice.$cycle.siceg_akw.grib2

  $DBNROOT/bin/dbn_alert MODEL OMBSICE_GB2_WIDX $job $COMOUT/sice.$cycle.siceg.grib2.idx
  $DBNROOT/bin/dbn_alert MODEL OMBSICE_GB2_WIDX $job $COMOUT/sice.$cycle.siceg_1x1.all.grib2.idx
  $DBNROOT/bin/dbn_alert MODEL OMBSICE_GB2_WIDX $job $COMOUT/sice.$cycle.siceg_akw.grib2.idx

  fi

fi

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

# ---------------END OF EXSICE SCRIPT----------------------------
