如何替换xarray变量中的值?

时间:2021-10-01 21:07:32

I have an xarray dataset ds

我有一个xarray数据集ds

<xarray.Dataset>
Dimensions:         (elevation_band: 4, latitude: 1, longitude: 1)
Coordinates:
  * longitude       (longitude) float64 -111.4
  * latitude        (latitude) float64 44.51
  * elevation_band  (elevation_band) int32 1 2 3 4
Data variables:
    area_frac       (elevation_band, latitude, longitude) float64 0.005109 ...
    mean_elev       (elevation_band, latitude, longitude) float64 2.45e+03 ...
    prec_frac       (elevation_band, latitude, longitude) float64 0.005109 ...

And I want to replace the values of mean_elev with these values [0.1, 0.2, 0.3, 0.4] leading to this error:

我想用这些值[0.1,0.2,0.3,0.4]替换mean_elev的值,从而导致此错误:

Trial 1

ds['mean_elev'].values = np.atleast_3d([0.1, 0.5, 0.3, 0.6]).reshape((4, 1, 1))

Error 2

MissingDimensionsError: cannot set variable 'mean_elev' with 3-dimensional data without explicit dimension names. Pass a tuple of (dims, data) instead.

MissingDimensionsError:无法在没有显式维名称的情况下使用三维数据设置变量'mean_elev'。传递一个(dims,data)元组代替。

Trial 2

So, far I have tried to create a separate dataArray for merging:

所以,到目前为止,我已经尝试创建一个单独的dataArray用于合并:

lat = ds['latitude'].values
long = ds['longitude'].values
elevation_band = ds['elevation_band'].values

mean_elev = np.array([0.1, 0.5, 0.3, 0.6]).reshape((4, 1, 1))

xr.DataArray(mean_elev, coords={'latitude': lat, 'longitude': long, 
                                'elevation_band': elevation_band},
             dims=['latitude', 'longitude', 'elevation_band'])

Error 2

ValueError: conflicting sizes for dimension 'latitude': length 4 on the data but length 1 on coordinate 'latitude'

ValueError:尺寸'纬度'的尺寸冲突:数据长度为4,坐标'纬度'长度为1

Open to alternative solutions or making one of these work.

开放替代解决方案或使其中一个工作。

1 个解决方案

#1


1  

I messed up the order of the dims. This works:

我弄乱了暗淡的顺序。这有效:

lat = ds['latitude'].values
long = ds['longitude'].values
elevation_band = ds['elevation_band'].values

mean_elev = np.array([0.1, 0.5, 0.3, 0.6]).reshape((4, 1, 1))

me = xr.DataArray(mean_elev, coords={'latitude': lat, 'longitude': long, 
                                'elevation_band': elevation_band},
             dims=['elevation_band', 'latitude', 'longitude'])
ds['mean_elev'] = me

Confirmation

ds['mean_elev']

<xarray.DataArray 'mean_elev' (elevation_band: 4, latitude: 1, longitude: 1)>
array([[[ 0.1]],

       [[ 0.5]],

       [[ 0.3]],

       [[ 0.6]]])
Coordinates:
  * longitude       (longitude) float64 -111.4
  * latitude        (latitude) float64 44.51
  * elevation_band  (elevation_band) int32 1 2 3 4

#1


1  

I messed up the order of the dims. This works:

我弄乱了暗淡的顺序。这有效:

lat = ds['latitude'].values
long = ds['longitude'].values
elevation_band = ds['elevation_band'].values

mean_elev = np.array([0.1, 0.5, 0.3, 0.6]).reshape((4, 1, 1))

me = xr.DataArray(mean_elev, coords={'latitude': lat, 'longitude': long, 
                                'elevation_band': elevation_band},
             dims=['elevation_band', 'latitude', 'longitude'])
ds['mean_elev'] = me

Confirmation

ds['mean_elev']

<xarray.DataArray 'mean_elev' (elevation_band: 4, latitude: 1, longitude: 1)>
array([[[ 0.1]],

       [[ 0.5]],

       [[ 0.3]],

       [[ 0.6]]])
Coordinates:
  * longitude       (longitude) float64 -111.4
  * latitude        (latitude) float64 44.51
  * elevation_band  (elevation_band) int32 1 2 3 4