[英]Access data by month number in 3D xarray
我有给定年份的 1 月、2 月、3 月、4 月、10 月、11 月和 12 月的数据数组 (361x361)。
到目前为止,我一直将它们存储在一年中每个月的单独 netcdf 中(例如 03.nc、10.nc)
我想将所有月份合并为一个 netcdf,以便我可以执行以下操作:
march_data = data.sel(month='03')
或者data.sel(month=3))
到目前为止,我只能将每月数据堆叠在一个 361x361x7 的数组中,并且它的索引无济于事,因此要获得 3 月的数据,您需要执行 data[:,:,2] 并获得 10 月的数据 [:,:, 4]。 显然,2 和 4 并不直观地对应于三月和十月。 这部分是因为 python 从零开始索引,部分是因为我错过了夏季月份。 我可以将 nan 字段放入缺失的月份,但这并不能解决 index-0 问题。
到目前为止我的尝试:
data = xarray.Dataset( data_vars={'ice_type':(['x','y','time'],year_array),},
coords={'lon':(['x','y'],lon_target),
'lat':(['x','y'],lat_target),
'month_number':(['time'],month_int)})
这里year_array
是一个 361x361x7 numpy 数组,而month_int
是一个列表,它将year_array
的第三个索引year_array
到月份编号: [1,2,3,4,10,11,12]
。
当我尝试使用oct = data.sel(month_number=10)
获取 Oct 数据时,它会引发错误。
在一个侧面说明,我知道,有可能被发现的解决方案在这里,但说实话,我不明白它是如何工作的。 我的困惑主要是基于他们如何同时使用“时间”作为字典键和时间列表。
我想我已经写了一个辅助函数来做这样的事情:
def combine_new_ds_dim(ds_dict, new_dim_name):
"""
Combines a dictionary of datasets along a new dimension using dictionary keys
as the new coordinates.
Parameters
----------
ds_dict : dict
Dictionary of xarray Datasets or dataArrays
new_dim_name : str
The name of the newly created dimension
Returns
-------
xarray.Dataset
Merged Dataset or DataArray
Raises
------
ValueError
If the values of the input dictionary were of an unrecognized type
"""
expanded_dss = []
for k, v in ds_dict.items():
expanded_dss.append(v.expand_dims(new_dim_name))
expanded_dss[-1][new_dim_name] = [k]
new_ds = xr.concat(expanded_dss, new_dim_name)
return new_ds
如果您在单独的 netcdfs 中拥有所有数据,那么您应该能够将它们导入到单独的dataArray
。 假设你已经这样做了,那么你可以做
month_das = {
1: january_da,
2: february_da,
...
12: december_da
}
year_data = combine_new_ds_dim(month_das, 'month')
这将是沿新维度month
的所有数据与所需坐标的串联。 如果你想单独使用它,我认为函数的主循环很容易分开。
编辑:
对于将来看到这个的任何人来说,使用内置的 xarray 函数有一种更简单的方法来做到这一点。 您可以沿着新维度串联
year_data = xr.concat([january_da, february_da, ..., december_da], dim="month")
这将创建一个新的dataArray
其中包含沿新维度连接的组成数组,但在该维度上没有坐标。 要添加坐标,
year_data["month"] = [1, 2, ..., 12]
此时year_data
将沿新维度“月”连接,并沿该维度具有所需的坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.