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

check areal weight calculation #2

Open
gnodnooh opened this issue Apr 30, 2024 · 3 comments
Open

check areal weight calculation #2

gnodnooh opened this issue Apr 30, 2024 · 3 comments

Comments

@gnodnooh
Copy link
Member

gnodnooh commented Apr 30, 2024

Hi @Shrad-Shukla1 and @Yoon-Eric

(edited on April 30)

Okay, (xr_da * aw_factor).mean(dim=[lon_name, lat_name]) calculates the areal-weighted average per grid cell. see below example.

def calc_spatial_mean(
xr_da, lon_name="longitude", lat_name="latitude", radius=EARTH_RADIUS
):
"""
Calculate spatial mean of xarray.DataArray with grid cell weighting.
Parameters
----------
xr_da: xarray.DataArray
Data to average
lon_name: str, optional
Name of x-coordinate
lat_name: str, optional
Name of y-coordinate
radius: float
Radius of the planet [metres], currently assumed spherical (not important anyway)
Returns
-------
Spatially averaged xarray.DataArray.
"""
lon = xr_da[lon_name].values
lat = xr_da[lat_name].values
area_weights = grid_cell_areas(lon, lat, radius=radius)
aw_factor = area_weights / area_weights.max()
return (xr_da * aw_factor).mean(dim=[lon_name, lat_name])

@Shrad-Shukla1
Copy link
Member

Shrad-Shukla1 commented Apr 30, 2024 via email

@gnodnooh
Copy link
Member Author

Here is an example. What we are calculating is the average precip over 9 grid cells.
image

@Shrad-Shukla1
Copy link
Member

Shrad-Shukla1 commented May 1, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants