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

Refactor Spatial Averaging: More Robust Handling of Longitude Spanning Prime Meridian #150

Closed
pochedls opened this issue Nov 12, 2021 · 0 comments · Fixed by #152
Closed
Assignees
Labels
type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors.

Comments

@pochedls
Copy link
Collaborator

pochedls commented Nov 12, 2021

What versions of software are you using?

  • Package Version: CDAT v0.1.0

What are the steps to reproduce this issue?

fn = '/p/css03/esgf_publish/CMIP6/CMIP/CSIRO/ACCESS-ESM1-5/historical/r10i1p1f1/Amon/tas/gn/v20200605/tas_Amon_ACCESS-ESM1-5_historical_r10i1p1f1_gn_185001-201412.nc'
ds = xcdat.open_dataset(fn)
tsa = ds.xcdat.spatial_avg(lat_bounds=(-30, 30), lon_bounds=(0, 360)).tas.load()
print(tsa[0].values)

297.44541842495704

But the correct value is: 297.150202135895 (Difference of 0.31 K)

I think this can be traced to this line: domain_bounds should be dom_bounds in this case.

This will fix the issue in this case, but I realize there are specific situations that will give an (slightly) incorrect solution when the specified regional bounds are within a grid cell that spans across the prime meridian (e.g., -1 to 1).

Any other comments?

This code should be refactored to more robustly deal with averaging along the longitude axis (it needs to robustly handle domain bounds and regional selections that span the prime meridian). An approach I would like to try is to:

  1. Convert longitude axes to 0 to 360 domain
  2. If a grid cell boundary crosses the prime meridian, concatenate a version of that grid cell spanning 0 and 360 (e.g., [-1, 1] and [359, 361])
  3. Limit the bounds of each to within 0 to 360 (e.g., [0, 1] and [359, 360]) so that the domain is the same, but now represented by two grid cells
  4. Calculate weights based on this new dimension bounds array
  5. Add the weight from each of the repeated grid cells together (so that the weight array is the length of the longitude dimension)
@pochedls pochedls added the type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors. label Nov 12, 2021
@pochedls pochedls self-assigned this Nov 12, 2021
pochedls added a commit that referenced this issue Nov 12, 2021
pochedls added a commit that referenced this issue Nov 23, 2021
…nning prime meridian (#152)

* Initial attempt at #150

* update formatting / lintering / style

* add test to address missing coverage

* Refactor `_get_weights()` into multiple methods
- New methods include `_get_longitude_weights()` and `_get_latitude_weights()`

* Update docstring

* Add `.copy()` in dict

* Refactor `_align_longitude_to_360_axis()`
- Update variable names for prime meridian index

* Add tests for latest methods
- Methods include `_get_longitude_weights()`, `_get_latitude_weights()`, and `_calculate_weights()`

* Fix comments and docstrings

* Update `_get_weights()` to get weights for `axis` arg
- Fix incorrect axes refs to axis

* Update default val of `weights` to `"generate"`

* Update docstring for `weights` kwarg

* Refactor `_get_weights()` into multiple methods (#154)

- New methods include `_get_longitude_weights()`, `_get_latitude_weights()`, and `_calculate_weights()`
- Add `.copy()` for bounds dict

Refactor `_align_longitude_to_360_axis()`
- Update variable names for prime meridian index

Update `_get_weights()` to get weights for `axis` arg

Fix incorrect 'axes' refs to 'axis'

Update default val of `weights` to `"generate"`

* Fix pre-commit issues

* Apply suggestions from code review

Co-authored-by: Tom Vo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors.
Projects
None yet
1 participant