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

#def Grey(data,other):
#       Result(data,'grey label2=Trace labelsz=10 titlesz=10 clip=1 color=g label1=Time unit1=s screenratio=0.8 %s'%other)

def Grey(data,other):
        Result(data,'window j2=1 | wiggle transp=y yreverse=y poly=y label2=Trace labelsz=10 titlesz=10 clip=0.5 color=g label1=Time unit1=s screenratio=1.2 %s'%other)

def Graph(data,other):
        Result(data,'graph label1="Time" label2="Amplitude" unit2= unit1="s" labelsz=10 labelfat=4 font=2 titlesz=10 titlefat=4 title="" wherexlabel=b wheretitle=t screenratio=0.5 %s' %other)


########################################################################
# INITIALIZATION
########################################################################
matlab         = WhereIs('matlab')
matROOT = '../matfun/'
matfun = 'Gendata'
matlabpath = os.environ.get('MATLABPATH',os.path.join(RSFROOT,'lib'))

if not matlab:
    sys.stderr.write('\nCannot find Matlab.\n')
    sys.exit(1)
n1=151
n2=61
dt=0.004
lf=5
hf=120
N=20
r=2
verb=0
put='d1=0.004 o1=0 d2=1 o2=1'
############################################################
## Generate data
############################################################
Flow('flat-c0 flat-0',[os.path.join(matROOT,matfun+'.m')],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${TARGETS[0]}','${TARGETS[1]}');quit"
     '''%vars(),stdin=0,stdout=-1)
Flow('flat-c','flat-c0','put %s'%put)
Flow('flat','flat-0','put %s'%put)
############################################################
## Processing OPT
############################################################    
matfun = 'OPT0'
matlabpath = os.environ.get('MATLABPATH',os.path.join(RSFROOT,'lib'))

Flow('flat-opt0',[os.path.join(matROOT,matfun+'.m'),'flat'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(N)d,%(r)d);quit"
     '''%vars(),stdin=0,stdout=-1)
Flow('flat-opt','flat-opt0','put %s'%put)

############################################################
## Processing KL
############################################################    
matfun = 'KL'
matlabpath = os.environ.get('MATLABPATH',os.path.join(RSFROOT,'lib'))
r=3
Flow('flat-kl0',[os.path.join(matROOT,matfun+'.m'),'flat'],
     '''MATLABPATH=%(matlabpath)s %(matlab)s 
     -nosplash -nojvm -r "addpath %(matROOT)s;%(matfun)s('${SOURCES[1]}','${TARGETS[0]}',%(n1)d,%(n2)d,%(r)d);quit"
     '''%vars(),stdin=0,stdout=-1)
Flow('flat-kl','flat-kl0','put %s'%put)

Flow('flat-kl-dif','flat flat-kl','add scale=1,-1 ${SOURCES[1]}')
Flow('flat-opt-dif','flat flat-opt','add scale=1,-1 ${SOURCES[1]}')

Grey('flat-c','title="Clean"')
Grey('flat','title="Noisy"')
Grey('flat-kl','title="KL"')
Grey('flat-opt','title="OPT"')
Grey('flat-kl-dif','title="KL"')
Grey('flat-opt-dif','title="OPT"')


Flow('flat-ss','flat-c flat-opt flat flat-kl','cat axis=3 ${SOURCES[1:4]} | window n2=1 f2=20')
Graph('flat-ss','plotfat=10 plotcol="7,3,5,6"')

Flow('flat-ss-z','flat-c flat-opt flat flat-kl','cat axis=3 ${SOURCES[1:4]} | window n2=1 f2=20 |window min1=0.149 max1=0.251')
Graph('flat-ss-z','plotfat=10 plotcol="7,3,5,6"')


End()

sfput
sfadd
sfwindow
sfwiggle
sfcat
sfgraph