echo "------------------------------------------------"
echo "  Verification - QCMON Data Processing"
echo "------------------------------------------------"

###############################################################################
# This utility script performs the following functions:                       #
#   1. Generates the header files and the appropriate data cards              #
#   2. Joins daily stats from qcmond.s and then stores the output             #
#      data in the approaiate directory                                       #
#   3. Generates monthly summaries reports of aircraft, AMDAT/ASDAR           #
#      and ACARS data                                                         #
#   4. Generates monthly summaries reports of radiosonde observation          #
#      of geopotential height and wind                                        #
#   5. Writes ship-based monthly summary table for ship-based radiosonde      #
#      observations                                                           #
#   6. Reads monthly summary statistics written by raobsum and writes         #
#      table of wind statistics for stations having one level exceeding a     #
#      certain normalized threshhold                                          #
#   7. Gemerates the WMO Monthly Reports                                      #
#   8. Archiving the monthly data                                             #
#   9. Cleaning up the fix files and compressing the data                     #
###############################################################################

msg="VERFM_QCMON_RPTS HAS BEGUN"
postmsg "$jlogfile" "$msg"

set -x

cd $DATA

export XLFRTEOPTS="unit_vars=yes"

################################################################################
# SECTION 1.  QCAIRSUM.s makes separate monthly summaries for aircraft,        #
# AMDAT/ASDAR, and ACARS.  Writes tables giving identification characters,     #
# and the following wind and temperature statistics: mean difference           #
# (OBS - FG), RMS difference, number of obs, number of "gross" differences,    #
# and number of rejections by quality-marker.  In additions, mean speed        #
# difference, RMS vector difference, mean temperature difference, and RMS      #
# temperature difference are computed for all observations within each         #
# five-degree latitude-longitude "square".                                     #
################################################################################
 
export XLFUNIT_7="options"
export XLFUNIT_11="$QCMON_MONTHLYdata/qcair$mm"
export XLFUNIT_12="$QCMON_MONTHLYdata/qcasd$mm"
export XLFUNIT_13="$QCMON_MONTHLYdata/qcacr$mm"
export XLFUNIT_21="airbox$mm"
export XLFUNIT_22="asdbox$mm"
export XLFUNIT_23="acrbox$mm"
export XLFUNIT_31="airsumxx"
export XLFUNIT_32="asdsumxx"
export XLFUNIT_33="acrsumxx"
 
$EXEChrly/verf_qcairsum 1>qcairsum.out 2>qcairsum.err
  export err=$?; err_chk
 
############################################
# sort table by descending order of rmsv   #
# sorting aircraft by carrier table ...    #
############################################
     
tail +4 airsumxx > unsorted
  sort -o sorted -r -n -k 3,3 unsorted
    cat sorted >> $QCMON_MONTHLYreports/airsum$mm
     
tail +4 asdsumxx > unsorted
  sort -o sorted -r -n -k 3,3 unsorted
    cat sorted >> $QCMON_MONTHLYreports/asdsum$mm
     
tail +4 acrsumxx > unsorted
  sort -o sorted -r -n -k 3,3 unsorted
    cat sorted >> $QCMON_MONTHLYreports/acrsum$mm
     
#######################
# SECTION 1. FINISH   #
#######################

###########################################################################
# SECTION 2. RAOBSUM.s generates raw monthly statistics of radiosonde     #
# observation of geopotential height and wind compared to first           #
# guess values at the same location and cycle time.  The number of        #
# reports, the mean and RMS differences of ( OBS - FG ), and number       #
# of "gross" differences are computed separately for 00Z and 12Z          #
# cycles, for each mandatory level.                                       #
###########################################################################
 
export XLFUNIT_7="options"
export XLFUNIT_10="$QCMON_MONTHLYdata/qcraob$mm"
export XLFUNIT_20="raw$mm"
 
$EXEChrly/verf_raobsum  1>raobsum.out 2>raobsum.err
  export err=$?; err_chk
 
################################################################
# Raw Statistics of radiosonde and gross differences ...done   #
################################################################

cp  raobsum.out raw$mm
 
#######################
# SECTION 2. FINISH   #
#######################

