#!/bin/bash -f
#
#   Remember to run script "init_seisnet" (sioseis-seisnet daemon)
#
if [ "$#" = "0" ]; then
    echo "Usage: brute line-name near_trace offset [plot-direction(ltr/rtl) channel_set source(odd/even)]"
    echo "brute stacks the close 2km (160 traces) of the SEG-D shots"
    echo "near_trace is the trace number of the hydrophone closest to the ship"
    echo "(brute assumes the closest trace is numbered higher than the furthest)"
    echo "offset is the distance between the source and near_trace"
    echo "plot-direction is optional - rtl means right-to-left"
    echo "channel_set is optional - which streamer to process"
    echo "source is optional - which source string to use - alternate shots are the same source"
    echo "Make sure script init_seisnet is running e.g.  init_seisnet seisnet-directory-name"
    echo "The stacked SEG-Y file is in the current working directory and named "line-name"-stack.segy"
    echo "The Atlantek plotfile is in the current working directory and named "line-name"-stack.atlantek"
    exit
fi
declare -i chan_set=1 ftrc ltrc nargs near_trace offset
nargs=$#
line=$1
near_trace=$2
offset=$3
if [ $offset -gt 0 ]; then
    offset=offset*-1
fi
DIR="ltr"
if [ "$4" = "rtl" ]; then
    DIR="rtl"
fi
if [ $nargs > 4 ]; then
   channel_set=$5
fi
shot=" "
if [ "$6" = "odd" ]; then
   shot="odd"
fi
if [ "$6" = "even" ]; then
   shot="even"
fi

ltrc=$near_trace
ftrc=$ltrc-159
if [ $ftrc -lt 1 ]; then
   ftrc=1
fi

#outdir="/home/logadmin/output"
outdir="."
latest="/tmp/latest"

#   where is the sioseis program?
#/home/logadmin/bin/sioseis << eof
sioseis << eof
procs segddin prout geom wbt gather nmo stack diskoa avenor filter plot end

segddin
   fcset $chan_set lcset $chan_set
   ftr $ftrc ltr $ltrc   #  remove this
   stack $latest $shot
   logpath ./$line-stack.log
   end
end

prout
 fno 0 lno 9999999 ftr 1 ltr 1 end
end

geom
  type 9 # Use realtime GPS that's in the SEG-Y trace header
  fs 1 ls 999999  # all shot have the same parameters (preset)
  gxp $near_trace $offset
  ggx -12.5         # Used to extrapolate gxp!
  dfls 37.5   # ignored with type 9
  dbrps 6.25
  rpadd 1000 end
end

wbt
    vel 1500 track .1 end
end

gather
   maxtrs 50  maxrps 170 END
end

nmo
#
# real time nmo, replace interpolation by RP to WB depth in Meters.
# If water depth changes by > 500 m, use previous value.
# interpolation by iso-velocity layering
vtrkwb 500 stretc 1.50 dstretch 50

fno 10 lno 10
vtp 1500 0.013   #  The water bottom two way travel time at 10m is .013sec
 1700 0.884
 2171 1.312
 2486 1.687
 3092 2.208
 5733 8.588
 6556 13.088 end

fno 500 lno 500
vtp 1500 0.667
 1630 1.549
 1977 1.978
 2238 2.353
 2775 2.873
 5673 8.391
 6788 13.391 end

fno 1000 lno 1000
vtp 1500 1.333
 1564 2.216
 1761 2.644
 1932 3.019
 2089 3.332
 5413 7.470
 7181 13.470 end

fno 2000 lno 2000
vtp 1500 2.667
 1521 3.549
 1593 3.978
 1664 4.353
 1734 4.665
 4410 7.769
 7122 14.269 end

fno 2500 lno 2500
vtp 1500 3.333
 1502 3.627
 1521 3.885
 1546 4.135
 1555 4.260
 3312 6.329
 7244 13.829 end

fno 3000 lno 3000
vtp 1500 4.000
 1501 4.294
 1504 4.408
 1505 4.483
 1564 4.858
 2900 6.551
 7064 14.051 end
end

diskoa   # Write out disk file
  opath $outdir/$line-stack.segy
end end

avenor
  hold 300
  addwb yes sets 0 .5 5 6 end
end


filter
  pass 5 90 ftype 0 dbdrop 48 end
end

plot
  dir $DIR
  scalar -1
  tlines 0.5 1 nibs 7225 ann gmtint anninc 5 ann2 shotno
  def 0.04 trpin 125 wiggle 0
  vscale 1.25 clip .03
  opath $outdir/$line-stack.atlantek
  end
end

end
eof