#!/bin/sh

set -x

############################################################################
echo "---------------------------------------------------------------------"
echo "cpc_make_dlysum.sh.sms preprocessing"
echo "---------------------------------------------------------------------"
#
############################################################################
#
# Function:  Creates the Climate Prediction Center daily global precipitation
#            and temperature summary file.
#
############################################################################
# author:    George G. Fulwood
#
# created:   10/28/2003
#
############################################################################
#
# Input Files:
#
#  *******************************************************************  
#
#          CPC Development Name       CPC Operational Name
#
#          fort.1 metar_slibhyq.dat   fort.11 cpc.metar.library.dat
#                                     READ IN SUBROUTINE RDLIB
#
#          fort.3 weatbl.dat          fort.12 cpc.weather.table.dat
#                                     READ IN SUBROUTINE INIT
#
#          fort.11 prime.dat          fort.13 cpc.prime.time.dat
#                                     READ IN SUBROUTINE INIT  
#
#          fort.15 card.dat           fort.14 cpc.process.dlysum_dates.dat
#                                     READ IN SUBROUTINE INIT
#
#          fort.8  hrly.dat           fort.15 cpc.10zto6z.metar.dat
#                                     METAR DATA READ IN SUBROUTINE INMET 
#
#          fort.50 edfile.dat         fort.16 cpc.global.synoptic.obs.dat
#                                     SYNOPTIC DAT READ IN SUBROUTINE INSYN
#
##############################################################################
#
# Output Files:
#
##############################################################################
#
#          CPC Development Name       CPC Operational Name
#
#          fort.9 pdiag.dat           fort.51 cpc.prcp.diagnostics.dat
#                                     WRITTEN IN SUBROUTINE SUMDAY 
#
#          fort.12 badpval.dat        fort.52 cpc.bad.prcpvals.dat 
#                                     WRITTEN IN SUBROUTINE PSUM
#
#          fort.10 wzfile.dat         fort.53 cpc.dailysum.wzfile.dat 
#                                     WRITTEN IN SUBROUTINE OUTSUM  
#
#          fort.13 hspr.dat           fort.54 cpc.dailysum.hspr.dat 
#                                     WRITTEN IN SUBROUTINE OUTSUM
#
#          fort.14 qzfl_62new.dat     fort.55 cpc.dailysum.qzfl.dat
#                                     WRITTEN IN SUBROUTINE OUTSUM
#
#          fort.16 sdfile.dat         fort.56 cpc.dailysum.snowfile.dat 
#                                     (SNOW FILE) WRITTEN IN SUBROUTINE OUTSUM
#
#          fort.17 syncmp.dat         fort.57 cpc.dailysum.syncmp.dat 
#                                     WRITTEN IN SUBROUTINE SUMDAY
#
#          fort.18 metcmp.dat         fort.58 cpc.dailysum.metcmp.dat
#                                     WRITTEN IN SUBROUTINE SUMDAY
#############################################################################
#
#  Setup work files and test variables
#
touch cpc.dailysum.wzfile.dat      cpc.dailysum.hspr.dat
touch cpc.dailysum.qzfl.dat    cpc.dailysum.snowfile.dat 
touch cpc.dailysum.syncmp.dat    cpc.dailysum.metcmp.dat
#
zvoh='99KZZV 99KYKN 99KYIP 99KXVG 99KXNA 99KWYS 99KWWD 99KWVL 99KWVI 99KWST'
LABEL='CPCG03C'
BLANK=' '
#
#  determine processing and validation days
#
echo $PDYm2 $PDYm1 $PDY >   cpc.process.dlysum_dates.dat 
#
# echo 'input dates are ' $PDYm2 $PDYm1 $PDY
echo 'input dates are ' 
cat cpc.process.dlysum_dates.dat
#
# Copy formatted metar and synoptic data files from /com to current 
# working directory work files
#
cp $COMIN/cpc.t10z_$PDYm1.tot09z_$PDY.metar.hrlsr       hrlsr_ops.dat
#
cp $COMIN/cpc.t10z_$PDYm2.tot09z_$PDYm1.metar.hslsr     hslsr_ops.dat
#
cp $COMOUT/cpc.t03z_$PDYm2.tot06z_$PDY.edfile  cpc.global.synoptic.obs.dat 
#
# Combine metar files to produce 48 hours of continuous metar observations
#
cat hslsr_ops.dat hrlsr_ops.dat >                     hrly_ops.dat
#
sort -o cpc.10zto6z.metar.dat -T $DATA -k1,1 hrly_ops.dat 
rm hrly_ops.dat
#
# Initiate prep step for using xlfunit
#
export pgm=cpc_make_dlysum
. prep_step 
#
#   Copy fixed files to work area
#
cp $CPCfix/cpc.metar.library.dat               .
cp $CPCfix/cpc.weather.table.dat               .
cp $CPCfix/cpc.prime.time.dat                  .
cp $CPCfix/cpc.prcp.diagnostics.dat            .
cp $CPCfix/cpc.bad.prcpvals.dat                .
#
#  input files
#
export XLFUNIT_11="cpc.metar.library.dat"
export XLFUNIT_12="cpc.weather.table.dat"
export XLFUNIT_13="cpc.prime.time.dat"
export XLFUNIT_14="cpc.process.dlysum_dates.dat"
export XLFUNIT_15="cpc.10zto6z.metar.dat"
export XLFUNIT_16="cpc.global.synoptic.obs.dat"
#
#   output files
#
export XLFUNIT_51="cpc.prcp.diagnostics.dat"
export XLFUNIT_52="cpc.bad.prcpvals.dat"
export XLFUNIT_53="cpc.dailysum.wzfile.dat"
export XLFUNIT_54="cpc.dailysum.hspr.dat"
export XLFUNIT_55="cpc.dailysum.qzfl.dat"
export XLFUNIT_56="cpc.dailysum.snowfile.dat"
export XLFUNIT_57="cpc.dailysum.syncmp.dat"
export XLFUNIT_58="cpc.dailysum.metcmp.dat"
#
startmsg
$CPCexec/cpc_make_dlysum >> $pgmout 2>errfile
export err=$?;err_chk
#
#  Eliminate any bad date records from the U.S. only data file
#
# exit
cp cpc.dailysum.hspr.dat    cpc.dailysum.hspr.hold
grep -v $PDY cpc.dailysum.hspr.dat > holdit
mv holdit cpc.dailysum.hspr.dat
#
# create a copy of daily summary plot file and substitute data for Marquette,MI 
#
cp cpc.dailysum.qzfl.dat    cpc.dailysum.qzfl.hold
#
# Create dummy dataset for Marquette
#
if [ -f mqt.dat ]
 then
  chmod 777 mqt.dat
  rm mqt.dat
  touch mqt.dat
  chmod 777 mqt.dat
