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

[PR]: Add Z axis support for spatial averaging #606

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pochedls
Copy link
Collaborator

Description

This PR adds functionality to compute vertical averages.

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)

@github-actions github-actions bot added the type: enhancement New enhancement request label Feb 14, 2024
@pochedls
Copy link
Collaborator Author

I'd appreciate testing with real-world data (and comparisons with CDAT).

Copy link

codecov bot commented Feb 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (5de4d7f) to head (98ea3d7).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #606   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           15        15           
  Lines         1588      1595    +7     
=========================================
+ Hits          1588      1595    +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +65 to +75
xr.testing.assert_allclose(result["so"], expected)

# check that vertical averaging returns the correct weights
expected = xr.DataArray(
data=np.array([2000, 2000, 1000, 0.0]),
coords={"time": ds.time, "lev": ds.lev, "lat": ds.lat, "lon": ds.lon},
dims=["lev"],
attrs={"xcdat_bounds": True},
)

xr.testing.assert_allclose(result["lev_wts"], expected)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Are two-in-one tests permitted?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah there's no problem with that here. As long as the tests are relatively easy to maintain.

@@ -73,10 +73,12 @@ def average(
keep_weights: bool = False,
lat_bounds: Optional[RegionAxisBounds] = None,
lon_bounds: Optional[RegionAxisBounds] = None,
lev_bounds: Optional[RegionAxisBounds] = None,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think lev_bounds is a good generic name, but I'm open to other possibilities.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good to me

xcdat/spatial.py Show resolved Hide resolved
Comment on lines +512 to +537
def _get_vertical_weights(
self, domain_bounds: xr.DataArray, region_bounds: Optional[np.ndarray]
) -> xr.DataArray:
"""Gets weights for the vertical axis.

This method scales the domain to a region (if selected) and returns weights
proportional to the difference between each pair of level bounds.

Parameters
----------
domain_bounds : xr.DataArray
The array of bounds for the vertical domain.
region_bounds : Optional[np.ndarray]
The array of bounds for vertical selection.

Returns
-------
xr.DataArray
The vertical axis weights.
"""
if region_bounds is not None:
domain_bounds = self._scale_domain_to_region(domain_bounds, region_bounds)

weights = self._calculate_weights(domain_bounds)
return weights

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is sufficiently different from _get_longitude_weights (which deals with the prime meridian) and _get_latitude_weights (sine of bounds) to justify its own function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree

@tomvothecoder tomvothecoder changed the title Feature/596 vertical averaging [PR]: Add Z axis support for spatial averaging Feb 14, 2024
@tomvothecoder
Copy link
Collaborator

Tom:

  • Compare with cdms2 average with Z axis and get floating point diffs
  • Try out in e3sm_diags

@tomvothecoder
Copy link
Collaborator

I'm going to pick this back up for review. We might be able to get this in for v0.7.0.

Comment on lines +54 to +56
so = ds["so"]
so[:] = np.array([1, 2, 3, 4])
ds["so"] = so
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
so = ds["so"]
so[:] = np.array([1, 2, 3, 4])
ds["so"] = so
ds["so"].values = np.array([1, 2, 3, 4])

Assigning the numpy array directly to the DataArray will work too

@tomvothecoder
Copy link
Collaborator

I'd appreciate testing with real-world data (and comparisons with CDAT).

Can you provide me some datasets to test with? I wrote a script and tested a single E3SM dataset, which produced nearly identical results (1e-16 max rel diff).

@pochedls
Copy link
Collaborator Author

I'd appreciate testing with real-world data (and comparisons with CDAT).

Can you provide me some datasets to test with? I wrote a script and tested a single E3SM dataset, which produced nearly identical results (1e-16 max rel diff).

Do you have xsearch running? I think you could look for monthly, historical data, for the thetao or ta (Amon table) variables.

@tomvothecoder
Copy link
Collaborator

I'd appreciate testing with real-world data (and comparisons with CDAT).

Can you provide me some datasets to test with? I wrote a script and tested a single E3SM dataset, which produced nearly identical results (1e-16 max rel diff).

Do you have xsearch running? I think you could look for monthly, historical data, for the thetao or ta (Amon table) variables.

I'll check on xsearch. Thanks for the pointers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New enhancement request
Projects
Status: In Review
Development

Successfully merging this pull request may close these issues.

Add Z axis support for spatial averaging
2 participants