#!/bin/ksh
####################################################################################
# Name of script:    exverf_precip_getppt.sh.sms
# Purpose of script: This script extracts the precip data from the various model 
#                    and outputs it in the format of $model_$yyyy$mm$dd$hh_$hr1_$hr2
#                    where hr1 and hr2 (3-digit) are the beginning and ending time of
#                    the accumulation period. 
# History:           
#
# Usage:  exverf_precip_getppt.sh.sms $yyyy$mm$dd
#  Models that we currently use:
#  -----------------------------
#  - Stage2
#  - Stage4
#  - nam
#  - namx
#  - namy
#  - gfs
#  - ngm
#  - westarw ($day)
#  - eastarw ($day)
#  - westnmm ($day)
#  - eastnmm ($day)
#  - ndas  ($daym1)
#  - ndassoil  ($daym1)
#  - ndasx ($daym1)
#  - ndasxsoil ($daym1)
#  - ndasy ($daym1)
#  - ndasysoil ($daym1)
#  - cmc, 00Z
#  - cmc, 12Z
#  - cmcglb, 00Z
#  - cmcglb, 12Z
#  - dwd 
#  - ecmwf ($daym1)
#  - ukmo ($daym1)
#  - jma

#  - various sref runs
#  - srmean    ($daym1)
#  - srmeanpar ($daym1)
#############################################################################
set -x

cd $DATA

