汇总数据集的数据变量

Sum Data variables of Dataset

我使用以下代码将 DataArray 列表合并到一个 DataSet 中:

    surface_dataarray = []

    for (key, value) in surfaces_item_service.items():
        print(f'{key}: {value}')
        single_surface_class = __yearly_surface_type(folder_path, provider, data_source, bins, key)
        single_surface_class.name = key
        if single_surface_class.count() > 1:
            single_surface_class.rio.to_raster(output_file_path + f'/{key}.tif', driver="GTiff")
            surface_dataarray.append(single_surface_class)

    surface_data = xr.merge(surface_dataarray)

并且我获得了如下数据集:

<xarray.Dataset>
Dimensions:            (x: 1868, y: 1373)
Coordinates:
    band               int64 1
  * x                  (x) float64 4.269e+05 4.269e+05 ... 4.455e+05 4.455e+05
  * y                  (y) float64 4.53e+06 4.53e+06 ... 4.516e+06 4.516e+06
    spatial_ref        int64 0
    year               int64 2020
    variable           <U15 'CLASSIFIED DATA'
Data variables:
    water_surface      (y, x) float32 nan nan nan nan nan ... nan nan nan nan
    non_green_surface  (y, x) float32 nan nan nan nan nan ... nan nan nan nan
    green_surface      (y, x) float32 nan nan nan nan nan ... nan nan nan nan

是否可以对数据变量求和? 我需要保存为单波段。 注意:有 nan 个值,因为我的区域在边界处没有值。

是的,一种方法是使用 Dataset.to_array,它将所有数据变量沿新的 "variable" 维度合并到一个数组中:

sum_of_data_variables = ds.to_array().sum("variable")