def pygmt_map(ts_objects, myparams, deltas):

    offset = 0.2
    geothermals_x = [-115.528300, -115.250000, -115.515300, -115.600000]
    geothermals_y = [32.716700, 32.783300, 33.015300, 33.200000]

    lons = []
    lats = []
    names = []
    for i in range(len(ts_objects)):
        lons.append(ts_objects[i].coords[0])
        lats.append(ts_objects[i].coords[1])
        names.append(ts_objects[i].name)
    region = [
        min(lons) - offset,
        max(lons) + offset,
        min(lats) - offset,
        max(lats) + offset
    ]

    # Make a new color bar
    min_vert = np.min(deltas)
    max_vert = np.max(deltas)
    max_vert = 20
    label_interval = 2.0
    pygmt.makecpt(C="jet",
                  T=str(min_vert - 0.1) + "/" + str(max_vert + 0.1) + "/0.1",
                  H="mycpt.cpt")

    fig = pygmt.Figure()
    fig.basemap(region=region, projection="M8i", B="0.25")
    fig.coast(shorelines="0.5p,black", G='peachpuff2', S='skyblue', D="h")
    fig.coast(N='1', W='1.0p,black')
    fig.coast(N='2', W='0.5p,black')
    fig.text(x=[i + 0.035 for i in lons],
             y=lats,
             text=names,
             font='15p,Helvetica-Bold,black')
    fig.plot(x=geothermals_x,
             y=geothermals_y,
             S='i0.2i',
             G="purple",
             W='0.5p,black')
    fig.plot(x=lons,
             y=lats,
             S='c0.2i',
             C="mycpt.cpt",
             G=deltas,
             W='0.5p,black')
    fig.plot(x=myparams.center[0],
             y=myparams.center[1],
             S='a0.1i',
             G='red',
             W='0.5p,red')
    fig.colorbar(D="JBC+w4.0i+h",
                 C="mycpt.cpt",
                 G=str(min_vert) + "/" + str(max_vert),
                 B=["x" + str(label_interval), "y+LVert(mm)"])
    fig.savefig(myparams.outdir + "/" + myparams.outname + '_map.png')
    return
Example #2
0
def test_makecpt_output_cpt_file():
    """
    Save the generated static color palette table to a .cpt file.
    """
    with GMTTempFile(suffix=".cpt") as cptfile:
        makecpt(output=cptfile.name)
        assert os.path.exists(cptfile.name)
Example #3
0
def simple_pygmt_map(mycat, filename, legendfile=None, scalelength=1, cbar_interval=1.0, map_frame_int=0.05):
    """A basic pygmt plot for earthquake catalogs, color-coded by depth and size-coded by magnitude."""
    lons = [eq.lon for eq in mycat];
    lats = [eq.lat for eq in mycat];
    depths = [eq.depth for eq in mycat];
    mags = [0.14*eq.Mag for eq in mycat];  # scaling for symbol size

    region = [min(lons), max(lons), min(lats), max(lats)];
    pygmt.makecpt(cmap="turbo", series=str(min(depths)) + "/" + str(max(depths)) + "/"+str(0.1),
                  output="mycpt.cpt", background=True);
    proj = "M7i"
    fig = pygmt.Figure()
    fig.coast(region=region, projection=proj, borders='2', shorelines='0.5p,black', water='lightblue',
              resolution='h', frame=str(map_frame_int),
              map_scale="jBR+c"+str(region[2])+"+o0.6/0.7+w"+str(scalelength)+"k");
    fig.plot(x=lons, y=lats, color=depths, size=mags, style='c', cmap="mycpt.cpt", pen="thin,black");  # earthquakes
    fig.colorbar(position="jBr+w3.5i/0.2i+o3.5c/1.5c+h", cmap="mycpt.cpt",
                 frame=["x" + str(cbar_interval), "y+L\"km\""]);
    starttime, endtime = catalog_functions.get_start_stop_time(mycat);
    startstr = dt.datetime.strftime(starttime, "%Y-%m-%d");
    endstr = dt.datetime.strftime(endtime, "%Y-%m-%d");
    fig.text(text=startstr + " to " + endstr + ", "+str(len(mycat))+" events", position='TR',
             font="12p,Helvetica,black", pen="0.5p,black", fill='white', offset="-0.1/-0.1");
    if legendfile:
        fig.legend(position="JBL+jBL+o0.2c", spec=legendfile, box='+gwhite+p0.5p', projection=proj);   # user-provided
    fig.savefig(filename);
    print("Saving pygmt map %s" % filename);
    return;
