-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add function to swap dataset longitude axis orientation #145
Conversation
TODO @pochedls: add comments from meeting about covering some axis orientation edge cases and tag Jiwoo. |
Codecov Report
@@ Coverage Diff @@
## main #145 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 8 8
Lines 381 481 +100
==========================================
+ Hits 381 481 +100
Continue to review full report at Codecov.
|
4a9c54a
to
6508ac2
Compare
@tomvothecoder and @lee1043 - I've noted the CDAT behavior here. This has some advantages. If you purely convert the bounds to 0-360 units, then a bound that spans the prime meridian has a range that may be unexpected (e.g., [-1, 1] becomes [359, 1]). This has created headaches for spatial averaging (since the weight in each dimension scales as the difference between the bounds; |
6508ac2
to
614171c
Compare
We can pull some similar code from #152 to handle this edge case at the Dataset level |
I was thinking the same, they are useful as general purpose functionality. :) |
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 functionality that I have not used, but I suspect that users would want one change: a reordering of the longitude axis after the longitude units are converted. I suspect that a lot of what you would want to do might be complicated if you go from [-180, -179, ..., -1, 0, 1, ..., 179]
to [180, 181, ..., 359, 0, 1, ..., 179]
(e.g., plotting). There would ideally be a third step that also reorders the longitude axis (and the corresponding dataarrays and bounds) to [0, 1, ..., 179, 180, 181, ..., 359]
.
Bounds are a special issue. The example above would have bounds of (notice the weird first bound):
lon_bnds = [[359.5, 0.5],
[0.5, 1.5],
...,
[178.5, 179.5],
[179.5, 180.5],
[180.5, 190.5],
...,
[358.5, 359.5]]
CDAT breaks this first bound up making the longitude axis go from length 360 to 361 (see added/modified values marked with asterisks): [0, 1, ..., 179, 180, 181, ..., 359, *360*]
and
lon_bnds = [*[0, 0.5]*,
[0.5, 1.5],
...,
[178.5, 179.5],
[179.5, 180.5],
[180.5, 190.5],
...,
[358.5, 359.5],
*[359.5, 360.5]*]
This is what is done in #152.
The corresponding data array (e.g., tas
) would simply repeat the values for the first and last longitude. I think CDAT might actually modify the first and last longitude coordinate value, too (e.g., [*0.5*, 1, ..., 179, 180, 181, ..., 359, *359.5*]
).
@lee1043 - could you comment here on this PR (and let me know if you agree with this comment).
614171c
to
d214a74
Compare
d8e1094
to
23d0d3a
Compare
c491e90
to
da94939
Compare
Hey @pochedls this PR is ready for review. @lee1043 and @chengzhuzhang, if you want to test out this feature that'd be nice too. After checking out the branch, there are two ways to test it:
|
@tomvothecoder it is great that this capability has been added, thanks! I like that there are 2 ways of using it. I have mostly used above way 1 in PMP, but have noticed that in some case I needed above way 2 to consider different areas with one open file (e.g., one area 60 to 120, another -30 to 30). Is there any chance that parameters for |
I haven't reviewed this code yet, but the syntax looks fine for my use cases. @lee1043 is suggesting we should do a bit more to orient and subset in the same function call. An even more advanced version of this was suggested here - it was noted we should even be able to "tile" the grid (e.g., -180 to 360 which would repeat part of the grid). @tomvothecoder - should this all be lumped together or should the subsetting be a future feature? |
@pochedls thanks for your comment. I was not thinking that to parameter should be more flexible like [-180, 360] or any number e.g., [20, 270] or something. I was simply thinking |
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.
LGTM
b671abb
to
43338ed
Compare
- Add option to swap longitude axis orientation in `open_dataset()` and `open_mfdataset()` - Add `swap_lon_axes()` to `__init__.py` Update ref to `_get_coords()` Move `swap_lon_axis()` to `spatial_avg.py` to `axis.py` - Add tests for `swap_lon_axis()` and prime meridian cells - Add `swap_lon_axis_to` kwarg to `open_dataset()` and `open_mfdataset()` Move `_align_longitude_to_360_axis` to `axis.py` - Move related tests to `test_axis.py` - Rename kwarg `swap_lon_axis_to` to `lon_orient` Add `_align_lon_to_360()` to handle prime meridian - Add option for sorting ascending in `swap_lon_axis()` - Extract helper function `_get_prime_meridian_index()` from `_align_lon_bounds_to_360()` - Update `_get_longitude_weights()` to use `_get_prime_meridian_index()` Update docstrings on axis orientations Update `lon_orient` and `to` kwargs to tuple - This allows for extension of the swapping feature for subsets
43338ed
to
51f6cc4
Compare
Description
This PR adds the ability to swap the axis orientation of longitude coordinates and bounds in the Dataset, either upon opening via
xcdat.open_dataset
/xcdat.open_mfdataset
or as an independent function call. It also handles the prime meridian cell when converting from the 180 axis orientation to 360 by splitting it into east and west parts (reuses_align_lon_bounds_to_360()
from the spatial averaging feature).Summary of Changes
swap_lon_axis()
toaxis.py
lon_orient
kwarg toopen_dataset()
andopen_mfdataset()
for specifying orientation upon opening a Dataset_align_lon_to_360()
to handle prime meridian cell for the entire Dataset_align_lon_bounds_to_360()
fromspatial_avg.py
toaxis.py
_get_prime_meridian_index()
from_align_lon_bounds_to_360()
_get_longitude_weights()
to use_get_prime_meridian_index()
Checklist
If applicable: