include ${SEPINC}/SEP.top
#LATOPTS = include='\renewcommand{\baselinestretch}{2.0}'
COLOR = yes
BINDIR = /net/koko/tang/bin/LINUX
RESDIR = ./Fig
SRCDIR = ./Src
DATDIR = /net/koko/tang/Research/ylib

FFTWLIB = -L/opt/FFTW/lib -lfftw3f
UF90LIBS  = -lsupersetf90 -lsepgeef90 -lsuperset -lsepauxf90 -lsepmathf90 -lsep2df90 -lsep3df90 -lsep3d -lsepf90 -lsep -openmp -lsvml ${FFTWLIB}
# for pompei
UF90FLAGS = -O3 -axP -openmp -static-libcxa -Bstatic

FREQ = f1=5 f2=10 f3=30 f4=35


#------------ Constant velocity model --------------------------------------------------------------------
CONST_LINEAR = ./const_linear
CONST_IMGWIN = image_xmin=-1000 image_xmax=1000 image_zmin=800 image_zmax=800
CONST_OFFDSZ = nx_offd=21 dx_offd=10 ox_offd=-100 nz_offd=21 dz_offd=10 oz_offd=-100

#----------- additional examples --------------------------------------------------------------------------
LAYER_SOU_SAMP = nsx=11  osx=-2000 dsx=400
LAYER_REC_SAMP = ngx=401 ogx=-2000 dgx=10

const.vmod.H: ${BINDIR}/layer_mod.x
	${BINDIR}/layer_mod.x vmod=$@ refl=const.refl.H nx=601 ox=-3000 dx=10 nz=301 dz=10 oz=0

const.csou.H: ${BINDIR}/wei_transf.x
	Wavelet n1=4000 o1=0 d1=0.002 tdelay=0.1 domain=time fund=20 wavelet=ricker2 | Add scale=-1 > junk1.H
	echo d2=1 d3=1 d4=1 d5=1 o2=0 o3=0 o4=0 o5=0 n2=1 n3=1 n4=1 n5=1 >> junk1.H
	${BINDIR}/wei_transf.x < junk1.H adj=y ${FREQ} > junk2.H
	Transp plane=13 < junk2.H > $@
	Rm junk*.H

# 1st receiver
const.crec.1.H: ${BINDIR}/generate_spike.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_spike.x soufnc=junk1.H output=$@ linear=n random=n\
        ngx=401 dgx=10 ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=1   dsx=10 osx=-600  nsy=1 dsy=1 osy=0\
        loc1=600 loc2=600 tdelay=0.
	Rm junk1.H

# 2nd receiver
const.crec.2.H: ${BINDIR}/generate_spike.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_spike.x soufnc=junk1.H output=$@ linear=n random=n\
        ngx=401 dgx=10 ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=1   dsx=10 osx=-600  nsy=1 dsy=1 osy=0\
        loc1=1200 loc2=1200 tdelay=0.
	Rm junk1.H

# 1st + 2nd receivers
const.crec.total.H: ${BINDIR}/generate_spike.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_spike.x soufnc=junk1.H output=$@ linear=n random=n\
        ngx=401 dgx=10 ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=1   dsx=10 osx=-600  nsy=1 dsy=1 osy=0\
        loc1=600 loc2=1200 tdelay=0.
	Rm junk1.H

# example for 401 receivers
const.crec.1s401r.H: ${BINDIR}/generate_plane.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_plane.x soufnc=junk1.H output=$@\
        ngx=401 dgx=10  ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=1   dsx=10  osx=-1000 nsy=1 dsy=1 osy=0\
        angle=0. v0=2000 refx=0

const.crec.401s401r.H: ${BINDIR}/generate_plane.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_plane.x soufnc=junk1.H output=$@\
        ngx=401 dgx=10  ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=401 dsx=10  osx=-2000 nsy=1 dsy=1 osy=0\
        angle=0. v0=2000 refx=0

# 1st + 2nd receivers + random encoding
const.crec.random.%.H: ${BINDIR}/generate_spike.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_spike.x soufnc=junk1.H output=$@ linear=n random=y \
        ngx=401 dgx=10 ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=1   dsx=10 osx=-600  nsy=1 dsy=1 osy=0\
        loc1=600 loc2=1200 tdelay=0. phase_random=const.phase.random.H
	Rm junk1.H

