up [pdf]
from rsf.proj import*
from rsf.prog import RSFROOT

def Grey1(data,other):
        Result(data,
          '''
       grey clip=0.2 wanttitle=n label2=Trace unit2= label1=Time unit1=s
       labelfat=4 font=2 labelsz=10 titlefat=4 color=g %s 
       '''%other)

def Grey2(data,other):
        Result(data,
          '''
       put d1=0.004 o1=0 d2=1 o2=0 | grey clip=0.2 wanttitle=n label2=Trace unit2= label1=Time unit1=s
       labelfat=4 font=2 labelsz=10 titlefat=4 color=g %s 
       '''%other)

def Grey22(data,data0,other):
        Result(data,data0,
          '''
       put d1=0.004 o1=0 d2=1 o2=0 | grey clip=0.2 wanttitle=n label2=Trace unit2= label1=Time unit1=s
       labelfat=4 font=2 labelsz=10 titlefat=4 color=g %s 
       '''%other)

def Greydip(data,other):
        Result(data,
          '''
       put d1=0.004 o1=0 d2=1 o2=0 | grey wanttitle=n label2=Trace unit2= unit1=s label1=Time 
       labelfat=4 font=2 labelsz=10 titlefat=4 color=j %s 
       '''%other)

# multi-step
########################################################################
# INITIALIZATION
#######################################################################
matlab         = WhereIs('matlab')
matROOT = '../matfun'
matfun = 'Dipfilter'
matlabpath = os.environ.get('MATLABPATH',os.path.join(RSFROOT,'lib'))

if not matlab:
    sys.stderr.write('\nCannot find Matlab.\n')
    sys.exit(1)

nt=501
nx=76
twin=50
xwin=40
r=1

############################################################
## generate and process synthetic data
############################################################
Flow('plane-c plane planes',[os.path.join(matROOT,matfun+'.m')],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${TARGETS[0]}','${TARGETS[1]}','${TARGETS[2]}');quit"
     '''%vars(),stdin=0,stdout=-1)
Flow('planes3','planes','put n2=256 n3=6 o3=0')

Flow('sep1','planes3','window n3=2 f3=1 | stack norm=n axis=3')
Flow('sep2','planes3','window n3=3 f3=3 | stack norm=n axis=3')
Grey2('plane-c','')
Grey2('plane','')
Grey2('planes','')
Grey2('sep1','')
Grey2('sep2','')

Flow('plane-dip','plane','threshold1 thr=15|dip rect1=7 rect2=7')
Flow('sep1-dip','sep1','threshold1 thr=10 | dip rect1=5 rect2=5')
Flow('sep2-dip','sep2','threshold1 thr=10 | dip rect1=5 rect2=5')
Greydip('plane-dip','')
Greydip('sep1-dip','')
Greydip('sep2-dip','')

Flow('plane-seis','plane plane-dip','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.2 type=b')
Flow('sep1-seis','sep1 sep1-dip','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.01 type=b')
Flow('sep2-seis','sep2 sep2-dip','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.01 type=b')
Result('plane-seis','put d1=0.004 o1=0 d2=1 o2=0 |grey label1=Time unit1=s label2=Trace unit2= wanttitle=n ')
Result('sep1-seis','put d1=0.004 o1=0 d2=1 o2=0 |grey label1=Time unit1=s label2=Trace unit2= wanttitle=n ')
Result('sep2-seis','put d1=0.004 o1=0 d2=1 o2=0 |grey label1=Time unit1=s label2=Trace unit2= wanttitle=n ')

Flow('plane-thr','plane-seis','threshold1 thr=30')
Flow('sep1-thr','sep1-seis','threshold1 thr=10')
Flow('sep2-thr','sep2-seis','threshold1 thr=10')

Flow('plane-recon','plane-thr plane-dip','seislet adj=n inv=y dip=${SOURCES[1]} eps=0.2 type=b')
Flow('sep1-recon','sep1-thr sep1-dip','seislet adj=n inv=y dip=${SOURCES[1]} eps=0.01 type=b')
Flow('sep2-recon','sep2-thr sep2-dip','seislet adj=n inv=y dip=${SOURCES[1]} eps=0.01 type=b')

Grey2('sep1-recon','')
Grey2('sep2-recon','')


## EMD & Seis
Flow('plane-emdseis','sep1-recon sep2-recon','add scale=1,1 ${SOURCES[1]}')
Flow('plane-emdseis-dif','plane plane-emdseis','add scale=1,-1 ${SOURCES[1]}')
Grey2('plane-emdseis','')
Grey2('plane-emdseis-dif','')

## Seis
Flow('plane-seis-dif','plane plane-recon','add scale=1,-1 ${SOURCES[1]}')
Grey2('plane-recon','')
Grey2('plane-seis-dif','')

##
Flow('plane-fx','plane','put d1=0.004 o1=0 d2=1 o2=0 | fxdecon lenf=40 n2w=256')
Flow('plane-fx-dif','plane plane-fx','add scale=1,-1 ${SOURCES[1]}')
Grey2('plane-fx','')
Grey2('plane-fx-dif','')

## dip component
Flow('dip1','planes3','window n3=1 f3=1')
Flow('dip2','planes3','window n3=1 f3=2')
Flow('dip3','planes3','window n3=1 f3=3')
Flow('dip4','planes3','window n3=1 f3=4')
Flow('dip5','planes3','window n3=1 f3=5')
Grey2('dip1','')
Grey2('dip2','')
Grey2('dip3','')
Grey2('dip4','')
Grey2('dip5','')

Flow('dip33','dip3 dip4 dip5','add scale=1,1,1 ${SOURCES[1:3]}')
Grey2('dip33','')

Flow('dip44','dip4 dip5','add scale=1,1 ${SOURCES[1]}')
Grey2('dip44','')





matcykROOT='../matfun'
############################################################
## EMD thresholding
############################################################
matfun='FX_MSSA'
perc=20
n1=512
n2=256
dt=0.004
lf=1
hf=120
N1=2
N2=3
N3=2
N4=1
N5=2
verb=1
Flow('dip1-emd0',[os.path.join(matcykROOT,matfun+'.m'),'dip1'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(dt)g,%(lf)g,%(hf)g,%(N1)d,%(verb)d);quit"
     '''%vars(),stdin=0,stdout=-1)
