Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在3D xarray中按月数访问数据_Python_Netcdf_Python Xarray_Netcdf4 - Fatal编程技术网

Python 在3D xarray中按月数访问数据

Python 在3D xarray中按月数访问数据,python,netcdf,python-xarray,netcdf4,Python,Netcdf,Python Xarray,Netcdf4,我有给定年份的1月、2月、3月、4月、10月、11月和12月的数据阵列(361x361) 到目前为止,我在一年中每个月都将它们存储在单独的NetCDF中(例如03.nc、10.nc) 我想将所有月份合并到一个netcdf中,这样我就可以执行以下操作: march_data = data.sel(month='03') 或者data.sel(月=3)) 到目前为止,我只能将每月数据堆叠在361x361x7数组中,而且它的索引没有任何帮助,因此要获取三月数据,您需要执行数据[:,:,2],要获取

我有给定年份的1月、2月、3月、4月、10月、11月和12月的数据阵列(361x361)

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

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

march_data = data.sel(month='03') 
或者
data.sel(月=3))

到目前为止,我只能将每月数据堆叠在361x361x7数组中,而且它的索引没有任何帮助,因此要获取三月数据,您需要执行数据[:,:,2],要获取十月数据[:,:,4]。显然,2和4并不直观地对应于3月和10月。这部分是因为python的索引是从零开始的,部分是因为我错过了夏季。我可以在缺失的几个月内输入nan字段,但这并不能解决索引-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\u array
是一个361x361x7 numpy数组,
month\u int
是一个列表,它将
year\u array的第三个索引映射到月份编号:
[1,2,3,4,10,11,12]

当我尝试使用
Oct=data.sel(month\u 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
如果您的所有数据都在单个NetCDF中,那么您应该能够将它们导入到单个的
数据数组中。假设你已经做到了,那么你就可以做到了

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\u数据
将沿新维度“month”连接,并沿该维度具有所需的坐标