我对使用xarrays还比较新,我想原地修改NetCDF文件的属性。但是内置函数只提供了另一个新数据集。
ds = xr.open_dataset(file_)
# ds has "time" as one of the coordinates whose attributes I want to modify
#here is ds for more clarity
ds
>><xarray.Dataset>
Dimensions: (lat: 361, lev: 1, lon: 720, time: 1)
Coordinates:
* lon (lon) float32 0.0 0.5 1.0 1.5 2.0 ... 357.5 358.0 358.5 359.0 359.5
* lat (lat) float32 -90.0 -89.5 -89.0 -88.5 -88.0 ... 88.5 89.0 89.5 90.0
* lev (lev) float32 1.0
* time (time) timedelta64[ns] 00:00:00
Data variables:
V (time, lev, lat, lon) float32 ...
Attributes:
Conventions: CF
constants_file_name: P20000101_12
institution: IACETH
lonmin: 0.0
lonmax: 359.5
latmin: -90.0
latmax: 90.0
levmin: 250.0
levmax: 250.0
我尝试分配新属性,但它给了一个新的数据数组。
newtimeattr = "some time"
ds.time.assign_attrs(units=newtimeattr)
另外,如果我将这个属性分配给数据集变量 "V",它会添加另一个变量到数据集
ds['V '] = ds.V.assign_attrs(units='m/s')
## here it added another variable V .So, ds has 2 variables with same name as V
ds #trimmed output
>>Data variables:
V (time, lev, lat, lon) float32 ...
V (time, lev, lat, lon) float32 ...