# 401 receivers + random encoding
const.crec.1s401r.random.%.H: ${BINDIR}/generate_random.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_random.x soufnc=junk1.H output=$@ \
        ngx=401 dgx=10  ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=1   dsx=10  osx=-1000 nsy=1 dsy=1 osy=0
	Rm junk1.H

# 401 shots 401 receivers + random encoding
const.crec.401s401r.random.%.H: ${BINDIR}/generate_random.x const.csou.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/generate_random.x soufnc=junk1.H output=$@ \
        ngx=401 dgx=10  ogx=-2000 ngy=1 dgy=1 ogy=0\
        nsx=401 dsx=10  osx=-2000 nsy=1 dsy=1 osy=0
	Rm junk1.H

# 1st + 2nd receivers + plane-wave encoding
CONST_PLANE_CREC_ENC1 = psx_min=-0.000492403 psx_max=0.000492403 sx0_pos=-2000. sx0_neg=2000 npsx=7
CONST_PLANE_CSOU_ENC1 = psx_min=0. psx_max=0. npsx=7
CONST_PLANE_CSOU_HIS1 = n4=7 o4=-0.0004924 d4=0.00016413

CONST_PLANE_CREC_ENC2 = psx_min=-0.000492403 psx_max=0.000492403 sx0_pos=-2000. sx0_neg=2000 npsx=15
CONST_PLANE_CSOU_ENC2 = psx_min=0. psx_max=0. npsx=15
CONST_PLANE_CSOU_HIS2 = n4=15 o4=-0.0004924 d4=7.0343e-05

CONST_PLANE_CREC_ENC3 = psx_min=-0.000492403 psx_max=0.000492403 sx0_pos=-2000. sx0_neg=2000 npsx=31
CONST_PLANE_CSOU_ENC3 = psx_min=0. psx_max=0. npsx=31
CONST_PLANE_CSOU_HIS3 = n4=31 o4=-0.0004924 d4=3.2826e-05

CONST_PLANE_CREC_ENC4 = psx_min=-0.000492403 psx_max=0.000492403 sx0_pos=-2000. sx0_neg=2000 npsx=61
CONST_PLANE_CSOU_ENC4 = psx_min=0. psx_max=0. npsx=61
CONST_PLANE_CSOU_HIS4 = n4=61 o4=-0.0004924 d4=1.6413e-05


CONST_HESS_OFFD = nx_offd=81 ox_offd=-400 dx_offd=10 nz_offd=81 oz_offd=-400 dz_offd=10 image_zmin=1500 image_zmax=1500 zmin_modl=1500 zmax_modl=1500

const.crec.planes.%.H: ${BINDIR}/hesdct_efnc.x ${BINDIR}/encode.x 
	${BINDIR}/hesdct_efnc.x nws=241 ows=30.6305 dws=0.785398 nsx=2 dsx=600 osx=600 ${CONST_PLANE_CREC_ENC$*} domain="encsou" enctyp="planes" efns=junk1.H
	${BINDIR}/encode.x outsou=y outrec=n csou=const.csou.H efns=junk1.H wfds=$@ ngx=401 ogx=-2000 dgx=10 nsx=2 osx=600 dsx=600
	Rm junk1.H

const.crec.1s401r.planes.%.H: ${BINDIR}/hesdct_efnc.x ${BINDIR}/encode.x
	${BINDIR}/hesdct_efnc.x nws=241 ows=30.6305 dws=0.785398 nsx=401 dsx=10 osx=-2000 ${CONST_PLANE_CREC_ENC$*} domain="encsou" enctyp="planes" efns=junk1.H
	${BINDIR}/encode.x outsou=y outrec=n csou=const.csou.H efns=junk1.H wfds=$@ ngx=401 ogx=-2000 dgx=10 nsx=401 osx=-2000 dsx=10
	Rm junk1.H


