#!/bin/ksh

#############################################################################
####  UNIX Script Documentation Block                                       #
#                                                                           #
# Script name:         exakrtma_getguess.sh.sms                             #
# Script description:  Read in first guess in Grib1 as created by smartinit #
#                      and rewrite as GSI-compliant initial condition       #
#                      Get copy of the prepbufr file which contains the obs #
#                      Generate GSI-compliant observation reject lists      #
#                                                                           #
# 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
####################################################
#CDATE00=`echo $CDATE |cut -c 1-6`00
#bsm - initially CYCLE00 not set and CDATE00 not
#bsm - used elsewhere - assuming ???
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=/com/nam/${envir}/nam.${YYYYMMDDused}/nam.t${HHused}z.smartak_rtmages${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.smartak_rtmages${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/akrtma.t${cyc}z.envir.sh

cp $COMIN/rtma.t${cyc}z.prepbufr.tm00 prepbufr

####################################################
#==> reject lists
####################################################
cp ${FIXrtma}/akrtma_bigrjlist.txt bigrjlist.txt

CDATEm1=`/nwprod/util/exec/ndate -01 $CDATE`
SDATE=`echo $CDATEm1 |cut -c1-8`
gesdir0=/nwges/${envir}/akrtma.${SDATE}

if [ -e ${gesdir0}/mass_rjlist.txt_dynamic_$CDATEm1} ] ; then 
  cp ${gesdir0}/mass_rjlist.txt_dynamic_$CDATEm1  mass_rjlist.txt_dynamic
fi 

if [ -e ${gesdir0}/t_rjlist.txt_dynamic_$CDATEm1 } ] ; then 
  cp ${gesdir0}/t_rjlist.txt_dynamic_$CDATEm1     t_rjlist.txt_dynamic
fi

if [ -e ${gesdir0}/q_rjlist.txt_dynamic_$CDATEm1 } ] ; then 
  cp ${gesdir0}/q_rjlist.txt_dynamic_$CDATEm1     q_rjlist.txt_dynamic
fi

if [ -e ${gesdir0}/p_rjlist.txt_dynamic_$CDATEm1 } ] ; then 
  cp ${gesdir0}/p_rjlist.txt_dynamic_$CDATEm1     p_rjlist.txt_dynamic
fi

#if [ -e ${gesdir0}/w_rjlist.txt_dynamic_$CDATEm1 } ] ; then 
#   cp ${gesdir0}/w_rjlist.txt_dynamic_$CDATEm1     w_rjlist.txt_dynamic
#fi
#####################################################################
# 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='alaska'
    lbiascor=.false.,
    lpbiascor=.false.,
    pbiascor=0.0,
    ltbiascor=.false.,
    tbiascor=0.0,
    lqbiascor=.false.,
    qbiascor=0.0,
    lubiascor=.false.,
    ubiascor=0.0,
    lvbiascor=.false.,
    vbiascor=0.0,
    ltdbiascor=.false.,
    tdbiascor=0.0,
    mkrjlists=.true.
/
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/akrtma_latlon_mpfactor.dat"
export XLFUNIT_40="$FIXrtma/akrtma_slmask.dat"
export XLFUNIT_88="twodvar_input_bi"

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

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.tm00
    cp gridname_input $COMOUT/${RUN}.t${cyc}z.gridname_input
    if [ -e t_rejectlist ] ; then
       cp t_rejectlist $COMOUT/${RUN}.t${cyc}z.t_rejectlist
    fi
    if [ -e q_rejectlist ] ; then
       cp q_rejectlist $COMOUT/${RUN}.t${cyc}z.q_rejectlist
    fi
    if [ -e p_rejectlist ] ; then
       cp p_rejectlist $COMOUT/${RUN}.t${cyc}z.p_rejectlist
    fi
    if [ -e w_rejectlist ] ; then
       cp w_rejectlist $COMOUT/${RUN}.t${cyc}z.w_rejectlist
    fi
fi

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

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

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

