####  UNIX Script Documentation Block
#
# Script name: ingest_cloud_SH.sh
#
# JIF contact:  O'Reilly        org: NP11         date: 2006-08-01
#
# Abstract: This script processes AFWA cloud files ingested from
#           NESDIS' DDS and creates a global gridded cloud field
#	    in grib format.
#
# Script history log:
# 2006-08-14  Patrick O'Reilly  original version for implementation
#
# Usage: ingest_cloud_SH.sh $1 $2
#
#   Script parameters: $1 - full path definition for BUFR mnemonic table
#                      $2 - full path definition for NPR.CLO[NS].* file
#
#   Modules and files referenced:
#     scripts     : None
#     executables : $pgm (either cld_getdate, cld_convert2box, cld_interp_hgtbl,
#                         cld_vertcompact_qc, cld_compact2global or cld_global2grib1)
#
# Remarks:
#
#   Invoked by the script ingest_translate_orbits.
#
#   Imported Variables that must be passed in:
#      DATA     - path to current working directory
#      USHbufr  - path to ush scripts
#      FIXbufr  - path to fixed field files
#      EXECbufr - path to executables
#      TANKDIR  - path to output IEEE and BUFR tank (e.g., /dcom/us007003)

#   Condition codes:
#     0     - no problem encountered
#     > 0 - some problem encountered
#	Specifically: 90 - Program cld_getdate failure
#		      91 - Program cld_convert2box failed for NH processing
#                     91 - Program cld_convert2box failed for SH processing
#                     91 - Program cld_interp_hgtbl failure
#                     91 - Program cld_vertcompact_qc failure
#                     91 - Program cld_compact2global failure
#                     91 - Program cld_global2grib1 failure
#                     197 - gdas1.tccz.sanlfile not present
#                     198 - NPR.CLON.* file not present
#                     199 - NPR.CLOS.* file not present
#
# Attributes:
#
#   Language: /bin/sh script
#   Machine:  IBM SP
####

set -x

cd $DATA

# Get the date information out of $2 in order to
# set the date variables
#-----------------------------------------------
pgm=cld_getdate
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_20="$2"
export XLFUNIT_67="date_sh.grib"

$EXECbufr/cld_getdate
err=$?
if [ $err -eq 0 ]; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
		  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"

else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
		  postmsg "$jlogfile" "$msg"
		  exit 90
fi

grep yyyymmddhh date_sh.grib | awk '{print $2}'
fdate=`grep yyyymmddhh date_sh.grib | awk '{print $2}'`
echo "File date is: $fdate"
wwmcadate=$fdate
targetfiledate=`echo $wwmcadate | cut -c 1-8`
anldate=`/nwprod/util/exec/ndate -24 $wwmcadate`
echo $anldate
anldatePDY=`echo $anldate | cut -c 1-8`
echo $anldatePDY
filedate=`echo $anldate | cut -c 1-8`
filehour=`echo $anldate | cut -c 9-10`

if [ ! -d $TANKDIR/$targetfiledate/$TANKFILE/cloud ] ; then
  mkdir -p $TANKDIR/$targetfiledate/$TANKFILE/cloud
fi

if [ $SENDCOM = "YES" ] ; then
  cp $2 $TANKDIR/$targetfiledate/$TANKFILE/cloud/$2
fi

## -- 1-DEG job ------ 1-DEG job ------ 1-DEG job ------------
#
#  COMPUTE H,M,L,BL cloud,COMPRESS,and store global 1deg
#  FOR YESTERDAY (to be sure both SH and NH are on-line)
#
#       yesterday's 23Z and today's 00Z-04Z with 00Z FNL data,
#       05Z-10Z with 06Z FNL data,
#       11Z-16Z with 12Z FNL data,
#       17Z-22Z with 18Z FNL data,
#-------------------------------------------------------------
if [ $filehour -le 04 ]; then
 anlhr=t00z
fi
if [ $filehour -ge 05 -a $filehour -le 10 ]; then
 anlhr=t06z
fi
if [ $filehour -ge 11 -a $filehour -le 16 ]; then
 anlhr=t12z
fi
if [ $filehour -ge 17 ]; then
 anlhr=t18z
fi
if [ $filehour -ge 23 ]; then
 anlhr=t00z
 oldate=$wwmcadate
 filedate=`echo $oldate | cut -c 1-8`
fi

echo "The filedate is: $filedate"
echo "The filehour is: $filehour"
echo "The anlhr is: $anlhr"

# Check to see if GDAS file from yesterday is there
# If not, exit with error, if it is, copy it here.
#--------------------------------------------------

if [ ! -e /com/gfs/prod/gdas.${filedate}/gdas1.${anlhr}.sanl  ] ; then
   echo "******************************************************************************"
   echo "$0: File /com/gfs/prod/gdas.${filedate}/gdas1.${anlhr}.sanl doesn't exist."
   echo "******************************************************************************"
   exit 198
else
   cp /com/gfs/prod/gdas.${filedate}/gdas1.${anlhr}.sanl z00
fi

#--------------------------------------------------
#  convert hybrid sigma to old sigma 13Mar07

export SIGINP=z00
export SIGOUT=z00.sig
export OROGRAPHY=/dev/null
export JCAP=382
export LEVS=64
export LONB=768
export LATB=384
export IDVC=1
export SIGLEVEL=/nwprod/fix/global_siglevel.l64.txt
export CHGRESEXEC=/nwprod/exec/global_chgres
export VERBOSE=YES

