コード例 #1
0
def plot_googlemap(gdir, ax=None):
    """Plots the glacier over a googlemap."""

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid

    dofig = False
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        dofig = True

    s = salem.utils.read_shapefile(gdir.get_filepath('outlines'))
    gm = salem.GoogleVisibleMap(np.array(s.geometry[0].exterior.xy[0]),
                                np.array(s.geometry[0].exterior.xy[1]),
                                src=s.crs)

    img = gm.get_vardata()[..., 0:3]  # sometimes there is an alpha
    cmap = cleo.Map(gm.grid, countries=False, nx=gm.grid.nx)
    cmap.set_rgb(img)

    cmap.set_shapefile(gdir.get_filepath('outlines'))

    cmap.plot(ax)
    title = gdir.rgi_id
    if gdir.name is not None and gdir.name != '':
        title += ': ' + gdir.name
    ax.set_title(title)

    if dofig:
        plt.tight_layout()
コード例 #2
0
def plot_catchment_width(gdir, ax=None, corrected=False):
    """Plots the catchment widths out of a glacier directory."""

    dofig = False
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        dofig = True

    nc = netCDF4.Dataset(gdir.get_filepath('gridded_data'))
    topo = nc.variables['topo'][:]
    nc.close()

    mp = cleo.Map(gdir.grid, countries=False, nx=gdir.grid.nx)
    mp.set_topography(topo)

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid
    for i in gdir.divide_ids:
        geom = gdir.read_pickle('geometries', div_id=i)

        # Plot boundaries
        poly_pix = geom['polygon_pix']
        mp.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2)
        for l in poly_pix.interiors:
            mp.set_geometry(l, crs=crs, color='black', linewidth=0.5)

        # plot Centerlines
        cls = gdir.read_pickle('inversion_flowlines', div_id=i)[::-1]
        color = gpd.plotting.gencolor(len(cls) + 1, colormap='Set1')
        for l, c in zip(cls, color):
            mp.set_geometry(l.line, crs=crs, color=c, linewidth=2.5, zorder=50)
            if corrected:
                for wi, cur, (n1, n2) in zip(l.widths, l.line.coords,
                                             l.normals):
                    l = shpg.LineString([
                        shpg.Point(cur + wi / 2. * n1),
                        shpg.Point(cur + wi / 2. * n2)
                    ])

                    mp.set_geometry(l,
                                    crs=crs,
                                    color=c,
                                    linewidth=0.6,
                                    zorder=50)
            else:
                for wl, wi in zip(l.geometrical_widths, l.widths):
                    col = c if np.isfinite(wi) else 'grey'
                    for w in wl:
                        mp.set_geometry(w,
                                        crs=crs,
                                        color=col,
                                        linewidth=0.6,
                                        zorder=50)

    mp.plot(ax)
    if dofig:
        plt.tight_layout()
コード例 #3
0
def plot_domain(gdir, ax=None):  # pragma: no cover
    """Plot the glacier directory."""

    # Files
    nc = netCDF4.Dataset(gdir.get_filepath('gridded_data'))
    topo = nc.variables['topo'][:]
    nc.close()

    dofig = False
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        dofig = True

    mp = cleo.Map(gdir.grid, countries=False, nx=gdir.grid.nx)
    cm = truncate_colormap(colormap.terrain, minval=0.25, maxval=1.0, n=256)
    mp.set_cmap(cm)
    mp.set_plot_params(nlevels=256)
    mp.set_data(topo, interp='linear')

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid

    for i in gdir.divide_ids:
        geom = gdir.read_pickle('geometries', div_id=i)

        # Plot boundaries
        poly_pix = geom['polygon_pix']

        mp.set_geometry(poly_pix,
                        crs=crs,
                        fc='white',
                        alpha=0.3,
                        zorder=2,
                        linewidth=.2)
        for l in poly_pix.interiors:
            mp.set_geometry(l, crs=crs, color='black', linewidth=0.5)

    mp.plot(ax)
    cb = mp.append_colorbar(ax, "right", size="5%", pad=0.2)
    cb.set_label('Alt. [m]')
    title = gdir.rgi_id
    if gdir.name is not None and gdir.name != '':
        title += ': ' + gdir.name
    ax.set_title(title)

    if dofig:
        plt.tight_layout()