Example #4
0
def test_makecpt_truncated_zlow_zhigh(position):
    """
    Use static color palette table that is truncated to z-low and z-high.
    """
    fig = Figure()
    makecpt(cmap="rainbow", truncate=[0.15, 0.85], series=[0, 1000])
    fig.colorbar(cmap=True, frame=True, position=position)
    return fig
Example #5
0
def test_makecpt_plot_colorbar(position):
    """
    Use static color palette table to plot a colorbar.
    """
    fig = Figure()
    makecpt(cmap="relief")
    fig.colorbar(cmap=True, frame=True, position=position)
    return fig
Example #6
0
def test_makecpt_truncated_zlow_zhigh(grid):
    """
    Use static color palette table that is truncated to z-low and z-high.
    """
    fig = Figure()
    makecpt(cmap="rainbow", truncate=[0.15, 0.85], series=[-4500, 4500])
    fig.grdimage(grid, projection="W0/10c")
    return fig
Example #7
0
def test_makecpt_reverse_color_only(position):
    """
    Use static color palette table with its colors reversed.
    """
    fig = Figure()
    makecpt(cmap="earth", reverse=True, series=[0, 1000])
    fig.colorbar(cmap=True, frame=True, position=position)
    return fig
Example #8
0
def test_makecpt_cyclic(region):
    """
    Use static color palette table that is cyclic.
    """
    fig = Figure()
    makecpt(cmap="cork", cyclic=True)
    fig.colorbar(cmap=True, region=region, frame=True, position="JBC")
    return fig
Example #9
0
def test_makecpt_reverse_color_only(grid):
    """
    Use static color palette table with its colors reversed.
    """
    fig = Figure()
    makecpt(cmap="earth", reverse=True)
    fig.grdimage(grid, projection="W0/10c")
    return fig
Example #10
0
def test_makecpt_categorical(region):
    """
    Use static color palette table that is categorical.
    """
    fig = Figure()
    makecpt(cmap="categorical", categorical=True)
    fig.colorbar(cmap=True, region=region, frame=True, position="JBC")
    return fig
Example #11
0
def test_makecpt_reverse_zsign_only(grid):
    """
    Use static color palette table with its z-value sign reversed
    """
    fig = Figure()
    makecpt(cmap="earth", reverse="z")
    fig.grdimage(grid, projection="W0/6i")
    return fig
Example #12
0
def test_makecpt_truncated_at_zhigh_only(grid):
    """
    Use static color palette table that is truncated at z-high only
    """
    fig = Figure()
    makecpt(cmap="rainbow", truncate=[None, 0.5], series=[-4500, 4500])
    fig.grdimage(grid, projection="W0/6i")
    return fig
Example #13
0
def test_makecpt_plot_grid(grid):
    """
    Use static color palette table to change color of grid.
    """
    fig = Figure()
    makecpt(cmap="relief")
    fig.grdimage(grid, projection="W0/10c")
    return fig
Example #14
0
def test_makecpt_plot_grid_scaled_with_series(grid):
    """
    Use static color palette table scaled to a min/max series to change color
    of grid.
    """
    fig = Figure()
    makecpt(cmap="oleron", series=[-4500, 4500])
    fig.grdimage(grid, projection="W0/10c")
    return fig
Example #15
0
def test_makecpt_reverse_color_and_zsign(grid):
    """
    Use static color palette table with both its colors and z-value sign
    reversed.
    """
    fig = Figure()
    makecpt(cmap="earth", reverse="cz")
    fig.grdimage(grid, projection="W0/10c")
    return fig
Example #16
0
def test_makecpt_reverse_color_and_zsign(position):
    """
    Use static color palette table with both its colors and z-value sign
    reversed.
    """
    fig = Figure()
    makecpt(cmap="earth", reverse="cz", series=[0, 1000])
    fig.colorbar(cmap=True, frame=True, position=position)
    return fig
