html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/3937#issuecomment-1233445643,https://api.github.com/repos/pydata/xarray/issues/3937,1233445643,IC_kwDOAMm_X85JhOML,14314623,2022-08-31T21:36:51Z,2022-08-31T21:36:51Z,CONTRIBUTOR,"I am interested in the coarsen with weights scenario that @dcherian and @mathause described here for a current project of ours.
I solved the issue manually and its not that hard
```python
import xarray as xr
import numpy as np
# example data with weights
data = np.arange(16).reshape(4,4).astype(float)
# add some nans
data[2,2] = np.nan
data[1,1] = np.nan
# create some simple weights
weights = np.repeat(np.array([[1,2,1,3]]).T, 4, axis=1)
weights
da = xr.DataArray(data, dims=['x', 'y'], coords={'w':(['x','y'], weights)})
da
```
```python
masked_weights = da.w.where(~np.isnan(da)) # .weighted() already knows how to do this
da_weighted = da * masked_weights
da_coarse = da_weighted.coarsen(x=2, y=2).sum() / masked_weights.coarsen(x=2, y=2).sum()
da_coarse
```
but I feel all of this is duplicating existing functionality ([e.g. the masking of weights based on nans in the data](https://github.com/pydata/xarray/blob/b5207aa0bf2f3297d7c34d6daf29c9ea9dcefdde/xarray/core/weighted.py#L234)) and might be sensibly streamlined into something like:
```python
da.weighted(da.w).coarsen(...).mean()
```
at least from a user perspective (there might be unique challenges with the implementation that I am overlooking here).
Happy to help but would definitely need some guidance on this one.
I do believe that this would provide a very useful functionality for many folks who work with curvilinear grids and want to prototype things that depend on some sort of scale reduction (coarsening).
Also cc'ing @TomNicholas who is involved in the same project 🤗","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594669577
https://github.com/pydata/xarray/issues/3937#issuecomment-874796048,https://api.github.com/repos/pydata/xarray/issues/3937,874796048,MDEyOklzc3VlQ29tbWVudDg3NDc5NjA0OA==,6815953,2021-07-06T14:11:52Z,2021-07-06T14:11:52Z,NONE,"Would
```python
da.weighted(weights).integrate(...)
```
be in scope as well?","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594669577
https://github.com/pydata/xarray/issues/3937#issuecomment-656060893,https://api.github.com/repos/pydata/xarray/issues/3937,656060893,MDEyOklzc3VlQ29tbWVudDY1NjA2MDg5Mw==,10194086,2020-07-09T11:00:46Z,2020-07-09T11:00:46Z,MEMBER,"No that won't work. You need to mask the weights where the data is NaN. An untested and not very efficient way may be:
```python
def coarsen_weighted_mean(da, weights, dims, skipna=None, boundary=""exact""):
weighted_sum = (da * weights).coarsen(dims, boundary=boundary).sum(skipna=skipna)
masked_weights = weights.where(da.notnull())
sum_of_weights = masked_weights.coarsen(dims, boundary=boundary).sum()
valid_weights = sum_of_weights != 0
sum_of_weights = sum_of_weights.where(valid_weights)
return weighted_sum / sum_of_weights
```
An example (without NaNs though):
```python
import xarray as xr
import numpy as np
air = xr.tutorial.open_dataset(""air_temperature"").air
weights = np.cos(np.deg2rad(air.lat))
# we need to rename them from ""lat""
weights.name = ""weights""
c_w_m = coarsen_weighted_mean(air, weights, dict(lat=2), boundary=""trim"")
# to compare do it for one slice
alt = air.isel(lat=slice(0, 2)).weighted(weights).mean(""lat"")
# compare if it is the same
xr.testing.assert_allclose(c_w_m.isel(lat=0, drop=True), alt)
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594669577
https://github.com/pydata/xarray/issues/3937#issuecomment-656035267,https://api.github.com/repos/pydata/xarray/issues/3937,656035267,MDEyOklzc3VlQ29tbWVudDY1NjAzNTI2Nw==,8934026,2020-07-09T10:02:46Z,2020-07-09T10:02:46Z,NONE,@mathause Does the `skipna=True` flag not work in your last example?,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594669577
https://github.com/pydata/xarray/issues/3937#issuecomment-655983882,https://api.github.com/repos/pydata/xarray/issues/3937,655983882,MDEyOklzc3VlQ29tbWVudDY1NTk4Mzg4Mg==,10194086,2020-07-09T08:21:39Z,2020-07-09T08:21:39Z,MEMBER,"That's currently not possible. What you can try is the following:
```
(ds * coslat_weights).coarsen(lat=2, lon=2).sum() / coslat_weights.coarsen(lat=2, lon=2).sum()
```
but this only works if you don't have any NaNs.","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594669577
https://github.com/pydata/xarray/issues/3937#issuecomment-655726721,https://api.github.com/repos/pydata/xarray/issues/3937,655726721,MDEyOklzc3VlQ29tbWVudDY1NTcyNjcyMQ==,2448579,2020-07-08T20:01:59Z,2020-07-08T20:01:59Z,MEMBER,"@ahuang11 in #4210:
I want to do something similar as xesmf's weighted regridding, but without the need to install esmpy which has a lot of dependencies.
Are variations of the following possible?
```
ds.weighted(coslat_weights).coarsen(lat=2, lon=2).mean()
```
```
ds.coarsen(lat=2, lon=2).weighted(coslat_weights).mean()
```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594669577
https://github.com/pydata/xarray/issues/3937#issuecomment-611323242,https://api.github.com/repos/pydata/xarray/issues/3937,611323242,MDEyOklzc3VlQ29tbWVudDYxMTMyMzI0Mg==,1217238,2020-04-09T04:36:21Z,2020-04-09T04:36:21Z,MEMBER,"I think `da.groupby(...).weighted(weights).mean()` would be pretty sensible.
We could probably make both `da.groupby(...).weighted(weights)` and `da.weighted(weights).groupby(...)` return exactly equivalent objects, if desired.","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,594669577