Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to return 1D arrays in grid_coordinates #388

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion verde/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def grid_coordinates(
adjust="spacing",
pixel_register=False,
extra_coords=None,
meshgrid=True,
):
"""
Generate the coordinates for each point on a regular grid.
Expand Down Expand Up @@ -235,6 +236,14 @@ def grid_coordinates(
contain a constant value. Will generate an extra array per value given
in *extra_coords*. Use this to generate arrays of constant heights or
times, for example, that might be needed to evaluate a gridder.
meshgrid : bool
If True, will call :func:`numpy.meshgrid` on the generated coordinates
and return 2D arrays (useful if you need the coordinate values for
every single point on a grid). Otherwise, will return 1D coordinate
arrays (useful if you're making a :class:`xarray.DataArray` or looping
over grid coordinates with two ``for`` loops). Passing False to
*meshgrid* is **incompatible with *extra_coords* and an exception will
be raised** if used together (:class:`ValueError`).

Returns
-------
Expand Down Expand Up @@ -284,6 +293,18 @@ def grid_coordinates(
[ 7.5 7.5 7.5]
[10. 10. 10. ]]

If you don't need the 2D arrays, then use ``meshgrid=False``:

>>> east, north = grid_coordinates(
... region=(0, 5, 0, 10), spacing=2.5, meshgrid=False,
... )
>>> print(east.shape, north.shape)
(3,) (5,)
>>> print(east)
[0. 2.5 5. ]
>>> print(north)
[ 0. 2.5 5. 7.5 10. ]

The spacing can be different for northing and easting, respectively:

>>> east, north = grid_coordinates(region=(-5, 1, 0, 10), spacing=(2.5, 1))
Expand Down Expand Up @@ -444,8 +465,15 @@ def grid_coordinates(
if pixel_register:
east_lines = east_lines[:-1] + (east_lines[1] - east_lines[0]) / 2
north_lines = north_lines[:-1] + (north_lines[1] - north_lines[0]) / 2
coordinates = list(np.meshgrid(east_lines, north_lines))
if meshgrid:
coordinates = list(np.meshgrid(east_lines, north_lines))
else:
coordinates = (east_lines, north_lines)
if extra_coords is not None:
if not meshgrid:
raise ValueError(
"Using 'meshgrid=False' and 'extra_coords' is 'verde.grid_coordinates' is not allowed."
)
for value in np.atleast_1d(extra_coords):
coordinates.append(np.ones_like(coordinates[0]) * value)
return tuple(coordinates)
Expand Down
8 changes: 8 additions & 0 deletions verde/tests/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,11 @@ def test_invalid_geographic_coordinates():
latitude[0] = 100
with pytest.raises(ValueError):
longitude_continuity([longitude, latitude], region)


def test_meshgrid_extra_coords_error():
"Should raise an exception if meshgrid=False and extra_coords are used"
with pytest.raises(ValueError):
grid_coordinates(
region=(0, 1, 0, 3), spacing=0.1, meshgrid=False, extra_coords=10
)