#!/bin/csh 
# Shell script to extract data from track DHU files and put in data file
# with event time as 0 seconds.  Simple GMT5 plot is also created.
if( $#argv == 0 ) then
   echo "sh_extts <DHU file> <Sample rate> [day]"
   echo "where <DHU file> is name of track DHU format file"
   echo "      <Sample rate> is 5 or 1 depending on it 5Hz or 1Hz solution" 
   echo "                 used for file names and getting times"
   echo "      [day] optional day of year with leading zero.  094 (earthquake"
   echo "                 day is assumed if nothing passed)."
   echo "      [S] optional to set time -60 to 600 seconds
   echo ""
   exit
endif

set f = $1
set r = $2
set day = $3
set S = $4

if( $#day < 3 ) set day = 094

if( ! -e RMS.all ) then
   echo "* RMS Statistics Created `date`" >! RMS.all
endif

set site = `echo $f:r:e`
# Set name of extracted data file.  Formatted for Kaleigraph plotting program.
set of = "KG_${site}${day}_${r}Hz.dat"

echo "* Event time 2010/04/04 22:40:43.1 UTC, 2010/04/04 22:40:58.1 GPST (Epoch  30291 5-Hz, 6059 1-Hz)" >! $of
echo "  Seconds from event   DNorth $site (mm)  +-  DEast $site (mm)  +-   DHgt $site (mm)  +-   RMS (mm)  NumSat" >> $of
if( $r == 5 ) then
   awk '{if ( $18 > 28790 && $18 < 39292 ) {printf("  %10.1f   %10.1f  %10.1f   %10.1f  %10.1f  %10.1f  %10.1f  %10.2f  %5d\n", ($18-30291)*0.2,$7,$8,$9,$10,$11,$12, $13,$14)}}' $f >> $of
else
   awk '{if ( $18 > 5758 && $18 < 7860 ) {printf("  %10.1f   %10.1f  %10.1f   %10.1f  %10.1f  %10.1f  %10.1f  %10.2f  %5d\n", ($18-6059)*1.0,$7,$8,$9,$10,$11,$12, $13,$14)}}' $f >> $of
endif

echo Created $of

# Now create plot using GMT5 (commented lines are for GMT4)
set ps = $of:r.ps

tail -n +3 $of | awk '{print $1,$2,$3}' >! t.n
# Fit data before and after earthquake to get offset and statistics.s
awk '{if( $1 < 0 ) {print $0}}' t.n >! t.1 ; set fitnpre = `poly01r yes 0 no no < t.1 | awk '{print $2, $15,$13}'`
awk '{if( $1 > 900 ) {print $0}}' t.n >! t.1 ; set fitnpos = `poly01r yes 0 no no < t.1 | awk '{print $2, $15,$13}'`
#echo $site $fitnpre | awk '{printf(" 2 6 10 0 0 BL Site %s NORTH PreMean  %5.1f PreRMS  %5.1f mm NRMS %5.3f\n",$1,$2,$3,$4)}' >! t.tn
#echo $site $fitnpos | awk '{printf(" 2 2 10 0 0 BL Site %s NORTH PostMean %5.1f PostRMS %5.1f mm NRMS %5.3f\n",$1,$2,$3,$4)}' >> t.tn
echo $site $fitnpre | awk '{printf(" 2 10 Site %s NORTH PreMean  %5.1f PreRMS  %5.1f mm NRMS %5.3f\n",$1,$2,$3,$4)}' >! t.tn
echo $site $fitnpos | awk '{printf(" 2  7 Site %s NORTH PostMean %5.1f PostRMS %5.1f mm NRMS %5.3f\n",$1,$2,$3,$4)}' >> t.tn

tail -n +3 $of | awk '{print $1,$4,$5}' >! t.e
awk '{if( $1 < 0 ) {print $0}}' t.e >! t.1 ; set fitepre = `poly01r yes 0 no no < t.1 | awk '{print $2, $15,$13}'`
awk '{if( $1 > 900 ) {print $0}}' t.e >! t.1 ; set fitepos = `poly01r yes 0 no no < t.1 | awk '{print $2, $15,$13}'`
echo $site $fitepre | awk '{printf(" 2 10 Site %s EAST  PreMean  %5.1f PreRMS  %5.1f mm NRMS %5.3f\n",$1,$2,$3,$4)}' >! t.te
echo $site $fitepos | awk '{printf(" 2  7 Site %s EAST  PostMean %5.1f PostRMS %5.1f mm NRMS %5.3f\n",$1,$2,$3,$4)}' >> t.te

#set R = `cat t.n t.e | minmax -I10/10 | awk '{gsub("300/1800","100/600")} {print $0}'`
set R = `cat t.n t.e | gmt gmtinfo  -I10/10 `
# See if short time interval wanted.
if ( $S == S ) set R = `echo $R | awk '{gsub("-300/1800","-60/600")} {print $0}'`

set Bya = `echo $R | awk -F/ '{if( $4-$3 > 100 ) {print "ya50f10"} else {print "ya20f5"}}'`

#psxy -JX6.5/4 -Y6 $R -P -Ba240f60/a20f5/SWen  -K t.n >! $ps
#psxy -JX6.5/4 -Y6 $R -P -Bxa240f60 -Bya20f5+l"dN (mm)" -BSWen -K t.n >! $ps
psxy -JX6.5/4 -Y6 $R -P -Bxa240f60 -By${Bya}+l"dN (mm)" -BSWen -K t.n >! $ps
#psxy -JX -R -K -O -W1/255/0/0 <<! >> $ps
psxy -JX -R -K -O -W1p,255/0/0 <<! >> $ps
0  -300
0  +300
!
pstext -JX -R0/100/0/100 -K -O -F+f10p+jTL -N t.tn >> $ps

psxy -JX6.5/4 -Y-4.5 $R  -P -Bxa240f60+l"Seconds from Event Time" -By${Bya}+l"dE (mm)" -BSWen -K -O t.e >> $ps
psxy -JX -R -K -O -W1p,255/0/0 <<! >> $ps
0  -300
0  +300
!
pstext -JX -R0/100/0/100  -O -F+f10p+jTL -N t.te >> $ps
# Add to RMS.all file
cat t.tn t.te >> RMS.all

echo Created $ps, RMS.all updated

\rm t.n t.tn t.e t.te t.1