Example #17
0
def test_makecpt_plot_colorbar_scaled_with_series(position):
    """
    Use static color palette table scaled to a min/max series and plot it on a
    colorbar.
    """
    fig = Figure()
    makecpt(cmap="oleron", series=[0, 1000])
    fig.colorbar(cmap=True, frame=True, position=position)
    return fig
Example #18
0
def test_makecpt_continuous(position):
    """
    Use static color palette table that is continuous from blue to white and
    scaled from 0 to 1000 m.
    """
    fig = Figure()
    makecpt(cmap="blue,white", continuous=True, series=[0, 1000])
    fig.colorbar(cmap=True, frame=True, position=position)
    return fig
Example #19
0
def test_makecpt_continuous(grid):
    """
    Use static color palette table that is continuous from blue to white and
    scaled from -4500 to 4500m.
    """
    fig = Figure()
    makecpt(cmap="blue,white", continuous=True, series=[-4500, 4500])
    fig.grdimage(grid, projection="W0/10c")
    return fig
def main():
    ###files needed
    dem = '../data/dem_gebco2014_cut.grd'
    faults = '../data/Tectonic_faults'
    
    ###Prepare basemap using pygmt
    with pygmt.clib.Session() as session:
        session.call_module('gmtset', 'MAP_FRAME_TYPE plain')           ## A simple frame
        session.call_module('gmtset', 'FONT 10p')                       ## Fontsize for frame
        
    fig = pygmt.Figure()
    fig.basemap(region=[125,135,-10,0],projection='M0/0/5i',frame=True)  ## Set region and projection
    
    ###Colormap
    demcpt = "dem.cpt"
    pygmt.makecpt(cmap='globe', series="-10000/10000/100", output=demcpt )
    
    ###Plot data
    fig.grdimage(dem,cmap=demcpt)
    fig.grdcontour(dem, interval="2000", limit="-10000/-10", annotation=(True,"f6p"), 
                   pen="0.2p")
    fig.grdcontour(dem, interval="10000", pen="0.3p")
    
    ###Extract shapefile data
    flt = reading_shapefile(faults)
    Nfeatures = len(flt)                                                ## Number of features
    #Nfeatures = 1
    for i in range(Nfeatures):
        gmtry = flt.shape(i)                                            ## Extract shapefile geometry
        rcrds = flt.record(i)                                           ## Extract shapefile record/attribute
        lon  = np.empty(len(gmtry.points))
        lat  = np.empty(len(gmtry.points))
        for j in range(len(gmtry.points)):                              ## Extract feature vertices
            lon[j]  = gmtry.points[j][0]
            lat[j]  = gmtry.points[j][1]
        
        if rcrds.Mechanism == 'Thrust':                                 ## Read fault mechanism
            style = 'f1+r+t'
            pen   = '0.5p'
        elif rcrds.Mechanism == 'Normal':
            style = 'f1+r+b'
            pen   = '0.5p'
        elif rcrds.Mechanism == 'Trough':
            style = 'f1/0.00001'
            pen   = '0.5p,-'
        else:
            style = 'f1/0.00001'
            pen   = '0.5p'
        
        fig.plot(x=lon,y=lat,style=style,color='black',pen=pen)         ## Plot mechanism in GMT
    
        
    fig.text(x=134.8,y=-9.8,text="ignatiusryanpranantyo at gmail dot com [28/05/2020]",
             font="6p,blue", justify="RB")
    fig.savefig('../outputs/image-01.png',dpi=300)
