DataArray.mean不保留坐标
DataArray.mean does not retain coordinates
DataArray.mean
不保留取决于应用平均值的维度的坐标。
注意:XLAT 和 XLONG 与时间无关;然而,一些 netcdf 文件在这两个文件中有一个时间坐标。
我有这个 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 的维度数量从 4 个减少到 3 个。这是否也以某种方式影响了坐标?
我需要最终对象具有坐标 XLAT 和 XLONG,因为我会将其可视化。
感谢您的帮助!
在xarray中似乎没有直接解决这个问题
(据我所知),但我通过 NCO 找到了解决方案。
这来自另一个 post 这里。这些是我为解决这个问题所采取的步骤。
首先,XLAT 和 XLONG 没有出现在最终的 DataArray 中的原因是这些坐标取决于时间。
根据这个线程:
- 我们必须获取 NetCDF 文件并分离感兴趣的变量(时间相关)。
- 我们及时将 XLAT 和 XLONG 取平均值,并将其放入另一个 nc 文件中。
- 我们将两个文件附加在一起以获得与时间无关的结果 XLAT 和 XLONG。
ncks -v variable input.nc variable.nc
ncwa -a Time -v XLAT,XLONG input.nc latlon.nc
ncks -A latlon.nc variable.nc
这样,当访问xarray中的文件并计算平均值时,我们得到以下DataArray:
<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 ],
...,
Coordinates:
XLAT (south_north, west_east) float32 ...
XLONG (south_north, west_east) float32 ...
Dimensions without coordinates: bottom_top, south_north, west_east
希望这也能帮助遇到同样问题的其他人!
您只需要将坐标变量从 3 维转换为 2 维。
d = xr.open_dataset('.../pm25_sample.nc')
d['XLAT'] = d.XLAT.mean(dim = 'Time')
d['XLONG'] = d.XLONG.mean(dim = 'Time')
d['PM2_5_DRY'].mean(dim = 'Time')
...,
[ 0.03839084, 0.03837739, 0.03835952, ..., 0.03414929,
0.03412561, 0.03410038],
[ 0.03837854, 0.03836632, 0.03834687, ..., 0.03414606,
0.0341224 , 0.03409675],
[ 0.03836945, 0.03835024, 0.03833132, ..., 0.03414177,
0.03411727, 0.03409337]]], dtype=float32)
Coordinates:
XLAT (south_north, west_east) float32 14.086891 14.086907 ... 15.111996
XLONG (south_north, west_east) float32 120.49799 120.50717 ... 121.55791
Dimensions without coordinates: bottom_top, south_north, west_east
DataArray.mean
不保留取决于应用平均值的维度的坐标。
注意:XLAT 和 XLONG 与时间无关;然而,一些 netcdf 文件在这两个文件中有一个时间坐标。
我有这个 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 的维度数量从 4 个减少到 3 个。这是否也以某种方式影响了坐标?
我需要最终对象具有坐标 XLAT 和 XLONG,因为我会将其可视化。
感谢您的帮助!
在xarray中似乎没有直接解决这个问题 (据我所知),但我通过 NCO 找到了解决方案。
这来自另一个 post 这里。这些是我为解决这个问题所采取的步骤。
首先,XLAT 和 XLONG 没有出现在最终的 DataArray 中的原因是这些坐标取决于时间。
根据这个线程:
- 我们必须获取 NetCDF 文件并分离感兴趣的变量(时间相关)。
- 我们及时将 XLAT 和 XLONG 取平均值,并将其放入另一个 nc 文件中。
- 我们将两个文件附加在一起以获得与时间无关的结果 XLAT 和 XLONG。
ncks -v variable input.nc variable.nc ncwa -a Time -v XLAT,XLONG input.nc latlon.nc ncks -A latlon.nc variable.nc
这样,当访问xarray中的文件并计算平均值时,我们得到以下DataArray:
<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 ],
...,
Coordinates:
XLAT (south_north, west_east) float32 ...
XLONG (south_north, west_east) float32 ...
Dimensions without coordinates: bottom_top, south_north, west_east
希望这也能帮助遇到同样问题的其他人!
您只需要将坐标变量从 3 维转换为 2 维。
d = xr.open_dataset('.../pm25_sample.nc')
d['XLAT'] = d.XLAT.mean(dim = 'Time')
d['XLONG'] = d.XLONG.mean(dim = 'Time')
d['PM2_5_DRY'].mean(dim = 'Time')
...,
[ 0.03839084, 0.03837739, 0.03835952, ..., 0.03414929,
0.03412561, 0.03410038],
[ 0.03837854, 0.03836632, 0.03834687, ..., 0.03414606,
0.0341224 , 0.03409675],
[ 0.03836945, 0.03835024, 0.03833132, ..., 0.03414177,
0.03411727, 0.03409337]]], dtype=float32)
Coordinates:
XLAT (south_north, west_east) float32 14.086891 14.086907 ... 15.111996
XLONG (south_north, west_east) float32 120.49799 120.50717 ... 121.55791
Dimensions without coordinates: bottom_top, south_north, west_east