BIN=../Bin
PAR=./Par
# the .H files not computed in this makefile are in
# /net/koko/gabriel/Research/Multiples/FALL2005/NEWATTENUATION
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------

# create model data (cmps) with two flat reflectors
# contains two primaries and 4 multiples
cmps_all.H: ${BIN}/two_flat_layers.x
	${BIN}/two_flat_layers.x zwb=400 zref=1200 vw=1500 vr=2500 \
		nh=200 oh=100 dh=10 ncmp=100 ocmp=2000 dcmp=5  \
		nt=400 dt=0.008 fpeak=20 \
		wb1=y wb2=n wb3=n amps=n rhow=1. rhor=2. rhohs=2.5 > $@

shots_all.H: ${BIN}/two_flat_layers.x
	${BIN}/two_flat_layers.x zwb=400 zref=1200 vw=1500 vr=2500 \
		nh=200 oh=100 dh=10 ncmp=100 ocmp=2000 dcmp=5  \
		nt=400 dt=0.008 fpeak=20 \
		wb1=y wb2=n wb3=n amps=n rhow=1. rhor=2. rhohs=2.5 > $@

muls_only.H: ${BIN}/two_flat_layers.x
	${BIN}/two_flat_layers.x zwb=400 zref=1200 vw=1500 vr=2500 \
		nh=200 oh=100 dh=10 ncmp=100 ocmp=2000 dcmp=5  \
		nt=400 dt=0.008 fpeak=20 prim1=n prim2=n \
		wb1=y wb2=n wb3=n amps=n rhow=1. rhor=2. rhohs=2.5 > $@

prims_only.H: ${BIN}/two_flat_layers.x
	${BIN}/two_flat_layers.x zwb=400 zref=1200 vw=1500 vr=2500 \
		nh=200 oh=100 dh=10 ncmp=100 ocmp=2000 dcmp=5  \
		nt=400 dt=0.008 fpeak=20 prim1=y prim2=y \
		wb1=n wb2=n wb3=n sm1=n im1=n plg1=n amps=n rhow=1. rhor=2. rhohs=2.5 > $@

cmp_vels.H: #${BIN}/make_vels.x
	${BIN}/make_vels.x nl=3 v=1500,2500,3500 dipx=0,0 dipy=0,0 \
		zref0=400,1200 nz=1000 dz=5 oz=0 ncmpx=1000 ocmpx=0 \
		dcmpx=5 ncmpy=1 dcmpy=5 ocmpy=0 verb=1 >$@
cmp_vels_plot.H: #${BIN}/make_vels.x
	${BIN}/make_vels.x nl=3 v=1.5,2.5,3.5 dipx=0,0 dipy=0,0 \
		zref0=400,1200 nz=1000 dz=5 oz=0 ncmpx=1000 ocmpx=0 \
		dcmpx=5 ncmpy=1 dcmpy=5 ocmpy=0 verb=1 >$@

one_muls_only.H one_prims_only.H one_all.H: prims_only.H muls_only.H cmps_all.H
	Window3d < muls_only.H n3=1 > one_muls_only.H
	Window3d < prims_only.H n3=1 > one_prims_only.H
	Window3d < cmps_all.H n3=1 > one_all.H

# scale multiples and match them to the data with the standard goal
# Mf=d. The kinematics of the estmated multiples is perfect
matched_muls.H matched_prims.H: one_muls_only.H one_all.H
	Scale < one_muls_only.H dscale=0.1 > junk1.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		nfilt=27,1 npatch=1,1 wmode=1 mode=2 nsmode=1 \
		op=1 mu=1.0 2D=y niter=20 eps=1. dtest=n > $@
	Add scale=1,-1 one_all.H $@ > matched_prims.H

# scale multiples and match them to the data with the standard goal
# Mf=d. The kinematics of the estimated multiples are off by % samples
one_muls_only_shft%.H: one_muls_only.H
	Pad < $< beg1=$* | Window n1=400 > $@
	echo "o1=0">>$@