Example #21
0
def plot_maxshear(filename, region, outdir, positive_eigs, negative_eigs,
                  outfile):
    proj = 'M4i'
    fig = pygmt.Figure()
    pygmt.makecpt(cmap="polar",
                  series="0/300/2",
                  truncate="0/1.0",
                  background="o",
                  output=outdir + "/mycpt.cpt")
    fig.basemap(region=region, projection=proj, frame="+t\"Maximum Shear\"")
    fig.grdimage(filename,
                 projection=proj,
                 region=region,
                 cmap=outdir + "/mycpt.cpt")
    fig.coast(region=region,
              projection=proj,
              borders='1',
              shorelines='1.0p,black',
              water='lightblue',
              map_scale="n0.12/0.12+c" + str(region[2]) + "+w50",
              frame="1.0")
    elon, nlat, e, n = station_vels_to_arrays(positive_eigs)
    fig.plot(x=elon,
             y=nlat,
             style='v0.20+e+a40+gblue+h0.5+p0.3p,blue+z0.003+n0.3',
             pen='0.6p,blue',
             direction=[e, n])
    elon, nlat, e, n = station_vels_to_arrays(negative_eigs)
    fig.plot(x=elon,
             y=nlat,
             style='v0.20+b+a40+gred+h0.5+p0.3p,black+z0.003+n0.3',
             pen='0.6p,black',
             direction=[e, n])
    # Scale vector
    fig.plot(x=region[0] + 1.1,
             y=region[2] + 0.1,
             style='v0.20+b+a40+gred+h0.5+p0.3p,black+z0.003+n0.3',
             pen='0.6p,black',
             direction=[[200], [0]])
    fig.plot(x=region[0] + 1.1,
             y=region[2] + 0.1,
             style='v0.20+b+a40+gred+h0.5+p0.3p,black+z0.003+n0.3',
             pen='0.6p,black',
             direction=[[-200], [0]])
    fig.text(x=region[0] + 0.4,
             y=region[2] + 0.1,
             text="200 ns/yr",
             font='10p,Helvetica,black')
    fig.colorbar(position="JCR+w4.0i+v+o0.7i/0i",
                 cmap=outdir + "/mycpt.cpt",
                 truncate="0/300",
                 frame=["x50", "y+L\"Nanostr/yr\""])
    print("Saving MaxShear figure as %s." % outfile)
    fig.savefig(outfile)
    return
def map_vertical_def(params, inputs, outfile):
    """Simple map of grdfile with subsampled vertical deformation.
    Currently mess, but a proof of concept!
    Makes a grd file created by gmt surface from the xyz file written in this software. """
    print("Mapping vertical deformation in %s " % params.outdir)

    proj = 'M4i'
    region = [inputs.minlon, inputs.maxlon, inputs.minlat, inputs.maxlat]

    # First make surfaces of east/north/up deformation for later plotting
    utilities.call_gmt_surface(params.outdir + '/xyz_model.txt',
                               params.outdir + '/vert.grd',
                               region,
                               inc=0.0005)
    utilities.call_gmt_surface(params.outdir + '/xyu_model.txt',
                               params.outdir + '/east.grd',
                               region,
                               inc=0.0005)
    utilities.call_gmt_surface(params.outdir + '/xyv_model.txt',
                               params.outdir + '/north.grd',
                               region,
                               inc=0.0005)

    # Build a PyGMT plot
    fig = pygmt.Figure()
    pygmt.makecpt(cmap="roma",
                  series="-0.045/0.045/0.001",
                  background="o",
                  output="mycpt.cpt")
    fig.basemap(region=region,
                projection=proj,
                frame="+t\"Vertical Displacement\"")
    fig.grdimage(params.outdir + '/vert.grd', region=region, cmap="mycpt.cpt")
    fig.coast(region=region,
              projection=proj,
              borders='1',
              shorelines='1.0p,black',
              water='lightblue',
              map_scale="n0.23/0.06+c" + str(region[2]) + "+w20",
              frame="1.0")

    fig = annotate_figure_with_sources(fig, inputs, params, dotstyle='s0.05c')
    fig = annotate_figure_with_aftershocks(fig,
                                           aftershocks_file=params.aftershocks,
                                           style='c0.02c')

    fig.colorbar(position="JCR+w4.0i+v+o0.7i/0i",
                 cmap="mycpt.cpt",
                 truncate="-0.045/0.045",
                 frame=["x0.01", "y+L\"Disp(m)\""])
    fig.savefig(outfile)
    return