if [ $# -lt 1 ]
then
   echo "Invalid argument"
   echo "usage: exverf_precip_getppt.sh.sms $yyyy$mm$dd"
   err_exit
fi

export YWGRIB=$USHverf_precip/verf_precip_ywgrib.pl
export day=$1
export daym1=`/nwprod/util/exec/ndate -24 ${day}12 | cut -c 1-8`
export dayp1=`/nwprod/util/exec/ndate +24 ${day}12 | cut -c 1-8`
export dayp2=`/nwprod/util/exec/ndate +48 ${day}12 | cut -c 1-8`
export yymmdd=`echo $day |cut -c3-8`
export mmdd=`echo $day |cut -c5-8`
export mmddm1=`echo $daym1 |cut -c5-8`
export mmddp1=`echo $dayp1 |cut -c5-8`
export mmddp2=`echo $dayp2 |cut -c5-8`

mkdir -p $COMOUT${day} $COMOUT${daym1}

# log of missing files:
export LOG=$COMOUT${day}/archlog.$day
rm -f $LOG
echo Missing files for $day: > $LOG
echo ' ' >> $LOG

#########################################################################
# Step 0: Copy the international model data from /dcom
#########################################################################
# Setup directory for imported files:
IMPORT=$DATA/import
mkdir $IMPORT

cd $IMPORT

# For UKMO:

# For ECMWF. precip files are in the format of
#   UWD${daym1}1200${mmdd}12001
#   UWD${daym1}1200${mmddp1}12001
#   UWD${daym1}1200${mmddp2}12001

export DCOM=${DCOM:-/dcom/us007003}
export ICOM=$DCOM/$day/qpf_verif
export ICOMm1=$DCOM/$daym1/qpf_verif
cp $ICOM/dwd_${day}* .
cp $ICOM/cmc_${day}* .
cp $ICOM/cmcglb_${day}* .
cp $ICOM/jma_${day}* .
cp $ICOM/ukmo.${day}* .
cp $ICOMm1/UWD${daym1}1200${mmdd}12001 UWD.${daym1}12.24
cp $ICOMm1/UWD${daym1}1200${mmddp1}12001 UWD.${daym1}12.48
cp $ICOMm1/UWD${daym1}1200${mmddp2}12001 UWD.${daym1}12.72

# CMC and DWD precip files only need to be re-named.  First, check for
# missing files:

for file in cmc_${day}00_012_036 cmc_${day}12_000_024 cmc_${day}12_024_048 \
            cmcglb_${day}00_012_036 cmcglb_${day}00_036_060 cmcglb_${day}00_060_084 \
            cmcglb_${day}12_000_024 cmcglb_${day}12_024_048 cmcglb_${day}12_048_072 \
            dwd_${day}00_012_036 dwd_${day}00_036_060 \
            dwd_${day}12_000_024 dwd_${day}12_024_048 dwd_${day}12_048_072
do
  if ! [ -s $file ]; then
    echo $file >> $LOG
  fi
done

# for CMC/CMCGLB/DWD files, we just need to rename them:

if [ $run_cmc = 1 -a $SENDCOM = YES ]
then
   # CMC:
   cp cmc_${day}00_012_036 $COMOUT$day/cmc_${day}00_012_036
   cp cmc_${day}12_000_024 $COMOUT$day/cmc_${day}12_000_024
   cp cmc_${day}12_024_048 $COMOUT$day/cmc_${day}12_024_048

   # CMCGLB:
   cp cmcglb_${day}00_012_036 $COMOUT$day/cmcglb_${day}00_012_036
   cp cmcglb_${day}00_036_060 $COMOUT$day/cmcglb_${day}00_036_060
   cp cmcglb_${day}00_060_084 $COMOUT$day/cmcglb_${day}00_060_084
   cp cmcglb_${day}12_000_024 $COMOUT$day/cmcglb_${day}12_000_024
   cp cmcglb_${day}12_024_048 $COMOUT$day/cmcglb_${day}12_024_048
   cp cmcglb_${day}12_048_072 $COMOUT$day/cmcglb_${day}12_048_072
fi

# DWD:
if [ $run_dwd = 1 -a $SENDCOM = YES ]
then
   cp dwd_${day}00_012_036 $COMOUT$day/dwd_${day}00_012_036
   cp dwd_${day}00_036_060 $COMOUT$day/dwd_${day}00_036_060
   cp dwd_${day}12_000_024 $COMOUT$day/dwd_${day}12_000_024
   cp dwd_${day}12_024_048 $COMOUT$day/dwd_${day}12_024_048
   cp dwd_${day}12_048_072 $COMOUT$day/dwd_${day}12_048_072
fi

# End of getting the international data

cd $DATA

# ST2-ST4(daym1)
# For Stage II and Stage IV, copy over the 24h totals, decompress, rename.
# (the yyyymmddhh in the original ST2/4 names are the ending hour of the 
# accumulation period):
#   Stage II: ST2ml${day}12.24h.Z   --> st2_${daym1}12_000_024
#   Stage IV: ST4.${day}12.24h.Z    --> st4_${daym1}12_000_024
# 
if [ $run_st2 = 1 -a $SENDCOM = YES ]
then
   ST2FILE=ST2ml${day}12.24h
   cp $COMST2/${ST2FILE}.Z .
   uncompress $ST2FILE 
   cp $ST2FILE $COMOUT${daym1}/st2_${daym1}12_000_024
fi

if [ $run_st4 = 1 -a $SENDCOM = YES ]
then
   ST4FILE=ST4.${day}12.24h
   cp $COMST4/${ST4FILE}.Z .
   uncompress $ST4FILE
   cp $ST4FILE $COMOUT${daym1}/st4_${daym1}12_000_024
fi

if [ $run_st2x = 1 -a $LOGNAME = wx22yl ]
then
   ST2XFILE=ST2ml${day}12.24h
   cp /meso/noscrub/wx22yl/stage2/nam_pcpn_anal.$day/${ST2XFILE}.Z .
   uncompress $ST2XFILE 
   cp $ST2XFILE $COMOUT${daym1}/st2x_${daym1}12_000_024
fi

########################################
# Now start retrieving NCEP model data:
########################################
cp $PARMverf_precip/verf_precip_input.domains input.domains
cat input.domains | while read tmp
do
  # skip the comment lines
  first_char=`echo $tmp |cut -c1`
  if [ $first_char = "#" ]
  then
     echo "It's a comment line, skip this line"
  else
    modnam=`echo $tmp |awk -F"|" '{print $1}'`
    convert=`echo $tmp |awk -F"|" '{print $2}'`
    cgrid=`echo $tmp |awk -F"|" '{print $3}'`
    grid=`echo $tmp |awk -F"|" '{print $4}'`
    input_file=`echo $tmp |awk -F"|" '{print $5}'`
    output_file=`echo $tmp |awk -F"|" '{print $6}'`
    cycles=`echo $tmp |awk -F"|" '{print $7}'`
    shour=`echo $tmp |awk -F"|" '{print $8}'`
    ehour=`echo $tmp |awk -F"|" '{print $9}'`
    intv=`echo $tmp |awk -F"|" '{print $10}'`
   
    let "getmod=run_$modnam"
    if [ "$getmod" = 1 ]
    then
      rm -rf input_card.${modnam}

      echo "$modnam  $convert  $cgrid" >input_card.${modnam}
      echo "$grid" >>input_card.${modnam}
      echo "$input_file" >>input_card.${modnam}
      echo "$output_file" >>input_card.${modnam}
      echo "$cycles" >>input_card.${modnam}

      # Create teh hourlist
      hourlist=""
      fhr=$shour
      while [ $fhr -le $ehour ]
      do
        hourlist="$hourlist $fhr"
        let "fhr=fhr+$intv"
        if [ $fhr -lt 10 ]; then fhr=0$fhr; fi
      done
      echo "$hourlist" >>input_card.${modnam}

      sed -e "s/_DAY_/$day/g" -e "s/_DAYm1_/$daym1/g" input_card.${modnam} >input_card.${modnam}.1
      mv input_card.${modnam}.1 input_card.${modnam}
    
      $USHverf_precip/verf_precip_getpptfcst.sh input_card.${modnam}
    fi
  fi
done

#########################################
# Now start to get the NDAS precip data:
#########################################
for mod in `cat $PARMverf_precip/verf_precip_input_ndas.domains`
do
   let "getmod=run_$mod"
   if [ $getmod = 1 ]
   then
     rm -rf input_card
     cp $PARMverf_precip/verf_precip_input_$mod input_card
     sed -e "s/_DAY_/$day/g" -e "s/_DAYm1_/$daym1/g" input_card >input_card.$mod

     $USHverf_precip/verf_precip_getpptndas.sh input_card.$mod
   fi
done

# produce a "24h (12Z-12Z) total" for ruc and rap, using the first 12h from
# yesterday's 12Z run and today's 00Z run.

if [ $run_rapg = 1 ]
then
  for mod in rap 
  do
    cd $COMOUT$daym1 
    cp ${mod}_${daym1}12_000_003 ${mod}12_${daym1}12_000_003 
    cp ${mod}_${daym1}12_003_006 ${mod}12_${daym1}12_003_006
    cp ${mod}_${daym1}12_006_009 ${mod}12_${daym1}12_006_009
    cp ${mod}_${daym1}12_009_012 ${mod}12_${daym1}12_009_012

    cd $COMOUT$day
    cp ${mod}_${day}00_000_003 $COMOUT$daym1/${mod}12_${daym1}12_012_015
    cp ${mod}_${day}00_003_006 $COMOUT$daym1/${mod}12_${daym1}12_015_018
    cp ${mod}_${day}00_006_009 $COMOUT$daym1/${mod}12_${daym1}12_018_021
    cp ${mod}_${day}00_009_012 $COMOUT$daym1/${mod}12_${daym1}12_021_024
  done
fi

#####################################################################
#
# Send GFS precip files to ftpprd for international 
# centers to fetch.
if [ $SENDDBN = YES ]
then
  cd $COMOUT${day}
  for gfsfile in `ls -1  gfs_*`
  do
    $DBNROOT/bin/dbn_alert MODEL VERIF_PRECIP $job ${COMOUT}${day}/$gfsfile
  done
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 #######################
