1

I want to export a single date and variable fwi of a xarray dataset ds to a tif file. However, my dataarray has too many dimensions and I cannot figure out how to effectively remove the dimension 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

Exporting the dataarray raises the error 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

I already dropped the dimension lsm_time in a previous step, when I masked the dataset ds with a land sea mask lsm (single date), and had to duplicate/add the time dimension of the dataset ds. So maybe this is an error resulting from this data handling.. However, I could figure out how to mask otherwise.

    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)

So I already applied .drop("lsm_time")

But, it is still

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

When trying .drop_dims, it removes all data variables.

    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*

What am I missing or what did I do wrong? Thanks for any help!

3
  • 1
    try adding .squeeze after .sel or .drop Commented Feb 11, 2022 at 12:12
  • Amazing, .squeeze did the trick, thank you! Commented Feb 11, 2022 at 16:33
  • 1
    ds.squeeze(dim='lsm_time') solved it for me - I'm new to posting on the forum, @Val feel free to post the solution as answer :) Commented Feb 11, 2022 at 16:47

1 Answer 1

1

Dimensions with size 1 can be removed using the .squeeze method.

Conversely, you can add a dimension of size 1 using .expand_dims

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})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.