#!/bin/csh -f
#
# Shell script to update finals time series with repro 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*.e.org | tail -n -1 | awk '{print substr($1,4,4)}'`
   echo "Updating time series with week $lweek"
   echo "Use -w <week> for another week"   
   echo "Use -g <week> <num> for span of weeks"
else
   foreach a ($argv)
   switch($a)
       case -w:
          set sgpsw = $argv[2]
          set lweek = $sgpsw
          set num = 1
          set egpsw = `echo $sgpsw $num | awk '{printf("%4.4d",$1+$2-1)}'`
          set sgpsd = 0
          set egpsd = 6
          set OK = 'Y'
          breaksw
       case -g:
          set sgpsw = $argv[2]
          set lweek = $sgpsw
          set num = $argv[3]
          set egpsw = `echo $sgpsw $num | awk '{printf("%4.4d",$1+$2-1)}'`
          set sgpsd = 0
          set egpsd = 6
          set OK = 'Y'
          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 = "REPTS_${lweek}.log.${ts}"
echo "Creating $logf"
echo "CREATING REPRO TIME SERIES $ts" >! ../control/$logf
#
set com_upf = ''
set nmt_upf = ''
set cwu_upf = ''
##########################################################################
# Start at COM
cd ../COM
echo "Last repro week $lweek" >> ../control/$logf
#
# Check for a lock file
while ( -e ts_com/tssum.lock )
    sleep 1m
end
echo "Lock $sgpsw to $egpsw" >! ts_com/tssum.lock
set wk = $sgpsw
while ( $wk <= $egpsw )
    set com_upf = ( $com_upf com${wk}?.e.org )
    set wk = `echo $wk | awk '{printf("%4.4d",$1+1)}'`
end

tssum ts_com pbo.final_nam08+repro N $com_upf >> ../control/$logf

# Generate the IGS08 versions
\rm ts_igs08/*final*pos
tscon ts_igs08 pbo.final_igs08+repro ../tables/tscon_IGS08.cmd ts_com/*final*pos >&! /dev/null
\rm  ts_com/tssum.lock
#
############################################################################
# Move to NMT
#
cd ../NMT
# Check for a lock file
while ( -e ts_nmt/tssum.lock )
    sleep 1m
end
echo "Lock $sgpsw to $egpsw" >! ts_nmt/tssum.lock
set wk = $sgpsw
while ( $wk <= $egpsw )
    set nmt_upf = ( $nmt_upf nmt${wk}?.e.org )
    set wk = `echo $wk | awk '{printf("%4.4d",$1+1)}'`
#    @ wk++
end
tssum ts_nmt nmt.final_nam08+repro N $nmt_upf >> ../control/$logf
\rm ts_nmt/tssum.lock
#
#
############################################################################
# Move to CWU
#
cd ../CWU
# Check for a lock file
while ( -e ts_cwu/tssum.lock )
    sleep 1m
end
echo "Lock $sgpsw to $egpsw" >! ts_cwu/tssum.lock
set wk = $sgpsw
while ( $wk <= $egpsw )
    set cwu_upf = ( $cwu_upf cwu${wk}?.e.org )
    set wk = `echo $wk | awk '{printf("%4.4d",$1+1)}'`
#    @ wk++
end
tssum ts_cwu cwu.final_nam08+repro N $cwu_upf >> ../control/$logf
\rm  ts_cwu/tssum.lock list.$ts

############################################################################
