Skip to content

Commit

Permalink
Merge pull request #2050 from axnsantana/main
Browse files Browse the repository at this point in the history
Explicitly calling numpy asanyarray over the inputs.
  • Loading branch information
greglucas committed May 24, 2022
2 parents ad8a7be + f899bef commit c184ead
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/cartopy/tests/test_vector_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,35 @@ def test_with_scalar_field(self):
assert_array_almost_equal(u_grid, expected_u_grid)
assert_array_almost_equal(v_grid, expected_v_grid)
assert_array_almost_equal(s_grid, expected_s_grid)

def test_with_scalar_field_non_ndarray_data(self):
# Transform and regrid vector (with no projection transform) with an
# additional scalar field wich is not a ndarray.
expected_x_grid = np.array([[-10., -5., 0., 5., 10.],
[-10., -5., 0., 5., 10.],
[-10., -5., 0., 5., 10.]])
expected_y_grid = np.array([[5., 5., 5., 5., 5.],
[7.5, 7.5, 7.5, 7.5, 7.5],
[10., 10., 10., 10., 10]])
expected_u_grid = np.array([[np.nan, 2., 3., 2., np.nan],
[np.nan, 2.5, 3.5, 2.5, np.nan],
[2., 3., 4., 3., 2.]])
expected_v_grid = np.array([[np.nan, .8, .3, .8, np.nan],
[np.nan, 2.675, 2.15, 2.675, np.nan],
[5.5, 4.75, 4., 4.75, 5.5]])
expected_s_grid = np.array([[np.nan, 2., 3., 2., np.nan],
[np.nan, 2.5, 3.5, 2.5, np.nan],
[2., 3., 4., 3., 2.]])

src_crs = target_crs = ccrs.PlateCarree()
x_grid, y_grid, u_grid, v_grid, s_grid = \
vec_trans.vector_scalar_to_grid(src_crs, target_crs, (5, 3),
list(self.x), list(self.y),
list(self.u), list(self.v),
list(self.s))

assert_array_equal(x_grid, expected_x_grid)
assert_array_equal(y_grid, expected_y_grid)
assert_array_almost_equal(u_grid, expected_u_grid)
assert_array_almost_equal(v_grid, expected_v_grid)
assert_array_almost_equal(s_grid, expected_s_grid)
9 changes: 9 additions & 0 deletions lib/cartopy/vector_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ def vector_scalar_to_grid(src_crs, target_proj, regrid_shape, x, y, u, v,
scalar fields is the same as the number that were passed in.
"""
x = np.asanyarray(x)
y = np.asanyarray(y)
u = np.asanyarray(u)
v = np.asanyarray(v)

if u.shape != v.shape:
raise ValueError('u and v must be the same shape')
if x.shape != u.shape:
Expand All @@ -117,10 +122,14 @@ def vector_scalar_to_grid(src_crs, target_proj, regrid_shape, x, y, u, v,
raise ValueError('x and y coordinates are not compatible '
'with the shape of the vector components')
if scalars:
np_like_scalars = ()
for s in scalars:
s = np.asanyarray(s)
np_like_scalars = np_like_scalars + (s,)
if s.shape != u.shape:
raise ValueError('scalar fields must have the same '
'shape as the vector components')
scalars = np_like_scalars
try:
nx, ny = regrid_shape
except TypeError:
Expand Down

0 comments on commit c184ead

Please sign in to comment.