#Grey2('dip1-emd0','')

Flow('dip2-emd0',[os.path.join(matcykROOT,matfun+'.m'),'dip2'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(dt)g,%(lf)g,%(hf)g,%(N2)d,%(verb)d);quit"
     '''%vars(),stdin=0,stdout=-1)
#Grey2('dip2-emd0','')

Flow('dip3-emd0',[os.path.join(matcykROOT,matfun+'.m'),'dip3'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(dt)g,%(lf)g,%(hf)g,%(N3)d,%(verb)d);quit"
     '''%vars(),stdin=0,stdout=-1)
#Grey2('dip3-emd0','')

Flow('dip4-emd0',[os.path.join(matcykROOT,matfun+'.m'),'dip4'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(dt)g,%(lf)g,%(hf)g,%(N4)d,%(verb)d);quit"
     '''%vars(),stdin=0,stdout=-1)
#Grey2('dip4-emd0','')

Flow('dip5-emd0',[os.path.join(matcykROOT,matfun+'.m'),'dip5'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(dt)g,%(lf)g,%(hf)g,%(N5)d,%(verb)d);quit"
     '''%vars(),stdin=0,stdout=-1)
#Grey2('dip5-emd0','')


Flow('dip1-emd-p','dip1-emd0','threshold1 thr=10 | dip rect1=5 rect2=5')
Flow('dip2-emd-p','dip2-emd0','threshold1 thr=10 | dip rect1=5 rect2=5')
Flow('dip3-emd-p','dip3-emd0','threshold1 thr=10 | dip rect1=6 rect2=6')
Flow('dip4-emd-p','dip4-emd0','threshold1 thr=10 | dip rect1=7 rect2=7')
Flow('dip5-emd-p','dip5-emd0','threshold1 thr=10 | dip rect1=6 rect2=6')
Greydip('dip1-emd-p','')
Greydip('dip2-emd-p','')
Greydip('dip3-emd-p','')
Greydip('dip4-emd-p','')
Greydip('dip5-emd-p','')

