Skip to content
This repository was archived by the owner on Mar 6, 2023. It is now read-only.
This repository was archived by the owner on Mar 6, 2023. It is now read-only.

aggregate_spatial calls reducer separately on spatial dimensions #180

@clausmichele

Description

@clausmichele

The reducer operation applied to the data of each polygon in aggregate_spatial is applied separately on the two spatial dimensions, which is wrong.

xr_crop_list_reduced = reducer(reducer(xr_crop_list, dimension="x"), dimension="y")

Here's a simple example showing for instance the std computation, which gives different results if not computed on the whole data:

import xarray as xr
import numpy as np

np.random.seed(0)
temperature = 15 + 8 * np.random.randn(2, 2)
lon = [[-99.83, -99.32], [-99.79, -99.23]]
lat = [[42.25, 42.21], [42.63, 42.59]]

da_float = xr.DataArray(
    data=temperature,
    dims=["x", "y"],
    coords=dict(
        lon=(["x", "y"], lon),
        lat=(["x", "y"], lat)
    )
)

print(da_float)
print(da_float.std(dim='x').std(dim='y'))
print(da_float.std(dim=['x','y']))

<xarray.DataArray (x: 2, y: 2)>
array([[29.11241877, 18.20125767],
       [22.82990387, 32.92714559]])
Coordinates:
    lon      (x, y) float64 -99.83 -99.32 -99.79 -99.23
    lat      (x, y) float64 42.25 42.21 42.63 42.59
Dimensions without coordinates: x, y
<xarray.DataArray ()>
array(2.11084326)
<xarray.DataArray ()>
array(5.66406436)

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions