Skip to content

Commit 7944487

Browse files
authored
Allow make_xarray_grid to get data as None (#318)
Used to generate a `xr.Dataset` without any `data_vars` array, containing only the coordinates. Will be useful in some cases to define an `xr.Dataset` with coordinates only and add the `data_vars` afterwards.
1 parent ec1fc12 commit 7944487

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

verde/tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ def test_make_xarray_grid_multiple_data():
145145
assert dataset["data_{}".format(i)].shape == (5, 6)
146146

147147

148+
def test_make_xarray_grid_no_data():
149+
"""
150+
Check if the function creates a xarray.Dataset with no data
151+
"""
152+
region = (-10, -5, 6, 10)
153+
spacing = 1
154+
coordinates = grid_coordinates(region, spacing=spacing)
155+
dataset = make_xarray_grid(coordinates, data=None, data_names=None)
156+
# Check if no data is present in the grid
157+
assert len(dataset.data_vars) == 0
158+
# Check if coordinates are in the grid
159+
npt.assert_allclose(dataset.easting, [-10, -9, -8, -7, -6, -5])
160+
npt.assert_allclose(dataset.northing, [6, 7, 8, 9, 10])
161+
162+
148163
def test_make_xarray_grid_extra_coords():
149164
"""
150165
Check if xarray.Dataset with extra coords is correctly created

verde/utils.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,24 @@ def make_xarray_grid(
251251
vertical, etc. All arrays must be 2d and need to have the same *shape*.
252252
These coordinates can be generated through
253253
:func:`verde.grid_coordinates`.
254-
data : array or tuple of arrays
254+
data : array, tuple of arrays or None
255255
Array or tuple of arrays with data values on each point in the grid.
256256
Each array must contain values for a dimension in the same order as
257257
the coordinates. All arrays need to have the same *shape*.
258+
If None, the :class:`xarray.Dataset` will not have any ``data_var``
259+
array.
258260
data_names : str or list
259261
The name(s) of the data variables in the output grid.
260-
dims : list
262+
Ignored if ``data`` is None.
263+
dims : list (optional)
261264
The names of the northing and easting data dimensions, respectively,
262265
in the output grid. Must be defined in the following order: northing
263266
dimension, easting dimension.
264267
**NOTE: This is an exception to the "easting" then
265268
"northing" pattern but is required for compatibility with xarray.**
266269
The easting and northing coordinates in the :class:`xarray.Dataset`
267270
will have the same names as the passed dimensions.
268-
extra_coords_names : str or list
271+
extra_coords_names : str or list (optional)
269272
Name or list of names for any additional coordinates besides the
270273
easting and northing ones. Ignored if coordinates has
271274
only two elements. The extra coordinates are non-index coordinates of
@@ -316,6 +319,26 @@ def make_xarray_grid(
316319
Data variables:
317320
dummy (northing, easting) float64 1.0 1.0 1.0 1.0 1.0 1.0
318321
322+
>>> # Create a grid containing only coordinates and no data
323+
>>> coordinates = vd.grid_coordinates(
324+
... (-10, -6, 8, 10), spacing=2, extra_coords=-7
325+
... )
326+
>>> grid = make_xarray_grid(
327+
... coordinates,
328+
... data=None,
329+
... data_names=None,
330+
... extra_coords_names="upward",
331+
... )
332+
>>> print(grid)
333+
<xarray.Dataset>
334+
Dimensions: (easting: 3, northing: 2)
335+
Coordinates:
336+
* easting (easting) float64 -10.0 -8.0 -6.0
337+
* northing (northing) float64 8.0 10.0
338+
upward (northing, easting) float64 -7.0 -7.0 -7.0 -7.0 -7.0 -7.0
339+
Data variables:
340+
*empty*
341+
319342
"""
320343
# Check dimensions of the horizontal coordinates of the regular grid
321344
ndim = np.ndim(coordinates[0])
@@ -328,8 +351,6 @@ def make_xarray_grid(
328351
# Convert 2d horizontal coordinates to 1d arrays if needed
329352
if ndim == 2:
330353
coordinates = meshgrid_to_1d(coordinates)
331-
data = check_data(data)
332-
data_names = check_data_names(data, data_names)
333354
# dims is like shape with order (rows, cols) for the array
334355
# so the first element is northing and second is easting
335356
coords = {dims[1]: coordinates[0], dims[0]: coordinates[1]}
@@ -339,7 +360,13 @@ def make_xarray_grid(
339360
extra_coords_names = check_extra_coords_names(coordinates, extra_coords_names)
340361
for name, extra_coord in zip(extra_coords_names, coordinates[2:]):
341362
coords[name] = (dims, extra_coord)
342-
data_vars = {name: (dims, value) for name, value in zip(data_names, data)}
363+
# Initialize data_vars as None. If data is not None, build data_vars as
364+
# a dirctionary to be passed to xr.Dataset constructor.
365+
data_vars = None
366+
if data is not None:
367+
data = check_data(data)
368+
data_names = check_data_names(data, data_names)
369+
data_vars = {name: (dims, value) for name, value in zip(data_names, data)}
343370
return xr.Dataset(data_vars, coords)
344371

345372

0 commit comments

Comments
 (0)