-
Notifications
You must be signed in to change notification settings - Fork 171
New grid object for structured grids #1966
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
Merged
Merged
Changes from 36 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
5659a2b
Take xgcm.Grid and adapt for our uses
VeckoTheGecko f26a308
Adapt xgcm tests
VeckoTheGecko b181643
Remove unused functions and TODOs
VeckoTheGecko 72fadde
Rename file to xgcm_datasets.py
VeckoTheGecko b2ffce7
run pre-commit
VeckoTheGecko 5e9a145
Move v4 grid files
VeckoTheGecko 899f4e3
Run pre-commit and manual fixes
VeckoTheGecko b552b03
Update reprs and remove warning
VeckoTheGecko 0bffe9e
Remove unused imports
VeckoTheGecko ca7dcfc
remove Grid._ti from old grid
VeckoTheGecko bdc7041
Add grid adapter test cases and mock implementation
VeckoTheGecko ff62de7
Define GridAdapter tdim, xdim, ydim and zdim
VeckoTheGecko f175eea
Add time to test_gridadapter dataset
VeckoTheGecko f7fa370
Add Z dim to test_gridadapter dataset
VeckoTheGecko df93007
Define GridAdapter lon, lat, depth, and time
VeckoTheGecko 82b8271
Limit scope of keyerror except blocks
VeckoTheGecko 6ef7fc6
Define GridAdapter time_origin
VeckoTheGecko 1ec179a
Update adapter to return zero array
VeckoTheGecko ec49bc0
Add _z4d to gridadapter
VeckoTheGecko 8627730
Move vendored file to vendor folder
VeckoTheGecko 86f4d38
Update test suite repr
VeckoTheGecko c6de1cd
Add curvilinear test grid datasets
VeckoTheGecko 78889e1
Update grid datasets with depth and time
VeckoTheGecko 4c56c06
Move dataset and start with new test
VeckoTheGecko 29568b9
Update test
VeckoTheGecko 70588bc
Use lon, lat, depth arrays on underlying dataset
VeckoTheGecko faf7a6c
Add grid_type property to grid adapter
VeckoTheGecko b434eaa
Rename test file
VeckoTheGecko 9fdd993
Add mesh kwarg to adapter
VeckoTheGecko 1fd9b14
Remove mesh property from adapter
VeckoTheGecko 42d3715
Remove kwargs from Grid.create_grid
VeckoTheGecko 98436c6
Update isinstance to GridType check
VeckoTheGecko d34a45c
Update grid_type on adapter to use enum
VeckoTheGecko bc0f7b0
Remove GridCode alias
VeckoTheGecko 182a22b
Add lonlat_minmax to GridAdapter
VeckoTheGecko f52ef63
review feedback
VeckoTheGecko f3475cc
Move dataset functions
VeckoTheGecko 324047f
Patch grid._gtype call and pixi run tests-notebooks
VeckoTheGecko ca24d17
remove doubled up TimeConverter.__eq__
VeckoTheGecko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| from collections import OrderedDict | ||
|
|
||
| # Representation of axis shifts | ||
| axis_shift_left = -0.5 | ||
| axis_shift_right = 0.5 | ||
| axis_shift_center = 0 | ||
| # Characterizes valid shifts only | ||
| valid_axis_shifts = [axis_shift_left, axis_shift_right, axis_shift_center] | ||
|
|
||
|
|
||
| def assert_valid_comodo(ds): | ||
| """Verify that the dataset meets comodo conventions | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ds : xarray.dataset | ||
| """ | ||
| # TODO: implement | ||
| assert True | ||
|
|
||
|
|
||
| def get_all_axes(ds): | ||
| axes = set() | ||
| for d in ds.dims: | ||
| if "axis" in ds[d].attrs: | ||
| axes.add(ds[d].attrs["axis"]) | ||
| return axes | ||
|
|
||
|
|
||
| def get_axis_coords(ds, axis_name): | ||
| """Find the name of the coordinates associated with a comodo axis. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ds : xarray.dataset or xarray.dataarray | ||
| axis_name : str | ||
| The name of the axis to find (e.g. 'X') | ||
|
|
||
| Returns | ||
| ------- | ||
| coord_name : list | ||
| The names of the coordinate matching that axis | ||
| """ | ||
| coord_names = [] | ||
| for d in ds.dims: | ||
| axis = ds[d].attrs.get("axis") | ||
| if axis == axis_name: | ||
| coord_names.append(d) | ||
| return coord_names | ||
|
|
||
|
|
||
| def get_axis_positions_and_coords(ds, axis_name): | ||
| coord_names = get_axis_coords(ds, axis_name) | ||
| ncoords = len(coord_names) | ||
| if ncoords == 0: | ||
| # didn't find anything for this axis | ||
| raise ValueError(f"Couldn't find any coordinates for axis {axis_name}") | ||
|
|
||
| # now figure out what type of coordinates these are: | ||
| # center, left, right, or outer | ||
| coords = {name: ds[name] for name in coord_names} | ||
|
|
||
| # some tortured logic for dealing with malformed c_grid_axis_shift | ||
| # attributes such as produced by old versions of xmitgcm. | ||
| # This should be a float (either -0.5 or 0.5) | ||
| # this function returns that, or True of the attribute is set to | ||
| # anything at all | ||
| def _maybe_fix_type(attr): | ||
| if attr is not None: | ||
| try: | ||
| return float(attr) | ||
| except TypeError: | ||
| return True | ||
|
|
||
| axis_shift = {name: _maybe_fix_type(coord.attrs.get("c_grid_axis_shift")) for name, coord in coords.items()} | ||
| coord_len = {name: len(coord) for name, coord in coords.items()} | ||
|
|
||
| # look for the center coord, which is required | ||
| # this list will potentially contain "center", "inner", and "outer" points | ||
| coords_without_axis_shift = {name: coord_len[name] for name, shift in axis_shift.items() if not shift} | ||
| if len(coords_without_axis_shift) == 0: | ||
| raise ValueError(f"Couldn't find a center coordinate for axis {axis_name}") | ||
| elif len(coords_without_axis_shift) > 1: | ||
| raise ValueError(f"Found two coordinates without `c_grid_axis_shift` attribute for axis {axis_name}") | ||
| center_coord_name = list(coords_without_axis_shift)[0] | ||
| # the length of the center coord is key to decoding the other coords | ||
| axis_len = coord_len[center_coord_name] | ||
|
|
||
| # now we can start filling in the information about the different coords | ||
| axis_coords = OrderedDict() | ||
| axis_coords["center"] = center_coord_name | ||
|
|
||
| # now check the other coords | ||
| coord_names.remove(center_coord_name) | ||
| for name in coord_names: | ||
| shift = axis_shift[name] | ||
| clen = coord_len[name] | ||
| if clen == axis_len + 1: | ||
| axis_coords["outer"] = name | ||
| elif clen == axis_len - 1: | ||
| axis_coords["inner"] = name | ||
| elif shift == axis_shift_left: | ||
| if clen == axis_len: | ||
| axis_coords["left"] = name | ||
| else: | ||
| raise ValueError(f"Left coordinate {name} has incompatible length {clen:g} (axis_len={axis_len:g})") | ||
| elif shift == axis_shift_right: | ||
| if clen == axis_len: | ||
| axis_coords["right"] = name | ||
| else: | ||
| raise ValueError(f"Right coordinate {name} has incompatible length {clen:g} (axis_len={axis_len:g})") | ||
| else: | ||
| if shift not in valid_axis_shifts: | ||
| # string representing valid axis shifts | ||
| valids = str(valid_axis_shifts)[1:-1] | ||
|
|
||
| raise ValueError( | ||
| f"Coordinate {name} has invalid " | ||
| f"`c_grid_axis_shift` attribute `{shift!r}`. " | ||
| f"`c_grid_axis_shift` must be one of: {valids}" | ||
| ) | ||
| else: | ||
| raise ValueError(f"Coordinate {name} has missing `c_grid_axis_shift` attribute `{shift!r}`") | ||
| return axis_coords | ||
|
|
||
|
|
||
| def _assert_data_on_grid(da): | ||
| pass |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.