corteva / rioxarray

geospatial xarray extension powered by rasterio
https://corteva.github.io/rioxarray
Other
518 stars 82 forks source link

added band_as_variable option to open_rasterio #592

Closed clausmichele closed 1 year ago

clausmichele commented 1 year ago

First draft of a possible implementation of read_variable_as_bands. @snowman2 what do you think? Is it too naive as an approach?

import rioxarray

print(rioxarray.open_rasterio('./results/S2_L2A_data_mod.tiff',band_as_variable=True))
<xarray.DataArray (band: 3, y: 240, x: 626)>
[450720 values with dtype=uint16]
Coordinates:
  * band         (band) <U3 'B02' 'B03' 'B04'
  * x            (x) float64 6.609e+05 6.609e+05 ... 6.671e+05 6.671e+05
  * y            (y) float64 5.104e+06 5.104e+06 ... 5.102e+06 5.102e+06
    spatial_ref  int64 0
Attributes:
    AREA_OR_POINT:        Area
    PROCESSING_SOFTWARE:  0.6.4a1
    DESCRIPTION:          B02
    _FillValue:           0
    scale_factor:         1.0
    add_offset:           0.0
    long_name:            ('B02', 'B03', 'B04')
snowman2 commented 1 year ago

Thanks for taking a stab at this @clausmichele :+1:

The design in #296 would be to load the band data in separate variables so you can do this:

xds = rioxarray.open_rasterio('./results/S2_L2A_data_mod.tiff',band_as_variable=True)
xds["B02"]

This enables storing the description, nodata values, etc separately in the attributes of each variable.

An example implementation is here: https://github.com/bopen/xarray-gdal/blob/main/xarray_gdal/xarray_plugin.py

clausmichele commented 1 year ago

Ok, second iteration, thanks for the link to xarray_gdal, it was really helpful!

image image

snowman2 commented 1 year ago

@clausmichele thanks for working on this. I added some comments and will try to do a more thorough review later.

snowman2 commented 1 year ago

Okay, this got me down a rabbit hole to a different implementation to preserve the lazy loading (see #600). Want to give it a try and see what you think?