コード例 #4
0
ファイル: graphics.py プロジェクト: MachineAi/oggm
    def newplotfunc(gdir,
                    ax=None,
                    add_colorbar=True,
                    title=None,
                    title_comment=None,
                    horizontal_colorbar=False,
                    lonlat_contours_kwargs=None,
                    **kwargs):

        dofig = False
        if ax is None:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            dofig = True

        mp = cleo.Map(gdir.grid, countries=False, nx=gdir.grid.nx)
        if lonlat_contours_kwargs is not None:
            mp.set_lonlat_contours(**lonlat_contours_kwargs)

        out = plotfunc(gdir, ax=ax, cleomap=mp, **kwargs)

        if add_colorbar and 'cbar_label' in out:
            cbprim = out.get('cbar_primitive', mp)
            if horizontal_colorbar:
                cb = cbprim.append_colorbar(ax, "bottom", size="5%", pad=0.4)
            else:
                cb = cbprim.append_colorbar(ax, "right", size="5%", pad=0.2)
            cb.set_label(out['cbar_label'])

        if title is None:
            if 'title' not in out:
                # Make a defaut one
                title = gdir.rgi_id
                if gdir.name is not None and gdir.name != '':
                    title += ': ' + gdir.name
                out['title'] = title

            if title_comment is None:
                title_comment = out.get('title_comment', '')

            out['title'] += title_comment
            ax.set_title(out['title'])
        else:
            ax.set_title(title)

        if dofig:
            plt.tight_layout()
コード例 #5
0
ファイル: plot_submitted.py プロジェクト: MachineAi/oggm
from oggm.sandbox.itmix import find_path

pdir = os.path.join(PLOTS_DIR, 'submitted') + '/'
if not os.path.exists(pdir):
    os.mkdir(pdir)

for dgn in glob.glob(os.path.join(ITMIX_ODIR, '*')):
    gname = os.path.basename(dgn)
    print(gname)

    ifile = find_path(os.path.join(DATA_DIR, 'itmix', 'glaciers_sorted'),
                      '02_surface_' + gname + '*.asc')
    ds = salem.EsriITMIX(ifile)
    itmix_topo = ds.get_vardata()

    ifiles = find_path(ITMIX_ODIR, '*' + gname + '*.asc', allow_more=True)
    for ifile in ifiles:
        ds2 = salem.EsriITMIX(ifile)
        oggm_topo = ds2.get_vardata()

        thick = itmix_topo - oggm_topo

        cm = cleo.Map(ds.grid)
        cm.set_plot_params(nlevels=256)
        cm.set_cmap(plt.get_cmap('viridis'))
        cm.set_data(thick)
        cm.visualize()

        pname = os.path.basename(ifile).split('.')[0]
        plt.savefig(os.path.join(pdir, pname) + '.png')
        plt.close()
コード例 #6
0
def plot_modeloutput_map(gdir, model=None, ax=None, vmax=None):
    """Plots the result of the model output."""

    dofig = False
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        dofig = True

    nc = netCDF4.Dataset(gdir.get_filepath('gridded_data'))
    topo = nc.variables['topo'][:]
    nc.close()

    geom = gdir.read_pickle('geometries', div_id=0)
    poly_pix = geom['polygon_pix']

    ds = salem.GeoDataset(gdir.grid)
    mlines = shpg.GeometryCollection([l.line for l in model.fls] + [poly_pix])
    ml = mlines.bounds
    corners = ((ml[0], ml[1]), (ml[2], ml[3]))

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=RuntimeWarning)
        ds.set_subset(corners=corners, margin=10, crs=gdir.grid)

    mp = cleo.Map(ds.grid, countries=False, nx=gdir.grid.nx)
    mp.set_topography(topo, crs=gdir.grid)

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid
    mp.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2)
    for l in poly_pix.interiors:
        mp.set_geometry(l, crs=crs, color='black', linewidth=0.5)

    toplot_th = np.array([])
    toplot_lines = []

    # plot Centerlines
    cls = model.fls
    for l in cls:
        mp.set_geometry(l.line,
                        crs=crs,
                        color='gray',
                        linewidth=1.2,
                        zorder=50)
        toplot_th = np.append(toplot_th, l.thick)
        for wi, cur, (n1, n2) in zip(l.widths, l.line.coords, l.normals):
            l = shpg.LineString([
                shpg.Point(cur + wi / 2. * n1),
                shpg.Point(cur + wi / 2. * n2)
            ])
            toplot_lines.append(l)

    cm = plt.cm.get_cmap('YlOrRd')
    dl = cleo.DataLevels(cmap=cm,
                         nlevels=256,
                         data=toplot_th,
                         vmin=0,
                         vmax=vmax)
    colors = dl.to_rgb()
    for l, c in zip(toplot_lines, colors):
        mp.set_geometry(l, crs=crs, color=c, linewidth=3, zorder=50)
    mp.plot(ax)
    cb = dl.append_colorbar(ax, "right", size="5%", pad=0.2)
    cb.set_label('Glacier thickness [m]')
    tit = ' -- year: {:d}'.format(np.int64(model.yr))
    ax.set_title(gdir.rgi_id + tit)

    if dofig:
        plt.tight_layout()
