#!/bin/ksh

##########################################################################
####  UNIX Script Documentation Block                                    #
#                                                                        #
# Script name:         rtma_getguess.sh                                  #
# Script description:  Read in first guess in grib1 as created by the RUC#
#                      post and reformat to conform to GSI requirements. #
#                      Read also the prepbufr file that contains the obs #                      
#                                                                        #
# Author:      Manuel Pondeca        Org: NP22         Date: 2005-10-30  #
#                                                                        #
# Script history log:                                                    #
# 2005-10-30  Manuel Pondeca                                             #
#                                                                        #
##########################################################################
set -x

cd $DATA
                                                                                    
msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"
                                                                                    
CDATE=$PDY$cyc
####################################################
# Look for the best available first guess
####################################################
CYCLE00=`echo $CDATE |cut -c 1-8`00
CYCLE00minus12=`/nwprod/util/exec/ndate -12 $CYCLE00`
CYCLE00minus06=`/nwprod/util/exec/ndate -06 $CYCLE00`
CYCLE00plus06=`/nwprod/util/exec/ndate +06 $CYCLE00`
CYCLE00plus12=`/nwprod/util/exec/ndate +12 $CYCLE00`
CYCLE00plus18=`/nwprod/util/exec/ndate +18 $CYCLE00`

HH=`echo $CDATE |cut -c 9-10`
if [[(${HH} -eq 00)]]  ; then
   CYCLEbest=$CYCLE00minus06
   CYCLEbckup=$CYCLE00minus12
fi

if [[(${HH} -ge 01) && (${HH} -le 06)]] ; then
   CYCLEbest=$CYCLE00
   CYCLEbckup=$CYCLE00minus06
fi

if [[(${HH} -ge 07) && (${HH} -le 12)]] ; then
   CYCLEbest=$CYCLE00plus06
   CYCLEbckup=$CYCLE00
fi

if [[(${HH} -ge 13) && (${HH} -le 18)]] ; then
   CYCLEbest=$CYCLE00plus12
   CYCLEbckup=$CYCLE00plus06
fi

if [[(${HH} -ge 19)]]  ; then
   CYCLEbest=$CYCLE00plus18
   CYCLEbckup=$CYCLE00plus12
fi

HHbest=`echo ${CYCLEbest} | cut -c 9-10`
if [ ${HH} = 00 ] ; then
     let "FHH=24-${HHbest}"
    else
     let "FHH=${HH}-${HHbest}"
   fi

CYCLEused=${CYCLEbest}
YYYYMMDDused=`echo ${CYCLEused} | cut -c 1-8`
HHused=`echo ${CYCLEused} | cut -c 9-10`
typeset -2Z FHH
infile=${COMRUC}/nam.${YYYYMMDDused}/nam.t${HHused}z.smartpr${FHH}.tm00

ls -l $infile
status=$?

if ((status != 0))
  then
    echo 'No NDFD guess available from best cycle '${CYCLEbest}f${FHH}
    let FHH="${FHH}+6"
    CYCLEused=${CYCLEbckup}
    YYYYMMDDused=`echo ${CYCLEused} | cut -c 1-8`
    HHused=`echo ${CYCLEused} | cut -c 9-10`
     infile=/com/nam/${envir}/nam.${YYYYMMDDused}/nam.t${HHused}z.smartpr${FHH}.tm00
fi

ls -l $infile
status=$?

if ((status != 0))
  then
    echo 'No NDFD guess available from backup cycle '${CYCLEbckup}f${FHH} '-exiting'
    err_exit
fi

CDATEges=`echo ${CYCLEused}f${FHH}`         # !Need this in the post
echo "export CDATEges=$CDATEges" > $COMOUT/prrtma.t${cyc}z.envir.sh

cp $COMIN/rtma.t${cyc}z.prepbufr.tm00 prepbufr  #this is the observation input file. Must know
                                                #where it is stored. Comes from Dennis Keyser's dump
#bsm - troubleshooting
echo "is the prepbufr file good?"
echo `ls -l $COMIN/rtma.t${cyc}z.prepbufr.tm00`
#####################################################################
# write out records from grib file in binary format
# with header (http://www.cpc.ncep.noaa.gov/products/wesley/wgrib.html)

# write out fields in the order specified in varname_2d_ndfd of NDFDgrid.f

wgrib=/nwprod/util/exec/wgrib

if [[(${FHH} -le 9)]]  ; then
    typeset -1Z FHH
fi
rm -f slabs.dat
$wgrib -s $infile | grep ":PRES:sfc:${FHH}hr" | $wgrib -i $infile         -o slabs.dat
$wgrib -s $infile | grep ":HGT:sfc:${FHH}hr"  | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":TMP:sfc:${FHH}hr"  | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":DPT:sfc:${FHH}hr"  | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":UGRD:sfc:${FHH}hr" | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":VGRD:sfc:${FHH}hr" | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":SPFH:sfc:${FHH}hr" | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":LAND:sfc:${FHH}hr" | $wgrib -i $infile -append -o slabs.dat    #dummy field. keep it
$wgrib -s $infile | grep ":PRES:sfc:${FHH}hr" | $wgrib -i $infile -append -o slabs.dat    #dummy field. keep it
$wgrib -s $infile | grep ":PRES:sfc:${FHH}hr" | $wgrib -i $infile -append -o slabs.dat    #dummy field. keep it
#####################################################################
# run program that outputs first guess in the binary format
# required by the GSI-2DVAR. 

cat << EOF > gridname_input
&gridname
    cgrid='prico'
    lbiascor=.false.
    pbiascor=0.0,
    tbiascor=0.1,
    qbiascor=0.0,
    ubiascor=0.1,
    vbiascor=0.1,
    tdbiascor=0.1
/
EOF

cat << EOF > parm_ndfd_time_namelist 
&timeinfo
    iyear=`echo $PDY |cut -c1-4`,
    imonth=`echo $PDY |cut -c5-6`,
    iday=`echo $PDY |cut -c7-8`,
    ihour=$cyc
/
EOF

# ndfd bckg convert to gsi format

export pgm=rtma_firstguess
. prep_step

export XLFUNIT_9="parm_ndfd_time_namelist"
export XLFUNIT_20="slabs.dat"
export XLFUNIT_30="$FIXrtma/prrtma_latlon_mpfactor.dat"
export XLFUNIT_40="$FIXrtma/prrtma_slmask.dat"
export XLFUNIT_88="twodvar_input_bi"

$EXECrtma/prrtma_firstguess >>$pgmout 2>errfile
export err=$?; err_chk
cat $pgmout

if [ $SENDCOM = YES ]
then
    cp twodvar_input_bi $COMOUT/${RUN}.t${cyc}z.2dvar_input
    cp slabs.dat  $COMOUT/${RUN}.t${cyc}z.slabs.dat
    cp slabs2.dat  $COMOUT/${RUN}.t${cyc}z.slabs2.dat
    cp $infile $COMOUT/${RUN}.t${cyc}z.ndfd
    cp prepbufr $COMOUT/${RUN}.t${cyc}z.prepbufr
    cp gridname_input $COMOUT/${RUN}.t${cyc}z.gridname_input
fi

#bsm - troubleshooting
echo "is the prepbufr file good?"
echo `ls -l $COMOUT/${RUN}.t${cyc}z.prepbufr`

########################################################

msg='$job ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

################## END OF SCRIPT #######################