Flow('dip1-p','dip1','threshold1 thr=10|dip rect1=5 rect2=5')
Flow('dip2-p','dip2','threshold1 thr=10|dip rect1=5 rect2=5')
Flow('dip3-p','dip3','threshold1 thr=10|dip rect1=5 rect2=5')
Flow('dip4-p','dip4','threshold1 thr=10|dip rect1=5 rect2=5')
Flow('dip5-p','dip5','threshold1 thr=10|dip rect1=5 rect2=5')


thr=2
Flow('dip1-thr','dip1 dip1-p','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.2 type=b| threshold1 thr=%g |seislet adj=n inv=y dip=${SOURCES[1]} eps=0.2 type=b   '%thr)
Flow('dip2-thr','dip2 dip2-p','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.1 type=b| threshold1 thr=%g |seislet adj=n inv=y dip=${SOURCES[1]} eps=0.1 type=b   '%thr)
Flow('dip3-thr','dip3 dip3-p','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.1 type=b| threshold1 thr=%g |seislet adj=n inv=y dip=${SOURCES[1]} eps=0.1 type=b   '%thr)
Flow('dip4-thr','dip4 dip4-p','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.1 type=b| threshold1 thr=%g |seislet adj=n inv=y dip=${SOURCES[1]} eps=0.1 type=b   '%thr)
Flow('dip5-thr','dip5 dip5-p','seislet adj=y inv=y dip=${SOURCES[1]} eps=0.1 type=b| threshold1 thr=%g |seislet adj=n inv=y dip=${SOURCES[1]} eps=0.1 type=b   '%thr)
Grey2('dip1-thr','')
Grey2('dip2-thr','')
Grey2('dip3-thr','')
Grey2('dip4-thr','')
Grey2('dip5-thr','')


Grey22('dip1-emd0','dip1-thr','')
Grey22('dip2-emd0','dip2-thr','')
Grey22('dip3-emd0','dip3-thr','')
Grey22('dip4-emd0','dip4-thr','')
Grey22('dip5-emd0','dip5-thr','')


Flow('dips-thr','dip1-thr dip2-thr dip3-thr dip4-thr dip5-thr','add scale=1,1,1,1,1 ${SOURCES[1:5]}')
Grey2('dips-thr','')

Flow('dips-thr-dif','plane dips-thr','add scale=1,-1 ${SOURCES[1]}')
Grey2('dips-thr-dif','')


matcykROOT='../matfun'
############################################################
## Curvelet thresholding
############################################################
matfun='Curvelet'
perc=20
n1=512
n2=256
Flow('plane-curv',[os.path.join(matcykROOT,matfun+'.m'),'plane'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(perc)g);quit"
     '''%vars(),stdin=0,stdout=-1)
Flow('plane-curv-dif','plane plane-curv','add scale=1,-1 ${SOURCES[1]}')
Grey2('plane-curv','')
Grey2('plane-curv-dif','')

## Calculate SNR
"""
Flow('plane-diff1','plane-c plane','add scale=1,-1 ${SOURCES[1]} ')
Flow('plane-snr1','plane-c plane-diff1','snr2 noise=${SOURCES[1]}')

Flow('plane-diff2','plane-c plane-recon','add scale=1,-1 ${SOURCES[1]} ')
Flow('plane-snr2','plane-c plane-diff2','snr2 noise=${SOURCES[1]}')

Flow('plane-diff3','plane-c plane-emdseis','add scale=1,-1 ${SOURCES[1]} ')
Flow('plane-snr3','plane-c plane-diff3','snr2 noise=${SOURCES[1]}')

Flow('plane-diff4','plane-c plane-curv','add scale=1,-1 ${SOURCES[1]} ')
Flow('plane-snr4','plane-c plane-diff4','snr2 noise=${SOURCES[1]}')

Flow('plane-diff5','plane-c plane-fx','add scale=1,-1 ${SOURCES[1]} ')
Flow('plane-snr5','plane-c plane-diff5','snr2 noise=${SOURCES[1]}')
"""





End()

sfput
sfwindow
sfstack
sfgrey
sfthreshold1
sfdip
sfseislet
sfadd
sffxdecon