Example #23
0
def test_makecpt_to_plot_grid_scaled_with_series(grid):
    """
    Use static color palette table scaled to a min/max series to change color
    of grid
    """
    # Use single-character arguments for the reference image
    fig_ref = Figure()
    makecpt(C="oleron", T="-4500/4500")
    fig_ref.grdimage(grid, J="W0/6i")

    fig_test = Figure()
    makecpt(cmap="oleron", series="-4500/4500")
    fig_test.grdimage(grid, projection="W0/6i")
    return fig_ref, fig_test
Example #24
0
def test_makecpt_plot_points(points, region):
    """
    Use static color palette table to change color of points.
    """
    fig = Figure()
    makecpt(cmap="rainbow")
    fig.plot(
        x=points[:, 0],
        y=points[:, 1],
        color=points[:, 2],
        region=region,
        style="c1c",
        cmap=True,
    )
    return fig
Example #25
0
def plot_rotation(filename, station_vels, region, outdir, outfile):
    proj = 'M4i'
    fig = pygmt.Figure();
    pygmt.makecpt(C="magma", T="0/300/1", G="0.3/1.0", D="o", H=outdir+"/mycpt.cpt");
    fig.basemap(region=region, projection=proj, B="+t\"Rotation\"");
    fig.grdimage(filename, region=region, C=outdir+"/mycpt.cpt");
    fig.coast(region=region, projection=proj, N='1', W='1.0p,black', S='lightblue',
              L="n0.12/0.12+c" + str(region[2]) + "+w50", B="1.0");
    elon, nlat, e, n = station_vels_to_arrays(station_vels);
    fig.plot(x=elon, y=nlat, S='c0.04i', G='black', W='0.4p,white');  # station locations
    fig.plot(x=elon, y=nlat, style='v0.20+e+a40+gblack+h0+p1p,black+z0.04', pen='0.6p,black',
             direction=[e, n]);  # displacement vectors
    fig.plot(x=region[0] + 0.9, y=region[2] + 0.1, style='v0.20+e+a40+gblack+h0+p1p,black+z0.04', pen='0.6p,black',
             direction=[[20], [0]]);  # scale vector
    fig.text(x=region[0] + 0.5, y=region[2] + 0.1, text="20 mm/yr", font='10p,Helvetica,black')
    fig.colorbar(D="JCR+w4.0i+v+o0.7i/0i", C=outdir+"/mycpt.cpt", G="0/300", B=["x50", "y+L\"Rad/Ka\""]);
    print("Saving rotation figure as %s." % outfile)
    fig.savefig(outfile);
    return;
Example #26
0
def plot_rotation(filename, station_vels, region, outdir, outfile):
    proj = 'M4i'
    fig = pygmt.Figure()
    pygmt.makecpt(cmap="magma",
                  series="0/300/1",
                  truncate="0.3/1.0",
                  background="o",
                  output=outdir + "/mycpt.cpt")
    fig.basemap(region=region, projection=proj, frame="+t\"Rotation\"")
    fig.grdimage(filename, region=region, cmap=outdir + "/mycpt.cpt")
    fig.coast(region=region,
              projection=proj,
              borders='1',
              shorelines='1.0p,black',
              water='lightblue',
              map_scale="n0.12/0.12+c" + str(region[2]) + "+w50",
              frame="1.0")
    elon, nlat, e, n = station_vels_to_arrays(station_vels)
    fig.plot(x=elon, y=nlat, style='c0.04i', color='black', pen='0.4p,white')
    # station locations
    fig.plot(x=elon,
             y=nlat,
             style='v0.20+e+a40+gblack+h0+p1p,black+z0.04',
             pen='0.6p,black',
             direction=[e, n])
    # displacement vectors
    fig.plot(x=region[0] + 0.9,
             y=region[2] + 0.1,
             style='v0.20+e+a40+gblack+h0+p1p,black+z0.04',
             pen='0.6p,black',
             direction=[[20], [0]])
    # scale vector
    fig.text(x=region[0] + 0.5,
             y=region[2] + 0.1,
             text="20 mm/yr",
             font='10p,Helvetica,black')
    fig.colorbar(position="JCR+w4.0i+v+o0.7i/0i",
                 cmap=outdir + "/mycpt.cpt",
                 truncate="0/300",
                 frame=["x50", "y+L\"Rad/Ka\""])
    print("Saving rotation figure as %s." % outfile)
    fig.savefig(outfile)
    return