#!/bin/sh 
#######################################################################################
# Name of Script: exverf_precip_plotdata.sh.sms
# Purpose of Script: to plot the model precip data using GEMPAK
#
# History:
# 
# Usage: exverf_precip_plotdata.sh.sms $date
#######################################################################################

set -x

cd $DATA

msg="Begin job for $job"
postmsg "$jlogfile" "$msg"

NAGRIB_TABLE=/nwprod/gempak/fix/nagrib.tbl
cp /nwprod/gempak/fix/coltbl.xwp.wbg coltbl.xwp
cp /nwprod/gempak/fix/wmogrib.tbl .

export vdate=$1
export vday=`echo $vdate | cut -c 1-8`

export COMIN=${COMIN}.${vday}
export COMOUT=${COMOUT}.${vday}

# Copy the precip data over (generated from the 24h verification job)
cp $COMIN/modpcpsum.$vday.Z $DATA/.
uncompress modpcpsum.$vday.Z
tar -xvf modpcpsum.$vday

export vhour=24

typeset -Z3 fhr fhr0

cp $PARMverf_precip/verf_precip_plotpcp.domains plotpcp.domains
cat plotpcp.domains |while read tmp
do
   first_char=`echo $tmp |cut -c1`
   if [ "$first_char" = "#" ]
   then
     echo "This is a comment line, skip it"
   else
     mod=`echo $tmp |awk -F"|" '{print $1}'`
     hourlist=`echo $tmp |awk -F"|" '{print $2}'`
     grid=`echo $tmp |awk -F"|" '{print $3}'`

     for fhr in $hourlist
     do
       pdate=`/nwprod/util/exec/ndate -$fhr $vdate`
       fhr0=`expr $fhr - $vhour`
       GBFILE=${mod}_${pdate}_${fhr0}_${fhr}.$grid
       if [ -s $GBFILE ]; then
         GFFILE=${mod}.v${vdate}.${fhr}h.gif
         MOD=`echo $mod | tr '[a-z]' '[A-Z]'`
         $USHverf_precip/nam_pcpn_plotpcp.sh $GBFILE $GFFILE $vdate $vhour "$MOD ${fhr}h Forecast"
       fi
     done
   fi
done

# plot CPC analysis, or copy over the Stage IV 24h plot?
#
export VERFANL=`cat $COMIN/24h_verf_anl.${vday} | awk '{print $2}'`

if [ $VERFANL = CPCANL ]; then
  cp $COMIN/cpcanl.${vday}.grb .
  $USHverf_precip/nam_pcpn_plotpcp.sh cpcanl.${vday}.grb vanl.${vdate}.gif \
     ${vdate} 24 "CPC 1/8 deg Analysis"
else
  cp $COMHRLY/st4.${vdate}.24h.gif vanl.${vdate}.gif
fi

if [[ $RUN_ENVIR = dev && $LOGNAME = wx22yl ]]
then
  tar xvf /com/verf/prod/precip.$vday/pcpplot_${vdate}.tar
fi
  
# Let ush/verf_precip_indexplot.sh know whether CPC analysis or Stage IV is 
#  used.  If VERFANL=CPCANL, Stage IV will be shown on the left-hand-side.
#  If VERFANL=STAGE4 and CPC plot is available (i.e. the analysis exists but
#  has been deemed a bad one), the CPC plot is shown on the left-hand-side.  
ANLMARKER=$COMIN/24h_verf_anl.${vday}

if ! [ -s $ANLMARKER ]; then
  echo Verification for $vday not done.  Exit.   
  exit
fi

export VERFANL=`cat $ANLMARKER | awk '{print $2}'`

cd $DATA

# Create the index file and send them over to the RZDM server:
vyear=`echo $vdate |cut -c1-4`
vyearmon=`echo $vdate |cut -c1-6`
vday=`echo $vdate |cut -c1-8`

$USHverf_precip/verf_precip_indexplot.sh $vday

# Save the image and html files:
tar -cvf $COMOUT/pcpplot_${vdate}.tar *.gif index.html

# Send out the data to the webserver if running in the development mode (currently RZDM)
if [ $RUN_ENVIR = dev ]
then
   if [ $LOGNAME = wx22yl ]
   then
#    Put graphics under "TEST"?
     . /u/wx22yl/.Utils.sh
#    RZDMDIR=/home/www/emc/htdocs/mmb/ylin/$runmode/pcpverif/daily
     RZDMDIR=/home/www/emc/htdocs/mmb/ylin/pcpverif/daily
     ssh rzdm -l wd22yl "mkdir -p $RZDMDIR/$vyear/$vday"
     scp *.gif index.html wd22yl@rzdm:$RZDMDIR/$vyear/$vday/.
   else
     RZDMDIR=/home/www/emc/htdocs/mmb/yzhu/verification/precip/daily
     ssh -l $LOGNAME rzdm "mkdir -p $RZDMDIR/$vyear/$vyearmon"
     rsync -v -e ssh *.gif index.html $LOGNAME@rzdm:$RZDMDIR/$vyear/$vyearmon/$vday
   fi
# Clean up the old data in the directory:
   find . $COMIN -mtime +12 -exec rm -rf {} \;
fi

# Put the tar file on 45-day rotating archive:
if [ $RUN_ENVIR = dev -a $LOGNAME = "wx22yl" ]
then
  mkdir -p /meso/save/$LOGNAME/hold45days
  cp $COMOUT/pcpplot_${vdate}.tar /meso/save/$LOGNAME/hold45days/.
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 #######################


