我有一个包含许多维度和属性的大型netcdf文件。我想从这个文件中提取一个变量,并将其保存为一个新的netcdf文件,同时保留所有原始元数据。我正在使用xarray。
我使用以下命令打开数据集:
dr=xr.open_dataset("path_to_file")当我print它时,它看起来像这样(为了简单起见,删除了一些维度和元数据:
<xarray.Dataset>
Dimensions: (Time: 464, bottom_top: 39, bottom_top_stag: 40, snow_layers_stag: 3, snso_layers_stag: 7, soil_layers_stag: 4, south_north: 186, south_north_stag: 187, west_east: 246, west_east_stag: 247)
Coordinates:
XLAT (Time, south_north, west_east) float32 ...
XLONG (Time, south_north, west_east) float32 ...
XTIME (Time) datetime64[ns] ...
XLAT_U (Time, south_north, west_east_stag) float32 ...
XLONG_U (Time, south_north, west_east_stag) float32 ...
XLAT_V (Time, south_north_stag, west_east) float32 ...
XLONG_V (Time, south_north_stag, west_east) float32 ...
Dimensions without coordinates: Time, bottom_top, bottom_top_stag, snow_layers_stag, snso_layers_stag, soil_layers_stag, south_north, south_north_stag, west_east, west_east_stag
Data variables:
Times (Time) |S19 ...
UST (Time, south_north, west_east) float32 ...
ZNU (Time, bottom_top) float32 ...
ZNW (Time, bottom_top_stag) float32 ...
ZS (Time, soil_layers_stag) float32 ...
DZS (Time, soil_layers_stag) float32 ...
Attributes:
TITLE: OUTPUT FROM WRF V3.9 MODEL
START_DATE: 2017-10-31_00:00:00
SIMULATION_START_DATE: 2017-10-01_00:00:00
WEST-EAST_GRID_DIMENSION: 247
SOUTH-NORTH_GRID_DIMENSION: 187
BOTTOM-TOP_GRID_DIMENSION: 40
HYBRID_OPT: -1
ETAC: 0.0我只想提取UST,所以我尝试:
dr_u = dr['UST']但是当我对生成的dr_u执行print操作时,元数据就消失了:
<xarray.Dataset>
Dimensions: (Time: 464, south_north: 186, west_east: 246)
Coordinates:
XLAT (Time, south_north, west_east) float32 ...
XLONG (Time, south_north, west_east) float32 ...
XTIME (Time) datetime64[ns] ...
Dimensions without coordinates: Time, south_north, west_east
Data variables:
UST (Time, south_north, west_east) float32 ...我希望能够在原始文件中保留Attributes标题下的所有信息。我知道在xarray包中有一个名为keep_attrs的标志,它似乎对此很有用,但我不知道如何在此操作中使用它。
发布于 2019-11-07 01:51:35
可以使用ds.attrs从xarray对象检索属性字典
您可以手动分配属性:
dr_u.attrs = dr.attrshttps://stackoverflow.com/questions/58723939
复制相似问题