コード例 #7
0
def plot_inversion(gdir, ax=None):
    """Plots the result of the inversion out of a glacier directory."""

    dofig = False
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        dofig = True

    nc = netCDF4.Dataset(gdir.get_filepath('gridded_data'))
    topo = nc.variables['topo'][:]
    nc.close()

    mp = cleo.Map(gdir.grid, countries=False, nx=gdir.grid.nx)
    mp.set_topography(topo)

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid

    toplot_th = np.array([])
    toplot_lines = []
    for i in gdir.divide_ids:
        geom = gdir.read_pickle('geometries', div_id=i)
        inv = gdir.read_pickle('inversion_output', div_id=i)
        # Plot boundaries
        poly_pix = geom['polygon_pix']
        mp.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2)
        for l in poly_pix.interiors:
            mp.set_geometry(l, crs=crs, color='black', linewidth=0.5)

        # plot Centerlines
        cls = gdir.read_pickle('inversion_flowlines', div_id=i)
        for l, c in zip(cls, inv):

            mp.set_geometry(l.line,
                            crs=crs,
                            color='gray',
                            linewidth=1.2,
                            zorder=50)
            toplot_th = np.append(toplot_th, c['thick'])
            for wi, cur, (n1, n2) in zip(l.widths, l.line.coords, l.normals):
                l = shpg.LineString([
                    shpg.Point(cur + wi / 2. * n1),
                    shpg.Point(cur + wi / 2. * n2)
                ])
                toplot_lines.append(l)

    cm = plt.cm.get_cmap('YlOrRd')
    dl = cleo.DataLevels(cmap=cm, nlevels=256, data=toplot_th, vmin=0)
    colors = dl.to_rgb()
    for l, c in zip(toplot_lines, colors):
        mp.set_geometry(l, crs=crs, color=c, linewidth=3, zorder=50)
    mp.plot(ax)
    cb = dl.append_colorbar(ax, "right", size="5%", pad=0.2)
    cb.set_label('Glacier thickness [m]')
    title = gdir.rgi_id
    if gdir.name is not None and gdir.name != '':
        title += ': ' + gdir.name
    ax.set_title(title)
    if dofig:
        plt.tight_layout()