const.csou.planes.%.H: ${BINDIR}/hesdct_efnc.x ${BINDIR}/encode.x
	${BINDIR}/hesdct_efnc.x nws=241 ows=30.6305 dws=0.785398 nsx=1 dsx=600 osx=-600 ${CONST_PLANE_CSOU_ENC$*} domain="encsou" enctyp="planes" efns=junk1.H
	${BINDIR}/encode.x outsou=y outrec=n csou=const.csou.H efns=junk1.H wfds=$@ ngx=401 ogx=-2000 dgx=10 nsx=1 osx=-600 dsx=600
	echo ${CONST_PLANE_CSOU_HIS$*} >> $@
	Rm junk1.H

const.csou.1s401r.planes.%.H: ${BINDIR}/hesdct_efnc.x ${BINDIR}/encode.x
	${BINDIR}/hesdct_efnc.x nws=241 ows=30.6305 dws=0.785398 nsx=1 dsx=10 osx=-1000 ${CONST_PLANE_CSOU_ENC$*} domain="encsou" enctyp="planes" efns=junk1.H
	${BINDIR}/encode.x outsou=y outrec=n csou=const.csou.H efns=junk1.H wfds=$@ ngx=401 ogx=-2000 dgx=10 nsx=1 osx=-1000 dsx=10
	echo ${CONST_PLANE_CSOU_HIS$*} >> $@
	Rm junk1.H


# exact Hessian without crosstalks for only 2 reveivers
const.hess.1.H: ${BINDIR}/hesenc3d.x const.crec.1.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H
const.hess.2.H: ${BINDIR}/hesenc3d.x const.crec.2.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.2.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
        Rm junk1.H
const.hess.exact.H: const.hess.1.H const.hess.2.H
	Add const.hess.1.H const.hess.2.H > $@

# off-diagonals
const.hess.offd.1.H: ${BINDIR}/hesenc3d.x const.crec.1.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H
const.hess.offd.2.H: ${BINDIR}/hesenc3d.x const.crec.2.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.2.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H
const.hess.offd.exact.H: const.hess.offd.1.H const.hess.offd.2.H
	Add const.hess.offd.1.H const.hess.offd.2.H > $@

# Hessian with crosstalk
const.hess.xtalk.H: ${BINDIR}/hesenc3d.x const.crec.total.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.total.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H

const.hess.offd.xtalk.H: ${BINDIR}/hesenc3d.x const.crec.total.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.total.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H

# Hessian with random phase encoding
const.hess.random.%.H: ${BINDIR}/hesenc3d.x const.crec.random.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.random.$*.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H

const.hess.offd.random.%.H: ${BINDIR}/hesenc3d.x const.crec.random.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.random.$*.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H

# Hessian with plane-wave-phase encoding
const.hess.planes.%.H: ${BINDIR}/hesenc3d.x const.csou.planes.%.H const.crec.planes.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.planes.$*.H csou=const.csou.planes.$*.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        comsou=y wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H

const.hess.offd.planes.%.H: ${BINDIR}/hesenc3d.x const.csou.planes.%.H const.crec.planes.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.planes.$*.H csou=const.csou.planes.$*.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        comsou=y wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H

# 1 shot and 401 receivers: exact Hessian
#-----------------------------------------
const.grns.H: ${BINDIR}/grn3d.x const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	Interp < const.vmod.H o1out=-4000 n1out=801 type=0 > junk2.H
	${BINDIR}/grn3d.x csou=junk1.H vmod=junk2.H wfds=$@ ntpx=25 pdip=45 nvrf=1 nxpad_beg=100 nxpad_end=100 oper="phsft" \
                                nws=241 ox_surf=-2000 dx_surf=10 nx_surf=401 modl_xmin=-3000 modl_xmax=3000 offset=n
	Rm junk*.H

const.grns.transp.H:
	Transp < const.grns.H reshape=4,6 maxsize=4000 > $@

const.amsk.1s401r.H: ${BINDIR}/hesdct_mask.x
	${BINDIR}/hesdct_mask.x nsx=1 dsx=10 osx=-1000 ngx=401 dgx=10 ogx=-2000 offset=n mask=$@