fi
#
grep MQT cpc.dailysum.qzfl.dat > mqt.dat
cat mqt.dat
#
# use the sed command to change block from 99MQT to 72743
#
sed 's/99999MQT/72743MQT/' mqt.dat > nmqt.dat
cat mqt.dat
cat nmqt.dat
#
# now write record to end of daily file
#
cat nmqt.dat >> cpc.dailysum.qzfl.dat
chmod 777 nmqt.dat
rm nmqt.dat
#
#  sort daily file to get 72743 in the correct spot
#
sort -o sqzfl_62new.dat -T $DATA -k1,1 cpc.dailysum.qzfl.dat
#
# move sorted file back to original file
#
mv sqzfl_62new.dat cpc.dailysum.qzfl.dat
#
# Count number of lines in file cpc.dailysum.qzfl.dat to determine if
# processing completed normally. The file should contain atleast 6000 
# stations(which puts us inside block 98) 
#
wc -l cpc.dailysum.qzfl.dat > lastline
cat lastline
num=`cut -c 10-14 lastline`
min=6800
#
echo ' number of stations in cpc.dailysum.qzfl.dat is '  $num
#
echo ' number of stations needed for processing is ' $min
#
echo ' '
#
# Determine if last station in file is one of the 10 specified by variable 
# zvoh, else print abnormal termination message.
#
tail -1 cpc.dailysum.qzfl.dat > laststn
cat laststn
stn=`cut -c 13-18 laststn`
idate=`cut -c 1-8 laststn`
echo ' station and date are ' $stn $idate
#
fblk=`cut -c 10-11 laststn`
rm laststn
#
#  Print the first 10 and last 10 stations of the qzfl file
#
echo 'The first 10 lines of cpc.dailysum.qzfl.dat'
#
echo ' '
#
head -10 cpc.dailysum.qzfl.dat
#
echo ' '
#
echo 'The last 10 lines of cpc.dailysum.qzfl.dat'
#
echo ' '
#
tail -10 cpc.dailysum.qzfl.dat
#
echo ' '
#
echo ' last station in qzfl_62new.dat is ' $stn
# 
#  Are enough stations present to continue processing?
#
msg='Insufficient number of stations'
ok='F'
#
for i in $zvoh
do
 if [ $ok = 'F' ]
  then
   if [ $stn = $i ] && [ $idate = $PDYm1 ] 
    then
     echo 'stn='$stn ' i='$i ' idate='$idate ' nday='$nday
