import glob,os,os.path,sys
import SCons
from SEPSCons import *

# Define libraries we will be using
libs=['-lsepgeef90','-lsep2df90','-lsep3df90','-lsep3d','-lsepf90','-lsep']
libpath='/usr/local/SEP/lib'
# Create build environment
sepenv = Environment(ENV = {'PATH' : os.environ['PATH']})

# Modify build environment
SEPTop(sepenv)

# Set paths
sepenv.Replace(RESDIR = './Fig/')
sepenv.Replace(BINDIR = './')
sepenv.Replace(SRCDIR = './')
sepenv.Replace(OBJDIR = './')
sepenv.Replace(DATADIR = '/net/koko/homes/sep/prof/gee/Data') 
sepenv.Replace(HTMLDIR = './')
sepenv.Replace(libs = libs)
sepenv.Replace(libpath = libpath)
B = sepenv['BINDIR']
O = sepenv['OBJDIR']
R = sepenv['RESDIR']
D = sepenv['DATADIR']
P = sepenv['PARDIR']
os.environ['PSTEXPENOPTS']='color=n fat=1 fatmult=1.5'

GR = 'Grey title=" " > /dev/null'
GP = 'Graph title=" " transp=y yreverse=y plotfat=6 > /dev/null'
L = 'labelsz=20 titlesz=20 legendsz=12 dash=0,2,1 plotfat=5 legendloc=tl crowd=0.65'

##-----------1d-----------------------------------------------------------------------------------------------------------------------------------
Flow('velscan0.H',[B+'Veltran.x', 'velscan0.P', 'cmp0.H'],
     '''
     < ${SOURCES[2]}  ${SOURCES[0]} adj=1 inv=1 lint=1 par=$P/${SOURCES[1]}  >  ${TARGET}
	 ''')

Flow('cmp0.H',None,'Cp ./Data/cmp0.H  ${TARGET} > /dev/null')

Flow(['vrms0-auto.H','weight0-auto.H'], [B+'Pick_rms.x','velscan0.H','pick0.P'],
     '''
     < ${SOURCES[1]}  Envelope tc1=4 tc2=2 > pp.H ;
     < pp.H ${SOURCES[0]} par=${SOURCES[2]}  weight=${TARGETS[1]} > ${TARGETS[0]}
     ''')

Flow('picked_reflectors0.H',[B+'picks2seplib.x','picked_reflectors0.txt','pick0.P'],
     '''
	 ${SOURCES[0]} fname=${SOURCES[1]} > pp.H ;	
     Get < ${SOURCES[2]} o1 d1 >> pp.H ;
	 echo 'esize=4' >> pp.H ;
     Cp  pp.H ${TARGET}
     ''')

Flow(['vrms0-interp.H','weight0-interp.H','refl0.H'],['picked_reflectors0.H',B+'InterpRMS.x','pick0.P','mr.P'],
      '''
      < ${SOURCES[0]} ${SOURCES[1]} par=${SOURCES[2]} weight=w.H nframe=10 par=mr.P refl=${TARGETS[2]} > ${TARGETS[0]} ;
	  < w.H  Clip par=mr.P chop=g > pp.H ;
      < pp.H Add par=mr.P > ${TARGETS[1]}
      ''')

Flow('vint-reg0-auto.H',[B+'Vrms2int.x','vrms0-auto.H','weight0-auto.H','vrms2int0.P'],
     '''
     < ${SOURCES[1]} ${SOURCES[0]} weight=${SOURCES[2]} prec=0 par=${SOURCES[3]} > ${TARGET}
     ''')

Flow('vint-reg0-interp.H',[B+'Vrms2int.x','vrms0-interp.H','weight0-interp.H','vrms2int0.P'],
     '''
     < ${SOURCES[1]} Window n2=1 f2=0 > v.H ;
     < v.H ${SOURCES[0]} weight=${SOURCES[2]} prec=0 par=${SOURCES[3]} > ${TARGET}
     ''')

Flow('vint-reg0-interp-blocky.H',[B+'Vrms2int.x','vrms0-interp.H','weight0-interp.H','vrms2int0.P','refl0.H'],
     '''
     < ${SOURCES[1]} Window n2=1 f2=0 > v.H ;
     < v.H ${SOURCES[0]} weight=${SOURCES[2]} prec=0 par=${SOURCES[3]} refl=${SOURCES[4]} > ${TARGET}
     ''')

#-------Figures 1d-----------------------------------------------------------------------------------------------------------------------------------
ResultER(R+'velscan0.v',['velscan0.H','cmp0.H'],
         '''
         < ${SOURCES[0]} Envelope tc1=4 tc2=2 > vscan2.H ;
         < ${SOURCES[1]} %s title="CMP data" out=cmp.v ;
         < ${SOURCES[0]} %s title="Raw VelScan" out=vsc.v ;
         < vscan2.H      %s title="Processed VelScan" allpos=y out=env.v ;
         vp_SideBySideAniso cmp.v vsc.v env.v > ${TARGET}
         ''' %(GR,GR,GR))

