#!/bin/sh

#############################################################
# Script: excdas2_prep.sh.sms
# Purpose: to obtain the observation and model precipitation
#          analysis from CDAS and DCOM
# Auther: Wesley Ebisuzaki
# Log:   2004-06-09 Initial script from Wesley Ebisuzaki
#        2004-06-10 Modified for production--Julia Zhu
#############################################################
set -x

cd $DATA

###################################################################
# get needed files from CDAS and GFS: prepbufr, snow, sst, ice
#   for day+1  (R2 needs +/- 24hr, +/- 12hr and 00hr prepbufr files
###################################################################
export wgrib=${wgrib:-/nwprod/util/exec/wgrib}

ierr=0
cp $COMCDAS/cdas.t${cyc}z.snogrb $COMOUT/${RUN}.t${cyc}z.snogrb
ierr=$(( $ierr + $? ))
cp $COMCDAS/cdas.t${cyc}z.sstgrb $COMOUT/${RUN}.t${cyc}z.sstgrb
ierr=$(( $ierr + $? ))
cp $COMCDAS/cdas.t${cyc}z.engicegrb $COMOUT/${RUN}.t${cyc}z.engicegrb
ierr=$(( $ierr + $? ))
cp $COMCDAS/cdas.t${cyc}z.prepbufr_pre-qc $COMOUT/${RUN}.t${cyc}z.prepbufr_pre-qc
ierr=$(( $ierr + $? ))

if [ $ierr -ne 0 ]; then
   echo "CDAS files are not available" 
   exit 8
fi

if [ "$CHGRP_RSTPROD" = 'YES' ]; then
   chgrp rstprod $COMOUT/${RUN}.t${cyc}z.prepbufr_pre-qc
   errch=$?
   if [ $errch -eq 0 ]; then
      chmod 640 $COMOUT/${RUN}.t${cyc}z.prepbufr_pre-qc
   else
      cp /dev/null $COMOUT/${RUN}.t${cyc}z.prepbufr_pre-qc
      warning=yes
   fi
fi

#####################################
# get observerd (pentad) precip file
#####################################
# find number of the current pentad
mmdd=`echo $PDY | cut -c5-8`
year=`echo $PDY | cut -c1-4`
nnumm=`grep -n "$mmdd" $FIXcdas2/cdas2_pentads | cut -d':' -f1`

# find previous pentad
if [ $nnumm -eq 1 ] ; then
   nnumm=73
   pyyyy=`expr $year - 1`
else
   nnumm="`expr $nnumm - 1`"
   pyyyy=$year
fi

# find start and finish of the previous pentad
nams=`sed -n "$nnumm p" $FIXcdas2/cdas2_pentads | cut -d' ' -f1`
namf=`sed -n "$nnumm p" $FIXcdas2/cdas2_pentads | sed 's/.* //g'`
ppdate=$pyyyy$nams

# now, ppdate = 1st day of previous pentad
#      nnum = number of previous pentad

#changed line 75 from egrep ":d=${ppdate}..:PRATE:sfc:0-5d ave:" 
cp $DCOMPCP/pingrainT62.grb.$pyyyy pingraint62
$wgrib -s -4yr pingraint62 |  \
   egrep ":d=${ppdate}..:PRATE:sfc:0-(5|6)d ave:" \
  | $wgrib -s  pingraint62 -grib -i -o $DATA/cdas2.t${cyc}z.obs_precip.tmp
$EXECUTIL/copygb -g98 -i3 -x $DATA/cdas2.t${cyc}z.obs_precip.tmp \
       $COMOUT/cdas2.t${cyc}z.obs_precip
rm $DATA/cdas2.t${cyc}z.obs_precip.tmp

if [ ! -s $COMOUT/cdas2.t${cyc}z.obs_precip ] ; then

  # Check to see for how many days since the pentad data has been missing
  # The job will be aborted after it reaches the max_nopentad day

  iday=0
  nday=$pyyyy$namf
  while [ $nday -le $PDY ]
  do
     iday=`expr $iday + 1`
     nday=`/nwprod/util/ush/finddate.sh $nday d+1`
  done
  if [ $iday -ge $max_nopentad ]
  then
     msg="The obs precip data has been missing for over the maximum \
          allowed days -- $max_nopentad days, please check with CPC's \ 
          Pingping Xie @571-241-7019 or email him at pingping.xie@noaa.gov"

     postmsg "$jlogfile" "$msg"
     echo "No Observation Precipitation data"
     export err=9; err_chk
  else
     echo "No obs precip for $ppdate" 
     echo "CDAS2 will continue without obs precip surface analysis"
  fi
fi

#######################################
# get model precip for previous pentad
#######################################

odate=$pyyyy${nams}00
ldate=$pyyyy${namf}18
cdate=$odate

[ -f $COMOUT/cdas2.t${cyc}z.mdl_precip ] && rm $COMOUT/cdas2.t${cyc}z.mdl_precip
while [ $cdate -le $ldate ]
do
   export archdate=`echo $cdate | cut -c1-6`
   eval filename="$COMARC.$archdate/flx.ft06.$cdate.grib"
   if [ ! -s $filename ] ; then
      echo "missing file $filename" 
      exit 9
   fi
   $wgrib $filename | egrep "(PRATE|RUNOF|LAND)" | \
      $wgrib -i -grib $filename -append -o $COMOUT/cdas2.t${cyc}z.mdl_precip
   cdate=`$EXECUTIL/ndate +6 $cdate` 
done
echo "done getting the model precipitation"

echo "Complete the CDAS PREP job"
exit
