def plot_inversion(gdirs, ax=None, smap=None, linewidth=3, vmax=None): """Plots the result of the inversion out of a glacier directory.""" gdir = gdirs[0] with netCDF4.Dataset(gdir.get_filepath('gridded_data')) as nc: topo = nc.variables['topo'][:] # Dirty optim try: smap.set_topography(topo) except ValueError: pass toplot_th = np.array([]) toplot_lines = [] toplot_crs = [] vol = [] for gdir in gdirs: crs = gdir.grid.center_grid geom = gdir.read_pickle('geometries') inv = gdir.read_pickle('inversion_output') # Plot boundaries poly_pix = geom['polygon_pix'] smap.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2) for l in poly_pix.interiors: smap.set_geometry(l, crs=crs, color='black', linewidth=0.5) # plot Centerlines cls = gdir.read_pickle('inversion_flowlines') for l, c in zip(cls, inv): smap.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) toplot_crs.append(crs) vol.extend(c['volume']) 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, crs in zip(toplot_lines, colors, toplot_crs): smap.set_geometry(l, crs=crs, color=c, linewidth=linewidth, zorder=50) smap.plot(ax) return dict(cbar_label='Section thickness [m]', cbar_primitive=dl, title_comment=' ({:.2f} km3)'.format(np.nansum(vol) * 1e-9))
def plot_region_inversion(gdirs, ax=None, salemmap=None): """Plots the result of the inversion for a larger region.""" if ax is None: ax = plt.gca() toplot_th = np.array([]) toplot_lines = [] vol = [] crs_list = [] # loop over the directories to get the data for gdir in gdirs: crs = gdir.grid.center_grid 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'] 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) # plot Centerlines cls = gdir.read_pickle('inversion_flowlines', div_id=i) for l, c in zip(cls, inv): salemmap.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) crs_list.append(crs) vol.extend(c['volume']) # plot the data cm = plt.cm.get_cmap('YlOrRd') dl = salem.DataLevels(cmap=cm, nlevels=256, data=toplot_th, vmin=0) colors = dl.to_rgb() for l, c, crs in zip(toplot_lines, colors, crs_list): salemmap.set_geometry(l, crs=crs, color=c, linewidth=1, zorder=50) salemmap.plot(ax) cb = dl.append_colorbar(ax, "right", size="5%", pad=0.2) cb.set_label('Section thickness [m]')
def plot_domain_with_gtd(gdirs, ax=None, smap=None): """Plot the glacier directory.""" # Files gdir = gdirs[0] with netCDF4.Dataset(gdir.get_filepath('gridded_data')) as nc: topo = nc.variables['topo'][:] try: smap.set_data(topo) except ValueError: pass cm = truncate_colormap(colormap.terrain, minval=0.25, maxval=1.0, n=256) smap.set_cmap(cm) smap.set_plot_params(nlevels=256) try: for gdir in gdirs: crs = gdir.grid.center_grid geom = gdir.read_pickle('geometries') # Plot boundaries poly_pix = geom['polygon_pix'] smap.set_geometry(poly_pix, crs=crs, fc='white', alpha=0.3, zorder=2, linewidth=.2) for l in poly_pix.interiors: smap.set_geometry(l, crs=crs, color='black', linewidth=0.5) except FileNotFoundError: smap.set_shapefile(gdir.get_filepath('outlines'), color='black') smap.plot(ax) # Get the ref data i, j, lon, lat, ref_thick = g2tasks.get_ref_gtd_data(gdir) grids_file = gdir.get_filepath('gridded_data') with netCDF4.Dataset(grids_file) as nc: glacier_mask = nc.variables['glacier_mask'][:] pok = np.nonzero(glacier_mask[j, i]) assert len(pok[0]) > 0 i, j, ref_thick = i[pok], j[pok], ref_thick[pok] dl = salem.DataLevels(ref_thick) ax.scatter(i, j, color=dl.to_rgb(), s=50, edgecolors='k', linewidths=1) tstr = ' (N: {})'.format(len(ref_thick)) return dict(cbar_label='Alt. [m]', title_comment=tstr)
def plot_modeloutput_map(gdirs, ax=None, smap=None, model=None, vmax=None, linewidth=3, filesuffix='', modelyr=None): """Plots the result of the model output.""" gdir = gdirs[0] with utils.ncDataset(gdir.get_filepath('gridded_data')) as nc: topo = nc.variables['topo'][:] # Dirty optim try: smap.set_topography(topo) except ValueError: pass toplot_th = np.array([]) toplot_lines = [] toplot_crs = [] if model is None: models = [] for gdir in gdirs: model = FileModel( gdir.get_filepath('model_run', filesuffix=filesuffix)) model.run_until(modelyr) models.append(model) else: models = utils.tolist(model) for gdir, model in zip(gdirs, models): geom = gdir.read_pickle('geometries') poly_pix = geom['polygon_pix'] crs = gdir.grid.center_grid smap.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2) poly_pix = utils.tolist(poly_pix) for _poly in poly_pix: for l in _poly.interiors: smap.set_geometry(l, crs=crs, color='black', linewidth=0.5) # plot Centerlines cls = model.fls for l in cls: smap.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): line = shpg.LineString([ shpg.Point(cur + wi / 2. * n1), shpg.Point(cur + wi / 2. * n2) ]) toplot_lines.append(line) toplot_crs.append(crs) dl = salem.DataLevels(cmap=OGGM_CMAPS['section_thickness'], data=toplot_th, vmin=0, vmax=vmax) colors = dl.to_rgb() for l, c, crs in zip(toplot_lines, colors, toplot_crs): smap.set_geometry(l, crs=crs, color=c, linewidth=linewidth, zorder=50) smap.plot(ax) return dict(cbar_label='Section thickness [m]', cbar_primitive=dl, title_comment=' -- year: {:d}'.format(np.int64(model.yr)))
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)))
plt.annotate('e)', xy=(x, 0.32), xytext=(0, 4), size=fsiz, xycoords=('figure fraction', 'figure fraction'), textcoords='offset points') plt.tight_layout() f.subplots_adjust(right=0.89) # cax = f.add_axes([0.95,0.55,0.025,0.4]) # salem.DataLevels(ds, nlevels=8) # #kw1=salem.DataLevels.colorbarbase(cax, **kw1) # mpl.colorbar.ColorbarBase(cax, **kw1) cax = f.add_axes([0.90, 0.39, 0.017, 0.25]) dl = salem.DataLevels(ds, nlevels=8) #kw1=salem.DataLevels.colorbarbase(cax, **kw1) cb1 = mpl.colorbar.ColorbarBase(cax, label='Nocturnal | Afternoon', **kw3) #dl.set_extend(extend='both') #cb1.set_ticklabels(['']*6) #f.colorbar(cax).set_yticklabels(['','','','','','']) cax = f.add_axes([0.46, 0.07, 0.017, 0.25]) mpl.colorbar.ColorbarBase(cax, label='m', **kw) cax = f.add_axes([0.9, 0.72, 0.017, 0.25]) mpl.colorbar.ColorbarBase(cax, label='Frequency', **kw2) plt.savefig(spath + '/scales_map.png', dpi=300)
def plot_inversion(gdirs, ax=None, smap=None, linewidth=3, vmax=None, plot_var='thick', cbar_label='Section thickness (m)', color_map='YlGnBu'): """Plots the result of the inversion out of a glacier directory. Default is thickness (m). Change plot_var to u_surface or u_integrated for velocity (m/yr).""" gdir = gdirs[0] with utils.ncDataset(gdir.get_filepath('gridded_data')) as nc: topo = nc.variables['topo'][:] # Dirty optim try: smap.set_topography(topo) except ValueError: pass toplot_var = np.array([]) toplot_lines = [] toplot_crs = [] vol = [] for gdir in gdirs: crs = gdir.grid.center_grid geom = gdir.read_pickle('geometries') inv = gdir.read_pickle('inversion_output') # Plot boundaries poly_pix = geom['polygon_pix'] smap.set_geometry(poly_pix, crs=crs, fc='none', zorder=2, linewidth=.2) for l in poly_pix.interiors: smap.set_geometry(l, crs=crs, color='black', linewidth=0.5) # Plot Centerlines cls = gdir.read_pickle('inversion_flowlines') for l, c in zip(cls, inv): smap.set_geometry(l.line, crs=crs, color='gray', linewidth=1.2, zorder=50) toplot_var = np.append(toplot_var, c[plot_var]) for wi, cur, (n1, n2) in zip(l.widths, l.line.coords, l.normals): line = shpg.LineString([ shpg.Point(cur + wi / 2. * n1), shpg.Point(cur + wi / 2. * n2) ]) toplot_lines.append(line) toplot_crs.append(crs) vol.extend(c['volume']) dl = salem.DataLevels(cmap=plt.get_cmap(color_map), data=toplot_var, vmin=0, vmax=vmax) colors = dl.to_rgb() for l, c, crs in zip(toplot_lines, colors, toplot_crs): smap.set_geometry(l, crs=crs, color=c, linewidth=linewidth, zorder=50) smap.plot(ax) out = dict(cbar_label=cbar_label, cbar_primitive=dl) if plot_var == 'thick': out['title_comment'] = ' ({:.2f} km3)'.format(np.nansum(vol) * 1e-9) return out