#! /bin/sh
# Velocity analyses for the cmp gathers
# Authors: Dave Hale, Jack K. Cohen, with modifications by John Stockwell
# NOTE: Comment lines preceeding user input start with  ##
#set -x

## Set parameters
velpanel=modeldata
vpicks=stkvel.p1
normpow=0
slowness=0
cdpmin=1500
cdpmax=3500
dcdp=500
fold=12  # only have 12 shots, otherwise would be 64/2=32 for dsx=dgx



## Set velocity sampling and band pass filters
nv=120
dv=75.0
fv=4000.0
nout=501 # ns
dxout=0.004

nx=12
dx=.1
tau=.2

## set filter values
f=1,10,100,120
amps=0,1,1,0

## number of contours in contour plot
nc=15
fc=0.01       # This number should be around .1 or .2 for real data 

### Get header info
nout=`sugethw ns <$velpanel | sed 1q | sed 's/.*ns=//'`
dt=`sugethw dt <$velpanel | sed 1q | sed 's/.*dt=//'`
dxout=`bc -l <<END
	$dt / 1000000
END`


### Do the velocity analyses.
echo
echo
echo " Velocity Analysis using the Normalized Selective crosscorrelation sum"
echo
echo
echo "            Instructions for Velocity Analysis."
echo
echo "  A contour semblance map will appear on the left of your screen."
echo "  A wiggle trace plot of the cdp panel being analysed will appear"
echo "  on the right as a aid in picking."
echo
echo "  Pick velocities by placing cursor on each peak in the"
echo "  semblance plot and typing 's'. Type 'q' when last peak is picked."
echo "  Note, this is 'blind' picking. You will not see any indication"
echo "  on the contour plot that a point has been picked."
echo
echo "  There will be a maximum of 4 peaks to be picked, as this is the"
echo "  number of reflectors in the model. However, for the far-offset"
echo "  CDP gathers, it may be difficult to pick all 4 peaks."
echo
echo "  A graph of the velocity function will appear, and a prompt to" 
echo "  hit the return key will be seen in this terminal window. You"
echo "  will be asked if your picks are ok. This gives you a chance"
echo "  to re-pick the velocities if you do not like the velocity function"
echo "  you have obtained."

pause


cdp=$cdpmin
while [ $cdp -le $cdpmax ]
do
	ok=false
	while [ $ok = false ]
	do
		echo "Starting velocity analysis for cdp $cdp"
		suwind < $velpanel key=cdp min=$cdp max=$cdp count=$fold > panel.$cdp 
		suxwigb < panel.$cdp title="CDP gather for cdp=$cdp" xbox=700 \
			 mpicks=mpicks.$cdp &
		sugain tpow=2 < panel.$cdp |
		sufilter f=$f amps=$amps |
		suvelan_nsel nx=$nx dx=$dx nv=$nv dv=$dv fv=$fv |
		suxcontour tau=$tau nc=$nc bclip=0.2 wclip=0.0 f2=$fv d2=$dv \
			units="semblance" fc=$fc \
			label1="Time (sec)" label2="Velocity (m/sec)" \
			title="Velocity Scan (semblance plot) for CMP $cdp" mpicks=mpicks.$cdp

		sort <mpicks.$cdp  -n |
		mkparfile string1=tnmo string2=vnmo >par.$cdp

		echo "Putting up velocity function for cdp $cdp"
		sed <par.$cdp '
			s/tnmo/xin/
			s/vnmo/yin/
		' >unisam.p
		unisam nout=$nout fxout=0.0 dxout=$dxout \
			par=unisam.p method=spline |
		xgraph n=$nout nplot=1 d1=$dxout f1=0.0 \
			label1="Time (sec)" label2="Velocity (m/sec)" \
			title="Stacking Velocity Function: CMP $cdp" \
			grid1=solid grid2=solid \
			linecolor=2 style=seismic &

		pause

		echo  "Picks OK? (y/n) "  | tr -d "\012" >/dev/tty
		read response
		case $response in
		n*) ok=false ;;
		*) ok=true ;;
		esac

	done </dev/tty
	cdp=`bc -l <<END
		$cdp + $dcdp
END`

done

set +x


### Combine the individual picks into a composite sunmo par file
echo "Editing pick files ..."
>$vpicks
echo  "cdp=" | tr -d "\012" >>$vpicks
cdp=$cdpmin
echo  "$cdp"  | tr -d "\012" >>$vpicks
cdp=`bc -l <<END
	$cdp + $dcdp
END`
while [ $cdp -le $cdpmax ]
do
	echo  ",$cdp"  | tr -d "\012" >>$vpicks
	cdp=`bc -l <<END
		$cdp + $dcdp
END`
done
echo >>$vpicks

cdp=$cdpmin
while [ $cdp -le $cdpmax ]
do
	cat par.$cdp >>$vpicks
	cdp=`bc -l <<END
		$cdp + $dcdp
END`
done


echo "sunmo par file: $vpicks is ready"


### Clean up
cdp=$cdpmin
while [ $cdp -le $cdpmax ]
do
	rm mpicks.$cdp par.$cdp
	cdp=`bc -l <<END
		$cdp + $dcdp
END`
done
rm unisam.p
