Skip to content

Fix indexing on axis keys using generic map (related to spatial averaging) #172

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 8 commits into from
Dec 2, 2021

Conversation

tomvothecoder
Copy link
Collaborator

@tomvothecoder tomvothecoder commented Nov 29, 2021

Description

Summary of Changes

  • Add GENERIC_AXES_MAP for mapping CF compliant keys to their generic representations
  • Add _get_generic_axis_keys() for proper indexing in spatial averaging methods
  • Update bounds methods to use GENERIC_AXES_MAP
  • Update incorrect axis refs to axis

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

If applicable:

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass with my changes (locally and CI/CD build)
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have noted that this is a breaking change for a major release (fix or feature that would cause existing functionality to not work as expected) -- will include notes in next minor release instead

@tomvothecoder tomvothecoder added type: bug Inconsistencies or issues which will cause an issue or problem for users or implementors. Priority: High labels Nov 29, 2021
@tomvothecoder tomvothecoder self-assigned this Nov 29, 2021
@tomvothecoder
Copy link
Collaborator Author

tomvothecoder commented Nov 29, 2021

Hi @pochedls, this PR is ready for review.

It fixes #163, KeyError: "The data variable 'tas' is missing a 'lat' dimension, which is required for spatial averaging.". Basically, your example datasets use the CF long key "latitude" instead of the short key "lat" without a proper mapping of "lat" to "latitude". CF-xarray can't interpret "lat" as being "latitude" when retrieving the coordinates and bounds, so it fails (it finds "latitude" and "latitude " when you check the keys with ds.cf.keys()).

My solution is to use a map of CF long and short keys to their generic versions for a more robust retrieval of coordinates. For example, "lat" and "latitude" maps to "Y", which CF-xarray can interpret agnostically if the CF attribute Axis: "Y" is set.

@codecov-commenter
Copy link

codecov-commenter commented Nov 29, 2021

Codecov Report

Merging #172 (5ceb932) into main (69dd39d) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #172   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         8    +1     
  Lines          369       381   +12     
=========================================
+ Hits           369       381   +12     
Impacted Files Coverage Δ
xcdat/axis.py 100.00% <100.00%> (ø)
xcdat/bounds.py 100.00% <100.00%> (ø)
xcdat/spatial_avg.py 100.00% <100.00%> (ø)
xcdat/xcdat.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 69dd39d...5ceb932. Read the comment docs.

xcdat/bounds.py Outdated
@@ -40,7 +40,7 @@ class BoundsAccessor:
>>> lon_bounds = ds.bounds.get_bounds("lon") # or pass "longitude"
>>> time_bounds = ds.bounds.get_bounds("time")

Add coordinate bounds for a specific axis if they don't exist:
Add coordinate bounds for a specific axes if they don't exist:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be "axis" (singular)?

Copy link
Collaborator

@pochedls pochedls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Tom - this works and looks good to me. I didn't understand why you swapped axis / axes - it seemed like it may have made more sense before (looping over each axis (singular) in axes (plural)).

@tomvothecoder
Copy link
Collaborator Author

tomvothecoder commented Dec 2, 2021

Thanks Steve, I think I got carried away with my find and replace and forgot the definition of axis/axes again for the 10th time. I pushed a fix, but I'll search again just in case.

@tomvothecoder tomvothecoder force-pushed the bugfix/163-spatial-avg-lat branch from d2e5d37 to 722a014 Compare December 2, 2021 20:45
- Add GENERIC_AXIS_MAP for mapping CF compliant keys to their generic representations
- Add `_get_generic_axis_keys()` for proper indexing in spatial averaging methods
- Update bounds methods to use `GENERIC_AXIS_MAP`
@tomvothecoder tomvothecoder force-pushed the bugfix/163-spatial-avg-lat branch from ecb78f9 to 1a33507 Compare December 2, 2021 20:56
@tomvothecoder tomvothecoder changed the title Fix indexing on axes keys using generic map Fix indexing on axis keys using generic map Dec 2, 2021
@tomvothecoder tomvothecoder changed the title Fix indexing on axis keys using generic map Fix indexing on axis keys using generic map (related to spatial averaging) Dec 2, 2021
@tomvothecoder tomvothecoder merged commit 3c6f165 into main Dec 2, 2021
@tomvothecoder tomvothecoder deleted the bugfix/163-spatial-avg-lat branch December 2, 2021 23:00
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
Development

Successfully merging this pull request may close these issues.

spatial_average not interpreting latitude
3 participants