#! /bin/sh

set -x 

## The assumption is that source and receiver strengths are
## the only quantities that can influence the RMS power
## of a source or output channel gather
##
## The possible source of error in this approach is that we have
## not corrected for the response of the geology.

## correcting for souce strength
# remove output file
rm pbal.shot.su

# split the original data into shot gathers
# split shot data
susplit < seismic.su key=ep  


# loop over shot gather files
for i in `ls split_* `
do
	# apply rms power balance to each gather
	sugain panel=1 pbal=1 < $i >> pbal.shot.su
done

## clean up
# remove shot split files
rm split*

# remove output file
rm pbal.rec.su

## To correct for receiver strength, we need to sort the
## data into gathers that consist of data recorded by the
## same receiver channel. In an ocean survey the hydrophones are
## at a fixed offset from the source position, and the whole
## source receiver array is towed behind the boat.  So
## sorting the data by offset and ep will produce the proper gathers
## 
susort offset sx < pbal.shot.su  > channel.su

# split common channel gather data
susplit key=offset close=1  < channel.su 

# loop over common channel gather files
for i in `ls split_* `
do
	sugain panel=1 pbal=1 < $i >> pbal.channel.su
done

# sort back to shot gathers
susort ep offset < pbal.channel.su > pbal.su


exit 0
