如何将先前对象的坐标复制到新的DataArray(xarray帮助)

问题描述 投票:1回答:1

我有这个netcdf文件wrfout_d03.nc,其中我使用以下文件打开该文件:

ds = xr.open_dataset('/Users/jacob/Desktop/wrfpy/wrfout_d03_may.nc')

然后提供了一个DataSet对象:

<xarray.Dataset>
Dimensions:                (Time: 193, bio_emissions_dimension_stag: 41, bottom_top: 50, bottom_top_stag: 51, klevs_for_dvel: 1, seed_dim_stag: 12, soil_layers_stag: 4, south_north: 115, south_north_stag: 116, west_east: 115, west_east_stag: 116)
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, bio_emissions_dimension_stag, bottom_top, bottom_top_stag, klevs_for_dvel, seed_dim_stag, soil_layers_stag, south_north, south_north_stag, west_east, west_east_stag
Data variables:
datavars...

然后我使用以下代码访问PM2_5_DRY变量:

pm25 = ds.PM2_5_DRY

结果对象pm25的尺寸和坐标如下:

<xarray.DataArray 'PM2_5_DRY' (Time: 193, bottom_top: 50, south_north: 115, west_east: 115)>
[127621250 values with dtype=float32]
Coordinates:
    XLAT     (Time, south_north, west_east) float32 ...
    XLONG    (Time, south_north, west_east) float32 ...
    XTIME    (Time) datetime64[ns] ...
Dimensions without coordinates: Time, bottom_top, south_north, west_east
Attributes:
    FieldType:    104
    MemoryOrder:  XYZ
    description:  pm2.5 aerosol dry mass
    units:        ug m^-3
    stagger: 

然后,我操纵pm25对象,并通过以下方法获取time维度中的均值:

pm25_mean = pm25.mean(dim='Time', keep_attrs = True)

结果对象是一个DataArray,但没有坐标XLAT或XLON。

<xarray.DataArray 'PM2_5_DRY' (bottom_top: 50, south_north: 115, west_east: 115)>
array([[[14.73083   , 14.756626  , 14.796355  , ..., 20.325712  ,
         20.855696  , 21.381271  ],
        [14.651459  , 14.34477   , 14.371858  , ..., 18.00389   ,
         18.4109    , 21.337002  ],
        [14.59026   , 14.257076  , 14.293012  , ..., 17.391146  ,
         18.217058  , 20.882664  ],
        ...,
        [27.356459  , 27.21468   , 27.757051  , ...,  8.084272  ,
          8.010168  ,  7.989942  ],
        [27.185486  , 27.02623   , 27.776043  , ...,  7.944748  ,
          7.8795266 ,  7.8552976 ],
        [26.926008  , 27.724253  , 28.427626  , ...,  7.8269224 ,
          7.773637  ,  7.741844  ]],

Dimensions without coordinates: bottom_top, south_north, west_east
Attributes:
    FieldType:    104
    MemoryOrder:  XYZ
    description:  pm2.5 aerosol dry mass
    units:        ug m^-3
    stagger:

以下要检查的代码给出:

pm25_mean.coords

Coordinates:
*empty*

我尝试查看xarray中mean函数的文档;但是,我找不到任何将坐标从以前的对象复制到新对象的选项。

有关如何进行此操作的任何提示?我想我需要从文件访问这些坐标,然后再次将它们组合。但是我不确定如何执行此过程。

而且,这与此有关吗?

XLAT     (Time, south_north, west_east) float32

XLAT是多维坐标,它也取决于时间。由于我获得了时间维度的均值,因此维度pm25的数量已减少至3,而不是4。这是否也可能会影响坐标?

我需要最终对象具有坐标,因为我将对此进行可视化。

感谢您的帮助!

python netcdf python-xarray
1个回答
-1
投票

我认为结果中仍然存在坐标XLAT和XLON。尝试pm25_mean.coords进行检查。如果没有,您可以给我发送数据集链接吗?

© www.soinside.com 2019 - 2024. All rights reserved.