def main(mdlname,dispdic,title): os.system('gpdcreport '+mdlname+' >tmp.mdl') models, data, nd, ne, nrow = get_models('tmp.mdl') gmt = GMT(config={'BASEMAP_TYPE':'plain','ANOT_FONT_SIZE':8, 'LABEL_FONT_SIZE':10,'COLOR_BACKGROUND':'255/255/255', 'COLOR_FOREGROUND':'0/0/0','COLOR_NAN':'255/255/255', 'PAGE_ORIENTATION':'landscape', 'HEADER_FONT_SIZE':15} ) xyz=gmt.tempfilename('testxyz.txt') xyz2=gmt.tempfilename('testxyz2.txt') grd=gmt.tempfilename('tmp.grd') grdcpt=gmt.tempfilename('tmp.cpt') fileout='dens_test.ps' rng='1/5/0/40' scl='X4.2/-6' dreso = 0.2 sreso = 0.05 misfit = 0.1 #grid1, grid2, x, y, smean, dmean = dplot.dplot(models,data,nd,ne,dreso=dreso, sreso=sreso,mf=misfit) grid1, grid2, x, y, smean, dmean = dplot.dplot(models,data,nd,ne,dreso=dreso, sreso=sreso,mf=misfit) #grid1, grid2, x, y = dplot.dplotpy(models,data,nd,ne,dreso=dreso, sreso=sreso,mf=misfit) # matshow(grid1) # show() f = open(xyz,'w') for ii in range(len(y)): for jj in range(len(x)): if grid1[ii,jj]>0.0: print >>f, x[jj],y[ii], grid1[ii,jj] f.close() f = open(xyz2,'w') for ii in range(len(y)): for jj in range(len(x)): if grid2[ii,jj] > 0: print >>f, x[jj], y[ii], '0.5' f.close() anot = int(grid1.max()/1000.)*1000/2. tick = anot/2 gmt.xyz2grd(xyz,G=grd,R=rng,I='%f/%f'%(sreso,dreso),out_discard=True) gmt.grd2cpt(grd,C="wysiwyg",Z=True,out_filename=grdcpt) gmt.psmask(xyz2,R=rng,T=True,J=scl,I='%f/%f'%(sreso,dreso),G='lightgray') gmt.grdimage(grd,J=scl,R=rng,Q=True,C=grdcpt) gmt.psbasemap(R=rng,J=scl,B='a1f.5:S-velocity [km/s]:/a10f5:Depth [km]::.%s:WnSe'%title) gmt.psxy(R=True,J=True,B=True,W='3,black',in_columns=[smean,dmean]) f = open('/home/behrya/dev/data/mt_fixed_layers_ray_c_u_mean.txt','w') for _p,_v in zip(dmean,smean): print >>f,_p,_v f.close() gmt.psscale(C=grdcpt,D='1.0/1./4c/.4ch',B='a%df%d:No. of models:/::'%(anot,tick)) ### plot dispersion curves gmt.psbasemap(R='5/30/2.0/5.0',J='X4.2/2.5',X='5',B='a1f.5:Period [s]:/a1f.5:Velocity [km/s]:WnSe') for _d in dispdic.keys(): vo = load(dispdic[_d][0]) p,v = gpdccurve(mdlname,wtype=dispdic[_d][1],ptype=dispdic[_d][2]) gmt.psxy(R=True,J=True,B=True,W='3,black',in_columns=[p,v]) gmt.psxy(R=True,J=True,B=True,W='3,red',in_columns=[vo[:,0],vo[:,1]]) gmt.save(fileout) os.system('gv '+fileout+'&')
def plotnad(fnad,fout): gmt = GMT(config={'BASEMAP_TYPE':'plain','ANOT_FONT_SIZE':8, 'LABEL_FONT_SIZE':10,'COLOR_BACKGROUND':'255/255/255', 'COLOR_FOREGROUND':'0/0/0','COLOR_NAN':'255/255/255'} ) grd=gmt.tempfilename('tmp.grd') grdcpt=gmt.tempfilename('tmp.cpt') xyz=gmt.tempfilename('xyz.txt') xyz2=gmt.tempfilename('xyz2.txt') rng='1/5/0/40' scl='X4.2/-6' kosu1,kosu2,x,y,dbest,sbest,dmean,smean = nadplot(fnad,smin=1.5) f = open(xyz,'w') for ii in range(len(y)): for jj in range(len(x)): if kosu1[ii,jj] > 0: print >>f, x[jj], y[ii], kosu1[ii,jj] f.close() f = open(xyz2,'w') for ii in range(len(y)): for jj in range(len(x)): if kosu2[ii,jj] > 0: print >>f, x[jj], y[ii], '0.5' f.close() gmt.xyz2grd(xyz,G=grd,R=rng,I='.02/.5',out_discard=True) gmt.grd2cpt(grd,C="wysiwyg",Q='o',Z=True,out_filename=grdcpt) gmt.psmask(xyz2,R=rng,T=True,J=scl,I='.02/.5',G='lightgray') gmt.grdimage(grd,J=scl,R=rng,Q=True,P=True,C=grdcpt) gmt.psbasemap(R=rng,J=scl,B='a1f.5:Velocity [km/s]:/a10f5:Depth [km]:WNse') gmt.psxy(R=True,J=True,B=True,W='3,red',in_columns=[sbest,dbest]) gmt.psxy(R=True,J=True,B=True,W='3,white',in_columns=[smean,dmean]) gmt.psscale(C=grdcpt,D='1.0/1./4c/.4ch',B='a40f10:No. of models:/::') gmt.save(fout) os.system('gv '+fout+'&') return 1
from gmtpy import GMT gmt = GMT( config={'PAGE_COLOR':'247/247/240'} ) gmt.psbasemap( R=(0,5,0,5), J='X%gi/%gi' % (5,3), B='%g:Time:/%g:Amplitude:SWne' % (1,1) ) # Make four different datasets # (1) a nested list, with the first dim corresponding to columns data_as_columns = [ [ 0,1,2,3,4,5 ], [0,1,0.5,1,0.5,1] ] # (2) a nested list, with the first dim corresponding to rows data_as_rows = [ [0,1], [1,2], [2,3], [3,3.5], [4,3], [5,2] ] # (3) a string containing an ascii table data_as_string = '''0 5 1 4 2 3.5 3 4 4 4.5 5 5''' # (4) write ascii table in a temporary file... # Get a filename in the private tempdir of the GMT instance. # Files in that directory get deleted automatically. filename = gmt.tempfilename('table.txt')
import numpy as np # some data to plot... x = np.linspace(0, 5, 101) ys = (np.sin(x) + 2.5, np.cos(x) + 2.5) gmt = GMT(config={'PAGE_COLOR': '247/247/240'}) layout = GridLayout(1, 2) widgets = [] for iwidget in range(2): inner_layout = FrameLayout() layout.set_widget(0, iwidget, inner_layout) widget = inner_layout.get_widget('center') widget.set_horizontal(7 * cm) widget.set_vertical(7 * cm / golden_ratio) widgets.append(widget) #gmt.draw_layout( layout ) #print layout for widget, y in zip(widgets, ys): gmt.psbasemap(R=(0, 5, 0, 5), B='%g:Time [ s ]:/%g:Amplitude [ m ]:SWne' % (1, 1), *widget.XYJ()) gmt.psxy(R=True, W='2p,blue,o', in_columns=(x, y), *widget.XYJ()) gmt.save('example4.pdf', bbox=layout.bbox())
def plot_rep(repfile, paramfile, wtype, pixfile, show=True, misfit=0.1, minmax=True): """ Plot 1D velocity profile. """ # ## read in models from dinver-output os.system('gpdcreport ' + repfile + ' >tmp.mdl') models, data, nd, ne, nrow, nlayer = get_models('tmp.mdl') os.remove('tmp.mdl') if misfit == 'all': misfit = data.max() elif misfit <= data.min(): misfit = median(data) # ## calculate density sreso = 0.05 # horizontal resolution dreso = .5 # vertical resolution grid1, grid2, x, y, smean, dmean = dplot(models, data, nd, ne, dreso=dreso, sreso=sreso, mf=misfit, nlayer=nlayer, dmax=120) # ## write output into temporary files that can be plotted by gmt xyz = tempfile.mktemp() xyz2 = tempfile.mktemp() grd = tempfile.mktemp() grdcpt = tempfile.mktemp() fout = pixfile f = open(xyz, 'w') for ii in range(len(y)): for jj in range(len(x)): if grid1[ii, jj] > 0.0: print >> f, x[jj], -y[ii], grid1[ii, jj] f.close() f = open(xyz2, 'w') for ii in range(len(y)): for jj in range(len(x)): if grid2[ii, jj] > 0: print >> f, x[jj], -y[ii], '0.5' f.close() lat, lon = dissect_fname(repfile) if wtype == 'love': wt = 'L' if wtype == 'rayleigh': wt = 'R' # print repfile, lat, lon # ## gmt plot rng = '1/5/-120/0' step = '%f/%f' % (sreso, dreso) anot = int(grid1.max() / 1000.) * 1000 / 2. tick = anot / 2 gmt = GMT(config={'ANOT_FONT_SIZE':8, 'LABEL_FONT_SIZE':10, 'ANNOT_OFFSET_SECONDARY':'0.1c', 'ANNOT_OFFSET_PRIMARY':'0.1c', 'LABEL_OFFSET':'0.1c', 'FRAME_PEN':'.5p'}) widgets, layout = make_widget() if 1: widget = widgets[2] gmt.xyz2grd(xyz, G=grd, R=rng, I='%f/%f' % (sreso, dreso), out_discard=True) gmt.grd2cpt(grd, L='0/%d' % 3000, C="wysiwyg", D=True, Z=True, out_filename=grdcpt) gmt.psbasemap(R=True, B='a1f.5:S-velocity [km/s]:/a10f5:Depth [km]:WnSe', *widget.XYJ()) # gmt.psmask(xyz2, R=True, T=True, I='%f/%f' % (sreso, dreso), G='lightgray', *widget.XYJ()) gmt.grdimage(grd, R=True, Q=True, C=grdcpt, *widget.XYJ()) gmt.psxy(R=True, B=True, W='3,black', in_columns=[smean, -dmean], *widget.XYJ()) bmdl = get_best_model(repfile, dreso, dmax=120., dmin=0.) gmt.psxy(R=True, B=True, W='3,black,-', in_rows=bmdl, *widget.XYJ()) gmt.psscale(widget.XYJ()[0], widget.XYJ()[1], C=grdcpt, D='1.7c/1.5c/2.5c/.4ch', B='a%df%d10:No. of models:/::' % (1000, 500)) txtstr = "1.2 -7. 8 0 1 LT lat = %3.2f" % lat gmt.pstext(R=rng, G='0/0/0', N=True, in_string=txtstr, *widget.XYJ()) txtstr = "1.2 -12. 8 0 1 LT lon = %3.2f" % lon gmt.pstext(R=True, G='0/0/0', N=True, in_string=txtstr, *widget.XYJ()) # plot the minimum and maximum model that is allowed # by the parameterisation if minmax: mdl_min, mdl_max = minmax_mdl(paramfile) gmt.psxy(R=True, B=True, W='3,red,-', in_rows=mdl_min, *widget.XYJ()) gmt.psxy(R=True, B=True, W='3,red,-', in_rows=mdl_max, *widget.XYJ()) if 1: p, v, e = get_disp(repfile, wtype='phase') plot_disp(gmt, repfile, widgets[1], p, v, e, 'phase', ptype='p', wtype=wt, mode=0, misfit=misfit) p, v, e = get_disp(repfile, wtype='group') plot_disp(gmt, repfile, widgets[0], p, v, e, 'group', ptype='g', wtype=wt, mode=0, misfit=misfit) if 0: p, v, e = get_disp(repfile, wtype='grouponly') plot_disp(gmt, repfile, widgets[0], p, v, e, 'group', ptype='g', wtype=wt, mode=0, misfit=misfit) if 0: plot_hist(gmt, widgets, models, nd, ne) gmt.save(fout) meanfout = fout.replace('.eps', '_mean.txt') savetxt(meanfout, vstack((-dmean, smean)).T) if show: os.system('gv %(fout)s&' % vars())
# plot cross-section on map gmt = GMT( config={ "ANOT_FONT_SIZE": 14, "LABEL_FONT_SIZE": 14, "ANNOT_OFFSET_SECONDARY": "0.1c", "ANNOT_OFFSET_PRIMARY": "0.1c", "LABEL_OFFSET": "0.1c", "FRAME_PEN": ".5p", "PLOT_DEGREE_FORMAT": "-D", } ) rng = "-145/-50/41/85" scl = "L-97.5/63/41/85/15c" markers = "markers.xyp" gmt.psbasemap(R=rng, J=scl, B="a15f5WSne", Y="2c", X="2c") gmt.pscoast("-N1", R=True, J=True, D="i", W="0.5p,black", N="2") cnt = 0 for _slon, _slat, _elon, _elat in zip(slon, slat, elon, elat): gmt.project(C="%f/%f" % (_slon, _slat), E="%f/%f" % (_elon, _elat), G=100, Q=True, out_filename=markers) gmt.psxy(R=True, J=True, W="2p,red,", in_rows=[[_slon, _slat], [_elon, _elat]]) # gmt.psxy(markers,R=True,J=True,S='y0.1c',W='1p,blue') gmt.psxy(markers, R=True, J=True, S="s0.1c", W="1p,blue") lbl1, lbl2 = anot[cnt] txtstr1 = """%f %f 10 0 1 RB %s""" % (_slon, _slat, lbl1) txtstr2 = """%f %f 10 0 1 LB %s""" % (_elon, _elat, lbl2) gmt.pstext(R=True, J=True, W="white", G="blue", N=True, in_string=txtstr1) gmt.pstext(R=True, J=True, W="white", G="blue", N=True, in_string=txtstr2) cnt += 1 for lon, lat in invres: gmt.psxy(R=True, J=True, S="t0.5c", G="red", in_rows=[[lon, lat]])
from gmtpy import GMT gmt = GMT(config={'PAGE_COLOR': '247/247/240'}) gmt.psbasemap(R=(0, 5, 0, 5), J='X%gi/%gi' % (5, 3), B='%g:Time:/%g:Amplitude:SWne' % (1, 1)) # Make four different datasets # (1) a nested list, with the first dim corresponding to columns data_as_columns = [[0, 1, 2, 3, 4, 5], [0, 1, 0.5, 1, 0.5, 1]] # (2) a nested list, with the first dim corresponding to rows data_as_rows = [[0, 1], [1, 2], [2, 3], [3, 3.5], [4, 3], [5, 2]] # (3) a string containing an ascii table data_as_string = '''0 5 1 4 2 3.5 3 4 4 4.5 5 5''' # (4) write ascii table in a temporary file... # Get a filename in the private tempdir of the GMT instance. # Files in that directory get deleted automatically. filename = gmt.tempfilename('table.txt') f = open(filename, 'w') f.write('0 3\n1 3\n5 1.2\n')
# We will plot in the 'center' widget: plot_widget = layout.get_widget('center') # Set width of plot area to 8 cm and height of the 'top' margin # to 1 cm. The other values are calculated automatically. plot_widget.set_horizontal( 8*cm ) layout.get_widget('top').set_vertical( 1*cm ) # Define how the widget's output parameters are translated # into -X, -Y and -J option arguments. (This could be skipped # in this example, because the following templates # are just the built-in defaults) plot_widget['X'] = '-Xa%(xoffset)gp' plot_widget['Y'] = '-Ya%(yoffset)gp' plot_widget['J'] = '-JX%(width)gp/%(height)gp' gmt.psbasemap( R=(0,5,0,5), B='%g:Time [ s ]:/%g:Amplitude [ m ]:SWne' % (1,1), *plot_widget.XYJ()) gmt.psxy( R=True, W='2p,blue,o', in_columns=(x,y), *plot_widget.XYJ() ) # Save the output, while replacing GMT's default bounding box # with the exact box of the layout. gmt.save('example3.pdf', bbox=layout.bbox())