####  UNIX Script Documentation Block
#
# Script name:   ingest_sncvgrib96.sh
#
# JIF contact:  Keyser      org: NP22        date: 2006-05-12
#
# Abstract: Executes the program BUFR_SNO96GRB which reads in a global IMS
#   96th mesh NESDIS snow cover/sea ice file in ASCII format and converts it to
#   GRIB.  Then copies output to path wgrbbul/imssnow96.grb under date determined
#   by qualifier in input filename.
#
# Script history log:
# 2005-05-16  Geo  Gayno  Original version for implementation (based on
#                         ingest_sncvgrib.sh).
# 2006-05-12  D. Keyser   Improved DOCBLOCK, comments, stdout, messages posted
#                         to joblog, message posted to history file
#
# Usage: ingest_sncvgrib96.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_sno96grb
#
# Remarks: Invoked by the script ingest_translate_orbits.
#   The following variables must be set: DATA, EXECbufr, TANKDIR, DEBUGSCRIPTS,
#    jlogfile, USERDIR, utilexec, SENDDBN, DBNROOT.
#
#   Condition codes:
#     0 - no problem encountered
#   > 0 - some problem encountered
#     Specifically:  95 - BUFR_SNO96GRB could not read standard input in unit 05
#                    96 - BUFR_SNO96GRB had bad write to grib file in unit 51
#                    97 - BUFR_SNO96GRB had bad open to grib file in unit 51
#                    98 - BUFR_SNO96GRB had unexpected end-of-file reading
#                         in IMS snow cover/sea ice ascii file in unit 11
#                    99 - BUFR_SNO96GRB 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_SNCVGRIB96.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_sno96grb
set +u
. prep_step
set -u
export XLFUNIT_11="$2"
export XLFUNIT_51="$TANKDIR/$yyyymmdd/wgrbbul/imssnow96.grb"

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

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

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

exit $err
