####  UNIX Script Documentation Block
#
# Script name:   ingest_sncvgrib.sh
#
# JIF contact:  Keyser      org: NP22        date: 2006-05-12
#
# Abstract: Executes the program BUFR_SNO16GRB which reads in a global IMS
#   16th mesh NESDIS snow cover/sea ice file in ASCII format and converts it to
#   GRIB.  Then copies output to path wgrbbul/imssnow.grb under date determined
#   by qualifier in input filename.
#
# Script history log:
# 1997-09-23  Bert Katz   Original version for implementation.
# 1998-05-12  Bert Katz   Changes for y2k compliance.
# 2005-05-18  Geo  Gayno  Some bug fixes.
# 2006-05-12  D. Keyser   Improved DOCBLOCK, comments, stdout, messages posted
#                         to joblog, message posted to history file
# 2006-10-27  P. O'Reilly Modified script to have bufr_sno16grb write output
#			  file to working directory instead of directly to
#			  /dcom, then write output to /dcom if no error occurs. Also,
#			  added DBNet alert for imssnow.grb file.
#
# Usage: ingest_sncvgrib.sh
#
#   Script parameters: (1) dummy (not used by this script)
#                      (2) name of input IMS snow cover/sea ice ascii file
#                          (in form imsYYYYDDD.asc)
#
#   Modules and files referenced:
#     scripts    : $DATA/postmsg
#                  $DATA/prep_step
#     data cards : none
#     executables: $EXECbufr/bufr_sno16grb
#
# Remarks: Invoked by the script ingest_translate_orbits.
#   The following variables must be set: DATA, EXECbufr, TANKDIR, DEBUGSCRIPTS,
#    jlogfile, USERDIR, utilexec.
#
#   Condition codes:
#     0 - no problem encountered
#   > 0 - some problem encountered
#     Specifically:  98 - BUFR_SNO16GRB had unexpected end-of-file reading
#                         in IMS snow cover/sea ice ascii file in unit 11
#                    99 - BUFR_SNO16GRB found unrecognized data value in IMS
#                         snow cover/sea ice ascii file in unit 11
#
# Attributes:
#   Language: aix unix
#   Machine:  NCEP CCS
####

set -au

echo
echo "#######################################################################"
echo "                   START INGEST_SNCVGRIB.SH                            "
echo "#######################################################################"
echo

if [ $DEBUGSCRIPTS = ON -o $DEBUGSCRIPTS = YES ] ; then
   set -x
fi

#  Determine data YYYYMMDD based on qualifier in name of IMS file
#  --------------------------------------------------------------

yyyyddd=$(echo $2 | cut -d"." -f1 | cut -c4-)
set +A DAYSBEFORE 0 31 59 90 120 151 181 212 243 273 304 334 365
IYR=$(($yyyyddd/1000))
DOY=$(($yyyyddd-1000*$IYR))
LPYR=$(($IYR/4*4))
LPYR100=$(($IYR/100*100))
LPYR400=$(($IYR/400*400))
MON=1
yyyymmdd=0
while [ $yyyymmdd -eq 0 ] ; do
   if [ $MON -ge 2 ] ; then
      if [ $LPYR -eq $IYR -a $LPYR100 -ne $IYR ] ; then 
         LEAP=1
      elif [ $LPYR400 -eq $IYR ] ; then
         LEAP=1
      else
         LEAP=0
      fi
   else
      LEAP=0
   fi
   if [ $DOY -le $(expr ${DAYSBEFORE[$MON]} + $LEAP) ] ; then
      MONSUB=$(expr $MON - 1)
      if [ $MONSUB -lt 2 ] ; then
         LEAP=0
      fi
      if [ $MON -lt 10 ] ; then
         MON=0$MON
      fi
      DOM=$(expr $DOY - ${DAYSBEFORE[$MONSUB]} - $LEAP)
      if [ $DOM -lt 10 ] ; then
         DOM=0$DOM
      fi
      yyyymmdd=$IYR$MON$DOM
   fi
   MON=$(expr $MON + 1)
done

if [ ! -d $TANKDIR/$yyyymmdd/wgrbbul ] ; then
   mkdir -p $TANKDIR/$yyyymmdd/wgrbbul
fi

# change to working directory

cd $DATA

pgm=bufr_sno16grb
set +u
. prep_step
set -u
export XLFUNIT_11="$2"
export XLFUNIT_51="$DATA/imssnow.grb"

msg="$pgm start for $yyyymmdd data"
postmsg "$jlogfile" "$msg"

echo $yyyyddd | $EXECbufr/bufr_sno16grb
err=$?
#err_chk

if [ $err -eq 0 ]; then
   cp $DATA/imssnow.grb $TANKDIR/$yyyymmdd/wgrbbul/imssnow.grb
   msg="$pgm completed normally"
   postmsg "$jlogfile" "$msg"
   echo "imssnow.grb (for $yyyymmdd) CREATED and WRITTEN to \
$TANKDIR/$yyyymmdd/wgrbbul AT `date -u +%Y/%m/%d' '%H:%M:%S' GMT'`" \
    >> $USERDIR/imssnow.grb.history
   msg="imssnow.grb CREATED for $yyyymmdd"
   postmsg "$jlogfile" "$msg"
   if [ $SENDDBN = YES ]; then
      if [ -s $TANKDIR/$yyyymmdd/wgrbbul/imssnow.grb ]; then
         $DBNROOT/bin/dbn_alert MODEL IMSSNOW_GB $job \
         $TANKDIR/$yyyymmdd/wgrbbul/imssnow.grb
      fi
   fi
   /nwprod/util/exec/cnvgrib -g12 -p40 $DATA/imssnow.grb $DATA/imssnow.grb.grib2
   if [ -s $DATA/imssnow.grb.grib2 ] ; then
      cp $DATA/imssnow.grb.grib2 $TANKDIR/$yyyymmdd/wgrbbul/imssnow.grb.grib2
      if [ $SENDDBN_GB2 = YES ] ; then
         $DBNROOT/bin/dbn_alert MODEL IMSSNOW_GB_GB2 $job \
         $TANKDIR/$yyyymmdd/wgrbbul/imssnow.grb.grib2
      fi
   fi
fi

rm -f $2  # remove IMS snow cover/sea ice file from temporary working directory

gribrc=$err
if [ $gribrc -eq 0 ] ; then
   if [ $timetype = GMT ] ; then
      currdate=$(date -u '+%Y%m%d')
   elif [ $timetype = LOCAL ] ; then
      currdate=$(date '+%Y%m%d')
   else
      currdate=`echo $user_spec_timedatecurr | cut -f1 -d" "`
   fi
   nextdatehr=$($utilexec/ndate 24 ${yyyymmdd}00)
   nextdate=$(echo "$nextdatehr/100 \n quit" | bc)
   if [ $currdate = $nextdate ] ; then
#################### ASK BERT about this - is this right?????
      if [ -s $TANKDIR/$currdate/wgrbbul/imssnow.grb ] ; then
         cp $TANKDIR/$yyyymmdd/wgrbbul/imssnow.grb \
          $TANKDIR/$currdate/wgrbbul/imssnow.grb
      fi
   fi
fi

exit $err
