-
Notifications
You must be signed in to change notification settings - Fork 168
Adding unit tests #2113
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
Adding unit tests #2113
Changes from all commits
f17fc23
7744022
f4584d2
b74c8d2
170e94f
842611c
56cc4f2
978a6b4
e38b201
3e64469
a92eff8
ae38599
3f6a82e
a9be1f5
88eb065
9a27794
72b810c
01dbffa
ed9abda
33f355f
a65283f
69b1806
ee6344f
2b2adcf
c10a9a4
4969bb3
c677708
5d5e4d5
cc67d06
8517bf6
f0ef29c
e60d24d
f519d1c
f80c131
182affe
1293758
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
|
|
||
| def simple_UV_dataset(dims=(360, 2, 30, 4), maxdepth=1, mesh_type="spherical"): | ||
| max_lon = 180.0 if mesh_type == "spherical" else 1e6 | ||
|
|
||
| return xr.Dataset( | ||
| {"U": (["time", "depth", "YG", "XG"], np.zeros(dims)), "V": (["time", "depth", "YG", "XG"], np.zeros(dims))}, | ||
| coords={ | ||
| "time": (["time"], xr.date_range("2000", "2001", dims[0]), {"axis": "T"}), | ||
| "depth": (["depth"], np.linspace(0, maxdepth, dims[1]), {"axis": "Z"}), | ||
| "YC": (["YC"], np.arange(dims[2]) + 0.5, {"axis": "Y"}), | ||
| "YG": (["YG"], np.arange(dims[2]), {"axis": "Y", "c_grid_axis_shift": -0.5}), | ||
| "XC": (["XC"], np.arange(dims[3]) + 0.5, {"axis": "X"}), | ||
| "XG": (["XG"], np.arange(dims[3]), {"axis": "X", "c_grid_axis_shift": -0.5}), | ||
| "lat": (["YG"], np.linspace(-90, 90, dims[2]), {"axis": "Y", "c_grid_axis_shift": 0.5}), | ||
| "lon": (["XG"], np.linspace(-max_lon, max_lon, dims[3]), {"axis": "X", "c_grid_axis_shift": -0.5}), | ||
| }, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| from parcels import xgcm | ||
| from parcels._index_search import _search_indices_curvilinear_2d | ||
| from parcels.basegrid import BaseGrid | ||
| from parcels.tools.statuscodes import FieldOutOfBoundError, FieldOutOfBoundSurfaceError | ||
|
|
||
| _XGRID_AXES = Literal["X", "Y", "Z"] | ||
| _XGRID_AXES_ORDERING: Sequence[_XGRID_AXES] = "ZYX" | ||
|
|
@@ -271,6 +272,15 @@ def search(self, z, y, x, ei=None): | |
| ds = self.xgcm_grid._ds | ||
|
|
||
| zi, zeta = _search_1d_array(ds.depth.values, z) | ||
| if zi == -1: | ||
| if zeta < 0: | ||
| raise FieldOutOfBoundError( | ||
| f"Depth {z} is out of bounds for the grid with depth values {ds.depth.values}." | ||
| ) | ||
| elif zeta > 1: | ||
| raise FieldOutOfBoundSurfaceError( | ||
| f"Depth {z} is out of the surface for the grid with depth values {ds.depth.values}." | ||
| ) | ||
|
|
||
| if ds.lon.ndim == 1: | ||
| yi, eta = _search_1d_array(ds.lat.values, y) | ||
|
|
@@ -453,6 +463,9 @@ def _search_1d_array( | |
| float | ||
| Barycentric coordinate. | ||
| """ | ||
| # TODO v4: We probably rework this to deal with 0D arrays before this point (as we already know field dimensionality) | ||
| if len(arr) < 2: | ||
| return 0, 0.0 | ||
|
Comment on lines
+467
to
+468
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know which test required this change? (I assume something to do with 2d fields?)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, every test that had 1D depth fields. E.g.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. I'm not sure what it means to have a 1 length dimension with a coordinate for that dimension (shown in This might be something to either deal with earlier on, or be something to make a bit more explicit internally. Let's keep this for now so that it can be revisited later with refactored tests. Leaving a TODO comment.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear (also for future reference), I'm a bit concerned here that we're assuming that the 0D dimension has a coordinate assigned to it, when really it doesn't matter if it does. We need to make sure (and test) that we aren't implicitly assuming a coordinate for this dim.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, you can check the code in |
||
| i = np.argmin(arr <= x) - 1 | ||
| bcoord = (x - arr[i]) / (arr[i + 1] - arr[i]) | ||
| return i, bcoord | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.