const.hess.1s401r.exact.H: ${BINDIR}/hesdct.x const.csou.H const.grns.transp.H const.amsk.1s401r.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesdct.x grns=const.grns.transp.H mask=const.amsk.1s401r.H hess=$@ csou=junk1.H offset=n obcrec=y wntwgt=n nws=1\
        nsx=1 dsx=10 osx=-1000 ngx=401 dgx=10 ogx=-2000 domain="shtpro"\
	Rm junk1.H

# 1 shot and 401 receivers: off-diagonals
const.hess.offd.1s401r.exact.H: ${BINDIR}/hesdct.x const.csou.H const.grns.transp.H const.amsk.1s401r.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesdct.x grns=const.grns.transp.H mask=const.amsk.1s401r.H hess=$@ csou=junk1.H offset=n obcrec=y wntwgt=n nws=1\
        nsx=1 dsx=10 osx=-1000 ngx=401 dgx=10 ogx=-2000 domain="shtpro" ${CONST_HESS_OFFD}\
	Rm junk1.H

# 1 shot and 401 receivers: crosstalk
const.hess.1s401r.xtalk.H: ${BINDIR}/hesenc3d.x const.crec.1s401r.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1s401r.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H

const.hess.offd.1s401r.xtalk.H: ${BINDIR}/hesenc3d.x const.crec.1s401r.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1s401r.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H

# 1 shot and 401 receivers: Hessian with random phase encoding
const.hess.1s401r.random.%.H: ${BINDIR}/hesenc3d.x const.crec.1s401r.random.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1s401r.random.$*.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
        Rm junk1.H

const.hess.offd.1s401r.random.%.H: ${BINDIR}/hesenc3d.x const.crec.1s401r.random.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1s401r.random.$*.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H

# 1 shot and 401 receivers: plane-wave encoding
const.hess.1s401r.planes.%.H: ${BINDIR}/hesenc3d.x const.csou.1s401r.planes.%.H const.crec.1s401r.planes.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1s401r.planes.$*.H csou=const.csou.1s401r.planes.$*.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        comsou=y wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H

const.hess.offd.1s401r.planes.%.H: ${BINDIR}/hesenc3d.x const.csou.1s401r.planes.%.H const.crec.1s401r.planes.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.1s401r.planes.$*.H csou=const.csou.1s401r.planes.$*.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        comsou=y wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H

#------------------------------------------------------
# 401 shots and 401 receivers: exact
const.amsk.401s401r.H: ${BINDIR}/hesdct_mask.x
	${BINDIR}/hesdct_mask.x nsx=401 dsx=10 osx=-2000 ngx=401 dgx=10 ogx=-2000 offset=n mask=$@

const.hess.401s401r.exact.H: ${BINDIR}/hesdct.x const.csou.H const.grns.transp.H const.amsk.401s401r.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesdct.x grns=const.grns.transp.H mask=const.amsk.401s401r.H hess=$@ csou=junk1.H offset=n obcrec=y wntwgt=n nws=1\
        nsx=401 dsx=10 osx=-2000 ngx=401 dgx=10 ogx=-2000 domain="shtpro"\
        Rm junk1.H

const.hess.offd.401s401r.exact.H: ${BINDIR}/hesdct.x const.csou.H const.grns.transp.H const.amsk.401s401r.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesdct.x grns=const.grns.transp.H mask=const.amsk.401s401r.H hess=$@ csou=junk1.H offset=n obcrec=y wntwgt=n nws=1\
        nsx=401 dsx=10 osx=-2000 ngx=401 dgx=10 ogx=-2000 domain="shtpro" ${CONST_HESS_OFFD}\
	Rm junk1.H


# 401 shots and 401 receivers: random encoding
const.hess.401s401r.random.%.H: ${BINDIR}/hesenc3d.x const.crec.401s401r.random.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.401s401r.random.$*.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H

const.hess.offd.401s401r.random.%.H: ${BINDIR}/hesenc3d.x const.crec.401s401r.random.%.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.401s401r.random.$*.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H


# 401 shots and 401 receivers: crosstalk
const.hess.401s401r.xtalk.H: ${BINDIR}/hesenc3d.x const.crec.401s401r.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.401s401r.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft"\
	Rm junk1.H