ResultER(R+'vrms0.v',[B+'Plot_fairway.x','vrms0-auto.H','pick0.P','velscan0.H','weight0-auto.H','cmp0.H','minmax0.P'],
         '''
         < ${SOURCES[1]} Window n2=1 f2=0 > va.H ;
         < ${SOURCES[4]} Window n3=1 f3=0 > wa.H ;
         < ${SOURCES[5]} Window n3=1 f3=0 > ca.H ;
         < ${SOURCES[3]} Window n3=1 f3=0 > sa.H ;
         ${SOURCES[0]} par=${SOURCES[2]}  > pp.H ;
         < pp.H Real > fairway.H ;
         Cat axis=2 fairway.H va.H | %s label1="Time(sec)" par=${SOURCES[6]} out=lines.v ;
         < sa.H  Envelope tc1=4 tc2=2 | %s par=${SOURCES[6]} allpos=y out=vsc.v ;
         vp_Overlay vsc.v lines.v > left.v ;
         < wa.H  %s title="Stack Power" out=right.v ;
         vp_SideBySideAniso left.v right.v > top.v ;
         < ca.H  %s title="CMP data" out=cmp.v ;
         Math exp="1.0/file1" file1=va.H > vv.H	 ;
         < ca.H  NMO velfile=vv.H | %s title="CMP data + NMO" out=nmo.v ;
         cp nmo.v nmo1.v;
         vp_SideBySideAniso cmp.v nmo.v > bot.v ;
         vp_OverUnderAniso top.v bot.v > ${TARGET}
         ''' %(GP,GR,GP,GR,GR))

ResultER(R+'multreal0.v',['vrms0-interp.H','minmax0.P','refl0.H'],
         '''
	     < ${SOURCES[0]}  Window n2=1  f2=0 > vi0.H ;
         < ${SOURCES[0]}  Window n2=10 f2=1 > vi.H  ;
	     < ${SOURCES[2]}  Add scale=100 | Add add=-98 | %s par=${SOURCES[1]} plotfat=1 out=pick.v  ;
	     Cat axis=2 vi0.H vi0.H vi0.H vi0.H vi0.H vi0.H vi0.H vi0.H vi0.H vi0.H > v0.H  ;
	     Cat v0.H vi.H axis=1 > vall.H  ;
	     < vall.H  %s par=${SOURCES[1]} legend=1 n2=2 n3=10 dash=1,0 `Get < vi.H n1` \
	                curvelabel=Known:Realization legendloc=tl out=Fig/multreal0.v3  ;
	     < vall.H  Window n2=3 | %s par=${SOURCES[1]} n2=4 n3=1 dash=0,1 `Get < vi.H n1` out=a.v  ;
	     vp_Overlay pick.v a.v > ${TARGET}
         ''' %(GP,GP,GP))

ResultER(R+'vrms-interp0.v',['vrms0-auto.H','vrms0-interp.H','velscan0.H','cmp0.H','weight0-auto.H','weight0-interp.H','minmax0.P'],
         '''
         < ${SOURCES[1]}  Window n2=1 f2=0 > vi.H ;
	     Cat ${SOURCES[0]} vi.H | %s %s n2=2 label1="Time(sec)" curvelabel=Autopick:Handpick \
	                               legend=1 par=${SOURCES[6]} dash=1,0 out=lines.v  ; 
	     < ${SOURCES[2]}  Envelope tc1=4 tc2=2 | %s %s par=${SOURCES[6]} allpos=y out=vsc.v ;
         vp_Overlay vsc.v lines.v > 1.v ;
	     < ${SOURCES[4]} %s %s label1="Time(sec)" title="Autopick - Residual Weight" out=wta.v ;
	     < ${SOURCES[5]} %s %s label1="Time(sec)" title="Handpick - Residual Weight" out=wti.v ;
	     < ${SOURCES[3]} %s %s title="CMP data" out=cmp.v ;
	     Math exp="1.0/file1" file1=${SOURCES[0]} > vva.H	 ;
	     < ${SOURCES[3]} NMO velfile=vva.H | %s %s title="NMO - Autopick" out=nmoa.v ;
	     Math exp="1.0/file1" file1=${SOURCES[1]} > vvh.H	 ;
	     < ${SOURCES[3]}  NMO velfile=vvh.H | %s %s title="NMO - Handpick" out=nmoh.v ;
	     vp_SideBySideAniso 1.v wta.v wti.v > top.v ;
	     vp_SideBySideAniso cmp.v nmoa.v nmoh.v > bot.v ;
	     vp_OverUnderAniso top.v bot.v > ${TARGET}
         ''' %(GP,L,GR,L,GP,L,GP,L,GR,L,GR,L,GR,L))

