I have a list of multiple dataarray with same dimensions in name and size, I want to sum all of dataarrays and create a single dataarray with the same dimensions of dataarrays, what is the best way for do it?
[<xarray.DataArray (latitude: 501, longitude: 894)>
array(..., dtype=float32)
Coordinates:
* longitude (longitude) float64 49.8 49.81 49.81 49.82 ... 56.98 56.99 57.0
* latitude (latitude) float64 27.0 27.01 27.02 27.03 ... 31.98 31.99 32.0
year int64 2000,
.......
.......
<xarray.DataArray (latitude: 501, longitude: 894)>
array(..., dtype=float32)
Coordinates:
* longitude (longitude) float64 49.8 49.81 49.81 49.82 ... 56.98 56.99 57.0
* latitude (latitude) float64 27.0 27.01 27.02 27.03 ... 31.98 31.99 32.0
year int64 2020]
CodePudding user response:
Since you can add two xarrays a
and b
elementwise with a simple a b
and you say you have a list of xarrays all with identical dimensions (let's call it xrs
), then this is how you'd add all of them:
result = xrs[0]
for xr in xrs[1:]:
result = xr
By the way, that's not specific to xarray
at all. If you have a list of integers xs
and you want to add all of them:
result = xrs[0]
for xr in xrs[1:]:
result = xr
And as long as the addition operators are appropriately defined, sum(xs)
would work just as well. And it does for xarray
:
result = sum(xrs)