#!/bin/sh
set -x

##################################################################
# first, produce monthly stats that Steve Lilly sends to Bracknell
##################################################################

wc -l $DATA/archive.sort > $DATA/numrep.total.all
grep -i ship $DATA/archive.sort | wc -l > $DATA/numrep.ship.all

export pgm=genall
. prep_step
startmsg

#input files
export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default names.
export XLFUNIT_11="$DATA/archive.sort"   
export XLFUNIT_12="$DATA/numrep.total.all"   
export XLFUNIT_13="$DATA/numrep.ship.all"   

#output files
export XLFUNIT_40="$DATA/slp.stats.all"
export XLFUNIT_41="$DATA/air.stats.all"
export XLFUNIT_42="$DATA/wdi.stats.all"
export XLFUNIT_43="$DATA/wsp.stats.all"
export XLFUNIT_44="$DATA/sst.stats.all"

#run compiled fortran
$EXEChrly/marine_stats_genall > $DATA/out.all

#cat output files together

cat $DATA/slp.stats.all $DATA/air.stats.all > $DATA/tmp1      
cat $DATA/wdi.stats.all $DATA/wsp.stats.all > $DATA/tmp2
cat $DATA/sst.stats.all > $DATA/tmp3       

cat $DATA/tmp1 $DATA/tmp2 $DATA/tmp3 > $DATA/all.stats

##################################################################
# copy file to bracknell.stats.  Main script will move
# this file to $MARARCH (/dcom/us007003/marine_archive/$year/$mon)
###################################################################

cp $DATA/all.stats bracknell.stats

######################################################################
# Add platform names to file for version that's uploaded to the public
# web server.
######################################################################

export pgm=newstats
. prep_step
startmsg

#input files
export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default names.
export XLFUNIT_11="$FIXhrly/ship_names"
export XLFUNIT_12="$DATA/all.stats"
export XLFUNIT_20="$DATA/newall.stats"
$EXEChrly/marine_stats_newstats

##################################################################
# now produce monthly buoy stats (step 1 of buoy stat production)
##################################################################
export pgm=genstx
. prep_step
startmsg
export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default names.

#input files
export XLFUNIT_11="$DATA/archive.sort"

#output files
export XLFUNIT_40="$DATA/newbuoy.stats.tmp"


#run compiled fortran
$EXEChrly/marine_stats_genstx > $DATA/out.stx

##########################################
# sort output file from previous step
############################################
sort -t, -k 2d,2 -k 3d,3 -k 5d,5 $DATA/newbuoy.stats.tmp > $DATA/nsort

################################################################
# cat output file from previous step to end of header file
################################################################
cat $FIXhrly/buoy_hdr_oneline.txt $DATA/nsort > $DATA/newbuoy.stats

###########################################################################
# now continue with script marine_stats_sortrunqm.sh which copies and sorts the file
# /com/hourly/prod/hourly.PDY/sfc_marine.qmarch.prev_mon and then 
# runs marine_stats_eomqm.sh to complete the monthly buoy statistics.
#################################################################################
exit