const.hess.offd.401s401r.xtalk.H: ${BINDIR}/hesenc3d.x const.crec.401s401r.H const.csou.H const.vmod.H
	Window3d < const.csou.H > junk1.H
	${BINDIR}/hesenc3d.x crec=const.crec.401s401r.H csou=const.csou.H sfnc=junk1.H vmod=const.vmod.H hess=$@ \
        wnttap=y pdip=45 ntpx=25 nws=241 nxpad_beg=100 nxpad_end=100 oper="phsft" ${CONST_HESS_OFFD}\
	Rm junk1.H




# sigsbee
SIGS_CSOU = /net/koko/tang/Research/Hessian/Sigs/ww.sigsb2a.soufunc.H
SIGS_CREC = /net/koko/tang/Research/Sigsb2a/sigsb2a.rec.freq.H
SIGS_VMOD = /net/koko/tang/Research/ylib/wem.sigsb2a.vmod.H
SIGS_VMOD_SMOOTH = /net/koko/tang/Research/ylib/wem.sigsb2a.migvel.H
SIGS_IMAG = /net/koko/tang/Research/ylib/wem.sigsb2a.imag.fsec.all.H
SIGS_VMOD_WIN = /net/koko/tang/Research/ylib/wem.sigsb2a.vmod.win.H
SIGS_REFL_WIN = /net/koko/tang/Research/ylib/sigsb2a.refl.win.H


SIGS_HESS_SIZE = image_xmin=25000 image_xmax=45000 image_zmin=10000 image_zmax=20000 
SIGS_HESS_OFFD = nx_offd=31 dx_offd=75 ox_offd=-1125 nz_offd=31 dz_offd=25 oz_offd=-375
SIGS_HESS_OFFD2= nx_offd=21 dx_offd=75 ox_offd=-750  nz_offd=21 dz_offd=25 oz_offd=-250

sigsb2a.crec.500s348r.random.%.H: ${BINDIR}/generate_random.x
	Window3d < ${SIGS_CSOU} > junk1.H
	${BINDIR}/generate_random.x soufnc=junk1.H output=$@ \
        ngx=348 dgx=75   ogx=0     ngy=1 dgy=1 ogy=0\
        nsx=500 dsx=150  osx=10925 nsy=1 dsy=1 osy=0
	Rm junk1.H

sigsb2a.hess.500s348r.random.%.H: ${BINDIR}/hesenc3d.x sigsb2a.crec.500s348r.random.%.H
	Window3d < ${SIGS_CSOU} > junk1.H
	${BINDIR}/hesenc3d.x crec=sigsb2a.crec.500s348r.random.$*.H csou=${SIGS_CSOU} sfnc=junk1.H vmod=${SIGS_VMOD} hess=$@ \
        wnttap=y pdip=45 ntpx=40 nws=361 nxpad_beg=400 nxpad_end=52 oper="foufd" wntwgt=n offset=y \
        Rm junk1.H

sigsb2a.hess.offd.500s348r.random.%.H: ${BINDIR}/hesenc3d.x sigsb2a.crec.500s348r.random.%.H
	Window3d < ${SIGS_CSOU} > junk1.H
	${BINDIR}/hesenc3d.x crec=sigsb2a.crec.500s348r.random.$*.H csou=${SIGS_CSOU} sfnc=junk1.H vmod=${SIGS_VMOD_SMOOTH} hess=$@ \
        wnttap=y pdip=45 ntpx=40 nws=361 nxpad_beg=400 nxpad_end=52 oper="foufd" wntwgt=n offset=y ${SIGS_HESS_SIZE} ${SIGS_HESS_OFFD2}\
	Rm junk1.H

sigsb2a.imag.target.H: ${BINDIR}/wem3d.x
	${BINDIR}/wem3d.x csou=${SIGS_CSOU} crec=${SIGS_CREC} vmod=${SIGS_VMOD_SMOOTH} imag=$@ adj=y comsou=n \
        wnttap=y pdip=45 ntpx=40 nws=361 nxpad_beg=400 nxpad_end=52 oper="foufd" offset=y ${SIGS_HESS_SIZE}


sigsb2a.imag.transp.target.H:
	Transp < sigsb2a.imag.target.H reshape=2,5 > $@


