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

Fix dropping non-dimensional coordinates in grid_to_table #441

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Changes from 4 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
18 changes: 17 additions & 1 deletion verde/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,18 @@ def grid_to_table(grid):
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
>>> print(table.wind_speed.values)
[20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]

>>> # Non-dimensional coordinates are also handled
>>> temperature = xr.DataArray(
... np.arange(20).reshape((4, 5)),
... coords=(np.arange(4), np.arange(5, 10)),
... dims=['northing', 'easting']
... )
>>> temperature = temperature.assign_coords(upward=(("northing", "easting"), np.arange(20).reshape((4, 5))))
>>> table = grid_to_table(temperature)
leouieda marked this conversation as resolved.
Show resolved Hide resolved
>>> list(sorted(table.columns))
['easting', 'northing', 'scalars', 'upward']
>>> print(table.upward.values)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
"""
if hasattr(grid, "data_vars"):
# It's a Dataset
Expand All @@ -637,6 +648,11 @@ def grid_to_table(grid):
# Need to flip the coordinates because the names are in northing and
# easting order
coordinates = [i.ravel() for i in np.meshgrid(east, north)][::-1]
# Identify and add extra coordinates
extra = [coord for coord in grid.coords.keys() if coord not in coordinate_names]
for coord in extra:
coordinates.append(grid[coord].values.ravel())
coordinate_names.append(coord)
data_dict = dict(zip(coordinate_names, coordinates))
data_dict.update(dict(zip(data_names, data_arrays)))
return pd.DataFrame(data_dict)
Expand Down
Loading