首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python-xarray从多维netcdf文件中提取单个变量,同时保留属性/元数据

使用python-xarray从多维netcdf文件中提取单个变量,同时保留属性/元数据
EN

Stack Overflow用户
提问于 2019-11-06 14:01:35
回答 1查看 515关注 0票数 1

我有一个包含许多维度和属性的大型netcdf文件。我想从这个文件中提取一个变量,并将其保存为一个新的netcdf文件,同时保留所有原始元数据。我正在使用xarray

我使用以下命令打开数据集:

代码语言:javascript
复制
dr=xr.open_dataset("path_to_file")

当我print它时,它看起来像这样(为了简单起见,删除了一些维度和元数据:

代码语言:javascript
复制
<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,所以我尝试:

代码语言:javascript
复制
dr_u = dr['UST']

但是当我对生成的dr_u执行print操作时,元数据就消失了:

代码语言:javascript
复制
<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的标志,它似乎对此很有用,但我不知道如何在此操作中使用它。

EN

回答 1

Stack Overflow用户

发布于 2019-11-07 01:51:35

可以使用ds.attrs从xarray对象检索属性字典

您可以手动分配属性:

代码语言:javascript
复制
dr_u.attrs = dr.attrs
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58723939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档