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


clip=0.8 #display percentage
nfw=7

def Grey(data,other):
        Result(data,'grey label2=Position unit2="km" label1=Time unit1="s" title="" wherexlabel=b scalebar=y wheretitle=t %s'%other)

##########################################
#    Make synthetic data:data1* & data2
##########################################
Flow('data1',None,
     '''
     spike n1=512 k1=130,150,180,250,300 nsp=5|
     ricker1 frequency=30 |
     spray axis=2 n=256 d=0.01 o=-1 label=Offset unit=km |
     nmostretch inv=y v0=4 half=n | 
     scale axis=12 | noise var=0.1
     ''')
Flow('data2',None,
     '''
     spike n1=512 k1=130,150,180,250,300 nsp=5 |
     ricker1 frequency=30 |
     spray axis=2 n=256 d=0.01 o=1 label=Offset unit=km |
     nmostretch inv=y v0=4 half=n |
     scale axis=12 | noise var=0.1
     ''')
#############################################
#               Experiment
#############################################
## Apply dithering (can also use sfblend, 
#  they are equal in the case of small dithering range)
Flow('dither','data1',
     '''
     window n1=1 |
     noise rep=y seed=122011 var=0.01
     ''')
Flow('data2d','data2 dither',
     '''
     datstretch datum=${SOURCES[1]}
     ''')
## Blending 
Flow('datas','data1 data2d','add ${SOURCES[1]}')

# Apply shift and get the blended data2
Flow('udatas','datas dither',
     '''
     datstretch datum=${SOURCES[1]} inv=y
     ''')

#######################################################################
## filtering using median filter and space-varying median filter
#######################################################################
## using median filter
Flow('deblended1mf','datas','transp | mf nfw=%d | transp'%nfw)
Flow('deblended2mf','udatas','transp | mf nfw=%d | transp'%nfw)

## using svmf filter
Flow('deblended1tsmf L1-tsmf-n','datas','transp  | tsmf nfw=%d L=${TARGETS[1]} | transp'%nfw)
Flow('deblended2tsmf L2-tsmf-n','udatas','transp | tsmf nfw=%d L=${TARGETS[1]} | transp'%nfw)

Flow('deblended1svmf-mf','datas','transp | mf nfw=%d | transp'%nfw)
Flow('deblended2svmf-mf','udatas','transp | mf nfw=%d | transp'%nfw)
Flow('simi1-n','deblended1svmf-mf datas','similarity other=${SOURCES[1]} rect1=5 rect2=5 | transp  | scale axis=2')
Flow('simi2-n','deblended2svmf-mf udatas','similarity other=${SOURCES[1]} rect1=5 rect2=5 | transp | scale axis=2')
Result('simi1-n','transp | grey color=j label2=Position unit2="km" label1=Time unit1="s" title="Signal reliability" wherexlabel=b scalebar=y wheretitle=t scalebar=y allpos=y')

Result('simi2-n','transp | grey color=j label2=Position unit2="km" label1=Time unit1="s" title="Signal reliability" wherexlabel=b scalebar=y wheretitle=t scalebar=y allpos=y')

Result('energy1-n','deblended1mf','scale axis=2 | math output="abs(input)" | grey color=j label2=Position unit2="km" label1=Time unit1="s" title="Signal energy" wherexlabel=b scalebar=y wheretitle=t scalebar=y allpos=y')

Result('energy2-n','deblended2mf','scale axis=2 | math output="abs(input)" | grey color=j label2=Position unit2="km" label1=Time unit1="s" title="Signal energy" wherexlabel=b scalebar=y wheretitle=t scalebar=y allpos=y')

Flow('deblended1svmf L1-n','datas simi1-n','transp |  svmf nfw=%d L=${TARGETS[1]} similarity=${SOURCES[1]} lambda1=0.50 lambda2=0.75 lambda3=0.85 lambda4=0.95 | transp'%nfw)
Flow('deblended2svmf L2-n','udatas simi2-n','transp | svmf nfw=%d L=${TARGETS[1]} similarity=${SOURCES[1]} lambda1=0.50 lambda2=0.75 lambda3=0.85 lambda4=0.95 | transp'%nfw)


