#!/bin/ksh
# 
# This script calculates the four 6-hourly accumulations
# (12-18Z, 18-00Z, 00-06Z, 06-12Z) and the 12-12Z accumulation, for the 24h
# period ending the previous 12Z.  Run at 15Z, 21Z and the next day's 09Z.
# The 6-hour accumulations from the first two will be used to supplement
# the Stage IV multi-hour accumulations. 

set -x

# read in the ending time (has to be 12Z) of the 24h period:

date1=$1

day1=`echo $date1 | cut -c1-8`
hh1=`echo $date1 | cut -c9-10`

if [ $hh1 -ne 12 ]; then
  msg="EXIT - stage4 24h acc - ending time needs to be 12Z!"
  postmsg "$jlogfile" "$msg"
  exit
fi

WRKDIR=$DATAST4/acc

if [ -e $WRKDIR ]; then
  rm -f $WRKDIR/*
else
  mkdir -p $WRKDIR
fi

cd $WRKDIR
sh $utilscript/setup.sh

date12z=`/nwprod/util/exec/ndate -24 $date1`
date18z=`/nwprod/util/exec/ndate -18 $date1`
date00z=`/nwprod/util/exec/ndate -12 $date1`
date06z=`/nwprod/util/exec/ndate -06 $date1`

cat  > input_acc24 << EOF
obs
ST4.
EOF

for datebeg in $date12z $date18z $date00z $date06z
do
  dateend=`/nwprod/util/exec/ndate +6 $datebeg`
  dayend=`echo $dateend | cut -c 1-8`
  file6h=ST4.${dateend}.06h
  cp $COMOUT/${RUN}.$dayend/$file6h.Z .
  uncompress $file6h
  echo $file6h >> input_acc24
done

export pgm=nam_stage4_acc
. prep_step
startmsg
$EXECnam_pcpn_anal/nam_stage4_acc < input_acc24 >> $DATA/$pgmout 2>errfile
export err=$?;err_chk

compress ST4.${date1}.24h*
ls -1 *${date1}.24h.Z > files.list4
for file in `cat files.list4`
do
   if test $SENDCOM = 'YES'
   then
      cp $file $COMOUT/${RUN}.$day1/$file
   fi
   if test $SENDDBN = 'YES'
   then
      $DBNROOT/bin/dbn_alert MODEL PCPNANL $job $COMOUT/${RUN}.$day1/$file
   fi
done


###cp *${date1}.24h.Z $COMOUT/${RUN}.$day1

#####################################################################
# GOOD RUN
set +x
echo "**************JOB NAM_PCPN_ST4ACC COMPLETED NORMALLY ON THE IBM SP"
echo "**************JOB NAM_PCPN_ST4ACC COMPLETED NORMALLY ON THE IBM SP"
echo "**************JOB NAM_PCPN_ST4ACC COMPLETED NORMALLY ON THE IBM SP"
set -x
#####################################################################


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



