#!/bin/csh -f
#
# Shell script to make rapid time series files. Script needed
# because rapids only cover days not in the finals, plus we want 
# the reference positions to match (i.e., header from finals file
# is used).
#
# Script is assumed to start in anal/control and analyses are in
# anal/COM anal/NMT and anal/CWU
#
# Only option here is to pass a time tag
#
if( $#argv == 0 ) then
   set ts = `date +"%Y%m%d%H%M%S"`
else
   set ts = $1
endif
set logf = "RAPTS.log.${ts}"
echo "Creating $logf"
echo "CREATING RAPID RIME SERIES $ts" >! $logf
#
##########################################################################
# Start at COM
cd ../COM
set lweek = `ls com*b.org | tail -n -1 | awk '{print substr($1,4,4)}'`
echo "Last finals week $lweek" >> ../control/$logf
#
# Get list of sites
cd ts_com
set sites = `ls ????.???.final_nam08.pos | awk '{print substr($1,1,4)}'`
set cen = "pbo"
#
# Creat the headers for the rapid series
foreach s (`echo $sites`)
    if( -e $s.${cen}.final_nam08.pos ) then
# MOD TAH 051129: Modded to add version number
       set verp = `head  -3 $s.${cen}.final_nam08.pos | grep Vers | wc -l`
       if ( $verp == 0 ) then
          head  -8 $s.${cen}.final_nam08.pos >! $s.${cen}.rapid_nam08.pos
       else
          head  -9 $s.${cen}.final_nam08.pos >! $s.${cen}.rapid_nam08.pos
       endif 
    endif
end
#
cd ..
set raps = `echo $lweek | awk '{printf("com%4.4d?.a.org com%4.4d?.a.org com%4.4d?.a.org com%4.4d?.a.org com%4.4d?.a.org com%4.4d?.a.org com%4.4d?.a.org",$1+1,$1+2,$1+3,$1+4,$1+5,$1+6,$1+7)}'`
echo "Creating for $raps " >> ../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 ${cen}.rapid_nam08 N $raps

# Generate the IGS08 versions
\rm ts_igs08/*rapid*pos
tscon ts_igs08 ${cen}.rapid_igs08 ../tables/tscon_IGS08.cmd ts_com/*rapid*pos
\rm  ts_com/tssum.lock
#
# Remove any files that have now new results
cd ts_com
foreach s (`echo $sites`)
    set nd = `tail -n -1 $s.${cen}.rapid_nam08.pos | grep "^NEU" | wc -l`
    if( $nd > 0 ) then
       echo "No new data $s.${cen}.rapid_nam08.pos" >> ../../control/$logf
       \rm $s.${cen}.rapid_nam08.pos
    endif
end
cd ..
#
############################################################################
# Move to NMT
#
cd ../NMT
cd ts_nmt
set sites = `ls ????.???.final_nam08.pos | awk '{print substr($1,1,4)}'`
set cen = "nmt"
# Creat the headers for the rapid series
foreach s (`echo $sites`)
    if( -e $s.${cen}.final_nam08.pos ) then
# MOD TAH 051129: Modded to add version number
       set verp = `head  -3 $s.${cen}.final_nam08.pos | grep Vers | wc -l`
       if ( $verp == 0 ) then
          head  -8 $s.${cen}.final_nam08.pos >! $s.${cen}.rapid_nam08.pos
       else
          head  -9 $s.${cen}.final_nam08.pos >! $s.${cen}.rapid_nam08.pos
       endif 
    endif
end
#
cd ..
set raps = `echo $lweek | awk '{printf("nmt%4.4d?.a.org nmt%4.4d?.a.org nmt%4.4d?.a.org nmt%4.4d?.a.org nmt%4.4d?.a.org nmt%4.4d?.a.org nmt%4.4d?.a.org",$1+1,$1+2,$1+3,$1+4,$1+5,$1+6,$1+7)}'`
echo "Creating for $raps " >> ../control/$logf
#
# Check for a lock file
while ( -e ts_nmt/tssum.lock )
    sleep 1m
end
echo "Lock $lweek" >! ts_nmt/tssum.lock
tssum ts_nmt ${cen}.rapid_nam08 N $raps
\rm ts_nmt/tssum.lock
#
# Remove any files that have now new results
cd ts_nmt
foreach s (`echo $sites`)
    set nd = `tail -n -1 $s.${cen}.rapid_nam08.pos | grep "^NEU" | wc -l`
    if( $nd > 0 ) then
       echo "No new data $s.${cen}.rapid_nam08.pos" >> ../../control/$logf
       \rm $s.${cen}.rapid_nam08.pos
    endif
end
cd ..
#
############################################################################
# Move to CWU
#
cd ../CWU
cd ts_cwu
set sites = `ls ????.???.final_nam08.pos | awk '{print substr($1,1,4)}'`
set cen = "cwu"
# Creat the headers for the rapid series
foreach s (`echo $sites`)
    if( -e $s.${cen}.final_nam08.pos ) then
# MOD TAH 051129: Modded to add version number
       set verp = `head  -3 $s.${cen}.final_nam08.pos | grep Vers | wc -l`
       if ( $verp == 0 ) then
          head  -8 $s.${cen}.final_nam08.pos >! $s.${cen}.rapid_nam08.pos
       else
          head  -9 $s.${cen}.final_nam08.pos >! $s.${cen}.rapid_nam08.pos
       endif 
    endif
end
#
cd ..
set raps = `echo $lweek | awk '{printf("cwu%4.4d?.a.org cwu%4.4d?.a.org cwu%4.4d?.a.org cwu%4.4d?.a.org cwu%4.4d?.a.org cwu%4.4d?.a.org cwu%4.4d?.a.org",$1+1,$1+2,$1+3,$1+4,$1+5,$1+6,$1+7)}'`
echo "Creating for $raps " >> ../control/$logf
#
# Check for a lock file
while ( -e ts_cwu/tssum.lock )
    sleep 1m
end
echo "Lock $lweek" >! ts_cwu/tssum.lock
tssum ts_cwu ${cen}.rapid_nam08 N $raps
\rm  ts_cwu/tssum.lock
#
# Remove any files that have now new results
cd ts_cwu
foreach s (`echo $sites`)
    set nd = `tail -n -1 $s.${cen}.rapid_nam08.pos | grep "^NEU" | wc -l`
    if( $nd > 0 ) then
       echo "No new data $s.${cen}.rapid_nam08.pos" >> ../../control/$logf
       \rm $s.${cen}.rapid_nam08.pos
    endif
end
cd ..
#
