How obtain the mean of a series of DataArray? #5509
-
Hello, I would like to know if it is correct to add DataArray in this way? import xarray as xr
with xr.open_dataset('sst.mean.anom.nc') as ds:
sst = ds.sst[:, 20:87, 60:70] # [:,20:87,55:87]
sst_1948_1998 = sst.loc['1948-03-01':'1998-03-01']
meansst194803_199803 = sst.loc['1948-03-01':'1998-03-01':12].mean().values
Hx, X = list(), list()
suma, sumaHx, sumaX = 0, 0, 0
#Obtain the averages of the values of the hot and cold SST temperature fields in the time series
for j in range(0, len(sst_1948_1998.time.values), 12):
anho = str(sst_1948_1998.time.values[j])[0:4]
promedio = sst_1948_1998.isel(time=j).mean()#The mean SST temperature for each year
if promedio < meansst194803_199803:
X.append(sst_1948_1998.isel(time=j))#values of the cold temperature field of the SST in the time series
sumaX = sumaX + sst_1948_1998.isel(time=j)#accumulated of the values of the cold temperature fields of the SST in the time series
else:
Hx.append(sst_1948_1998.isel(time=j))#values of the hot temperature field of the SST in the time series
sumaHx = sumaHx + sst_1948_1998.isel(time=j)#accumulated values of the SST hot temperature fields in the time series
meanSumaX = sumaX / len(X)
meanSumaHx = sumaHx / len(Hx)
print(" mean of the values of the cold temperature field of the SST in the time series \n",meanSumaX)#mean of the values of the cold temperature field of the SST in the time series
print(" mean of the SST hot temperature field values in the time series \n",meanSumaHx)#mean of the SST hot temperature field values in the time series
print(" values of the cold temperature field of the SST in the time series \n",X)#values of the cold temperature field of the SST in the time series
print(" values of the hot temperature field of the SST in the time series \n",Hx)#values of the hot temperature field of the SST in the time series reprs
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 16 replies
-
Could you reduce the example to its minimal form? I'm not that clear what you're asking about. You can also format it, check out did on GitHub for formatting code. Thanks |
Beta Was this translation helpful? Give feedback.
-
from what I could understand, you first select a specific region of interest, select a time slice, compute a mean over all Marches in that time slice and the region of interest, group each individual March by comparing its mean over the region of interest with that total mean and finally compute a mean over those two groups. In other words: with xr.open_dataset('sst.mean.anom.nc') as ds:
# region of interest
sst = ds.sst.isel(lat=slice(20, 87), lon=slice(60, 70))
# time slice
sst_1948_1998 = sst.sel(time=slice('1948-03-01', '1998-03-01'))
# all first-of-Marches
# not sure if there's a better way for this, you can probably also use .isel(time=slice(None, None, 12)) instead
selected = sst_1948_1998.where(lambda x: x.time.dt.month == 3, drop=True)
all_mean = selected.mean()
sums = selected.groupby(selected.mean(dim=["lat", "lon"]) < all_mean).mean()
postprocessed = (
sums
.rename({"sst": "labels"})
.assign_coords(labels=["aX", "aHx"])
) which should be faster and a bit more readable. Note that the rename and the call to If that's not helpful, please describe in more detail (like I tried to at the top) what you actually wanted to do. |
Beta Was this translation helpful? Give feedback.
from what I could understand, you first select a specific region of interest, select a time slice, compute a mean over all Marches in that time slice and the region of interest, group each individual March by comparing its mean over the region of interest with that total mean and finally compute a mean over those two groups.
In other words: