up [pdf]
from rsf.proj import *
from rsf.recipes.beg import server as private

input = 'input.curve.segy'
Fetch(input,'ray',private)
Flow('curve hcurve bcurve',input,
     '''
     segyread tape=$SOURCE read=d hfile=${TARGETS[1]} bfile=${TARGETS[2]}
     ''',stdin=0)

Flow('curve2','curve','window min1=1.2 max1=2 | bandpass fhi=60')

# Nonstationary PEFs
Flow('cpad cmask','curve2','lpad jump=4 mask=${TARGETS[1]}')
Flow('cdmask','cpad','math output=1.')
Flow('capef','cpad cdmask',
     '''
     apef a=20,3 jump=4 rect1=20 rect2=3 niter=200 verb=y
     maskin=${SOURCES[1]}
     ''')
Flow('cacov','cpad capef cmask',
     'miss4 filt=${SOURCES[1]} mask=${SOURCES[2]} verb=y')
Plot('cacov',
     '''
     grey clip=0.02 yreverse=y transp=y poly=y title="Adaptive PEF"
     ''')
Result('jcacov','cacov',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="Input"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
####################
# TEST ONE
####################
# Add noise
Flow('noiz','cacov',
     '''
     noise rep=n type=n seed=20106 range=0.0234818 |
     smooth rect1=3
     ''')
Result('noiz',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="Noisy data"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

#######################
# Stationary PEF
#######################
Flow('npef nlag','noiz','lpef a=2,1 lag=${TARGETS[1]}')
Flow('spef slag','noiz','lpef a=5,2 lag=${TARGETS[1]}')
Flow('npef2 nlag2','npef','hconv other=$SOURCE lag=${TARGETS[1]}')
Flow('sign2','noiz spef npef',
     '''
     signoi epsilon=1. sfilt=${SOURCES[1]} nfilt=${SOURCES[2]}
     spitz=y niter=100
     ''')#spitz=y niter=100
Result('pss','sign2',
       '''
       window n3=1 f3=0 |
       grey yreverse=y transp=y poly=y label2=Position title="PEF signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2
       ''')
Result('pnn','sign2',
       '''
       window n3=1 f3=1 |
       grey yreverse=y transp=y poly=y label2=Position title="PEF noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2
       ''')

#######################
# Adaptive PF
#######################
Flow('aspred','noiz',
     '''
     txrna2 a=5,6 rect1=60 rect2=20 niter=50
     verb=y
     ''')

Result('aspred',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="t-x APF signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
Result('asnoiz','noiz aspred',
       '''
       add scale=1,-1 ${SOURCES[1]} |
       grey yreverse=y transp=y poly=y label2=Position title="t-x APF noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

# 2D fxRNA
Flow('fx','noiz','fft1 | transp plaen=12 memsize=1000')
################
# fx RNA
###############
na = 4
n1=240
shifts = []
for s in range(1,na):
    shift = 'shift-%d' % s
    Flow(shift,'fx','window f1=%d | pad end1=%d' % (s,s))
    shifts.append(shift)

    shift = 'shift%d' % s
    Flow(shift,'fx',' pad beg1=%d | window n1=%d' % (s,n1))
    shifts.append(shift)
Flow('shift',shifts,'cat axis=3 ${SOURCES[1:%d]} | put o1=0' % len(shifts))

Flow('pef pre','shift fx',
     'clpf match=${SOURCES[1]} rect2=20 rect1=10 niter=30 pred=${TARGETS[1]}')

Flow('fxrna','pre','transp plane=12 memszie=1000 | fft1 inv=y')
Result('fxrna',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="f-x RNA signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

Flow('fxnoiz','noiz fxrna','add scale=1,-1 ${SOURCES[1]}')
Result('fxnoiz',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="f-x RNA noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

# FX decon
Flow('patch','noiz','patch w=401,80')
Flow('wpatch','patch','window')
fxds = []
mpas = []
for nw in range(0,5):
    data = 'data%d' % nw
    fxd  = 'fx%d'   % nw
    Flow(data,'wpatch','window n3=1 f3=%d' % nw)
    Flow(fxd,data,'fxdecon lenf=4 n2w=10')
    fxds.append(fxd)

    lom = 'lom%d' %nw
    lag = 'lag%d' %nw
    mpa = 'mpa%d' %nw
    Flow([lom, lag],data,'lpef niter=100 a=5,5 lag=${TARGETS[1]}')
    Flow(mpa,[data,lom],'helicon filt=${SOURCES[1]}')
    mpas.append(mpa)
Flow('fxpatch',fxds,
     '''
     cat ${SOURCES[1:%d]} axis=3 | transp plane=34 |
     patch inv=y weight=y
     ''' % len(fxds))
Result('fxpatch',
       '''
       grey yreverse=y transp=y poly=y label2=Position
       title="f-x deconvolution signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.01 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
Flow('fxdiff','noiz fxpatch','add scale=1,-1 ${SOURCES[1]}')
Result('fxdiff',
       '''
       grey yreverse=y transp=y poly=y label2=Position
       title="f-x deconvolution noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

Flow('txdiff',mpas,
     '''
     cat ${SOURCES[1:%d]} axis=3 | transp plane=34 |
     patch inv=y weight=y
     ''' % len(mpas))
Result('txdiff',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="t-x PF noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
Flow('txpatch','noiz txdiff','add scale=1,-1 ${SOURCES[1]}')
Result('txpatch',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="t-x PF signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
####################
# TEST TWO
####################
Flow('noiz1','cacov',
     '''
     noise rep=n type=n seed=20106 range=0.0234818 |
     saltpepper inten=5. den=0.5 seed=201406 |
     smooth rect1=3
     ''')
Result('noiz1',
       '''
       wiggle yreverse=y transp=y poly=y label2=Position
       title="Hybrid noise"
       wheretitle=b wherexlabel=t
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

#######################
# Stationary PEF
#######################
Flow('npef1 nlag1','noiz1','lpef a=2,1 lag=${TARGETS[1]}')
Flow('spef1 slag1','noiz1','lpef a=5,2 lag=${TARGETS[1]}')
Flow('npef12 nlag12','npef1','hconv other=$SOURCE lag=${TARGETS[1]}')
Flow('sign12','noiz1 spef1 npef1',
     '''
     signoi epsilon=1. sfilt=${SOURCES[1]} nfilt=${SOURCES[2]}
     spitz=y niter=100
     ''')#spitz=y niter=100
Result('pss1','sign12',
       '''
       window n3=1 f3=0 |
       grey yreverse=y transp=y poly=y label2=Position title="PEF signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2
       ''')
Result('pnn1','sign12',
       '''
       window n3=1 f3=1 |
       grey yreverse=y transp=y poly=y label2=Position title="PEF noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2
       ''')
#######################
# Adaptive PF
#######################
Flow('aspred1','noiz1',
     '''
     txrna2 a=5,6 rect1=60 rect2=20 niter=50
     verb=y
     ''')

Result('aspred1',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="t-x APF signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
Result('asnoiz1','noiz1 aspred1',
       '''
       add scale=1,-1 ${SOURCES[1]} |
       grey yreverse=y transp=y poly=y label2=Position title="t-x APF noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

# 2D fxRNA
Flow('fx-1','noiz1','fft1 | transp plaen=12 memsize=1000')
################
# fx RNA
###############
na = 4
n1=240
shifts1 = []
for s in range(1,na):
    shift1 = 'shift1-%d' % s
    Flow(shift1,'fx-1','window f1=%d | pad end1=%d' % (s,s))
    shifts1.append(shift1)

    shift1 = 'shift1%d' % s
    Flow(shift1,'fx-1',' pad beg1=%d | window n1=%d' % (s,n1))
    shifts1.append(shift1)
Flow('shift2-1',shifts1,'cat axis=3 ${SOURCES[1:%d]} | put o1=0' % len(shifts1))

Flow('pef1 pre1','shift2-1 fx-1',
     'clpf match=${SOURCES[1]} rect2=20 rect1=10 niter=30 pred=${TARGETS[1]}')

Flow('fxrna1','pre1','transp plane=12 memszie=1000 | fft1 inv=y')
Result('fxrna1',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="f-x RNA signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

Flow('fxnoiz1','noiz1 fxrna1','add scale=1,-1 ${SOURCES[1]}')
Result('fxnoiz1',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="f-x RNA noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

# FX decon
Flow('patch1','noiz1','patch w=401,80')
Flow('wpatch1','patch1','window')
fxds1 = []
mpas1 = []
for nw in range(0,5):
    data1 = 'data1-%d' % nw
    fxd1  = 'fx1-%d'   % nw
    Flow(data1,'wpatch1','window n3=1 f3=%d' % nw)
    Flow(fxd1,data1,'fxdecon lenf=4 n2w=10')
    fxds1.append(fxd1)

    lom1 = 'lom1%d' %nw
    lag1 = 'lag1%d' %nw
    mpa1 = 'mpa1%d' %nw
    Flow([lom1, lag1],data1,'lpef niter=100 a=5,5 lag=${TARGETS[1]}')
    Flow(mpa1,[data1,lom1],'helicon filt=${SOURCES[1]}')
    mpas1.append(mpa1)
Flow('fxpatch1',fxds1,
     '''
     cat ${SOURCES[1:%d]} axis=3 | transp plane=34 |
     patch inv=y weight=y
     ''' % len(fxds1))
Result('fxpatch1',
       '''
       grey yreverse=y transp=y poly=y label2=Position
       title="f-x deconvolution signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
Flow('fxdiff1','noiz1 fxpatch1','add scale=1,-1 ${SOURCES[1]}')
Result('fxdiff1',
       '''
       grey yreverse=y transp=y poly=y label2=Position
       title="f-x deconvolution noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')

Flow('txdiff1',mpas1,
     '''
     cat ${SOURCES[1:%d]} axis=3 | transp plane=34 |
     patch inv=y weight=y
     ''' % len(mpas1))
Result('txdiff1',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="t-x PF noise"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')
Flow('txpatch1','noiz1 txdiff1','add scale=1,-1 ${SOURCES[1]}')
Result('txpatch1',
       '''
       grey yreverse=y transp=y poly=y label2=Position title="t-x PF signal"
       screenratio=0.4 screenht=6. labelsz=5. titlesz=7 clip=0.02 
       labelfat=2 font=2 titlefat=2 unit2=km
       ''')


End()

sfsegyread
sfwindow
sfbandpass
sflpad
sfmath
sfapef
sfmiss4
sfgrey
sfnoise
sfsmooth
sflpef
sfhconv
sfsignoi
sftxrna2
sfadd
sffft1
sftransp
sfpad
sfcat
sfput
sfclpf
sfpatch
sffxdecon
sfhelicon
sfsaltpepper
sfwiggle