sigsb2a.hess.offd.transp.500s348r.random.1.save.H:
	Transp < sigsb2a.hess.offd.500s348r.random.1.save.H  reshape=3,6 > $@

sigsb2a.hess.offd.transp.500s348r.random.1.H:
	Transp < sigsb2a.hess.offd.500s348r.random.1.H  reshape=3,6 > $@


sigsb2a.mask.H: ${BINDIR}/create_mask.x ${SIGS_VMOD_WIN}
	${BINDIR}/create_mask.x < ${SIGS_VMOD_WIN} nwnd=5 nbnd=15 vcut=12000. > junk1.H
	Smooth rect1=5 rect3=5 < junk1.H > $@
	Rm junk1.H

sigsb2a.hess.diag.H.random.1.H: sigsb2a.hess.offd.transp.500s348r.random.1.H
	Window3d < $< n4=1 min4=0 n6=1 min6=0 > $@


sigsb2a.invt.random.H: ${BINDIR}/hesconv2.x sigsb2a.mask.H
	${BINDIR}/hesconv2.x adj=y imgsign=1 data=sigsb2a.imag.transp.target.H modl=$@ hess=sigsb2a.hess.offd.transp.500s348r.random.1.H \
         mask=sigsb2a.mask.H mmov=sigsb2a.invt.random.mmov.H rmov=sigsb2a.invt.random.rmov.H fmov=sigsb2a.invt.random.fmov.H\
         beta_max=0.01 eps=0.000001 niter=50 vareps=n search=y solver=2 rbar=0.02 reglar=1 wntmsk=y wntwgt=n wntnlz=y nfreq=3 nswch=20 eps1=0.0001 eps2=0.001


sigsb2a.invt.random.lbfgsb.H: ${BINDIR}/hesconv_lbfgsb.x sigsb2a.mask.H
	${BINDIR}/hesconv_lbfgsb.x adj=y data=sigsb2a.imag.target.H modl=$@ hess=sigsb2a.hess.offd.transp.500s348r.random.1.H \
        mask=sigsb2a.mask.H wght=sigsb2a.hess.diag.H.random.1.H mmov=test.mmov.H rmov=test.rmov.H fmov=test.fmov.H \
        rbar=0.01 eps=0.01 wntmsk=y wntwgt=n wntnlz=y niter=50



sigsb2a.test.data.H: ${BINDIR}/hesconv2.x
	${BINDIR}/hesconv2.x adj=n data=$@ modl=${SIGS_REFL_WIN} hess=sigsb2a.hess.offd.transp.500s348r.random.1.H


sigsb2a.refl.filt.win.H: ${SIGS_REFL_WIN}
	Window3d < $< | Bandpass fhi=0.004 flo=0. > junk1.H
	Transp < junk1.H | Bandpass fhi=0.008 flo=0. > $@

sigsb2a.refl.interp.H: ${SIGS_REFL_WIN}
	Window3d < $< | Interp d1out=25 n1out=799 1597 3193 d2out=12.5 n2out=801 type=2 > $@

sigsb2a.refl.fft.H: sigsb2a.refl.interp.H
	Pad < $< n1out=1024 n2out=1024 > junk1.H
	Rtoc < junk1.H | Ft3d sign1=1 sign2=1 center1=1 center2=1 maxsize=1000 > $@




test.small.H: 
	Window3d < sigsb2a.hess.offd.transp.500s348r.random.1.H min1=27500 max1=44075 min3=15500 max3=18525 squeeze=n > junk1.H
	Window3d < sigsb2a.imag.transp.target.H min1=27500 max1=44075 min3=15500 max3=18525 squeeze=n > junk2.H
	${BINDIR}/hesconv2.x adj=y imgsign=1 data=junk2.H modl=$@ hess=junk1.H \
         beta_max=0.01 eps=0.000001 niter=50 vareps=n search=y solver=2 rbar=0.02 reglar=1 wntmsk=n wntwgt=n wntnlz=y nfreq=3 nswch=20 eps1=0.0001 eps2=0.001



# create super Hessian

sigsb2a.refl.super.H:
	Spike n1=2001 d1=75 o1=-25000 n2=1 d2=1 o2=0 n3=1201 d3=25 o3=0 k1=801 k2=1 k3=601 > $@

