#!/bin/sh 
########################################################################################
# Name of Script: exverf_precip_verfgen_24h.sh.sms
# Purpose of Script: To generate the 24h (12Z-12Z) precipitation verification 
#   F/H/O and SL1L2 statistics for various operational models to be used by the
#   Forecast Verification System
# Arguments: exverf_precip_verfgen_24h.sh.sms $yyyy$mm$dd
# Log history:
########################################################################################
set -x

mincpcvalpts=${mincpcvalpts:-50000}

if [ $# -lt 1 ]
then
   echo "Invalid argument"
   echo "usage: verfgen_24h $yyyy$mm$dd"
   exit
fi
export CPGB=${CPGB:-$utilexec/copygb}

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

mkdir $DATA/vsdb ${COMOUT}.${vday}


# 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*

# Prepare the verifying analysis file.  Use CPC 1/8 deg analysis if it is
# available and if the GRIB'd file is greater than 95,000 bytes (faulty files
# are nearly always smaller than that, good files are larger).  Otherwise
# use Stage IV 24h sum.
#
# Unit 52,53,54 are the 1/2/3-day "persistence forecasts".

cp /dcom/us007003/$vday/wgrdbul/$vday.ll .

if [ -s $vday.ll ]; then
  datem1=`/nwprod/util/exec/ndate -24 ${vday}12`  
  echo $datem1 > startdate
  export XLFRTEOPTS="unit_vars=yes"
  export XLFUNIT_11="startdate"
  export XLFUNIT_12="$vday.ll"
  export XLFUNIT_51="cpcanl.$vday.grb"
  export XLFUNIT_52="psstcpanl_${vday}12_000_024"
  export XLFUNIT_53="psstcpanl_${vday}12_024_048"
  export XLFUNIT_54="psstcpanl_${vday}12_048_072"
  export XLFUNIT_55="itrue.cpcanl.$vday"
  $EXECverf_precip/nam_cpc2grbrfc8

  if [[ -s cpcanl.$vday.grb && -s itrue.cpcanl.$vday ]]; then
    cp itrue.cpcanl.$vday ${COMOUT}.${vday}/.
    validpts=`cat itrue.cpcanl.$vday`
    if [ $validpts -lt $mincpcvalpts ]; then
      echo There are only $validpts valid points in CPC analysis
      export VERFANL=STAGE4
    else
      export VERFANL=CPCANL
    fi
  else 
    echo no valid CPC anl or point count file
    export VERFANL=STAGE4
  fi
else
  export VERFANL=STAGE4
  echo 'Cannot find CPC analysis on /dcom'
fi

if [ $VERFANL = CPCANL ]; then
  cp cpcanl.$vday.grb ${COMOUT}.${vday}/.
  cp psstcpanl_${vday}12_* ${COMIN}.${vday}/.
  cp cpcanl.$vday.grb vanl24.$vdate
  echo $vday ' CPCANL' > ${COMOUT}.${vday}/24h_verf_anl.$vday
  echo $vday ' CPCANL' >> $COMVSDB/24h_verf_anl.log
else
  cp $COMHRLY/ST4.${vday}12.24h.Z .
  uncompress ST4.${vday}12.24h.Z
  if [ -s ST4.${vday}12.24h ]; then
    export VERFANL=STAGE4
    cp ST4.${vday}12.24h ${COMOUT}.${vday}/.
    cp ST4.${vday}12.24h vanl24.$vdate
    echo $vday ' STAGE4' > ${COMOUT}.${vday}/24h_verf_anl.$vday
    echo $vday ' STAGE4' >> $COMVSDB/24h_verf_anl.log
  else
    echo No valid Stage IV file either.  Exit.
    exit
  fi
fi

for grid in 211 212 218 
do
  $CPGB -g${grid} -i3 -x vanl24.$vdate vanl24.${grid}.$vdate
done

# Arguments for verf_precip_verf24.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 to be verified.  This is not necessarily the 
#       limit of the model forecast length - e.g. forecast might be out to
#       10 days but only verified for 84 hours, then this number should
#       be '84'.  If this number is '96' and the model has a 12Z cycle,
#       then 96h fcst will be verified.
#  8: special mask?  (nests)

cp $PARMverf_precip/verf_precip_verf24.domains verf24.domains
cat verf24.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\/24/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_verf24.sh $modnam $vdate $vlen "$grids" "$cycs" $blen $fcsthour $nests
     fi
   fi
done
 
##########################################################################
# Compute 24h and 48h precipitation forecast scores from the average of 8
# Operational models
##########################################################################
if [ $run_medley = 1 ]
then
  cd $DATA
  $USHverf_precip/verf_precip_medley24.sh $vdate
fi

cd $DATA/vsdb

# tar up the VSDB files so job on tempest can fetch them.

tar cvf ../vsdb24.$vday.tar .
cd ..

# save the *.212 and *.218 files in case we need to re-plot them:

cd $DATA
tar cvf modpcpsum.$vday *_*.212 *_*.218 vanl24.218.*
compress modpcpsum.$vday

if [ $SENDCOM = YES ]
then
  cp vsdb24.$vday.tar ${COMOUT}.${vday}/. 
  cp modpcpsum.$vday.Z ${COMOUT}.${vday}/.

  if [ $RUN_ENVIR = dev ]
  then
    if [ $LOGNAME = "wx22yl" ]
    then
      mkdir /meso/save/$LOGNAME/hold45days
      cp vsdb24.$vday.tar /meso/save/$LOGNAME/hold45days/.
      scp vsdb24.$vday.tar gcp@rzdm:/home/ftp/emc/mmb/gcp/precip/files/.
    fi
  fi

  if [ $SENDDBN = YES ]
  then
    $DBNROOT/bin/dbn_alert MODEL VERIF_PRECIP $job ${COMOUT}.${vday}/vsdb24.$vday.tar
  fi
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 #######################