ResultER(R+'vint0.v',['vrms0-auto.H','vrms0-interp.H','vint-reg0-auto.H','vint-reg0-interp.H','vint-reg0-interp-blocky.H','pick0.P','weight0-interp.H','weight0-auto.H'],
         '''
	     < ${SOURCES[1]}  Window n2=1 f2=0 > vi.H ;
	     Cat      ${SOURCES[0]} ${SOURCES[2]} > vela.H ;
	     Cat vi.H ${SOURCES[3]} ${SOURCES[4]} > veli.H ;
	     < vela.H %s title="Autopick" dash=1,0   par=${SOURCES[5]} n2=2 n3=1 legend=1 legendloc=tl curvelabel=Vrms:Vint        out=vela.v ;
	     < veli.H %s title="Handpick" dash=1,2,0 par=${SOURCES[5]} n2=3 n3=1 legend=1 legendloc=tl curvelabel=Vrms:Vint:Blocky out=veli.v ;
	     < ${SOURCES[6]} %s title="Handpick Weight" out=wti.v ;
	     < ${SOURCES[7]} %s title="Autopick Weight" out=wta.v ;
	     vp_SideBySideAniso vela.v veli.v > top.v ;
	     vp_SideBySideAniso wta.v wti.v > bot.v ;
	     vp_OverUnderAniso top.v bot.v > ${TARGET}
         ''' %(GP,GP,GP,GP))

##-----------2d-----------------------------------------------------------------------------------------------------------------------------------
Flow('velscan2d.H',[B+'Veltran.x', 'velscan2d.P', 'cmp2d.H'],
     '''
     < ${SOURCES[2]}  ${SOURCES[0]} adj=1 inv=1 lint=1 par=${SOURCES[1]}  >  ${TARGET}
	 ''')

Flow('cmp2d.H',None,
     '''
     <  /net/koko/book/bei/Data/midpts.HH  Window n3=40 f3=0 j3=2 |\
	 Add scale=0.0001 | Mute tmute=0.2 vmute=1.5 > ${TARGET}
     ''')

Flow(['vrms2d-auto.H','weight2d-auto.H'],[B+'Pick_rms.x','velscan2d.H','pick2d.P'],
     '''
     < ${SOURCES[1]}  Envelope tc1=4 tc2=2 > pp.H ;
     < pp.H ${SOURCES[0]} par=${SOURCES[2]}  weight=${TARGETS[1]} > ${TARGETS[0]}
     ''')

Flow('vint-prec2d-nosr.H',[B+'Vrms2int.x','vrms2d-auto.H','weight2d-auto.H','vrms2int2d-nosr.P'],
     ''' 
	 < ${SOURCES[1]} ${SOURCES[0]} weight=${SOURCES[2]} prec=1 par=${SOURCES[3]} > ${TARGET} 
     ''')



#-------Figures 2d-----------------------------------------------------------------------------------------------------------------------------------
ResultER(R+'vrms2d.v',[B+'Plot_fairway.x','vrms2d-auto.H','pick2d.P','velscan2d.H','weight2d-auto.H','cmp2d.H','minmax2d.P'],
         '''
         < ${SOURCES[1]} Window n2=1 f2=0 > va.H ;
         < ${SOURCES[4]} Window n3=1 f2=0 > wa.H ;
         < ${SOURCES[5]} Window n3=1 f3=0 > ca.H ;
         < ${SOURCES[3]} Window n3=1 f2=0 | Envelope tc1=4 tc2=2 > sa.H ;
         ${SOURCES[0]} par=${SOURCES[2]}  > pp.H ;
         < pp.H Real > fairway.H ;
         < sa.H   %s par=${SOURCES[6]} allpos=y out=sem.v ;
	     < ${SOURCES[1]}  %s bias=0.5 wantscalebar=y out=vel.v ;
         Cat axis=2 fairway.H va.H | %s label1="Time(sec)" par=${SOURCES[6]} out=lines.v ;
	     vp_Overlay sem.v lines.v > vsc.v ;
	     < ca.H  %s title="CMP data" out=cmp.v ;
	     Math exp="1.0/file1" file1=${SOURCES[1]} > vv.H ;
	     < ${SOURCES[5]}  NMO velfile=vv.H | Stack | %s title="Stack" out=stack.v ;
	     vp_SideBySideAniso vsc.v vel.v > top.v ;
	     vp_SideBySideAniso cmp.v stack.v > bot.v ;
	     vp_OverUnderAniso top.v bot.v > ${TARGET} 
         ''' %(GR,GR,GP,GR,GR))

ResultER(R+'vcomp2d.v',['vrms2d-auto.H','vint-prec2d-nosr.H'],
        '''
    	< ${SOURCES[0]} %s bias=0.5 wantscalebar=y title="RMS Velocity" out=rms.v ;
    	< ${SOURCES[1]} %s pclip=95 bias=.5 wantscalebar=y title="1-D V_int inversion" out=${TARGET}  ;
    	vp_Movie rms.v ${TARGET}  > Fig/vcomp2d.v3
        ''' %(GR,GR))


Paper('paper',lclass='sep2e')

SEPBottom(sepenv)