sigsb2a.crec.super.H: ${BINDIR}/wem3d.x sigsb2a.refl.super.H
	Transp < sigsb2a.refl.super.H reshape=3,5,6 > junk1.H
	${BINDIR}/wem3d.x csou=${SIGS_CSOU} crec=$@ vmod=${SIGS_VMOD} imag=junk1.H adj=n offset=y\
        nvrf=1 pdip=45 ntpx=40 nws=361 nxpad_beg=400 nxpad_end=52 oper="foufd"\
        ngx=348 dgx=75   ogx=0     ngy=1 dgy=1 ogy=0\
        nsx=500 dsx=150  osx=10925 nsy=1 dsy=1 osy=0
	Rm junk1.H

sigsb2a.hess.super.H:
	${BINDIR}/wem3d.x csou=${SIGS_CSOU} crec=sigsb2a.crec.super.H vmod=${SIGS_VMOD} imag=$@ adj=y offset=y\
        nvrf=1 pdip=45 ntpx=40 nws=361 nxpad_beg=400 nxpad_end=52 oper="foufd"\


# randomly encode 401 receivers
layer.crec.more.random.%.H: ${BINDIR}/generate_random.x layer.csou.H
	Window3d < layer.csou.H > junk1.H
	${BINDIR}/generate_random.x soufnc=junk1.H output=$@ \
        ${LAYER_SOU_SAMP} ${LAYER_REC_SAMP}
	Rm junk1.H

layer.hesenc.H: ${GBINDIR}/hesenc3d.x layer.csou.H layer.refl.H
	Window3d < layer.csou.H > junk1.H
	${GBINDIR}/hesenc3d.x csou=layer.csou.H crec=layer.crec.more.H sfnc=junk1.H vmod=layer.vmod.H hess=$@ \
        oper="phsft" nws=205 ntpx=25 nxpad_beg=100 nxpad_end=100 


layer.hesenc.random.%.H: ${GBINDIR}/hesenc3d.x layer.csou.H layer.refl.H layer.crec.more.random.%.H
	Window3d < layer.csou.H > junk1.H
	${GBINDIR}/hesenc3d.x csou=layer.csou.H crec=layer.crec.more.random.$*.H sfnc=junk1.H vmod=layer.vmod.H hess=$@ \
        oper="phsft" nws=205 ntpx=25 nxpad_beg=100 nxpad_end=100 image_xmin=-2000 image_xmax=2000 \
        nx_offd=31 ox_offd=-150 dx_offd=10 nz_offd=31 oz_offd=-150 dz_offd=10


# model Born data
layer.crec.born.H: ${GBINDIR}/wem3d.x layer.refl.H layer.csou.H
	Transp < layer.refl.H reshape=3,5,6 > junk1.H
	${GBINDIR}/wem3d.x csou=layer.csou.H crec=$@ vmod=layer.vmod.H imag=junk1.H adj=n \
        nvrf=1 pdip=45 ntpx=25 nws=205 oper="phsft" nxpad_beg=100 nxpad_end=100\
        ${LAYER_SOU_SAMP} ${LAYER_REC_SAMP}
	Rm junk1.H

layer.imag.born.H: ${GBINDIR}/wem3d.x layer.crec.born.H layer.csou.H
	${GBINDIR}/wem3d.x csou=layer.csou.H crec=layer.crec.born.H vmod=layer.vmod.H imag=$@ adj=y \
        nvrf=1 pdip=45 ntpx=25 nws=205 oper="phsft" nxpad_beg=100 nxpad_end=100


# inversion
layer.refl.win.H: layer.refl.H
	Window3d < $< min1=-2000 n1=401 squeeze=n > $@

layer.imag.born.win.H: layer.imag.born.H
	Transp < $< reshape=2,5 | Window3d n1=401 min1=-2000 squeeze=n > $@

layer.mask.H: ${GBINDIR}/create_mask.x layer.vmod.H
	Window3d < layer.vmod.H min1=-2000 n1=401 squeeze=n > junk1.H
	${GBINDIR}/create_mask.x < junk1.H nwnd=5 nbnd=15 vcut=12000. > junk2.H
	Smooth rect1=5 rect3=5 < junk2.H > $@
	Rm junk*.H

