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

def cubeplot(title,clip='',extra=''):
    return '''
    window n1=751 |
    byte gainpanel=all %s  bar=bar.rsf |
    grey3 frame1=400 frame2=100 frame3=100 flat=y point1=0.7 point2=0.5
    crowd1=0.45 label1=Time unit1=s label2=Inline label3=Crossline
    labelfat=4 font=2 titlefat=4
    title="%s" %s wanttitle=n scalebar=n bar=bar.rsf
    ''' % (clip,title,extra)

def dipplot(title,clip='',extra=''):
    return '''
    window n1=751 |
    byte gainpanel=all %s  bar=bar.rsf |
    grey3 frame1=400 frame2=100 frame3=100 flat=y point1=0.7 point2=0.5
    crowd1=0.45 label1=Time unit1=s label2=Inline label3=Crossline
    labelfat=4 font=2 titlefat=4
    title="%s" %s wanttitle=n bar=bar.rsf barlabel=Slope barunit=samples
    ''' % (clip,title,extra)
##################
# Field data test
##################

segy = 'sgr.sgy'

Fetch(segy,'hongliu',private)

# convert segy to RSF
Flow('sgr tsgr sgr.asc sgr.bin',segy,
     '''
     segyread tape=%s tfile=${TARGETS[1]} read=b
     hfile=${TARGETS[2]} bfile=${TARGETS[3]}
     ''' % segy,stdin=0)
Flow('3dsgr','sgr','put o1=1 d1=0.004 n2=201 o2=0 d2=1 n3=201 o3=0 d3=1')

Result('sgr-ori','3dsgr',cubeplot('Original Section','clip=6000','color=I'))

ns2=2
ns3=2

# Dip
Flow('sgr-dip','3dsgr',
     'dip rect1=10 rect2=10 rect3=10 order=3')

Flow('sgr-dip1','sgr-dip','window n4=1')
Flow('sgr-dip2','sgr-dip','window f4=1')

Result('sgr-dip1',
       dipplot('Inline Dip','allpos=n',
               '''
               color=j wanttitle=n scalebar=y bartype=h
               '''))
Result('sgr-dip2',
       dipplot('Crossline Dip','allpos=n',
               '''
               color=j wanttitle=n scalebar=y bartype=h
               '''))
Flow('sgr-dips','sgr-dip1 sgr-dip2','cat axis=4 ${SOURCES[1]}')

# Spray
Flow('sgr-spray','3dsgr sgr-dip',
     'pwspray2 ns2=%d ns3=%d dip=${SOURCES[1]}'% (ns2,ns3))

Flow('sgr-spray3','3dsgr sgr-dip',
     'pwspray3 ns2=%d ns3=%d dip=${SOURCES[1]}'% (ns2,ns3))

# LUM filtering
Flow('sgr-lum','sgr-spray',
     '''
     transp plane=12 | lum nfw=25 shnclip=13 smnclip=13 boundary=y |
     sfwindow n1=1 f1=13
     ''')
Result('sgr-lum',cubeplot('LUM filter','clip=6000','color=I'))
Flow('sgr-ldif','3dsgr sgr-lum','sfadd scale=1,-1 ${SOURCES[1]}')
Result('sgr-ldif',
       cubeplot('Differenct data (LUM filter)','clip=6000','color=I'))

# Improved similarity stacking (Gaussian taper)
Flow('sgr-stacks','3dsgr','spray axis=2 n=%d ' % ((2*ns2+1)*(2*ns3+1)))

Flow('sgr-weight','sgr-stacks sgr-spray',
     '''
     similarity other=${SOURCES[1]} rect1=10 rect4=1 verb=n
     ''')

Flow('sgr-weight0','sgr-weight','threshold pclip=99')
Flow('sgr-gausweight','sgr-spray',
     '''
     window n1=1 n3=1 n4=1 squeeze=n |
     put n2=5 n3=5 o2=-0.016 o3=-0.016 d2=0.008 d3=0.008 | 
     sfmath output="exp(-(x2*x2+x3*x3)/((0.02)*(0.02)))" |
     put n2=25 o2=0 d2=1  n3=1 o3=1 d3=1 | window |
     spray axis=1 n=751 |spray axis=3 n=201 | spray axis=4 n=201 |
     put d1=0.004 o1=1 d3=1 o3=0 d4=1 o4=0
     ''')

Flow('sgr-weight1','sgr-gausweight sgr-weight0',
     '''
     add mode=p ${SOURCES[1]}
     ''')


Flow('norm','sgr-weight1','stack')
Flow('sgr-gsstack','sgr-spray sgr-weight1 norm',
     'add mode=p ${SOURCES[1]} | stack | add mode=d ${SOURCES[2]}')
Result('sgr-gsstack',
       cubeplot('Improved similarity-mean filter','clip=6000','color=I'))

Flow('sgr-gdif','3dsgr sgr-gsstack','sfadd scale=1,-1 ${SOURCES[1]}')
Result('sgr-gdif',
       cubeplot('Differenct data (similarity-mean filter)',\
                'clip=6000','color=I'))

End()

sfsegyread
sfput
sfwindow
sfbyte
sfgrey3
sfdip
sfcat
sfpwspray3
sftransp
sflum
sfadd
sfspray
sfsimilarity
sfthreshold
sfmath
sfstack