Skip to content

Commit

Permalink
[semver:patch] Fix density and lonlatbox in mapvector (#45)
Browse files Browse the repository at this point in the history
Merge pull request #45 from psyplot/develop
  • Loading branch information
Chilipp authored Apr 15, 2022
2 parents d065d58 + 5e0cec7 commit 0479a55
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
psyplot: psyplot/psyplot-ci-orb@1.5.31
psyplot: psyplot/psyplot-ci-orb@1.5.32
mattermost-plugin-notify: nathanaelhoun/mattermost-plugin-notify@1.2.0

executors:
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
V1.4.2
======
Fix mapvector issue

Fixed
-----
- This PR fixes an issue that arises when using ``density`` together with
``lonlatbox``, see `#43 <https://github.com/psyplot/psy-maps/issues/43>`__
and `#44 <https://github.com/psyplot/psy-maps/pull/44>`__

v1.4.1
======
Fix projection issues
Expand Down
36 changes: 26 additions & 10 deletions psy_maps/plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ class ProjectionBase(Formatoption):
#'vertical_perspective', # not available for cartopy
]

def transform_lonlatbox(self, value):
"""Transform a lon-lat-box to the specific projection"""
value = np.asarray(value)
transformed = self.projection.transform_points(
ccrs.PlateCarree(), value[:2], value[2:]
)[..., :2]
value[:2] = transformed[..., 0]
value[2:] = transformed[..., 1]
if value[0] == value[1] and isinstance(
self.projection, ccrs.PlateCarree
):
value[1] += 360
return value

@property
def cf_projection(self):
data = next(self.iter_data)
Expand Down Expand Up @@ -514,12 +528,17 @@ class Projection(ProjectionBase):

dependencies = ['clon', 'clat']

connections = ['transform']
connections = ['transform', 'lonlatbox']

def __init__(self, *args, **kwargs):
super(Projection, self).__init__(*args, **kwargs)
self.projection = None

@property
def lonlatbox_transformed(self):
"""Transform the lonlatbox according to the projection"""
return self.transform_lonlatbox(self.lonlatbox.lonlatbox)

def initialize_plot(self, value, clear=True):
"""Initialize the plot and set the projection for the axes
"""
Expand Down Expand Up @@ -715,15 +734,7 @@ class LonLatBox(BoxBase):

@property
def lonlatbox_transformed(self):
value = np.asarray(self.lonlatbox)
transformed = self.transform.projection.transform_points(
ccrs.PlateCarree(), value[:2], value[2:])[..., :2]
value[:2] = transformed[..., 0]
value[2:] = transformed[..., 1]
if value[0] == value[1] and isinstance(self.transform.projection,
ccrs.PlateCarree):
value[1] += 360
return value
return self.transform.transform_lonlatbox(self.lonlatbox)

def data_dependent(self, data, set_data=True):
if isinstance(data, InteractiveList):
Expand Down Expand Up @@ -1927,9 +1938,12 @@ class MapDensity(psyps.Density):
--------------
%(Density.possible_types)s"""

dependencies = psyps.Density.dependencies + ["projection"]

def _set_quiver_density(self, value):
if all(val == 1.0 for val in value):
self.plot._kwargs.pop('regrid_shape', None)
self.plot._kwargs.pop('target_extent', None)
elif self.decoder.is_unstructured(self.raw_data):
warnings.warn("Quiver plot of unstructered data does not support "
"the density keyword!", RuntimeWarning)
Expand All @@ -1940,6 +1954,8 @@ def _set_quiver_density(self, value):
shape = self.data.shape[-2:]
value = map(int, [value[0]*shape[0], value[1]*shape[1]])
self.plot._kwargs['regrid_shape'] = tuple(value)
lonlatbox = self.projection.lonlatbox_transformed
self.plot._kwargs["target_extent"] = lonlatbox

def _unset_quiver_density(self):
self.plot._kwargs.pop('regrid_shape', None)
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#
# You should have received a copy of the GNU LGPL-3.0 license
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import pytest
import os.path as osp

try:
# make sure we import QtWebEngineWidgets at the start
Expand All @@ -40,3 +42,8 @@ def pytest_configure(config):
if config.getoption('ref'):
import unittest
unittest.TestLoader.testMethodPrefix = 'ref'


@pytest.fixture
def regular_test_file():
return osp.join(osp.dirname(__file__), "test-t2m-u-v.nc")
15 changes: 15 additions & 0 deletions tests/test_plotters_vectorplotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,20 @@ class VectorPlotterTest2D(bt.TestBase2D, VectorPlotterTest):
var = ['u_2d', 'v_2d']


def test_density_with_lonlatbox(regular_test_file):
"""Test that the lonlatbox still works with density.
see https://github.com/psyplot/psy-maps/issues/43
"""
sp = psy.plot.mapvector(
regular_test_file, name=[["u", "v"]], density=0.5, lonlatbox="Europe"
)
ax = sp.plotters[0].ax
xmin, xmax, ymin, ymax = ax.get_extent()
assert xmax - xmin < 180
assert ymax - ymin < 90



if __name__ == '__main__':
unittest.main()

0 comments on commit 0479a55

Please sign in to comment.