include ${SEPINC}/SEP.top

UF90LIBS     = -lsepgeef90 -lsep2df90
INVRAS       = y
FAT          = 3

RESDIR = ./Fig
SRCDIR = ./Src
OBJDIR = ./Obj
BINDIR = ./Bin
DATDIR = /net/koko/homes/sep/prof/gee/Data/

R = ${RESDIR}
S = ${SRCDIR}
O = ${OBJDIR}
B = ${BINDIR}
D = ${DATDIR}

# Synthetic data example
mod.H: 	${B}/Velmod.x                                             # velocity scan model
	${B}/Velmod.x nt=75 nx=45 ns=35 xmin=-1. >$@
dat.H:   mod.H  ${B}/Veltran.x                                    # Modeled CMP gather
	< mod.H  ${B}/Veltran.x adj=0 >$@
mod2.H:  dat.H ${B}/Veltran.x                                    # Adjoint
	< dat.H  ${B}/Veltran.x adj=1 inv=0 >$@
dat2.H:  mod2.H ${B}/Veltran.x
	< mod2.H ${B}/Veltran.x adj=0 >$@
dat-lint.H:   mod.H ${B}/Veltran.x                               # Modeled CMP gather with linear interp.
	< mod.H ${B}/Veltran.x adj=0 lint=1 >$@
mod-lint.H:   dat-lint.H ${B}/Veltran.x                          # Adjoint with linear interp.
	< dat-lint.H ${B}/Veltran.x adj=1 lint=1 >$@
dat2-lint.H:   mod-lint.H ${B}/Veltran.x
	< mod-lint.H  ${B}/Veltran.x adj=0 lint=1 >$@
modi-lint.H:  dat-lint.H ${B}/Veltran.x                          # Inverse with linear interp.
	< dat-lint.H ${B}/Veltran.x adj=1 inv=1 lint=1 niter=50 >$@
dati-lint.H:  modi-lint.H ${B}/Veltran.x
	< modi-lint.H ${B}/Veltran.x adj=0 lint=1 >$@

# Plotting
${R}/%.v: %.H
	<$*.H Dots gaineach=0 dots=0 seemean=0 strings=0 constsep=1 wantaxis=0 \
	overlap=6. transp=1 title=" "  > /dev/null out=$@

# Adjoint
${R}/velvel.v : ${R}/mod.v ${R}/dat.v ${R}/mod2.v ${R}/dat2.v
	vp_SideBySideAniso ${R}/mod.v  ${R}/dat.v  > ${R}/top.v
	vp_SideBySideAniso ${R}/mod2.v ${R}/dat2.v > ${R}/bot.v
	vp_OverUnderAniso ${R}/top.v ${R}/bot.v | vppen vpstyle=n txscale=1.5 > $@

# Adjoint with linear interpolation
${R}/velvel-lint.v ${R}/velvel-lint.v3: ${R}/mod.v ${R}/dat-lint.v ${R}/mod-lint.v ${R}/dat2-lint.v ${R}/velvel.v
	vp_SideBySideAniso ${R}/mod.v  ${R}/dat-lint.v      > ${R}/top.v
	vp_SideBySideAniso ${R}/mod-lint.v ${R}/dat2-lint.v > ${R}/bot.v
	vp_OverUnderAniso ${R}/top.v ${R}/bot.v | vppen vpstyle=n txscale=1.5 > ${R}/velvel-lint.v
	vp_Movie ${R}/velvel.v ${R}/velvel-lint.v > ${R}/velvel-lint.v3

# Inverse with linear interpolation
${R}/velinv-lint.v ${R}/velinv-lint.v3: ${R}/mod.v ${R}/dat-lint.v ${R}/modi-lint.v ${R}/dati-lint.v ${R}/velvel-lint.v
	vp_SideBySideAniso ${R}/mod.v  ${R}/dat-lint.v  > ${R}/top.v
	vp_SideBySideAniso ${R}/modi-lint.v ${R}/dati-lint.v > ${R}/bot.v
	vp_OverUnderAniso ${R}/top.v ${R}/bot.v | vppen vpstyle=n txscale=1.5 > ${R}/velinv-lint.v
	vp_Movie ${R}/velvel-lint.v ${R}/velinv-lint.v > ${R}/velinv-lint.v3

# Difference
diff_dat.H: dat-lint.H dati-lint.H
	Add scale=1,-1 dat-lint.H dati-lint.H > $@

diff_mod.H: mod.H modi-lint.H
	Add scale=1,-1 modi-lint.H mod.H > $@

${R}/diff.v: dat-lint.H dati-lint.H diff_dat.H mod.H modi-lint.H diff_mod.H
	Window < mod.H n2=5  > empty_traces.H
	Cat dat-lint.H empty_traces.H dati-lint.H empty_traces.H diff_dat.H axis=2 > diff_top.H
	Cat mod.H      empty_traces.H modi-lint.H empty_traces.H diff_mod.H axis=2 > diff_bot.H
	Dots < diff_top.H gaineach=0 dots=0 seemean=1 strings=0 constsep=1 wantaxis=0 \
	                  overlap=6. transp=1 title=" "  > /dev/null out=${R}/diff_top.v
	Dots < diff_bot.H gaineach=0 dots=0 seemean=1 strings=0 constsep=1 wantaxis=0 \
	                  overlap=6. transp=1 title=" "  > /dev/null out=${R}/diff_bot.v
	vp_OverUnderAniso  ${R}/diff_top.v ${R}/diff_bot.v > $@

# Dot product test
dottest: ${B}/DPtest.x
	${B}/DPtest.x > /dev/null

# Create ps, pdf and gif files
%.ps: %.v
	pstexpen $< $@ fat=${FAT} invras=${INVRAS}

%.pdf: %.ps
	epstopdf $<

%.gif: %.v
	vplot2gif $*.v

# Default targets
default: ${R}/velvel.pdf ${R}/velvel-lint.pdf ${R}/velinv-lint.pdf ${R}/diff.pdf

clean: jclean

burn: clean
	rm -f ${B}/*
	rm -f ${O}/*
	rm -f ${R}/*.pdf
	rm -f ${R}/*.ps
	rm -f ${R}/*.v*

include ${SEPINC}/SEP.bottom
