#!/bin/ksh
########################################################################################
# Name of Script: exverf_precip_verfgen_03h.sh.sms
# Purpose of Script: Performs the 03h precipitation verification(00/03/06/09/12/15/18/21) 
#                    for $day, using Stage II analysis. The 'late' stage II run is made
#                    18 hours later, so data for ${day}21 would be ready before ${dayp1}16.
#                    We are running this job in the early morning each $day, for ${daym2}
# Arguments: exverf_precip_verfgen_03h.sh.sms $yyyy$mm$dd$cyc
# Log history:
########################################################################################
set -x

if [ $# -lt 1 ]
then
   echo "Invalid argument"
   echo "usage: verfgen_03h $yyyy$mm$dd$cyc"
   exit
fi

vdate=$1
vday=`echo $vdate |cut -c1-8`

export CPGB=${CPGB:-$utilexec/copygb}

cd $DATA

#############################
# Copy the mask files over:
#############################
cp $FIXverf_precip/verf_precip_regmask_211.Z regmask_211.Z
cp $FIXverf_precip/verf_precip_regmask_212.Z regmask_212.Z
cp $FIXverf_precip/verf_precip_regmask_218.Z regmask_218.Z
cp $FIXverf_precip/verf_precip_westmask_218.Z westmask_218.Z
cp $FIXverf_precip/verf_precip_eastmask_218.Z eastmask_218.Z

uncompress *mask*

# Analysis type in vsdb file is 'STAGE2':
export VERFANL=STAGE2

#################################################################
# get Stage2 hourly precip data covering 21Z $vdaym1 - 21Z $vday
#################################################################
vdaym1=`/nwprod/util/exec/ndate -24 ${vday}12 | cut -c 1-8`

cp ${COMHRLYm1}/ST2ml${vdaym1}22.Grb.Z .
cp ${COMHRLYm1}/ST2ml${vdaym1}23.Grb.Z .
cp ${COMHRLY}/ST2ml${vday}0*.Grb.Z .
cp ${COMHRLY}/ST2ml${vday}1*.Grb.Z .
cp ${COMHRLY}/ST2ml${vday}20.Grb.Z .
cp ${COMHRLY}/ST2ml${vday}21.Grb.Z .

uncompress ST2ml*.Grb.Z

accdate=${vday}00

while [ $accdate -le ${vday}21 ]; do
  time1=`/nwprod/util/exec/ndate -2 $accdate`  
  time2=`/nwprod/util/exec/ndate -1 $accdate`  
  time3=$accdate
  cat > input_acc3h << EOF
obs
ST2.
ST2ml$time1.Grb
ST2ml$time2.Grb
ST2ml$time3.Grb
EOF

  # Execute the program to calculate the accumulate precip:
  pgm=nam_stage4_acc
  . prep_step
  msg="`date` -- $pgm for verf_03 started"
  postmsg "$jlogfile" "$msg"

  startmsg
  $EXECverf_precip/nam_stage4_acc < input_acc3h >>$pgmout
  export err=$?; err_chk

  for grid in 218 
  do
    $CPGB -g${grid} -i3 -x ST2.$accdate.03h ST2.$accdate.03h.$grid
  done

  accdate=`/nwprod/util/exec/ndate +3 $accdate`  

done  # finished accumulating 3-hourly Stage II data.
  
#################################
# Run the verification  
#
# Arguments for verf_precip_verf3.sh:
#  1. Model name
#  2. Verification date/hour
#  3. Verification length
#  4: grids to be verified on
#  5: model cycles
#  6: bucket length
#  7: range of model forecast
#  8: special mask?  (nests)
#################################
mkdir $DATA/vsdb

cp $PARMverf_precip/verf_precip_verf03.domains verf03.domains
cat verf03.domains |while read tmp
do
   first_char=`echo $tmp |cut -c1`
   if [ "$first_char" = "#" ]
   then
     echo "This is a comment line, skip it"
   else
     modnam_m1=${modnam:-none}
     modnam=`echo $tmp |awk -F"|" '{print $1}'`
     vlen=`echo $tmp |awk -F"|" '{print $2}'`
     grids=`echo $tmp |awk -F"|" '{print $3}`
     cycs=`echo $tmp |awk -F"|" '{print $4}'`
     blen=`echo $tmp |awk -F"|" '{print $5}'`
     fcsthour=`echo $tmp |awk -F"|" '{print $6}'`
     nests=`echo $tmp |awk -F"|" '{print $7}'`

     let "runmod=run_$modnam"
     if [ $runmod = 1 ]
     then
       if [ $modnam != $modnam_m1 ]
       then
         if [ -s $COMVSDB/$modnam/${modnam}_${vday}.vsdb ]
         then
           sed -e "/APCP\/03/d" $COMVSDB/$modnam/${modnam}_${vday}.vsdb >$COMVSDB/$modnam/${modnam}_${vday}.vsdb1
           mv $COMVSDB/$modnam/${modnam}_${vday}.vsdb1 $COMVSDB/$modnam/${modnam}_${vday}.vsdb
         fi
       fi

       $USHverf_precip/verf_precip_verf03.sh $modnam $vday $vlen "$grids" "$cycs" $blen $fcsthour $nests
    fi
  fi
done

cd vsdb
tar cvf ../vsdb3.$vday.tar .

cd $DATA

if [ $SENDCOM = YES ]
then
  cp vsdb3.$vday.tar ${COMOUT}.${vday}/.
 
  if [ $RUN_ENVIR = "dev" -a $LOGNAME = "wx22yl" ]
  then
    cp vsdb3.$vday.tar /meso/save/$LOGNAME/hold45days/.
    scp vsdb3.$vday.tar gcp@rzdm:/home/ftp/emc/mmb/gcp/precip/files/.
  fi
fi

###############################################################################
# For operational implementation: send the tar'd VSDBs to ftpprd.
###############################################################################
if [ $SENDDBN = YES ]
then
  $DBNROOT/bin/dbn_alert MODEL VERIF_PRECIP $job ${COMOUT}.${vday}/vsdb3.$vday.tar
fi

#####################################################################
# GOOD RUN
set +x
echo "**************$job COMPLETED NORMALLY on `date`"
set -x
#####################################################################

msg="HAS COMPLETED NORMALLY!"
echo $msg
postmsg "$jlogfile" "$msg"

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