-
Notifications
You must be signed in to change notification settings - Fork 8
Getting scalar values of the weighted average when input is 2D and weights is a 2D array #28
Comments
A better version of the script:
Output:
(I don't think the |
@mnlevy1981, to make sure I am getting this, what should be the expected output's dimensions (shape) of With # 1. Want NO3 averaged over all files
field = ds.NO3.isel(z_t=0).mean('time')
# 2. TAREA is identical across files, so just use the first time dimension of it
TAREA = ds.TAREA.isel(time=0)
In [20]: field.shape
Out[20]: (384, 320)
In [21]: TAREA.shape
Out[21]: (384, 320) |
It should be a scalar quantity. I just realized I could come up with an even simpler example using 1D arrays rather than reading data from disk, but I need to run... I'll try to find time tonight to post more. But the gist is that if
Then I expect the weighted mean to be 5: the weighted sum of |
I think I figured it out:
ds = xr.open_mfdataset(in_files, decode_times=False).load() Or
w_mean = esmlab.statistics.weighted_mean(field, TAREA).data.compute() In [47]: field
Out[47]:
<xarray.DataArray 'NO3' (nlat: 384, nlon: 320)>
array([[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[22.465448, 22.383131, 22.292166, ..., nan, nan, nan],
...,
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan]],
dtype=float32)
Coordinates:
TLAT (nlat, nlon) float64 -79.22 -79.22 -79.22 ... 72.2 72.19 72.19
TLONG (nlat, nlon) float64 -39.44 -38.31 -37.19 ... -41.08 -40.65 -40.22
z_t float64 500.0
Dimensions without coordinates: nlat, nlon
In [48]: TAREA
Out[48]:
<xarray.DataArray 'TAREA' (nlat: 384, nlon: 320)>
dask.array<shape=(384, 320), dtype=float64, chunksize=(384, 320)>
Coordinates:
TLAT (nlat, nlon) float64 -79.22 -79.22 -79.22 ... 72.2 72.19 72.19
TLONG (nlat, nlon) float64 -39.44 -38.31 -37.19 ... -41.08 -40.65 -40.22
time float64 6.0
Dimensions without coordinates: nlat, nlon
Attributes:
long_name: area of T cells
units: cm^2
In [49]: w_mean = esmlab.statistics.weighted_mean(field, TAREA)
In [51]: w_mean
Out[51]:
<xarray.DataArray ()>
dask.array<shape=(), dtype=float64, chunksize=()>
Coordinates:
z_t float64 500.0
time float64 6.0
In [52]: w_mean.data
Out[52]: dask.array<truediv, shape=(), dtype=float64, chunksize=()>
In [53]: w_mean.data.compute()
Out[53]: 5.146317884602825
In [54]: np.nanmean(field)
Out[54]: 5.7132335 |
@mnlevy1981, do: w_mean = esmlab.statistics.weighted_mean(field, TAREA).load() instead of: w_mean = esmlab.statistics.weighted_mean(field, TAREA).data.compute() |
Thanks! The line
Works as expected. Is accessing
is another option. |
I think this is the appropriate way:
xref: http://xarray.pydata.org/en/stable/generated/xarray.DataArray.values.html#xarray.DataArray.values |
@mnlevy1981, should I close this? Or free feel to close it if you are satisfied with the conclusion from our last week's meeting. |
The text was updated successfully, but these errors were encountered: