#!/bin/csh -f
#
# Shell script to update finals time series with suppl analysis. 
# If no argument is passed, then the last week available will be
# used. 
#
# -w option can be used 
#
cd ../COM
if( $#argv == 0 ) then
   set lweek = `ls com*.c.org | tail -n -1 | awk '{print substr($1,4,4)}'`
   echo "Updating time series with week $lweek"
   echo "Use -w <week> for another week"   
else
   foreach a ($argv)
   switch($a)
       case -w:
          set lweek = $argv[2] 
          breaksw
       default:
#       echo Unknown option $a
#       grep '^## ' $0 | awk '{print substr($0,3)}'
#       exit

   endsw
   shift
   end
   
endif
set ts = `date +"%Y%m%d%H%M%S"`
set logf = "SUPTS_${lweek}.log.${ts}"
echo "Creating $logf"
echo "CREATING SUPPL TIME SERIES $ts" >! ../control/$logf
#
##########################################################################
# Start at COM
cd ../COM
echo "Last suppl week $lweek" >> ../control/$logf
#
# Check for a lock file
while ( -e ts_com/tssum.lock )
    sleep 1m
end
echo "Lock $lweek" >! ts_com/tssum.lock
tssum ts_com pbo.final_nam08+suppl N com${lweek}?.c.org >> ../control/$logf

# Generate the IGS08 versions
\rm ts_igs08/*final*pos
tscon ts_igs08 pbo.final_igs08+suppl ../tables/tscon_IGS08.cmd ts_com/*final*pos >&! /dev/null
\rm  ts_com/tssum.lock
#
############################################################################
# Move to NMT
#
set nnmt = `ls ../NMT/nmt${lweek}?.c.org | wc -l`
if( $nnmt > 0 ) then
   cd ../NMT
   # Check for a lock file
   while ( -e ts_nmt/tssum.lock )
       sleep 1m
   end
   echo "Lock $lweek" >! ts_nmt/tssum.lock
   tssum ts_nmt nmt.final_nam08+suppl N nmt${lweek}?.c.org >> ../control/$logf
   \rm ts_nmt/tssum.lock
endif
#
#
############################################################################
# Move to CWU
#
cd ../CWU
# Check for a lock file
while ( -e ts_cwu/tssum.lock )
    sleep 1m
end
echo "Lock $lweek" >! ts_cwu/tssum.lock
tssum ts_cwu cwu.final_nam08+suppl N cwu${lweek}?.c.org >> ../control/$logf
\rm  ts_cwu/tssum.lock

