#!/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 14Z, 20Z and the next day's 08Z.

set -x

date=$1

# reset WRKDIR to be consistent with 'plotone':

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

cd $WRKDIR
sh $utilscript/setup.sh

# Figure out the ending time of the 24h period:
hh=`echo $date | cut -c9-10`
date1=`echo $date | cut -c1-8`12
if [ $hh -lt 12 ]; then
  date1=`/$EXECutil/ndate -24 $date1`
fi

# The beginning hour:
date0=`/$EXECutil/ndate -23 $date1`

msg="st2acc: from $date0 to $date1"
postmsg "$jlogfile" "$msg"

date=$date0

while [ $date -le $date1 ]; do
  day=`echo $date | cut -c1-8`
  cp $COMIN/${RUN}.$day/ST2*${date}.Grb.Z .
  date=`/$EXECutil/ndate +1 $date`
done

ls -1 *.Z > files.list

for file in `cat files.list`
do 
  uncompress $file
done

date12z=`/$EXECutil/ndate -24 $date1`
date18z=`/$EXECutil/ndate -18 $date1`
date00z=`/$EXECutil/ndate -12 $date1`
date06z=`/$EXECutil/ndate -06 $date1`

cat  > input_ml24 << EOF
obs
ST2ml
EOF

cat  > input_gg24 << EOF
obs
ST2gg
EOF

cat  > input_rd24 << EOF
obs
ST2rd
EOF

cat  > input_un24 << EOF
obs
ST2un
EOF

export pgm=nam_stage4_acc

for datebeg in $date12z $date18z $date00z $date06z
do
  dateend=`/$EXECutil/ndate +6 $datebeg`
  date=`/$EXECutil/ndate +1 $datebeg`

cat  > input_ml06 << EOF
obs
ST2ml
EOF
echo ST2ml${dateend}.06h >> input_ml24

cat  > input_gg06 << EOF
obs
ST2gg
EOF
echo ST2gg${dateend}.06h >> input_gg24

cat  > input_rd06 << EOF
obs
ST2rd
EOF
echo ST2rd${dateend}.06h >> input_rd24

cat  > input_un06 << EOF
obs
ST2un
EOF
echo ST2un${dateend}.06h >> input_un24

  while [ $date -le $dateend ]; do
    echo ST2ml$date.Grb >> input_ml06
    echo ST2gg$date.Grb >> input_gg06
    echo ST2rd$date.Grb >> input_rd06
    echo ST2un$date.Grb >> input_un06
    date=`/$EXECutil/ndate +1 $date`
  done

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

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

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

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

done

. prep_step
startmsg
$EXECnam_pcpn_anal/nam_stage4_acc < input_ml24
export err=$?;err_chk

. prep_step
startmsg
$EXECnam_pcpn_anal/nam_stage4_acc < input_gg24
export err=$?;err_chk

. prep_step
startmsg
$EXECnam_pcpn_anal/nam_stage4_acc < input_rd24
export err=$?;err_chk

. prep_step
startmsg
$EXECnam_pcpn_anal/nam_stage4_acc < input_un24
export err=$?;err_chk

# Now map all 06h and 24h accumulations to 15km:

for datebeg in $date12z $date18z $date00z $date06z
do
  dateend=`/$EXECutil/ndate +6 $datebeg`

  export XLFUNIT_11=ST2ml${dateend}.06h
  export XLFUNIT_51=multi15.${dateend}.06h
  $EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap

  export XLFUNIT_11=ST2gg${dateend}.06h
  export XLFUNIT_51=gage15.${dateend}.06h
  $EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap

  export XLFUNIT_11=ST2rd${dateend}.06h
  export XLFUNIT_51=rad15.${dateend}.06h
  $EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap

  export XLFUNIT_11=ST2un${dateend}.06h
  export XLFUNIT_51=radunb15.${dateend}.06h
  $EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap
done

export XLFUNIT_11=ST2ml${date1}.24h
export XLFUNIT_51=multi15.${date1}.24h
$EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap

export XLFUNIT_11=ST2gg${date1}.24h
export XLFUNIT_51=gage15.${date1}.24h
$EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap

export XLFUNIT_11=ST2rd${date1}.24h
export XLFUNIT_51=rad15.${date1}.24h
$EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap

export XLFUNIT_11=ST2un${date1}.24h
export XLFUNIT_51=radunb15.${date1}.24h
$EXECnam_pcpn_anal/nam_remap >> $pgmout 2>>errfile.remap

compress *.06h *.24h

day0=`echo $date0 | cut -c1-8`
day1=`echo $date1 | cut -c1-8`

if ! [ -d $COMOUT/${RUN}.$day0 ]; then
  mkdir -p $COMOUT/${RUN}.$day0
fi

if ! [ -d $COMOUT/${RUN}.$day1 ]; then
  mkdir -p $COMOUT/${RUN}.$day1
fi

#send to com and dbnet

ls -1 *${day0}*.06h.Z > files.list1
for file in `cat files.list1`
do 
   if test $SENDCOM = 'YES'
   then
      cp $file $COMOUT/${RUN}.$day0/$file
   fi
   if test $SENDDBN = 'YES'
   then
      $DBNROOT/bin/dbn_alert MODEL PCPNANL $job $COMOUT/${RUN}.$day0/$file 
   fi
done
ls -1 *${day1}*.06h.Z > files.list2
for file in `cat files.list2`
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
ls -1 *${date1}.24h.Z > files.list3
for file in `cat files.list3`
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 *${day0}*.06h.Z $COMOUT/${RUN}.$day0
###cp *${day1}*.06h.Z $COMOUT/${RUN}.$day1
###cp *${date1}.24h.Z $COMOUT/${RUN}.$day1


cat $pgmout 

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


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