/nwprod/ush/global_chgres.sh

#--------------------------------------------------

cd $DATA

cat <<\EOFnh > nhparm
    1
EOFnh

cat <<\EOFsh > shparm
    2
EOFsh

# Check to see if NPR.CLON file from yesterday is there
# If not, exit with error, if it is, copy it here.
#------------------------------------------------------

if [ ! -e $TANKDIR/$anldatePDY/wgrbbul/cloud/NPR.CLON.*.S${filehour}00 ] ; then
   echo "******************************************************************************"
   echo "$0: File $TANKDIR/$anldatePDY/wgrbbul/cloud/NPR.CLON.*.S${filehour}00 doesn't exist."
   echo "******************************************************************************"
   exit 199
else
   cp $TANKDIR/$anldatePDY/wgrbbul/cloud/NPR.CLON.*.S${filehour}00 nh00
fi

pgm=cld_convert2box
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_20="nh00"
export XLFUNIT_31="nhlamts"
export XLFUNIT_32="nhltyps"
export XLFUNIT_33="nhlbase"
export XLFUNIT_34="nhltops"
export XLFUNIT_35="nhtotal"
export XLFUNIT_36="nhtimes"

$EXECbufr/cld_convert2box < nhparm
err=$?
if [ $err -eq 0 ] ; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
                  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"

else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
                  postmsg "$jlogfile" "$msg"
                  exit 91
fi

# Check to see if NPR.CLOS file from yesterday is there
# If not, exit with error, if it is, copy it here.
#------------------------------------------------------

if [ ! -e $TANKDIR/$anldatePDY/wgrbbul/cloud/NPR.CLOS.*.S${filehour}00 ] ; then
   echo "******************************************************************************"
   echo "$0: File $TANKDIR/$anldatePDY/wgrbbul/cloud/NPR.CLOS.*.S${filehour}00 doesn't exist."
   echo "******************************************************************************"
   exit 199
else
   cp $TANKDIR/$anldatePDY/wgrbbul/cloud/NPR.CLOS.*.S${filehour}00 sh00
fi

pgm=cld_convert2box
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_20="sh00"
export XLFUNIT_31="shlamts"
export XLFUNIT_32="shltyps"
export XLFUNIT_33="shlbase"
export XLFUNIT_34="shltops"
export XLFUNIT_35="shtotal"
export XLFUNIT_36="shtimes"

$EXECbufr/cld_convert2box < shparm
err=$?
if [ $err -eq 0 ] ; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
                  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"
else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
                  postmsg "$jlogfile" "$msg"
                  exit 92
fi

pgm=cld_interp_hgtbl
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_11="z00.sig"
export XLFUNIT_20="nhgts"
export XLFUNIT_21="shgts"

$EXECbufr/cld_interp_hgtbl
err=$?
if [ $err -eq 0 ] ; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
                  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"
else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
                  postmsg "$jlogfile" "$msg"
                  exit 93
fi

pgm=cld_vertcompact_qc
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_10="nhgts"
export XLFUNIT_15="shgts"
export XLFUNIT_30="nhlamts"
export XLFUNIT_31="nhltyps"
export XLFUNIT_32="nhlbase"
export XLFUNIT_33="nhltops"
export XLFUNIT_34="nhtotal"
export XLFUNIT_35="nhtimes"
export XLFUNIT_50="shlamts"
export XLFUNIT_51="shltyps"
export XLFUNIT_52="shlbase"
export XLFUNIT_53="shltops"
export XLFUNIT_54="shtotal"
export XLFUNIT_55="shtimes"
export XLFUNIT_60="nhtot"
export XLFUNIT_61="nhhi"
export XLFUNIT_62="nhmid"
export XLFUNIT_63="nhlo"
export XLFUNIT_64="nhblyr"
export XLFUNIT_80="shtot"
export XLFUNIT_81="shhi"
export XLFUNIT_82="shmid"
export XLFUNIT_83="shlo"
export XLFUNIT_84="shblyr"

$EXECbufr/cld_vertcompact_qc
err=$?
if [ $err -eq 0 ] ; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
                  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"
else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
                  postmsg "$jlogfile" "$msg"
                  exit 94
fi

pgm=cld_compact2global
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_20="nhtot"
export XLFUNIT_22="nhhi"
export XLFUNIT_30="nhmid"
export XLFUNIT_38="nhlo"
export XLFUNIT_46="nhblyr"
export XLFUNIT_21="shtot"
export XLFUNIT_23="shhi"
export XLFUNIT_31="shmid"
export XLFUNIT_39="shlo"
export XLFUNIT_47="shblyr"
export XLFUNIT_66="clouds"
export XLFUNIT_88="nhij"
export XLFUNIT_89="shij"

$EXECbufr/cld_compact2global
err=$?
if [ $err -eq 0 ] ; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
                  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"
else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
                  postmsg "$jlogfile" "$msg"
                  exit 95
fi

#     ----------------------------------
###   get Gribbed 1 deg data
#     ----------------------------------

pgm=cld_global2grib1
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_10="clouds"
export XLFUNIT_70="grbclouds"

$EXECbufr/cld_global2grib1
err=$?
if [ $err -eq 0 ] ; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
                  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"
else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
                  postmsg "$jlogfile" "$msg"
                  exit 96
fi

if [ $SENDCOM = "YES" ] ; then
   cp grbclouds $TANKDIR/$anldatePDY/wgrbbul/cloud/grbclouds.${anldate}00
fi

exit 0