matched_muls_shft%.H matched_prims_shft%.H: one_muls_only_shft%.H one_all.H
	Scale < one_muls_only_shft$*.H dscale=1.0 > junk1.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		nfilt=27,1 npatch=3,1 wmode=1 mode=2 nsmode=1 \
		op=1 mu=1.0 2D=y niter=50 eps=1.0 dtest=n > $@
	Add scale=1,-1 one_all.H $@ > matched_prims_shft$*.H

# scale both primaries and multiples and use them to match to the 
# original data with the rule Mf1+muPf2=d
matched_muls_row.H matched_prims_row.H: one_muls_only.H one_prims_only.H\
		one_all.H
	Scale < one_muls_only.H dscale=0.1 > junk1.H
	Scale < one_prims_only.H dscale=0.1 > junk2.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		primaries_out=matched_prims_row.H \
		nfilt=21,1 npatch=1,1 wmode=1 mode=2 nsmode=1 \
		op=2 mu=1.0 2D=y niter=20 eps=0. dtest=n > $@
	Add scale=1,1 matched_prims_row.H $@ > matched_all_row.H
	Add scale=1,-1 one_all.H matched_all_row.H > resid.H

# scale multiples and use them to match to the 
# original data with the rule W(Mf1-d)=0. The mask W comes from the
# estimated primaries
mask.H: one_prims_only.H
	Envelope < $< tc1=1 tc2=1 freq=0 > junk.H
	${BIN}/Myclip.x < junk.H min_amp=0.1 one=1. zero=0. > junk2.H
	Smooth < junk2.H tridiag=1 rect1=1 rect2=1 > $@
matched_muls_mask.H matched_prims_mask.H: one_muls_only.H mask.H\
		one_all.H
	Scale < one_muls_only.H dscale=0.1 > junk1.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
        weight=mask.H \
		primaries_out=matched_prims_row.H \
		nfilt=21,1 npatch=1,1 wmode=3 mode=2 nsmode=1 \
		op=1 mu=1.0 2D=y niter=20 eps=0. dtest=y > $@
	Add scale=1,-1 one_all.H $@ > matched_prims_mask.H
matched_muls_mask_shft%.H matched_prims_mask_shft%.H: one_muls_only_shft%.H \
		mask.H one_all.H
	Scale < one_muls_only_shft$*.H dscale=0.1 > junk1.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
        weight=mask.H \
		primaries_out=matched_prims_row.H \
		nfilt=21,1 npatch=1,1 wmode=3 mode=2 nsmode=1 \
		op=1 mu=1.0 2D=y niter=20 eps=0. dtest=y > $@
	Add scale=1,-1 one_all.H $@ > matched_prims_mask_shft$*.H

# now with the array of fitting goals 
# [M muP]    =[d]
# [M   0][m]=[d]
# [0 muP]    =[d] 
matched_muls_array.H matched_prims_array.H: one_muls_only.H one_prims_only.H\
		one_all.H
	Scale < one_muls_only.H dscale=0.1 > junk1.H
	Scale < one_prims_only.H dscale=0.1 > junk2.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		primaries_out=matched_prims_array.H \
		nfilt=21,1 npatch=1,1 wmode=1 mode=2 nsmode=1 \
		op=3 mu=1.0 2D=y niter=20 \
		eps=0. eps1=1 eps2=1 eps3=1 dtest=y > $@
	Add scale=1,1 matched_prims_array.H $@ > matched_all_array.H
	Add scale=1,-1 one_all.H matched_all_array.H > resid.H
matched_muls_array_shft%.H matched_prims_array_shft%.H: \
		one_muls_only_shft%.H one_prims_only.H one_all.H
	Scale < one_muls_only_shft$*.H dscale=0.1 > junk1.H
	Scale < one_prims_only.H dscale=0.1 > junk2.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		primaries_out=matched_prims_array_shft$*.H \
		nfilt=21,1 npatch=3,1 wmode=1 mode=2 nsmode=1 \
		op=3 mu=10.0 2D=y niter=100 \
		eps=0. eps1=1 eps2=1 eps3=1 dtest=y > $@
	Add scale=1,1 matched_prims_array_shft$*.H $@ > matched_all_array_shft$*.H
	Add scale=1,-1 one_all.H matched_all_array_shft$*.H > resid.H

