Exemplo n.º 1
0
def plot_modeloutput_map(gdir, ax=None, salemmap=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)

    salemmap = salem.Map(ds.grid, countries=False, nx=gdir.grid.nx)
    salemmap.set_topography(topo, crs=gdir.grid)

    # TODO: center grid or corner grid???
    crs = gdir.grid.center_grid
    salemmap.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2)
    for l in poly_pix.interiors:
        salemmap.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:
        salemmap.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 = salem.DataLevels(cmap=cm,
                          nlevels=256,
                          data=toplot_th,
                          vmin=0,
                          vmax=vmax)
    colors = dl.to_rgb()
    for l, c in zip(toplot_lines, colors):
        salemmap.set_geometry(l, crs=crs, color=c, linewidth=3, zorder=50)
    salemmap.plot(ax)

    return dict(cbar_label='Section thickness [m]',
                cbar_primitive=dl,
                title_comment=' -- year: {:d}'.format(np.int64(model.yr)))
Exemplo n.º 2
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()