layer.hesenc.transp.H: layer.hesenc.random.all.H
	Transp < layer.hesenc.random.all.H reshape=3,6 maxsize=2000 > $@

layer.hess.random.diag.H: layer.hesenc.transp.H
	Window3d < layer.hesenc.transp.H n4=1 n5=1 n6=1 min4=0 min5=0 min6=0 squeeze=n > $@

layer.invt.random.H: ${GBINDIR}/hesconv2.x layer.mask.H layer.imag.born.win.H layer.hesenc.transp.H layer.hess.random.diag.H 
	${GBINDIR}/hesconv2.x adj=y imgsign=1 data=layer.imag.born.win.H modl=$@ hess=layer.hesenc.transp.H \
        mask=layer.mask.H wght=layer.hess.random.diag.H \
        mmov=test.mmov.H rmov=test.rmov.H fmov=test.fmov.H\
        beta_max=0.001 eps=0. niter=5 vareps=n search=y solver=5 reglar=3


layer.lsm.H: ${GBINDIR}/lsm3d_incore.x layer.vmod.H layer.csou.H layer.crec.born.H
	${GBINDIR}/lsm3d_incore.x csou=layer.csou.H crec=layer.crec.born.H vmod=layer.vmod.H imag=$@ adj=y comsou=n cpximg=y \
        nvrf=1 pdip=45 ntpx=25 nws=181 oper="phsft" nxpad_beg=100 nxpad_end=100 \
        niter=5 eps=0.5 solver=1 regtyp=2 report=n \
        tmpm=lsm.layer.tmpm.H tmpd=lsm.layer.tmpd.H \
        mmov=lsm.layer.mmov.H rmov=lsm.layer.rmov.H

# new idea
# model the crosstalk in data space
# data that contains cross-talk
# (L+dL)m = Lm + dL m
layer.data.random.H: ${GBINDIR}/hesconv2.x layer.mask.H layer.imag.born.win.H layer.hesenc.transp.H layer.hess.random.diag.H
	${GBINDIR}/hesconv2.x adj=y imgsign=1 data=$@ modl=layer.imag.born.win.H hess=layer.hesenc.transp.H \
        mask=layer.mask.H wght=layer.hess.random.diag.H \
        mmov=test.mmov.H rmov=test.rmov.H fmov=test.fmov.H\
        beta_max=0.001 eps=0.05 niter=5 vareps=n search=y solver=5 reglar=3 adj=n

# dL m = Lm + dL m - d
layer.data.new.random.H: layer.data.random.H layer.imag.born.win.H
	${GBINDIR}/normalize3d.x < layer.data.random.H   > junk1.H
	${GBINDIR}/normalize3d.x < layer.imag.born.win.H > junk2.H
	Math file1=junk1.H file2=junk2.H exp="file1-file2" > $@
	Rm junk*.H

layer.invt.new.random.H: ${GBINDIR}/hesconv2.x layer.mask.H layer.data.new.random.H layer.hesenc.transp.H layer.hess.random.diag.H
	${GBINDIR}/hesconv2.x adj=y imgsign=1 data=layer.data.random.H modl=$@ hess=layer.hesenc.transp.H \
        mask=layer.mask.H wght=layer.hess.random.diag.H \
        mmov=test.mmov.H rmov=test.rmov.H fmov=test.fmov.H\
        beta_max=0.001 eps=0.05 niter=5 vareps=n search=y solver=5 reglar=3


layer.invt.test.H: ${GBINDIR}/hesconv3.x layer.mask.H layer.data.new.random.H layer.hesenc.transp.H layer.hess.random.diag.H
	${GBINDIR}/hesconv3.x adj=y imgsign=1 data=layer.imag.born.win.H modl=$@ hess=layer.hesenc.transp.H \
        mask=layer.mask.H wght=layer.hess.random.diag.H \
        mmov=test.mmov.H rmov=test.rmov.H fmov=test.fmov.H\
        eps_regl=0. eps_rowd=0.1 niter=5



clean:
	rm -f *.o *.mod

include ${SEPINC}/SEP.bottom