コード例 #8
0
def plot_centerlines(gdir, ax=None, use_flowlines=False, add_downstream=False):
    """Plots the centerlines of a glacier directory."""

    # Files
    filename = 'centerlines'
    if use_flowlines:
        filename = 'inversion_flowlines'

    nc = netCDF4.Dataset(gdir.get_filepath('gridded_data'))
    topo = nc.variables['topo'][:]
    nc.close()

    dofig = False
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        dofig = True

    mp = cleo.Map(gdir.grid, countries=False, nx=gdir.grid.nx)
    cm = truncate_colormap(colormap.terrain, minval=0.25, maxval=1.0, n=256)
    mp.set_cmap(cm)
    mp.set_plot_params(nlevels=256)
    mp.set_data(topo, interp='linear')

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid

    for i in gdir.divide_ids:
        geom = gdir.read_pickle('geometries', div_id=i)

        # Plot boundaries
        poly_pix = geom['polygon_pix']

        mp.set_geometry(poly_pix,
                        crs=crs,
                        fc='white',
                        alpha=0.3,
                        zorder=2,
                        linewidth=.2)
        for l in poly_pix.interiors:
            mp.set_geometry(l, crs=crs, color='black', linewidth=0.5)

        # plot Centerlines
        cls = gdir.read_pickle(filename, div_id=i)

        # Go in reverse order for red always being the longuest
        cls = cls[::-1]
        color = gpd.plotting.gencolor(len(cls) + 1, colormap='Set1')
        for l, c in zip(cls, color):
            mp.set_geometry(l.line, crs=crs, color=c, linewidth=2.5, zorder=50)
            mp.set_geometry(l.head,
                            crs=gdir.grid,
                            marker='o',
                            markersize=60,
                            alpha=0.8,
                            color=c,
                            zorder=99)

            for j in l.inflow_points:
                mp.set_geometry(j,
                                crs=crs,
                                marker='o',
                                markersize=40,
                                edgecolor='k',
                                alpha=0.8,
                                zorder=99,
                                facecolor='none')

        if add_downstream:
            line = gdir.read_pickle('downstream_line', div_id=i)
            mp.set_geometry(line,
                            crs=crs,
                            color='red',
                            linewidth=2.5,
                            zorder=50)

            mp.set_geometry(shpg.Point(line.coords[0]),
                            crs=crs,
                            marker='o',
                            markersize=40,
                            edgecolor='k',
                            alpha=0.8,
                            zorder=99,
                            facecolor='w')

    mp.plot(ax)
    cb = mp.append_colorbar(ax, "right", size="5%", pad=0.2)
    cb.set_label('Alt. [m]')
    title = gdir.rgi_id
    if gdir.name is not None and gdir.name != '':
        title += ': ' + gdir.name
    ax.set_title(title)

    if dofig:
        plt.tight_layout()
コード例 #9
0
ファイル: graphics.py プロジェクト: MachineAi/oggm
def plot_modeloutput_map(gdir, ax=None, cleomap=None, model=None, vmax=None):
    """Plots the result of the model output."""

    with netCDF4.Dataset(gdir.get_filepath('gridded_data')) as nc:
        topo = nc.variables['topo'][:]

    geom = gdir.read_pickle('geometries', div_id=0)
    poly_pix = geom['polygon_pix']

    ds = salem.GeoDataset(gdir.grid)
    mlines = shpg.GeometryCollection([l.line for l in model.fls] + [poly_pix])
    ml = mlines.bounds
    corners = ((ml[0], ml[1]), (ml[2], ml[3]))

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=RuntimeWarning)
        ds.set_subset(corners=corners, margin=10, crs=gdir.grid)

    cleomap = cleo.Map(ds.grid, countries=False, nx=gdir.grid.nx)
    cleomap.set_topography(topo, crs=gdir.grid)

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid
    cleomap.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2)
    for l in poly_pix.interiors:
        cleomap.set_geometry(l, crs=crs, color='black', linewidth=0.5)

    toplot_th = np.array([])
    toplot_lines = []

    # plot Centerlines
    cls = model.fls
    for l in cls:
        cleomap.set_geometry(l.line,
                             crs=crs,
                             color='gray',
                             linewidth=1.2,
                             zorder=50)
        toplot_th = np.append(toplot_th, l.thick)
        widths = l.widths.copy()
        widths = np.where(l.thick > 0, widths, 0.)
        for wi, cur, (n1, n2) in zip(widths, l.line.coords, l.normals):
            l = shpg.LineString([
                shpg.Point(cur + wi / 2. * n1),
                shpg.Point(cur + wi / 2. * n2)
            ])
            toplot_lines.append(l)

    cm = plt.cm.get_cmap('YlOrRd')
    dl = cleo.DataLevels(cmap=cm,
                         nlevels=256,
                         data=toplot_th,
                         vmin=0,
                         vmax=vmax)
    colors = dl.to_rgb()
    for l, c in zip(toplot_lines, colors):
        cleomap.set_geometry(l, crs=crs, color=c, linewidth=3, zorder=50)
    cleomap.plot(ax)

    return dict(cbar_label='Section thickness [m]',
                cbar_primitive=dl,
                title_comment=' -- year: {:d}'.format(np.int64(model.yr)))