#!/bin/csh -f
#
# Shell script to generate summary of PBO processing.
# See if arguments passed
if( $#argv == 0 ) then
   grep '^## ' $0 | awk '{print substr($0,3)}'
# Here are the options
## Usage: sh_summary <options> 
## where the following options are avaiable
##  -f   -- Name of summary file
##  -m <mailto> -- List to mail summary to
##
## Script run from <proc_dir>/control directory
##
   exit

endif

## Get the options
set logf = NONE
set mailto = NONE
foreach a ($argv)
switch($a)
    case -f:
       set logf = $argv[2]
       breaksw
#
    case -m:
       set mailto = $argv[2]
       breaksw
#
    endsw
    shift
end
# Make sure we have log file name
if( ! -e $logf ) then
   echo "Logfile $logf does not exist"
   exit
endif
set sumf = $logf:r".sum"
echo "Creating $sumf" 
set typen = `echo $logf | grep RAP | wc -c`
if( $typen > 0 ) then 
   set type = RAP
   set tl = a
else
   set typen = `echo $logf | grep FIN | wc -c`
   if( $typen > 0 ) then
       set type = FIN
       set tl = b
   else 
       set typen = `echo $logf | grep SUP | wc -c`
       if ( $typen > 0 ) then
          set type = SUP
          set tl = c
       else
          set typen = `echo $logf | grep SU6 | wc -c`
          if ( $typen > 0 ) then
             set type = SU6
             set tl = f
          else 
             set typen = `echo $logf | grep CAM | wc -c`
             if ( $typen > 0 ) then
                set type = CAM
                set tl = d
             else
                set type = REP
                set tl = e
             endif
          endif
       endif
   endif
endif
#
# Get the date from the log file name
set gpsw = `echo $logf:r | awk -F / '{print substr($NF,9,4)}'`
set gpsd = `echo $logf:r | awk -F / '{print substr($NF,13,1)}'`
set date = `doy ${gpsw}W $gpsd | head -1 | awk '{print substr($2,1,4)substr($2,6,2)substr($2,9,2)}'`
#
if( $type == 'RAP' ) then
   echo "PBO RAPID SUMMARY FOR Week ${gpsw} Day $gpsd Date $date" >! $sumf
else if( $type == 'FIN' ) then
   echo "PBO FINAL SUMMARY FOR Week ${gpsw} Day $gpsd Date $date" >! $sumf
else if( $type == 'SUP' ) then
   echo "PBO SUPPL SUMMARY FOR Week ${gpsw} Day $gpsd Date $date" >! $sumf
else if( $type == 'SU6' ) then
   echo "PBO SUPP6 SUMMARY FOR Week ${gpsw} Day $gpsd Date $date" >! $sumf
else if( $type == 'CAM' ) then
   echo "PBO CAMPD SUMMARY FOR Week ${gpsw} Day $gpsd Date $date" >! $sumf 
else
   echo "PBO REPRO SUMMARY FOR Week ${gpsw} Day $gpsd Date $date" >! $sumf
endif
#
echo "------------------------------------------------------------------------" >> $sumf
set NMT_sites = `grep 'sites found in ../NMT' $logf | tail -1 | awk '{print $1}'`
set CWU_sites = `grep 'sites found in ../CWU' $logf | tail -1 | awk '{print $1}'`
#
if ( $type != 'SUP' && $type != 'SU6' && $type != 'REM' && $type != 'CAM' ) then
   echo "NMT solution contains $NMT_sites stations" >> $sumf
   echo "CWU solution contains $CWU_sites stations" >> $sumf
else if ( $type == 'SUP' ) then
   set NMT_tot = `grep ' X coordinate  (m)' ../NMT/nmt${gpsw}${gpsd}.c.org | wc -l`
   set CWU_tot = `grep ' X coordinate  (m)' ../CWU/cwu${gpsw}${gpsd}.c.org | wc -l`
   echo "NMT solution contains $NMT_sites new stations; Total $NMT_tot stations" >> $sumf
   echo "CWU solution contains $CWU_sites new stations; Total $CWU_tot stations" >> $sumf

else if ( $type == 'SU6' ) then
   set NMT_tot = `grep ' X coordinate  (m)' ../NMT/nmt${gpsw}${gpsd}.f.org | wc -l`
   set CWU_tot = `grep ' X coordinate  (m)' ../CWU/cwu${gpsw}${gpsd}.f.org | wc -l`
   echo "NMT solution contains $NMT_sites new stations; Total $NMT_tot stations" >> $sumf
   echo "CWU solution contains $CWU_sites new stations; Total $CWU_tot stations" >> $sumf

else if ( $type == 'CAM' ) then
   set NMT_tot = `grep ' X coordinate  (m)' ../NMT/nmt${gpsw}${gpsd}.d.org | wc -l`
   set CWU_tot = `grep ' X coordinate  (m)' ../CWU/cwu${gpsw}${gpsd}.d.org | wc -l`
   echo "NMT solution contains $NMT_sites new stations; Total $NMT_tot stations" >> $sumf
   echo "CWU solution contains $CWU_sites new stations; Total $CWU_tot stations" >> $sumf
else 
   set tots = `grep '^*NUM' $logf | awk '{print $2,$4,$6}'`
   set xps = `grep '^*NUM' $logf | awk '{print $3,$5,$7}'`
   echo "PBO solution contains $tots[1] stations with $xps[1] edited" >> $sumf
   echo "NMT solution contains $tots[2] stations with $xps[2] edited" >> $sumf
   echo "CWU solution contains $tots[3] stations with $xps[3] edited" >> $sumf

endif
# Get number of Stations in combined solution
set ltype = `grep "^COMBINED SOLUTION" $logf | tail -n -1 | awk '{print $4}'`
set lname = `grep "^COMBINED SOLUTION" $logf | tail -n -1 | awk '{print $3}'`

set PBO_tot = `grep ' X coordinate  (m)' ../COM/com${gpsw}${gpsd}.${ltype}.org | wc -l`
echo "PBO solution contains $PBO_tot stations" >> $sumf
echo "------------------------------------------------------------------------" >> $sumf
echo "Center    dChi^2   Wght   Status" >> $sumf
grep 'USED' ../COM/com${gpsw}${gpsd}.${ltype}.org | awk '{printf("%5s   %8.3f   %4.1f   %s\n", substr($2,25,5),$5,$3,substr($0,82))}' >> $sumf
set files = `ls ../NMT/nmt${gpsw}${gpsd}.${ltype}.org ../CWU/cwu${gpsw}${gpsd}.${ltype}.org ../COM/com${gpsw}${gpsd}.${ltype}.org`
grep '^[ 1-9].... SCALE    (ppb) ' $files | awk '{printf("%s       %6.2f +- %4.2f ppb SCALE USED\n", substr($1,4,3),$4,$5)}' >>  $sumf
set nw = `grep '^[ 1-9].... SCALE    (ppb) ' $files | awk '{printf("%s       %6.2f +- %4.2f ppb SCALE USED\n", substr($1,4,3),$4,$5)}'`
if ( $type == 'REP' || $#nw == 0 ) then
# Scale not estimated so get the average height offset.
    grep '^POS MEANS:' $files | awk '{printf("%s       %6.2f +- %4.2f ppb SCALE INFERRED\n", substr($1,4,3),$15/6.378,$17/6.378)}' >>  $sumf
endif
echo "------------------------------------------------------------------------" >> $sumf
#cwu.a      0.150    9.6   USED        
#COM        -4.44 +- 0.17 ppb SCALE USED

if( $type != 'REM' ) then
   echo "META DATA Problems" >>  $sumf
   egrep '^Processing|^Err: |^Update |NoAntenna|NoMatch' $logf >>  $sumf
#
   echo "------------------------------------------------------------------------" >> $sumf
endif
grep '^*SC ' $logf | awk '{print substr($0,4)}' >> $sumf

echo "------------------------------------------------------------------------" >> $sumf
echo "COMBINED SOLUTION QUALITY (4-sigma outliers)" >> $sumf
grep '^*BC ' $logf | awk '{print substr($0,4)}' >> $sumf

echo "------------------------------------------------------------------------" >> $sumf
echo "NMT SOLUTION QUALITY (4-sigma outliers)" >> $sumf
grep '^*BB ' $logf | awk '{print substr($0,4)}' >> $sumf

echo "------------------------------------------------------------------------" >> $sumf
echo "CWU SOLUTION QUALITY (4-sigma outliers)" >> $sumf
grep '^*BW ' $logf | awk '{print substr($0,4)}' >> $sumf

echo "------------------------------------------------------------------------" >> $sumf
echo "COMPARISON OF OUTLIER SITES BY CENTER" >> $sumf
echo "CEN    Long      Lat         dE mm    dN mm      E +-      N +-  Rho          dH mm    dH +- STATION" >> $sumf
grep '^*BA'  $logf | egrep 'NMT|CWU|COM|\+\+\+' | awk '{print substr($0,9,3),substr($0,28)}' >> $sumf

# Check for missing site name
set ts = `grep "PRODUCT TAG" $logf | tail -1 | awk '{print $3}'`
echo "Possible missing site names" >>  $sumf
#echo ../products/pbo${gpsw}${gpsd}.${lname}_nam08.snx.$ts
if( -e ../products/pbo${gpsw}${gpsd}.${lname}_nam08.snx.$ts  ) then
   ls -l ../products/pbo${gpsw}${gpsd}.${lname}_nam08.snx.$ts >>  $sumf
   grep 'A --------- P' ../products/pbo${gpsw}${gpsd}.${lname}_nam08.snx.$ts >>  $sumf
else
   ls -l ../products/pbo${gpsw}${gpsd}.${lname}_nam08.snx.gz.$ts >>  $sumf
   zgrep 'A --------- P' ../products/pbo${gpsw}${gpsd}.${lname}_nam08.snx.gz.$ts >>  $sumf
endif 

# See if we need to mail
if( $mailto != NONE ) then
    set SUB = `head -1 $sumf`
#    mail -s "$SUB" $mailto < sumf >&! /dev/null
    mail -s "$SUB" $mailto < $sumf 
endif













