#!/bin/ksh

#############################################################################
####  UNIX Script Documentation Block                                       #
#                                                                           #
# Script name:         exrtma_getguess.sh.sms                               #
# Script description:  Read in first guess in Grib1 as created by the RAP   #
#                      post 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
CDATEm1=`/nwprod/util/exec/ndate -01 $CDATE`
  
##########################################################
# Get the ndfd guess file for the previous hour from RAP
##########################################################
found_rapges=no
ic=1
while [ $ic -le 6 ] ; do
  typeset -2Z RAPFHH
  RAPFHH=$ic 
  RAPCYCLE=`/nwprod/util/exec/ndate -$RAPFHH $CDATE`
  RAPPDY=`echo $RAPCYCLE |cut -c1-8`
  RAPCC=`echo $RAPCYCLE |cut -c9-10`

  if [ -s $COMRAP/rap.${RAPPDY}/rap.t${RAPCC}z.smartrapconus${RAPFHH} ]; then
      infile_g187=$COMRAP/rap.${RAPPDY}/rap.t${RAPCC}z.smartrapconus${RAPFHH}
      echo "export RAPCYCLE=$RAPCYCLE" >> $COMOUT/rtma.t${cyc}z.envir.sh
      echo "export RAPFHH=$RAPFHH" >> $COMOUT/rtma.t${cyc}z.envir.sh
      found_rapges=yes
     break
  else
     let "ic=ic+1"
   fi
done

if [[ ${found_rapges} = no ]] ; then
    echo 'No NDFD guess available - exiting'
    exit 0
fi

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

cp ${FIXrtma}/rtma_bigrjlist.txt bigrjlist.txt

PDYm1=`echo $CDATEm1 |cut -c1-8`
gesdir0=/nwges/${envir}/rtma.${PDYm1}

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

infile=rap.guess.${CDATE}
/nwprod/util/exec/copygb -g197 -x $infile_g187 $infile

wgrib=/nwprod/util/exec/wgrib

typeset -1Z RAPFHH
rapfhr=${RAPFHH}hr

rm -f slabs.dat
$wgrib -s $infile | grep ":PRES:sfc:${rapfhr} fcst:" | $wgrib -i $infile         -o slabs.dat
$wgrib -s $infile | grep ":HGT:sfc:${rapfhr} fcst:"  | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":TMP:sfc:${rapfhr} fcst:"  | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":DPT:sfc:${rapfhr} fcst:"  | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":UGRD:sfc:${rapfhr} fcst:" | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":VGRD:sfc:${rapfhr} fcst:" | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":SPFH:sfc:${rapfhr} fcst:" | $wgrib -i $infile -append -o slabs.dat
$wgrib -s $infile | grep ":PRES:sfc:${rapfhr} fcst:" | $wgrib -i $infile -append -o slabs.dat #dummy field
$wgrib -s $infile | grep ":PRES:sfc:${rapfhr} fcst:" | $wgrib -i $infile -append -o slabs.dat #dummy field
$wgrib -s $infile | grep ":PRES:sfc:${rapfhr} fcst:" | $wgrib -i $infile -append -o slabs.dat #dummy field
#####################################################################
# run program that outputs first guess in the binary format

cat << EOF > gridname_input
&gridname
    cgrid='conus',
    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/rtma_latlon_mpfactor.dat"
export XLFUNIT_40="$FIXrtma/rtma_slmask.dat_exp2ndfd"
export XLFUNIT_88="twodvar_input_bi"

$EXECrtma/rtma_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_g187 $COMOUT/${RUN}.t${cyc}z.smartrapconus01
    cp $infile $COMOUT/${RUN}.t${cyc}z.ndfd        #Change suffix from "ndfd01" to "ndfd"/MPondeca,2June2007
    #cp prepbufr $COMOUT/${RUN}.t${cyc}z.prepbufr.tm00 #bsm - added .tm00 to end of prepbufr file
    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 #######################

