#!/bin/tcsh
#### 
set STAT = 25
set PROF1 = Line1
set OFF1 = -100
set OFF2 = 75

set TIM1 = 0
set TIM2 = 8
set PASECH = 8    # Sample  rate [ms]
set INTIRS = 60  # Shot interval [s]
set SIZE = a4	  # Size of the plot

if ($SIZE == a3) then
  set HEIGHT = 9.0
  set WIDTH = 12.0
else if ($SIZE == a4) then
  set HEIGHT = 5.0
  set WIDTH = 8.0
endif

gmtset HEADER_FONT Helvetica-Bold
gmtset HEADER_FONT_SIZE 14
gmtset ANOT_FONT_SIZE 10
gmtset LABEL_FONT_SIZE 10
gmtset PAPER_MEDIA a4+
gmtset MEASURE_UNIT inch
gmtset PAGE_ORIENTATION portrait

sugethw < OBS${STAT}chan1.su key=offset output=geom > toto.dat

awk 'BEGIN{a=-999999999} {if ($1 <= a) print $1 ; if ($1 > a) print -$1 ; a = $1}' toto.dat > toto2.dat
#awk '{if (NR <= 2887) print $1 ; if (NR > 2887) print -$1}' toto.dat > toto2.dat

a2b < toto2.dat n1=1 > toto1.bin
sushw < OBS${STAT}chan1.su infile=toto1.bin key=offset > toto.su

sugain mbal=1 < toto.su |\
sufilter f=3,5,24,36 amps=0,1,1,0 | \
sureduce rv=6.0 |\
#surwght rpow=1.5 rmin=4 |\
sugain scale=0.5  |\
supswigb label1=" " f1=0.008 f1=0 key=offset xbox=0 ybox=0 \
d1num=1.0 n1tic=2 x2beg=${OFF1}000 x2end=${OFF2}000 \
n2tic=10 label2=" " va=1 wt=0 perc=85 titlesize=14 \
labelsize=1 wbox=${WIDTH} hbox=${HEIGHT} title=" " \
tracecolor=none nbpi=600 linewidth=0 x1beg=$TIM1 x1end=$TIM2 n1tic=10 > result.ps

#supswigb label1=" " f1=0.008 f1=0 key=ep xbox=0 ybox=0 \
#d1num=1.0 n1tic=2 x2beg=2500 x2end=3500 \
#n2tic=10 label2=" " va=1 wt=0 perc=55 titlesize=14 \
#labelsize=1 wbox=${WIDTH} hbox=${HEIGHT} title=" " \
#tracecolor=none nbpi=600 linewidth=0 x1beg=$TIM1 x1end=$TIM2 n1tic=10 > result.ps

#supswigb label1=" " f1=0.008 f1=0 key=offset xbox=0 ybox=0 \
#d1num=1.0 n1tic=2 x2beg=${OFF1}000 x2end=${OFF2}000 \
#n2tic=10 label2=" " va=1 wt=0 perc=85 titlesize=14 \
#labelsize=1 wbox=${WIDTH} hbox=${HEIGHT} title=" " \
#tracecolor=none nbpi=600 linewidth=0 x1beg=$TIM1 x1end=$TIM2 n1tic=10 > result.ps

sed 's/showpage/ /g' result.ps | sed 's/%%Trailer/ /g' > test.ps

psbasemap -R${OFF1}/${OFF2}/${TIM1}/${TIM2} -JX8/-5 \
  -Ba10f1:"Offset [km]":/a1f10:"Time-Offset/6 [s]":/g1:." ":SWne -X-0.0 -Y-0.0 -K >> test.ps


pstext -R-1/1/${TIM1}/${TIM2} -JX8/-5 -O -N -W255/255/255 -X0 -Y-5.3 -K << END >> test.ps
0 -1 10 0 5 BC Filter 3,5,24,36, gain proportional to offset
0 -9 14 0 5 BC Davis Strait Line 1 OBS $STAT Hydrophone
END


pstext -R-1/1/${TIM1}/${TIM2} -JX8/-5 -O -N -W255/255/255 -X0 -W255/255/255 -C0.1 << END2 >> test.ps
-0.9 -9 14 0 5 BC W
0.9  -9 14 0 5 BC E
END2


if ($SIZE == a3) then
/home/etamin/software/su35/bin/psmerge in=test.ps rotate=90 translate=8.5,0.5 > test2.ps
else if ($SIZE == a4) then 
/export/home/logiciels/SU/bin/psmerge in=test.ps rotate=90 translate=7,2 > test2.ps
sed 's/%%BoundingBox: 140 139 538 723/%%BoundingBox: 0 80 578 753/g' test2.ps > hydro.ps
endif

gv hydro.ps &
# rm -f temp temp1 



