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

Fetch('elf7_win.HH','total',private)

Flow('win','elf7_win.HH','dd form=native')

def cubeplot(title,clip='',extra=''):
    return '''
    window n1=194 min1=925 |
    byte gainpanel=all %s |
    grey3 frame1=116 frame2=150 frame3=18 flat=y point2=0.839 point1=0.776
    label1=Depth unit1=m label2=Inline unit2=m label3=Crossline unit3=m
    title="%s" %s screenratio=0.7184 labelsz=6
    ''' % (clip,title,extra)

Result('win',cubeplot(''))
Plot('win',cubeplot(''))

Flow('patch','win',
     'pad beg1=25 end1=25 | patch p=1,4,1')
Flow('mask','win',
     '''
     math output=1 | pad beg1=25 end1=25 |
     patch p=1,4,1
     ''')

Flow('dip','patch mask',
     'dip rect1=5 rect2=5 rect3=5 order=3 mask=${SOURCES[1]}',
     split=[5,4,[0,1]])

Flow('wdip1','dip','window n4=1 squeeze=n | patch inv=y weight=y dim=3')
Flow('wdip2','dip','window f4=1 squeeze=n | patch inv=y weight=y dim=3')

Result('wdip1',
       cubeplot('Inline Dip','',
                '''
                color=j wanttitle=n
                barlabel="Inline Dip"
                '''))
Result('wdip2',
       cubeplot('Crossline Dip','',
                '''
                color=j wanttitle=n
                barlabel="Crossline Dip"
                '''))
Flow('dips','wdip1 wdip2','cat axis=4 ${SOURCES[1]}')

Flow('shift1','win','window f2=1')
Flow('shift2','win','window f3=1')

Flow('last1','win','window f2=291 squeeze=n')
Flow('last2','win','window f3=55  squeeze=n')

Flow('ref1','shift1 last1','cat axis=2 ${SOURCES[1]}')
Flow('ref2','shift2 last2','cat axis=3 ${SOURCES[1]}')

Flow('ref1s','ref1','add mode=p $SOURCE | stack axis=1 norm=n')
Flow('ref2s','ref2','add mode=p $SOURCE | stack axis=1 norm=n')

Flow('corr1','ref1 win','add mode=p ${SOURCES[1]} | stack axis=1 norm=n')
Flow('corr2','ref2 win','add mode=p ${SOURCES[1]} | stack axis=1 norm=n')

Flow('win2','win','add mode=p $SOURCE | stack axis=1 norm=n')

Flow('wcos1','corr1 win2 ref1s',
     '''
     math s1=${SOURCES[1]} s2=${SOURCES[2]} output="(s1*s2)/(input*input)"
     ''')
Flow('wcos2','corr2 win2 ref2s',
     '''
     math s1=${SOURCES[1]} s2=${SOURCES[2]} output="(s1*s2)/(input*input)"
     ''')

Flow('wcost1','wdip1','stack axis=1 rms=y norm=n | math output="exp(input)" ')
Flow('wcost2','wdip2','stack axis=1 rms=y norm=n | math output="exp(input)" ')

for case in ('wcos','wcost'):
    Plot(case+'1',
         '''
         smooth rect1=25 rect2=25 |
         grey color=j scalebar=y bartype=v allpos=y bias=1
         title="Inline Cost" transp=n yreverse=n
         label1=Inline unit1=m label2=Crossline unit2=m
         ''')
    Plot(case+'2',
         '''
         smooth rect1=25 rect2=25 |
         grey color=j scalebar=y bartype=v allpos=y bias=1
         title="Crossline Cost" transp=n yreverse=n
         label1=Inline unit1=m label2=Crossline unit2=m
         ''')
    Flow(case,[case+'1',case+'2'],
         '''
         cat axis=3 ${SOURCES[1]} |
         smooth rect1=40 rect2=40 
         ''')
    Result(case,[case+'1',case+'2'],'OverUnderAniso')

    Flow('seed','dips','window n2=1 n3=1 n4=1 | math output=x1')

paths = [2*[50,100,150,200,250],5*[18]+5*[36]]

def plotpath(col,fat):
    return '''
    dd type=complex | window |
    graph plotcol=%d plotfat=%d min1=0 max1=291 min2=0 max2=55
    wantaxis=n wanttitle=n screenht=2.8 screenwd=14.6
    ''' % (col,fat)

pnames = map(lambda x,y: 'path-%d-%d' % (x,y),paths[0],paths[1])

Flow(['time']+pnames,'wcos',
     '''
     dijkstra ref1=150 ref2=18
     nf=%d paths=%s fin1=%s fin2=%s
     ''' % (len(pnames),
            string.join(map(lambda x: '${TARGETS[%d]}' % x,
                            range(1,len(pnames)+1)),':'),
            string.join(map(str,paths[0]),','),
            string.join(map(str,paths[1]),',')))
