将数据数组导出到 geotiff 的删除维度不起作用

Drop dimension for exporting dataarray to geotiff not working

我想将 xarray 数据集 ds 的单个日期和变量 fwi 导出到一个 tif 文件。但是,我的数据数组的维度太多,我不知道如何有效地删除维度 lsm_time.

    ds
    <xarray.Dataset>
    Dimensions:      (time: 2436, y: 28, x: 58, lsm_time: 1)
    Coordinates:
      * time         (time) datetime64[ns] 2015-05-02T12:00:00 ... 2021-12-31T12:...
        step         <U8 '12:00:00'
        surface      float64 0.0
      * y            (y) float64 36.01 36.11 36.21 36.31 ... 38.41 38.51 38.61 38.71
      * x            (x) float64 -7.64 -7.54 -7.44 -7.34 ... -2.24 -2.14 -2.04 -1.94
        spatial_ref  int32 0
    Dimensions without coordinates: lsm_time
    Data variables:
        ffmc         (time, y, x, lsm_time) float32 nan nan nan ... 88.93 88.53     
        dmc          (time, y, x, lsm_time) float32 nan nan nan ... 6.511 7.908 8.45
        dc           (time, y, x, lsm_time) float32 nan nan nan ... 406.2 428.5
        isi          (time, y, x, lsm_time) float32 nan nan nan ... 3.872 3.852
        bui          (time, y, x, lsm_time) float32 nan nan nan ... 15.08 16.11
        fwi          (time, y, x, lsm_time) float32 nan nan nan ... 5.303 5.486

导出数据数组引发错误 TooManyDimensions:

    ds.fwi.sel(time="07-14-2021").rio.to_raster("file_out.tif")

    raise TooManyDimensions(
    rioxarray.exceptions.TooManyDimensions: Only 2D and 3D data arrays supported. Data variable: fwi

我已经在上一步中删除了维度 lsm_time,当时我用海陆遮罩 lsm(单一日期)遮盖了数据集 ds,并且不得不 duplicate/add数据集的时间维度ds。所以这可能是此数据处理导致的错误。但是,我可以弄清楚如何屏蔽其他内容。

    lsm
    Dimensions:      (x: 58, y: 28, time: 1)
    Coordinates:
      * x            (x) float64 -7.64 -7.54 -7.44 -7.34 ... -2.24 -2.14 -2.04 -1.94
      * y            (y) float64 36.01 36.11 36.21 36.31 ... 38.41 38.51 38.61 38.71
      * time         (time) datetime64[ns] 2013-11-29
    Data variables:
        spatial_ref  int32 0
        lsm          (time, y, x) float64 0.0 0.0 0.0 0.0 0.0 ... 1.0 1.0 1.0 0.9996

    lsm = lsm.expand_dims({"new_time" : ds.time})
    lsm = lsm.rename({"time":"lsm_time"}).rename({"new_time" : "time"}).drop("lsm_time") #issue here: drop_dims removes variable..

    ds = ds.where(lsm>=threshold)

所以我已经申请了.drop("lsm_time")

但是,还是

    print(ds.fwi.sel(time="07-14-2021").dims)
    > ('time', 'y', 'x', 'lsm_time')

尝试 .drop_dims 时,它会删除所有数据变量。

    ds.drop_dims('lsm_time')
    <xarray.Dataset>
    Dimensions:      (time: 2436, y: 28, x: 58)
    Coordinates:
      * time         (time) datetime64[ns] 2015-05-02T12:00:00 ... 2021-12-31T12:...
        step         <U8 '12:00:00'
        surface      float64 0.0
      * y            (y) float64 36.01 36.11 36.21 36.31 ... 38.41 38.51 38.61 38.71
      * x            (x) float64 -7.64 -7.54 -7.44 -7.34 ... -2.24 -2.14 -2.04 -1.94
        spatial_ref  int32 0
    Data variables:
        *empty*

我错过了什么或者我做错了什么?感谢您的帮助!

可以使用 .squeeze 方法删除大小为 1 的维度。

相反,您可以使用 .expand_dims

添加大小为 1 的维度
import xarray as xr
x = xr.tutorial.load_dataset("rasm")
y = x.isel(time=slice(0,1))

y.dims
# Frozen({'time': 1, 'y': 205, 'x': 275})

y.squeeze().dims
# Frozen({'y': 205, 'x': 275})

y.squeeze().expand_dims("newdim").dims
# Frozen({'y': 205, 'x': 275, 'newdim': 1})