From 9d40a7802175f06993d2411567c6a1974024a8d4 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Thu, 16 Apr 2020 12:20:01 +0200 Subject: [PATCH 1/5] set clon to 0 only for global files if clon is None and clat is None --- psy_maps/plotters.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/psy_maps/plotters.py b/psy_maps/plotters.py index 17e5ed5..8e9309f 100755 --- a/psy_maps/plotters.py +++ b/psy_maps/plotters.py @@ -593,10 +593,12 @@ def update(self, value): value = None else: self.clon = value - if value is None and self.lonlatbox.value is not None: - self.clon = self.lon_mean - elif value is None: - self.clon = 0.0 + if value is None: + lonmin, lonmax = self.lonlatbox.lonlatbox[:2] + if lonmax - lonmin > 350: + self.clon = 0.0 + else: + self.clon = self.lon_mean class CenterLat(BoxBase): @@ -631,10 +633,8 @@ def update(self, value): value = None else: self.clat = value - if value is None and self.lonlatbox.value is not None: + if value is None: self.clat = self.lat_mean - elif value is None: - self.clat = 0.0 @docstrings.get_sectionsf('LonLatBox') From 068c6b685c1a9951b034b438b003c7a693a78586 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Thu, 16 Apr 2020 12:25:55 +0200 Subject: [PATCH 2/5] add change of clon and clat behaviour to changelog --- CHANGELOG.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c156592..69d478d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,11 @@ Changed see `#4 `__ * the default values for the `transform` and `projection` formatoptions are now ``'cf'`` (see `#5 `__) +* ``clat`` now always takes the mean latitude of the data if the formatoption + value is None (see `#8 `__) +* ``clon`` now takes the mean latitude of the data if the formatoption + value is None and the data does not span the entire world + (see `#8 `__) v1.2.0 ====== From 6aab1f3b9241941ed597bd12861de363251c56ab Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Thu, 16 Apr 2020 20:36:39 +0200 Subject: [PATCH 3/5] set clat to 0 for whole globe --- CHANGELOG.rst | 8 ++++---- psy_maps/plotters.py | 6 +++++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 69d478d..7e861b4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,10 +17,10 @@ Changed * the default values for the `transform` and `projection` formatoptions are now ``'cf'`` (see `#5 `__) * ``clat`` now always takes the mean latitude of the data if the formatoption - value is None (see `#8 `__) -* ``clon`` now takes the mean latitude of the data if the formatoption - value is None and the data does not span the entire world - (see `#8 `__) + value is None and the data does not span the entire latitudinal range. At the + same time, ``clon`` now takes the mean longitude of the data if the + formatoption value is None and the data does not span the entire longitudinal + range (see `#8 `__) v1.2.0 ====== diff --git a/psy_maps/plotters.py b/psy_maps/plotters.py index 8e9309f..1c04f5a 100755 --- a/psy_maps/plotters.py +++ b/psy_maps/plotters.py @@ -634,7 +634,11 @@ def update(self, value): else: self.clat = value if value is None: - self.clat = self.lat_mean + latmin, latmax = self.lonlatbox.lonlatbox[2:] + if latmax - latmin > 170: + self.clat = 0 + else: + self.clat = self.lat_mean @docstrings.get_sectionsf('LonLatBox') From 2b030f3ceeaffae5de91e0230ffc5f2de3ba93e3 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Thu, 16 Apr 2020 20:36:55 +0200 Subject: [PATCH 4/5] add tests for clat and clon centering --- tests/test_projections.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/tests/test_projections.py b/tests/test_projections.py index 15fd056..a046cb4 100644 --- a/tests/test_projections.py +++ b/tests/test_projections.py @@ -33,8 +33,15 @@ def grid(request): @pytest.fixture -def grid_ds(grid): - return psyd.open_dataset(osp.join(bt.test_dir, 'grids', grid + '.nc')) +def open_grid_ds(): + open_datasets = [] + def _grid_ds(grid): + ds = psyd.open_dataset(osp.join(bt.test_dir, 'grids', grid + '.nc')) + open_datasets.append(ds) + return ds + yield _grid_ds + for ds in open_datasets: + ds.close() @pytest.fixture @@ -42,11 +49,31 @@ def grid_projection(grid): return projection_mapping[grid] -def test_grid_plotting(grid_ds, grid, grid_projection): +def test_grid_plotting(open_grid_ds, grid, grid_projection): + grid_ds = open_grid_ds(grid) with grid_ds.psy.plot.mapplot() as sp: assert len(sp) == 1 plotter = sp.plotters[0] assert isinstance(plotter.transform.projection, grid_projection) assert plotter.plot._kwargs.get('transform') is \ plotter.transform.projection - assert isinstance(plotter.projection.projection, grid_projection) \ No newline at end of file + assert isinstance(plotter.projection.projection, grid_projection) + + +@pytest.mark.parametrize( + "grid,clon", [("rotated_latitude_longitude", 11), + (osp.join("..", "test-t2m-u-v"), 0)]) +def test_clon_centering(open_grid_ds, grid, clon): + grid_ds = open_grid_ds(grid) + with grid_ds.psy.plot.mapplot(projection='ortho') as sp: + plotter = sp.plotters[0] + assert plotter.clon.clon == pytest.approx(clon, abs=1) + +@pytest.mark.parametrize( + "grid,clat", [("rotated_latitude_longitude", 51), + (osp.join("..", "test-t2m-u-v"), 0)]) +def test_clat_centering(open_grid_ds, grid, clat): + grid_ds = open_grid_ds(grid) + with grid_ds.psy.plot.mapplot(projection='ortho') as sp: + plotter = sp.plotters[0] + assert plotter.clat.clat == pytest.approx(clat, abs=1) From 74ddbcfb4cb0b864bf314c3c79fdff27b2ccc469 Mon Sep 17 00:00:00 2001 From: "Philipp S. Sommer" Date: Thu, 16 Apr 2020 21:55:20 +0200 Subject: [PATCH 5/5] Minor bug fix for test --- tests/test_plotters.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_plotters.py b/tests/test_plotters.py index 6515134..8017be7 100755 --- a/tests/test_plotters.py +++ b/tests/test_plotters.py @@ -437,8 +437,9 @@ def test_lonlatbox(self): sp = psy.plot.mapplot(os.path.join(bt.test_dir, 'Stockholm.nc'), name='Population', transform='moll') ax = sp.plotters[0].ax - self.assertEqual(np.round(ax.get_extent(), 2).tolist(), - [17.66, 18.39, 59.1, 59.59]) + self.assertEqual( + np.round(ax.get_extent(ccrs.PlateCarree()), 2).tolist(), + [17.66, 18.39, 59.1, 59.59]) sp.update(lonlatbox=[17.8, 18.2, 59.2, 59.4]) self.assertEqual( np.round(ax.get_extent(ccrs.PlateCarree()), 2).tolist(),