Plot('time',
     '''
     grey color=j allpos=y
     title="Minimum Time" transp=n yreverse=n 
     screenht=2.8 screenwd=14.6
     label1=Inline unit1=m label2=Crossline unit2=m
     labelsz=6 wanttitle=n
     ''')

Flow('wtime2','wcos',
       '''
       mul $SOURCE | stack axis=3 norm=n |
       put o1=0 d1=1 o2=0 d2=1 o3=0 d3=1 |
       eikonal vel=n zshot=150 yshot=18
       ''')
Result('wtime2',
       '''
       grey color=j allpos=y
       title="Minimum Time" transp=n yreverse=n 
       screenht=2.8 screenwd=14.6
       label1=Inline unit1=m label2=Crossline unit2=m
       labelsz=6 wanttitle=n
       ''')

for path in pnames:
    Plot('w'+path,path,plotpath(7,1))
    Plot('b'+path,path,plotpath(0,6))
    Plot(path,['b'+path,'w'+path],'Overlay')

Result('wtime',['time']+pnames,'Overlay')

Flow('wpick3','dips seed wcos',
     '''
     pwpaint3 seed=${SOURCES[1]} cost=${SOURCES[2]} ref2=150 ref3=18 order=3 |
     bandpass fhi=0.02
     ''')
Flow('wpick','dips seed wtime2',
     'pwpaint2 seed=${SOURCES[1]} cost=${SOURCES[2]} order=3')
Result('wpick',cubeplot('Relative Age','allpos=y') + ' color=j')

Flow('wflat','win wpick',
     'pad beg1=25 end1=25 | iwarp warp=${SOURCES[1]} eps=1 n1=194 o1=925')
Result('wflat',cubeplot(' '))

# Contour3

Flow('wcont','win',
     '''
     window n2=1 n3=1 f2=150 f3=18 | pad beg1=25 end1=35 | envelope | 
     max1 | window n1=25 | real
     ''')
Plot('wcont','wpick wcont',
     '''
     window n1=194 min1=925 |
     contour3 frame1=116 frame2=150 frame3=18 flat=y point2=0.839 point1=0.776
     wanttitle=n wantaxis=n screenratio=0.7184 plotfat=5 cfile=${SOURCES[1]}
     ''')
Result('wcont','win wcont','Overlay')

Flow('k1.p','wcont',
     '''
     math output="1.5+(input-612.5)/12.5" | 
     dd type=int form=ascii format="%d," line=25 --out=$TARGET
     ''',stdout=0)
Command('k1.par','k1.p',
        'printf "k1=" > $TARGET && cat $SOURCE >> $TARGET')
Flow('spikes','k1.par',
     '''
     spike n1=244 nsp=16 par=$SOURCE | smooth rect1=2 repeat=2
     ''',stdin=0)

Flow('paint','dips spikes wtime2',
     'pwpaint2 seed=${SOURCES[1]} cost=${SOURCES[2]} order=3')
Flow('wpaint','paint win',
     'window n1=194 min1=925 | add scale=7e9,1 ${SOURCES[1]}')
Result('wpaint',cubeplot(' ','allpos=y','color=G'))

Flow('byte','win','byte gainpanel=all')
Flow('pick','wpick','window n1=194 min1=925')

cons = []
for i3 in range(56):
    win = 'win%d' % i3
    con = 'con%d' % i3
    Plot(win,'byte',
         '''
         grey3 frame1=116 frame2=150 frame3=%d flat=y point2=0.839 point1=0.776
         label1=Depth unit1=m label2=Inline unit2=m label3=Crossline unit3=m
         wanttitle=n screenratio=0.7184 labelsz=6
         ''' % i3)
    Plot(con,'pick wcont',
         '''
         contour3 frame1=116 frame2=150 frame3=%d point2=0.839 point1=0.776
         wanttitle=n wantaxis=n screenratio=0.7184 plotfat=5 cfile=${SOURCES[1]}
         ''' % i3)
    Plot('w'+con,[win,con],'Overlay')
    cons.append('w'+con)
Plot('cons',cons,'Movie')

# Painting

End()

sfdd
sfwindow
sfbyte
sfgrey3
sfpad
sfpatch
sfmath
sfdip
sfcat
sfadd
sfstack
sfsmooth
sfgrey
sfdijkstra
sfmul
sfput
sfeikonal
sfgraph
sfpwpaint3
sfbandpass
sfpwpaint2
sfiwarp
sfenvelope
sfmax1
sfreal
sfcontour3
sfspike