Repository navigation
Consistent coordinates for pre-compute and 1d profile generation #50
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,7 +224,11 @@ def copy(self) -> Self: | |
|
|
||
| @njit | ||
| def point_on_vertex( | ||
| boundary_lats: np.ndarray, boundary_lons: np.ndarray, lat: float, lon: float | ||
| boundary_lats: np.ndarray, | ||
| boundary_lons: np.ndarray, | ||
| lat: float, | ||
| lon: float, | ||
| tol: float = 1e-07, | ||
| ) -> bool: | ||
| """ | ||
| Check if a point lies on a vertex of a basin boundary. | ||
|
|
@@ -239,17 +243,20 @@ def point_on_vertex( | |
| Latitude of the point to check. | ||
| lon : float | ||
| Longitude of the point to check. | ||
| tol : float | ||
| Tolerance for floating point equality test, default: 1e-07 | ||
|
|
||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| True if the point matches a boundary vertex within tolerance (1e-07), otherwise False. | ||
| True if the point matches a boundary vertex within tolerance, otherwise False. | ||
| """ | ||
| # Vectorized comparison using NumPy with tolerance | ||
| lat_matches = ( | ||
| np.abs(boundary_lats - lat) <= 1e-07 | ||
| np.abs(boundary_lats - lat) <= tol | ||
| ) # np.isclose is not supported by njit | ||
| lon_matches = np.abs(boundary_lons - lon) <= 1e-07 | ||
| lon_matches = np.abs(boundary_lons - lon) <= tol | ||
|
|
||
| matches = np.logical_and(lat_matches, lon_matches) | ||
|
|
||
|
|
@@ -335,7 +342,9 @@ def find_edge_inds( | |
|
|
||
|
|
||
| @njit | ||
| def find_corner_inds(lats: np.ndarray, lons: np.ndarray, lat: float, lon: float): | ||
| def find_corner_inds( | ||
| lats: np.ndarray, lons: np.ndarray, lat: float, lon: float, tol: float = 1e-07 | ||
| ): | ||
| """ | ||
| Find the corner indices of the given latitude and longitude arrays. | ||
|
|
||
|
|
@@ -349,6 +358,8 @@ def find_corner_inds(lats: np.ndarray, lons: np.ndarray, lat: float, lon: float) | |
| Latitude of the given point. | ||
| lon : float | ||
| Longitude of the given point. | ||
| tol : float | ||
| Tolerance for floating point equality test, default : 1e-07 | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -361,18 +372,18 @@ def find_corner_inds(lats: np.ndarray, lons: np.ndarray, lat: float, lon: float) | |
| nlat = len(lats) | ||
| nlon = len(lons) | ||
|
|
||
| if np.abs(lat - lats[0]) <= 1e-07: | ||
| if np.abs(lat - lats[0]) <= tol: | ||
| corner_lat_ind = 0 | ||
| elif np.abs(lat - lats[-1]) <= 1e-07: | ||
| elif np.abs(lat - lats[-1]) <= tol: | ||
| corner_lat_ind = nlat - 1 | ||
| else: | ||
| raise ValueError( | ||
| f"Point lies outside of surface bounds. Lat {lat} outside [{lats[0]} {lats[-1]}]" | ||
| ) | ||
|
|
||
| if np.abs(lon - lons[0]) <= 1e-07: | ||
| if np.abs(lon - lons[0]) <= tol: | ||
| corner_lon_ind = 0 | ||
| elif np.abs(lon - lons[-1]) <= 1e-07: | ||
| elif np.abs(lon - lons[-1]) <= tol: | ||
| corner_lon_ind = nlon - 1 | ||
|
Comment on lines
-364
to
387
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. Similarly here you should use |
||
| else: | ||
| raise ValueError( | ||
|
|
@@ -384,7 +395,7 @@ def find_corner_inds(lats: np.ndarray, lons: np.ndarray, lat: float, lon: float) | |
|
|
||
| @njit | ||
| def find_basin_adjacent_points_numba( | ||
| lats: np.ndarray, lons: np.ndarray, lat: float, lon: float | ||
| lats: np.ndarray, lons: np.ndarray, lat: float, lon: float, tol: float = 1e-07 | ||
| ) -> tuple[bool, np.ndarray, np.ndarray]: | ||
| """ | ||
| Identify adjacent latitude/longitude indices for a given point in basin coordinates. | ||
|
|
@@ -399,6 +410,9 @@ def find_basin_adjacent_points_numba( | |
| Target latitude for adjacency checks. | ||
| lon : float | ||
| Target longitude for adjacency checks. | ||
| tol : float | ||
| Tolerance for floating point equality test, default: 1e-7 | ||
|
|
||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -424,10 +438,11 @@ def find_basin_adjacent_points_numba( | |
| if 0 < lat_idx < len(lats): | ||
| lat_ind[0] = lat_idx - 1 | ||
| lat_ind[1] = lat_idx | ||
| elif lat_idx == 0 and lats[0] == lat: | ||
|
|
||
| elif lat_idx == 0 and np.abs(lats[0] - lat) <= tol: | ||
| lat_ind[0] = 0 | ||
| lat_ind[1] = 1 | ||
| elif lat_idx == len(lats) and lats[-1] == lat: | ||
| elif lat_idx == len(lats) and np.abs(lats[-1] - lat) <= tol: | ||
| lat_ind[0] = len(lats) - 1 | ||
| lat_ind[1] = len(lats) | ||
|
|
||
|
|
@@ -441,10 +456,10 @@ def find_basin_adjacent_points_numba( | |
| if 0 < lon_idx < len(lons): | ||
| lon_ind[0] = lon_idx - 1 | ||
| lon_ind[1] = lon_idx | ||
| elif lon_idx == 0 and lons[0] == lon: | ||
| elif lon_idx == 0 and np.abs(lons[0] - lon) <= tol: | ||
| lon_ind[0] = 0 | ||
| lon_ind[1] = 1 | ||
| elif lon_idx == len(lons) and lons[-1] == lon: | ||
| elif lon_idx == len(lons) and np.abs(lons[-1] - lon) <= tol: | ||
| lon_ind[0] = len(lons) - 1 | ||
| lon_ind[1] = len(lons) | ||
|
|
||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just
np.isclose. bothnp.iscloseandnp.allclosehave been supported by numba since four years ago, also here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lispandfound Thanks for the update regarding np.isclose() support in Numba—I wasn't aware it was supported now.However, after reviewing the documentation, I prefer to stick with np.abs(a - b) <= tol for this specific case.
np.isclose uses a relative tolerance by default (rtol=1e-05)
and internally checks
For geographic coordinates, we need a fixed spatial tolerance (e.g., 1e-7 degrees) regardless of the coordinate magnitude (e.g., relative error at 170° is much larger than at 1°). To achieve this with
np.isclose(), I would have to explicitly disable the relative toleranceThis is more verbose, and less readable than the direct comparision.. Also inside
@njit, direct subtraction is the most efficient primitive operation avoiding the extra overheads ofnp.isclose()(eg. internal handing of NaN, infinites etc).https://numpy.org/doc/stable/reference/generated/numpy.isclose.html
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my observation, I even feel like to replace the 2 uses of "np.isclose()" with
np.abs() <=tolLine 1032-1040
Perhaps this is not critical as it is comparing with 0, and
np.isclose(x,0)isabs(x-0) <= atol + rtol*0, effectively `abs(x)<=atol (which is 1e-8)But for the consistency, I feel like to go ahead. Thought? @lispandfound