#####################################################################
################################################################
# Now introduce some leakage of the primaries into the multiples
one_muls_only_leak%.H: one_muls_only.H one_prims_only.H
	Add scale=1,$* one_muls_only.H one_prims_only.H > $@
one_prims_only_leak%.H: one_muls_only.H one_prims_only.H
	Add scale=$*,1 one_muls_only.H one_prims_only.H > $@
	
# scale multiples with leakage and match them to the data with the 
# standard goal Mf=d. The kinematics of the estmated multiples is perfect
matched_muls_leak%.H matched_prims_leak%.H: one_muls_only_leak%.H one_all.H
	Scale < one_muls_only_leak$*.H dscale=0.1 > junk1.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		nfilt=27,1 npatch=1,1 wmode=1 mode=2 nsmode=1 \
		op=1 mu=1.0 2D=y niter=20 eps=1.0 dtest=n > $@
	Add scale=1,-1 one_all.H $@ > matched_prims_leak$*.H
	Add scale=1,-1 one_prims_only.H matched_prims_leak$*.H > resid.H
matched_muls_mask_leak%.H matched_prims_mask_leak%.H: one_muls_only_leak%.H \
		mask.H one_all.H
	Scale < one_muls_only_leak$*.H dscale=0.1 > junk1.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
        weight=mask.H \
		primaries_out=matched_prims_row.H \
		nfilt=21,1 npatch=1,1 wmode=3 mode=2 nsmode=1 \
		op=1 mu=1.0 2D=y niter=20 eps=0. dtest=y > $@
	Add scale=1,-1 one_all.H $@ > matched_prims_mask_leak$*.H
	Add scale=1,-1 one_prims_only.H matched_prims_mask_leak$*.H > resid.H

# scale both primaries and multiples and use them to match to the 
# original data with the rule Mf1+muPf2=d. Introduce LEAKAGE
#nfilt=2,2 npatch=100,100 nouter=5 niter=50 eps=0.0 mu=1
#nfilt=2,2 npatch=200,200 nouter=5 niter=50 eps=0.1 mu=1
matched_muls_row_leak%.H matched_prims_row_leak%.H: one_muls_only_leak%.H \
	one_prims_only_leak%.H one_all.H
	Scale < one_muls_only_leak$*.H dscale=0.1 > junk1.H
	Scale < one_prims_only_leak$*.H dscale=0.1 > junk2.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		primaries_out=matched_prims_row_leak$*.H \
		nfilt=2,2 npatch=200,200 nouter=5 niter=50 eps=0.1 mu=1 \
		wmode=1 mode=2 nsmode=1 op=2 2D=y dtest=n > $@
	Add scale=1,1 matched_prims_row_leak$*.H $@ > matched_all_row_leak$*.H
	Add scale=1,-1 one_all.H matched_all_row_leak$*.H > resid.H
	Grey < $@ clip=1 title="Estimated multiples"| Tube &
	Grey < matched_prims_row_leak$*.H clip=1 title="Estimated primaries"| Tube &

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# take the lousy estimate of primaries and multiples and use 
# them to match to the 
# original data with the rule Mf1+muPf2=d. Introduce LEAKAGE
#nfilt=2,2 npatch=200,200 nouter=5 niter=30 eps=0.0 mu=1
one_est_muls.g.H one_est_prims.g.H: est_muls.g.H est_prims.g.H
	Window3d < est_muls.g.H n3=1 > one_est_muls.g.H
	Window3d < est_prims.g.H n3=1 > one_est_prims.g.H
matched_est_muls.g.H matched_est_prims_mul.g.H: one_est_muls.g.H \
	one_est_prims.g.H one_all.H
	Scale < one_est_muls.g.H dscale=1.0 > junk1.H
	Scale < one_est_prims.g.H dscale=1.0 > junk2.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		primaries_out=matched_est_prims.g.H \
		nfilt=2,2 npatch=200,200 nouter=5 niter=30 eps=0.0 mu=1.0 \
		wmode=1 mode=2 nsmode=1 op=2 2D=y dtest=n > $@
	Add scale=1,1 matched_est_prims.g.H $@ > matched_est_all.g.H
	Add scale=1,-1 one_all.H matched_est_all.g.H > resid.H
	Grey < $@ clip=1 title="Estimated multiples"| Tube &
	Grey < matched_est_prims.g.H clip=1 title="Estimated primaries"| Tube &

