Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python xarray从多维netcdf文件中提取单个变量,同时保留属性/元数据_Python_Attributes_Netcdf_Python Xarray - Fatal编程技术网

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

使用python xarray从多维netcdf文件中提取单个变量,同时保留属性/元数据,python,attributes,netcdf,python-xarray,Python,Attributes,Netcdf,Python Xarray,我有一个很大的netcdf文件,它有很多维度和属性。我想从这个文件中提取一个变量,并将其另存为一个新的netcdf文件,同时保留所有原始元数据。我正在使用xarray 我使用以下方法打开数据集: dr=xr.open_dataset("path_to_file") 当我打印它时,它看起来是这样的(为了简单起见,删除了一些维度和元数据: <xarray.Dataset> Dimensions: (Time: 464, bottom_top: 39, bo

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

我使用以下方法打开数据集:

dr=xr.open_dataset("path_to_file")
当我打印它时,它看起来是这样的(为了简单起见,删除了一些维度和元数据:

<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
但是当我打印出结果时,元数据就消失了:

<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 ...

尺寸:(时间:464,南北:186,东西:246)
协调:
XLAT(时间,南、北、西、东)浮动32。。。
XLONG(时间、南、北、西、东)32。。。
XTIME(时间)datetime64[ns]。。。
不带坐标的尺寸:时间、南北、东西
数据变量:
UST(时间,南、北、西、东)32。。。

我希望能够在原始文件中保留
属性
标题下的所有信息。我知道在
xarray
包中有一个名为
keep_attrs
的标志,它似乎对这项操作很有用,但我无法确定如何在此操作中使用它。

您可以检索属性dict使用
ds.attrs

您只需手动指定属性即可:

dr_u.attrs = dr.attrs

您可以使用
ds.attrs

您只需手动指定属性即可:

dr_u.attrs = dr.attrs

我认为这已经解决了问题,但不幸的是,我发现如果我尝试将xarray保存为netcdf文件并读回,则属性已消失。如果我读入netcdf文件,然后立即再次保存,则不会出现此问题,前提是我要先提取单个变量,然后添加attributes。你知道如何解决这个问题吗?我查看了to_netcdf的可能参数,没有发现任何有用的参数。你需要创建一个包含DataArray的数据集。然后将属性添加到数据集中。我认为这解决了问题,但不幸的是,如果我尝试将xarray保存为netcdf fil,我发现如果我读入一个netcdf文件,然后立即再次保存它,我就不会有这个问题,只要我经过提取单个变量然后添加属性的过程。你知道如何解决这个问题吗?我已经研究了to_netcdf的可能参数,但没有发现任何问题这似乎很有用。您需要创建一个包含DataArray的数据集。然后将属性添加到数据集。