【问题标题】:Access data by month number in 3D xarray在 3D xarray 中按月数访问数据
【发布时间】:2020-09-30 10:41:10
【问题描述】:

我有给定年份的一月、二月、三月、四月、十月、十一月和十二月的数据数组 (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 = data.sel(month_number=10) 获取 Oct 数据时,它会引发错误。

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

【问题讨论】:

    标签: python netcdf python-xarray netcdf4


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 2020-07-02
      • 2022-08-10
      • 2021-08-21
      • 2015-08-13
      • 2021-09-02
      • 2018-02-10
      • 1970-01-01
      • 2019-08-08
      相关资源
      最近更新 更多