From 8ea385957fe4ce113c18569ff2c035b68c6dc7c8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 6 Dec 2022 12:17:44 +0100 Subject: [PATCH 1/7] added functionality to plot multiple glaciers in same plot --- oggm/graphics.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/oggm/graphics.py b/oggm/graphics.py index e62c633e8..35225e68c 100644 --- a/oggm/graphics.py +++ b/oggm/graphics.py @@ -76,6 +76,76 @@ def surf_to_nan(surf_h, thick): return surf_h +def combine_grids(gdirs): + """ Combines individual grids of different glacier directories to show + multiple glaciers in the same plot. The resulting grid extent includes + all individual grids completely. + + Parameters + ---------- + gdirs : [], required + A list of GlacierDirectories. + + Returns + ------- + salem.gis.Grid + """ + + new_grid = { + 'proj': None, + 'nxny': None, + 'dxdy': None, + 'x0y0': None, + 'pixel_ref': None + } + + left_use = None + right_use = None + bottom_use = None + top_use = None + dx_use = None + dy_use = None + + for gdir in gdirs: + # use the first gdir to define some values + if new_grid['proj'] is None: + new_grid['proj'] = gdir.grid.proj + if new_grid['pixel_ref'] is None: + new_grid['pixel_ref'] = gdir.grid.pixel_ref + + # find largest extend including all grids completely + (left, right, bottom, top) = gdir.grid.extent_in_crs(new_grid['proj']) + if (left_use is None) or (left_use > left): + left_use = left + if right_use is None or right_use < right: + right_use = right + if bottom_use is None or bottom_use > bottom: + bottom_use = bottom + if top_use is None or top_use < top: + top_use = top + + # find smallest dx and dy for the estimation of nx and ny + dx = gdir.grid.dx + dy = gdir.grid.dy + if dx_use is None or dx_use > dx: + dx_use = dx + # dy is negative + if dy_use is None or dy_use < dy: + dy_use = dy + + # calculate nx and ny, the final extend could be one grid point larger or + # smaller due to round() + nx_use = round((right_use - left_use) / dx_use) + ny_use = round((top_use - bottom_use) / abs(dy_use)) + + # finally define the last values of the new grid + new_grid['x0y0'] = (left_use, top_use) + new_grid['nxny'] = (nx_use, ny_use) + new_grid['dxdy'] = (dx_use, dy_use) + + return salem.gis.Grid.from_dict(new_grid) + + def _plot_map(plotfunc): """ Decorator for common salem.Map plotting logic @@ -114,6 +184,8 @@ def _plot_map(plotfunc): save the figure to a file instead of displaying it savefig_kwargs : dict, optional the kwargs to plt.savefig + extend_plot_limit : bool, optional + set to True to extend the plotting limits for all provided gdirs grids """ # Build on the original docstring @@ -124,7 +196,7 @@ def newplotfunc(gdirs, ax=None, smap=None, add_colorbar=True, title=None, title_comment=None, horizontal_colorbar=False, lonlat_contours_kwargs=None, cbar_ax=None, autosave=False, add_scalebar=True, figsize=None, savefig=None, - savefig_kwargs=None, + savefig_kwargs=None, extend_plot_limit=False, **kwargs): dofig = False @@ -137,8 +209,13 @@ def newplotfunc(gdirs, ax=None, smap=None, add_colorbar=True, title=None, gdirs = utils.tolist(gdirs) if smap is None: - mp = salem.Map(gdirs[0].grid, countries=False, - nx=gdirs[0].grid.nx) + if extend_plot_limit: + grid_combined = combine_grids(gdirs) + mp = salem.Map(grid_combined, countries=False, + nx=grid_combined.nx) + else: + mp = salem.Map(gdirs[0].grid, countries=False, + nx=gdirs[0].grid.nx) else: mp = smap From 4370abf74d0202dc47bb0f127d1b5d9f694768b5 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 7 Dec 2022 09:56:23 +0100 Subject: [PATCH 2/7] distinguish sign of dy --- oggm/graphics.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/oggm/graphics.py b/oggm/graphics.py index 35225e68c..de1d2f9ca 100644 --- a/oggm/graphics.py +++ b/oggm/graphics.py @@ -129,8 +129,8 @@ def combine_grids(gdirs): dy = gdir.grid.dy if dx_use is None or dx_use > dx: dx_use = dx - # dy is negative - if dy_use is None or dy_use < dy: + # dy could be negative + if dy_use is None or abs(dy_use) > abs(dy): dy_use = dy # calculate nx and ny, the final extend could be one grid point larger or @@ -139,7 +139,10 @@ def combine_grids(gdirs): ny_use = round((top_use - bottom_use) / abs(dy_use)) # finally define the last values of the new grid - new_grid['x0y0'] = (left_use, top_use) + if np.sign(dy_use) < 0: + new_grid['x0y0'] = (left_use, top_use) + else: + new_grid['x0y0'] = (left_use, bottom_use) new_grid['nxny'] = (nx_use, ny_use) new_grid['dxdy'] = (dx_use, dy_use) From c862b2de27fd1c92093f559ab7174caceb600232 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 13 Dec 2022 13:36:22 +0100 Subject: [PATCH 3/7] added test --- oggm/tests/test_workflow.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/oggm/tests/test_workflow.py b/oggm/tests/test_workflow.py index 2f1fbf06a..c2295fbca 100644 --- a/oggm/tests/test_workflow.py +++ b/oggm/tests/test_workflow.py @@ -451,10 +451,15 @@ def test_plot_region_model(): sm.set_topography(get_demo_file('srtm_oetztal.tif')) # Give this to the plot function - fig, ax = plt.subplots() - graphics.plot_modeloutput_map(gdirs, smap=sm, ax=ax, + fig, (ax1, ax2) = plt.subplots(1, 2) + graphics.plot_modeloutput_map(gdirs, smap=sm, ax=ax1, filesuffix='_plot', vmax=250, modelyr=10, linewidth=1.5) + # test automatic definition of larger plotting grid with extend_plot_limit + graphics.plot_modeloutput_map(gdirs, ax=ax2, + filesuffix='_plot', vmax=250, + modelyr=10, linewidth=1.5, + extend_plot_limit=True) fig.tight_layout() return fig From e1913a3528064653b75563c21fca598421d1d7c0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 13 Dec 2022 13:42:07 +0100 Subject: [PATCH 4/7] added in whats-new --- docs/whats-new.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/whats-new.rst b/docs/whats-new.rst index b9aa36cae..cdb21144a 100644 --- a/docs/whats-new.rst +++ b/docs/whats-new.rst @@ -60,7 +60,12 @@ Enhancements or ``'trapezoidal'`` before calling ``init_present_time_glacier(gdir)``. By `Patrick Schmitt `_ - Added option to plot flowline velocities in ``graphics.plot_modeloutput_map()`` - (:pull:`1496`) + (:pull:`1496`). + By `Patrick Schmitt `_ +- Added option to extend the plot limits when plotting multiple gdirs. Could be + used with ``extend_plot_limits=True``, e.g. + ``graphics.plot_modeloutput_map(gdirs, extend_plot_limits=True)`` + (:pull:`1508`). By `Patrick Schmitt `_ Bug fixes From 8c8b2c017a611b3360b423716fd2efac6be74081 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 13 Dec 2022 17:24:59 +0100 Subject: [PATCH 5/7] changed test figure size --- oggm/tests/test_workflow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oggm/tests/test_workflow.py b/oggm/tests/test_workflow.py index c2295fbca..524d75fdc 100644 --- a/oggm/tests/test_workflow.py +++ b/oggm/tests/test_workflow.py @@ -451,7 +451,7 @@ def test_plot_region_model(): sm.set_topography(get_demo_file('srtm_oetztal.tif')) # Give this to the plot function - fig, (ax1, ax2) = plt.subplots(1, 2) + fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5)) graphics.plot_modeloutput_map(gdirs, smap=sm, ax=ax1, filesuffix='_plot', vmax=250, modelyr=10, linewidth=1.5) From c7d90459260eda2989d8b655883a7afa6f1b9bc9 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 13 Dec 2022 17:42:58 +0100 Subject: [PATCH 6/7] added also test_plot_region_inversion --- oggm/tests/test_workflow.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/oggm/tests/test_workflow.py b/oggm/tests/test_workflow.py index 524d75fdc..b80e4fdd2 100644 --- a/oggm/tests/test_workflow.py +++ b/oggm/tests/test_workflow.py @@ -424,8 +424,12 @@ def test_plot_region_inversion(): sm.set_topography(get_demo_file('srtm_oetztal.tif')) # Give this to the plot function - fig, ax = plt.subplots() - graphics.plot_inversion(gdirs, smap=sm, ax=ax, linewidth=1.5, vmax=250) + fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5)) + graphics.plot_inversion(gdirs, smap=sm, ax=ax1, linewidth=1.5, vmax=250) + + # test automatic definition of larger plotting grid with extend_plot_limit + graphics.plot_inversion(gdirs, ax=ax2, linewidth=1.5, vmax=250, + extend_plot_limit=True) fig.tight_layout() return fig From 569d2c01794fb085b0383d6b4f792bc76141ee96 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 13 Dec 2022 18:03:48 +0100 Subject: [PATCH 7/7] changed sample_data_commit --- oggm/utils/_downloads.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oggm/utils/_downloads.py b/oggm/utils/_downloads.py index e688d6309..6fa1a2386 100644 --- a/oggm/utils/_downloads.py +++ b/oggm/utils/_downloads.py @@ -69,7 +69,7 @@ # The given commit will be downloaded from github and used as source for # all sample data SAMPLE_DATA_GH_REPO = 'OGGM/oggm-sample-data' -SAMPLE_DATA_COMMIT = 'dbd57434df8fade8d8df9206839b9bf26a1ef8e6' +SAMPLE_DATA_COMMIT = 'd9f01846960a141690bab9d38a85524d89a0d9ae' CHECKSUM_URL = 'https://cluster.klima.uni-bremen.de/data/downloads.sha256.hdf' CHECKSUM_VALIDATION_URL = CHECKSUM_URL + '.sha256'