#Flow('deblended1svmf','datas','transp  | svmf nfw=%d | transp'%nfw)
#Flow('deblended2svmf','udatas','transp | tsmf nfw=%d | transp'%nfw)
## noise section using mf
Flow('diff1mf-t','datas deblended1mf','add scale=1,-1 ${SOURCES[1]}')
Flow('diff2mf-t','udatas deblended2mf','add scale=1,-1 ${SOURCES[1]}')
Flow('diff1svmf-t','datas deblended1svmf','add scale=1,-1 ${SOURCES[1]}')
Flow('diff2svmf-t','udatas deblended2svmf','add scale=1,-1 ${SOURCES[1]}')

## noise section using svmf
Flow('diff1svmf','datas deblended1svmf','add scale=1,-1 ${SOURCES[1]}')
Flow('diff2svmf','udatas deblended2svmf','add scale=1,-1 ${SOURCES[1]}')

## frame
Plot('label1',None,
        '''
        box x0=11.5 y0=6.5 label="" xt=-0.5 yt=0.5
        ''')
Plot('label2',None,
        '''
        box x0=10.5 y0=5.5 label="Signal" xt=0.5 yt=0.5
        ''')

## Ploting
Grey('data1','scalebar=n clip=%f   '%clip)
Grey('data2','scalebar=n clip=%f   '%clip)
Grey('datas','scalebar=n clip=%f   '%clip)
Grey('udatas','scalebar=n clip=%f   '%clip)
Grey('deblended1mf','scalebar=n clip=%f   '%clip)
Grey('deblended2mf','scalebar=n clip=%f   '%clip)
Grey('deblended1tsmf','scalebar=n clip=%f   '%clip)
Grey('deblended2tsmf','scalebar=n clip=%f   '%clip)
Grey('deblended1svmf','scalebar=n clip=%f   '%clip)
Grey('deblended2svmf','scalebar=n clip=%f   '%clip)

Grey('diff1mf-t','scalebar=n clip=%f   '%clip)
Result('diff1mf','Fig/diff1mf-t.vpl label1','Overlay')
Grey('diff1svmf-t','scalebar=n clip=%f   '%clip)
Result('diff1svmf','Fig/diff1svmf-t.vpl label1','Overlay')
Result('L1-n','transp | grey label2=Position unit2="km" label1=Time unit1="s" title="Filter length" wherexlabel=b scalebar=y wheretitle=t scalebar=y color=j allpos=y')
Result('L1-tsmf-n','transp | grey label2=Position unit2="km" label1=Time unit1="s" title="Filter length" wherexlabel=b scalebar=y wheretitle=t scalebar=y color=j allpos=y')
Result('L2-n','transp | grey label2=Position unit2="km" label1=Time unit1="s" title="Filter length" wherexlabel=b scalebar=y wheretitle=t scalebar=y color=j allpos=y')
Result('L2-tsmf-n','transp | grey label2=Position unit2="km" label1=Time unit1="s" title="Filter length" wherexlabel=b scalebar=y wheretitle=t scalebar=y color=j allpos=y')

Grey('diff2mf-t','scalebar=n clip=%f   '%clip)
Result('diff2mf','Fig/diff2mf-t.vpl label2','Overlay')
Grey('diff2svmf-t','scalebar=n clip=%f   '%clip)
Result('diff2svmf','Fig/diff2svmf-t.vpl label2','Overlay')

Flow('fkdata1','data1','put d2=1 | fft1 | fft3 axis=2 pad=1')
Flow('fkdatas','datas','put d2=1 | fft1 | fft3 axis=2 pad1=1')
Flow('fkdeblended1mf','deblended1mf','put d2=1 | fft1 | fft3 axis=2 pad1=1')
Flow('fkdeblended1svmf','deblended1svmf','put d2=1 | fft1 | fft3 axis=2 pad1=1 ')
Result('fkdata1','cabs | grey title="" label2="Normalized wavenumber" unit2="" color=j ')
Result('fkdatas','cabs | grey title="" label2="Normalized wavenumber" unit2="" color=j ')
Result('fkdeblended1mf','cabs | grey title="" label2="Normalized wavenumber" unit2="" color=j ')
Result('fkdeblended1svmf','cabs | grey title="" label2="Normalized wavenumber" unit2="" color=j ')


End()

sfspike
sfricker1
sfspray
sfnmostretch
sfscale
sfnoise
sfwindow
sfdatstretch
sfadd
sftransp
sfmf
sftsmf
sfsimilarity
sfgrey
sfmath
sfsvmf
sfbox
sfput
sffft1
sffft3
sfcabs