################################################################################
# SECTION 3. SHIPSUM.s writes ship-based monthly summary table for ship-based  #
# radiosonde observations versus concurrent and collocated first guess values  #
# at each mandatory level.  The table provides the ship call sign, pressure    #
# level, number of geopotential height observations, number of "gross"         #
# ( vector obs - FG ) differences, mean of ( UOBS - FG) and ( VOBS - FG )      #
# differences, and RMSV differences.   These statistics are provided separately#
# for 00Z and 12Z observations.  The table is manually edited to delete ship   #
# summaries for which there were generally fewer than 10 observations per      #
# pressure level for each cycle ( 00Z and 12Z ).                               #
################################################################################
 
export XLFUNIT_7="options"
export XLFUNIT_10="$QCMON_MONTHLYdata/qcship$mm"
export XLFUNIT_20="shipsum$mm"
 
$EXEChrly/verf_shipsum  1>shipsum.out 2>shipsum.err
  export err=$?; err_chk
 
########################################################
# 'Ship - based radiosonde ...done                     #
# The following puts the correct title into shpsum$mm  #
########################################################
 
if [ "$mm" = "01" ]; then
 sed -e 's/XXXXXXXXX/  JANUARY/g' shipsum.out > shpsum1

 elif [ "$mm" = "02" ]; then
   sed -e 's/XXXXXXXXX/ FEBRUARY/g' shipsum.out > shpsum1

   elif [ "$mm" = "03" ]; then
     sed -e 's/XXXXXXXXX/    MARCH/g' shipsum.out > shpsum1

     elif [ "$mm" = "04" ]; then
       sed -e 's/XXXXXXXXX/    APRIL/g' shipsum.out > shpsum1

       elif [ "$mm" = "05" ]; then
         sed -e 's/XXXXXXXXX/      MAY/g' shipsum.out > shpsum1

         elif [ "$mm" = "06" ]; then
           sed -e 's/XXXXXXXXX/     JUNE/g' shipsum.out > shpsum1

       elif [ "$mm" = "07" ]; then
         sed -e 's/XXXXXXXXX/     JULY/g' shipsum.out > shpsum1

      elif [ "$mm" = "08" ]; then
       sed -e 's/XXXXXXXXX/   AUGUST/g' shipsum.out > shpsum1

     elif [ "$mm" = "09" ]; then
       sed -e 's/XXXXXXXXX/SEPTEMBER/g' shipsum.out > shpsum1

   elif [ "$mm" = "10" ]; then
     sed -e 's/XXXXXXXXX/  OCTOBER/g' shipsum.out > shpsum1

  elif [ "$mm" = "11" ]; then
    sed -e 's/XXXXXXXXX/ NOVEMBER/g' shipsum.out > shpsum1

else 
  sed -e 's/XXXXXXXXX/ DECEMBER/g' shipsum.out > shpsum1

fi

#########################################################
# Removing the old shpsumXX which has the wrong title   #
# Them making shpsumXX with the right title             #
#########################################################
 
 $EXEChrly/verf_rmships < shpsum1 > shpsum2
  export err=$?; err_chk

   grep -v "1 SHIP" shpsum2 > $QCMON_MONTHLYreports/shpsum$mm

#######################
# SECTION 3. FINISH   #
#######################

################################################################################
# SECTION 4. VSTATS.s and ZSTATS.s read initial monthly summary statistics     #
# written by raobsum.f and writes table of wind statistics for stations having #
# at one level for which the RMSV difference between observation and first 
# guess exceeds a certain normalized ( by pressure level ) threshhold.   Only  ## the statistics for the lowest such pressure level ( if multiple ) is printed,#
# but the number of offending pressure levels is noted in a separate column.   #
# Other statistics printed are the station block and id number, the cycle ( 00Z#
# or 12Z ), the mean and RMSV (OB - FG) difference, the number of observations #
# ( minium of 10 ), and the number of "gross" differences.                     #
################################################################################
 
export XLFUNIT_7="raw$mm"
 
$EXEChrly/verf_vstats  1>vstats.out 2>vstats.err
  export err=$?; err_chk
 
