首页 > 解决方案 > 在 3D xarray 中按月数访问数据

问题描述

我有给定年份的一月、二月、三月、四月、十月、十一月和十二月的数据数组 (361x361)。

到目前为止,我一年中每个月都将它们存储在单独的 netcdfs 中(例如 03.nc、10.nc)

我想将所有月份合并为一个 netcdf,这样我就可以执行以下操作:

march_data = data.sel(month='03') 

或者data.sel(month=3))

到目前为止,我只能将每月数据堆叠在一个 361x361x7 数组中,并且它的索引没有帮助,因此要获取 3 月份的数据,您需要执行 data[:,:,2] 并获取 10 月份的数据 [:,:, 4]。显然 2 和 4 并不直观地对应于 3 月和 10 月。这部分是因为 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到月份编号的列表[1,2,3,4,10,11,12]

当我尝试使用它获取 Oct 数据时oct = data.sel(month_number=10)会引发错误。

顺便说一句,我知道这里可能有一个解决方案但老实说,我不明白它是如何工作的。我的困惑主要是基于他们如何同时使用“时间”作为字典键和时间列表。

标签: pythonnetcdfpython-xarraynetcdf4

解决方案


我想我已经写了一个辅助函数来做这样的事情:

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将沿新维度“月”连接起来,并将沿该维度具有所需的坐标。


推荐阅读