#
#   archive todays files
#
     if [ $stn = $zvoh ] && [ $idate = $PDYm1 ] 
#
      then
       echo ' stn is '$stn ' zvoh is '$zvoh

     if test $SENDCOM = "YES" 
     then
         #
         #   archive todays files
         #
              cp cpc.dailysum.qzfl.dat              cpc.dailysum.qzfl_$PDYm1.dat
              cp cpc.dailysum.qzfl.dat      $COMOUT/cpc.dailysum.qzfl_$PDYm1.dat

         #
              cp cpc.dailysum.snowfile.dat          cpc.dailysum.snowfile_$PDYm1.dat
              cp cpc.dailysum.snowfile.dat  $COMOUT/cpc.dailysum.snowfile_$PDYm1.dat
         #
              cp cpc.dailysum.wzfile.dat            cpc.dailysum.wx$PDYm1.dat
              cp cpc.dailysum.wzfile.dat    $COMOUT/cpc.dailysum.wx$PDYm1.dat
         #
              cp cpc.dailysum.hspr.dat              cpc.dailysum.hspr_$PDYm1.dat
              cp cpc.dailysum.hspr.dat      $COMOUT/cpc.dailysum.hspr_$PDYm1.dat
         #
         #
         #    Alert files to DBNet
         #
              if [ $SENDDBN = "YES" ]; then
                 $DBNROOT/bin/dbn_alert MODEL CPCMETSYN  $job  $COMOUT/cpc.dailysum.qzfl_$PDYm1.dat
                 $DBNROOT/bin/dbn_alert MODEL CPCMETSYN  $job  $COMOUT/cpc.dailysum.snowfile_${PDYm1}.dat
                 $DBNROOT/bin/dbn_alert MODEL CPCMETSYN  $job  $COMOUT/cpc.dailysum.wx$PDYm1.dat
                 $DBNROOT/bin/dbn_alert MODEL CPCMETSYN  $job  $COMOUT/cpc.dailysum.hspr_$PDYm1.dat
              fi

     fi

# Reset conditional operator to end loop
#
     ok='T'
#
# Set normal job termination message
#
     msg='Job completed normally.'
#
#  If the last station is one of the 10 stations we use to verify file
#  completion, continue processing, otherwise there are insufficient
#  stations in file to continue
#
    else
     echo ' stn ' $i ' missing for day '$nday' continue search' 
     echo ' stn ' $i ' missing for day '$nday' continue search' >> $DATA/make_dlysum.log
#
     echo ' if no match found in list, processing terminates.'
     echo ' if no match found in list, processing terminates.' >> $DATA/make_dlysum.log
#
   fi
  fi
 fi
#msg='Insufficient number of stations'
done
#
# end of script
#
echo $msg
postmsg "$jlogfile" "$msg"

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