#############################################################
#  sort the vstatx file; create vstats (finished product)   #
#############################################################
     
head -8 vstats.out > $QCMON_MONTHLYreports/vstats$mm
  tail +9 vstats.out > unsorted
    sort -o vsort  -n +0 -1 unsorted
 
  cat vsort >> $QCMON_MONTHLYreports/vstats$mm
 
##############################################################################
#    ZSTATS.s reads initial monthly summary statistics written by raobsum.f  #
#    and writes table of wind statistics for stations having at one level    #
#    for which the RMSV difference between observation and first guess       #
#    exceeds a certain normalized ( by pressure level ) threshhold.   Only   #
#    the statistics for the lowest such pressure level ( if multiple ) is    #
#    printed, but the number of offending pressure levels is noted in a      #
#    separate column.   Other statistics printed are the station block and   #
#    id number, the cycle ( 00Z or 12Z ), the mean and RMSV (OB - FG)        #
#    difference, the number of observations ( minium of 10 ), and the number #
#    of "gross" differences.                                                 #
##############################################################################
 
export XLFUNIT_7="raw$mm"
 
$EXEChrly/verf_zstats  1>zstats.out 2>zstats.err
  export err=$?; err_chk
 
##############################################################################
# Write table of Wind statistics ...created:                                 #
# ZREPORT.s fetchs zstatx file and then sort it creating final zstats file.  #
# script for fetching zstats from rawstats                                   #
##############################################################################
 
head -8 zstats.out > $QCMON_MONTHLYreports/zstats$mm
tail +9 zstats.out > unsorted
sort -o zsort  -n +0 -1 unsorted
cat zsort >> $QCMON_MONTHLYreports/zstats$mm

cat $QCMON_MONTHLYreports/zstats$mm | cat >> $QCMON_MONTHLYreports/raobsm$mm
cat blank | cat >> $QCMON_MONTHLYreports/raobsm$mm
cat $QCMON_MONTHLYreports/vstats$mm | cat >> $QCMON_MONTHLYreports/raobsm$mm

#######################
# SECTION 4. FINISH   #
#######################

##########################################
# SECTION 5. Concatenating the reports   #
##########################################

 cat rptheadr >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat cqcheadr >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat $QCMON_MONTHLYreports/cqc$mm   >> $QCMON_MONTHLYreports/qcmrpt$mm

 cat blanks   >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat $QCMON_MONTHLYreports/raobsm$mm >> $QCMON_MONTHLYreports/qcmrpt$mm

 cat blanks   >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat airheadr >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat $QCMON_MONTHLYreports/airsum$mm >> $QCMON_MONTHLYreports/qcmrpt$mm

 cat blanks   >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat asdheadr >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat $QCMON_MONTHLYreports/asdsum$mm >> $QCMON_MONTHLYreports/qcmrpt$mm

 cat blanks   >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat acrheadr >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat $QCMON_MONTHLYreports/acrsum$mm >> $QCMON_MONTHLYreports/qcmrpt$mm

 cat blanks   >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat $QCMON_MONTHLYreports/nospc$mm  >> $QCMON_MONTHLYreports/qcmrpt$mm

 cat blanks   >> $QCMON_MONTHLYreports/qcmrpt$mm
 cat $QCMON_MONTHLYreports/shpsum$mm >> $QCMON_MONTHLYreports/qcmrpt$mm


if test "$SENDDBN" = 'YES'
   then
      $DBNROOT/bin/dbn_alert TEXT VERF_QCREPORT $job $QCMON_MONTHLYreports/qcmrpt$mm
   fi


msg="VERFM_QCMON_RPTS completed normally"
postmsg "$jlogfile" "$msg"

##########################################################################
# GOOD RUN
set +x
echo "**************Script verfm_qcmon_rpts COMPLETED NORMALLY ON THE IBM"
echo "**************Script verfm_qcmon_rpts COMPLETED NORMALLY ON THE IBM"
echo "**************Script verfm_qcmon_rpts COMPLETED NORMALLY ON THE IBM"
set -x
##########################################################################

#------------- end script verfm_qcmon_rpts.sh ---------------------
