up [pdf]
from rsf.proj import *
top = project.get('RSFROOT')
libdir = os.path.join(top,'lib')

path = {'exe':WhereIs('matlab')}
for key in ('MATLAB','LD_LIBRARY_'):
    oldpath = os.environ.get(key+'PATH')
    if oldpath:
        paths = oldpath.split(':')
        if libdir not in paths:
            paths.append(libdir)
        path[key] = ':'.join(paths)
    else:
        path[key] = libdir

Flow('vel',None,'spike n1=256 d1=5 | math output=1000+x1')
Result('vel','graph title=Velocity')
Flow('fft','vel','cosft sign1=1')
dt=0.001
nt=500
Flow('sig',None,'spike n1=256 k1=128 d1=5 label1= unit1= | sfmath output="exp(-(32*(x1/(256.0*5)-0.5))*32*(x1/(256.0*5)-0.5))" ')
Result('sig','graph  title="Source Wavelet" ')

Flow('G Mexact Mlr Mapp Mfd10','vel',
     '''
     lfdc1 dt=0.001 eps=0.0001 npk=20 seed=2010
     Mexact=${TARGETS[1]}
     Mlr=${TARGETS[2]}
     Mapp=${TARGETS[3]}
     Mfd=${TARGETS[4]}
     ''')
#     lfdc1_10 dt=0.001 eps=0.0001 npk=20 seed=2010

cases = dict(Mexact='Exact Matrix',
             Mlr='Matrix approximated by Lowrank',
             Mapp='Matrix approximated by Lowrank',
             Mfd10='Matrix approximated by 10th-order FD',
             Mlrerr='Error by Lowrank',
             Mapperr='Error by 10th-order lowrank FD',
             Mfd10err='Error by 10th-order conventional FD')

for key in cases.keys():
    Result(key,'window max2=0.035 | grey color=j transp=n scalebar=y parallel2=n label2=Wavenumber unit2=1/m label1=Distance unit1=m pclip=100 title="%s" ' % cases[key])

Flow('Mlrerr','Mlr Mexact','add ${SOURCES[1]} scale=1,-1')
Flow('Mapperr','Mapp Mexact','add ${SOURCES[1]} scale=1,-1')
Flow('Mfd10err','Mfd10 Mexact','add ${SOURCES[1]} scale=1,-1')

Result('slicel','Mapperr Mfd10err ','cat ${SOURCES[1]} | window max2=0.035 n1=1 f1=128 | graph dash=0,2 plotfat=7 label1=Wavenumber unit1=1/m label2=Error title=')
#Flow('curve4 curve6 curve8 curve10 cut','vel ../Matlab/curve_1D.m ../Matlab/lowrank.m',
#     '''
#     MATLABPATH=%(MATLAB)s LD_LIBRARY_PATH=%(LD_LIBRARY_)s
#     %(exe)s -nosplash -nojvm -nodesktop 
#     -r "addpath ../Matlab; curve_1D('${SOURCES[0]}','${TARGETS[4]}',0.001,0.0001,30,'${TARGETS[0]}','${TARGETS[1]}','${TARGETS[2]}','${TARGETS[3]}');quit"
#     ''' % path,stdin=0,stdout=-1)
#Flow('order4','cut curve4','cmplx ${SOURCES[1]} | sftransp plane=12')
#Flow('order6','cut curve6','cmplx ${SOURCES[1]} | sftransp plane=12')
#Flow('order8','cut curve8','cmplx ${SOURCES[1]} | sftransp plane=12')
#Flow('order10','cut curve10','cmplx ${SOURCES[1]} | sftransp plane=12')
#Result('order4','sfgraph') 
#Result('order6','sfgraph') 
#Result('order8','sfgraph') 
#Result('order10','sfgraph') 
#Result('curves','order4 order6 order8 order10',
#       '''
#       sfcat ${SOURCES[1]} ${SOURCES[2]} ${SOURCES[3]} axis=2 |
#       graph dash=0,1,2,3 label1="CUT" label2="Error" title=" " plotfat=7
#       ''')

End()

sfspike
sfmath
sfgraph
sfcosft
sflfdc1
sfwindow
sfgrey
sfadd
sfcat