# for the autommatic computation
est.g.%:
	Scale < one_est_muls.g.H dscale=1.0 > junk1.H
	Scale < one_est_prims.g.H dscale=1.0 > junk2.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		primaries_out=p_$@.H \
		par=${PAR}/$@.P > m_$@.H
	Add scale=1,1 m_$@.H p_$@.H > junk.H
	Add scale=1,-1 target.H junk.H > r_$@.H
	Grey < m_$@.H label1="Time(s)" label2="Offset(m)"m \
		title="m_$@" clip=1 out=FF/m_$@.v >/dev/null
	Grey < p_$@.H label1="Time(s)" label2="Offset(m)"m \
		title="p_$@" clip=1 out=FF/p_$@.v >/dev/null
	Grey < r_$@.H label1="Time(s)" label2="Offset(m)"m \
		title="r_$@" clip=1 out=FF/r_$@.v >/dev/null
	vp_SideBySideAniso FF/m_$@.v FF/p_$@.v FF/p_$@.v > FF/comp_$@.v
	mv p_$@.H ./HH
	mv m_$@.H ./HH
	mv r_$@.H ./HH
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%












jjjj:
	Add scale=1,-1 one_all.H matched_muls_row_leak$*.H > resid_prims.H
	Add scale=1,-1 one_all.H matched_prims_row_leak$*.H > resid_muls.H
	Add scale=1,-1 matched_prims_row_leak$*.H resid_prims.H> resid_resid_prims.H
	Add scale=1,-1 matched_muls_row_leak$*.H resid_muls.H> resid_resid_muls.H
	Grey < $@ clip=1 title="Estimated multiples"| Tube &
	Grey < matched_prims_row_leak$*.H clip=1 title="Estimated primaries"| Tube &
	Grey < resid_prims.H clip=1 title="Residual primaries"| Tube &
	Grey < resid_muls.H clip=1 title="Residual multiples"| Tube &
	Grey < resid_resid_muls.H clip=1 title="Residual residual multiples"| Tube &
	Grey < resid_resid_prims.H clip=1 title="Residual residual primaries"| Tube &

# now with LEAKAGE and the array of fitting goals 
# [M muP]    =[d]
# [M   0][m]=[d]
# [0 muP]    =[d] 
matched_muls_array_leak%.H matched_prims_array_leak%.H: one_muls_only_leak%.H\
		one_prims_only_leak%.H one_all.H
	Scale < one_muls_only_leak$*.H dscale=1. > junk1.H
#Scale < one_prims_only_leak$*.H dscale=1. > junk2.H
	Add scale=1,-1 one_all.H junk1.H dscale=1. > junk2.H
	Scale < one_all.H dscale=1.0 > target.H
	time ${BIN}/Ultimatch-new-all.x < target.H \
		multiples_in=junk1.H primaries_in=junk2.H \
		primaries_out=matched_prims_array_leak$*.H \
		nfilt=21,1 npatch=1,1 wmode=1 mode=2 nsmode=1 \
		op=3 mu=1.0 2D=y niter=50 \
		eps=0. eps1=1 eps2=1 eps3=1 dtest=y > $@
	Add scale=1,1 matched_prims_array_leak$*.H $@ > matched_all_array_leak$*.H
	Add scale=1,-1 one_all.H matched_all_array_leak$*.H > resid.H






################################################################
################################################################

# an old example to match the migrated-demigrated data
one_all_inv.H: mig_cmps_all_inv.H
	Window3d < $< n3=1 > $@
matched_inv_prims.H matched_inv_muls.H: one_all.H one_all_inv.H
	time ${BIN}/Ultimatch-new-all.x < one_all.H \
		multiples_in=one_all_inv.H \
		mode=2 nsmode=1 wmode=1 nfilt=17,17 npatch=100,200 \
		niter=60 2D=1 eps=0.0 > matched_inv_muls.H
