From 03f881bd81246ffc56fe2bcaac0531770383d0f1 Mon Sep 17 00:00:00 2001 From: jgrss Date: Thu, 16 May 2024 20:36:50 +1000 Subject: [PATCH 1/4] add band setting --- src/geowombat/backends/xarray_.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/geowombat/backends/xarray_.py b/src/geowombat/backends/xarray_.py index ef5b4467..ef59cc5b 100644 --- a/src/geowombat/backends/xarray_.py +++ b/src/geowombat/backends/xarray_.py @@ -476,7 +476,6 @@ def reduce_func( xr.where(left != tmp_nodata, left, right), ) - # Open all the data pointers data_arrays = [ open_rasterio( @@ -548,7 +547,6 @@ def reduce_func( attrs.update(tags) darray = darray.assign_attrs(**attrs) - if dtype is not None: attrs = darray.attrs.copy() return darray.astype(dtype).assign_attrs(**attrs) @@ -615,7 +613,10 @@ def concat( Returns: ``xarray.DataArray`` """ - if stack_dim.lower() not in ['band', 'time']: + if stack_dim.lower() not in ( + 'band', + 'time', + ): logger.exception(" The stack dimension should be 'band' or 'time'.") with rio_open(filenames[0]) as src_: @@ -690,6 +691,7 @@ def concat( if not concat_list[0].gw.config['ignore_warnings']: check_alignment(concat_list) + # Warp all images and concatenate along the 'time' axis into a DataArray src = xr.concat(concat_list, dim=stack_dim.lower()).assign_coords( time=new_time_names @@ -711,6 +713,7 @@ def concat( ] if not warp_list[0].gw.config['ignore_warnings']: check_alignment(warp_list) + src = xr.concat(warp_list, dim=stack_dim.lower()) src = src.assign_attrs(**{'filename': [Path(fn).name for fn in filenames]}) @@ -740,7 +743,7 @@ def concat( src.coords['time'] = parse_filename_dates(filenames) if band_names: - src.coords['band'] = band_names + src = src.assign_coords(band=band_names) else: if src.gw.sensor: if src.gw.sensor not in src.gw.avail_sensors: @@ -768,6 +771,8 @@ def concat( src = src.assign_attrs( **{'sensor': src.gw.sensor_names[src.gw.sensor]} ) + else: + src = src.assign_coords(band=range(1, src.gw.nbands + 1)) if dtype: attrs = src.attrs.copy() From e01fd54c4581acb1c7894fa3e9ba9af86821616d Mon Sep 17 00:00:00 2001 From: jgrss Date: Thu, 16 May 2024 20:37:31 +1000 Subject: [PATCH 2/4] add check for chunks type --- src/geowombat/backends/xarray_rasterio_.py | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/geowombat/backends/xarray_rasterio_.py b/src/geowombat/backends/xarray_rasterio_.py index fc7a8855..3ffb3d7c 100644 --- a/src/geowombat/backends/xarray_rasterio_.py +++ b/src/geowombat/backends/xarray_rasterio_.py @@ -10,6 +10,7 @@ import os import typing as T import warnings +from functools import singledispatch from pathlib import Path import numpy as np @@ -174,6 +175,25 @@ def default(s): return parsed_meta +@singledispatch +def check_chunks(chunks: dict) -> dict: + return chunks + + +@check_chunks.register +def _(chunks: tuple) -> dict: + return dict( + zip( + ( + 'band', + 'y', + 'x', + ), + chunks, + ) + ) + + def open_rasterio( filename: T.Union[str, Path, DatasetReader, WarpedVRT], nodata: T.Optional[T.Union[float, int]] = None, @@ -424,7 +444,11 @@ def open_rasterio( mtime = None token = tokenize(filename, mtime, chunks) name_prefix = f"open_rasterio-{token}" - result = result.chunk(chunks, name_prefix=name_prefix, token=token) + result = result.chunk( + check_chunks(chunks), + name_prefix=name_prefix, + token=token, + ) # Make the file closeable result.set_close(manager.close) From 05c9623e05d0c8a1fc790d57048627fc547518fa Mon Sep 17 00:00:00 2001 From: jgrss Date: Thu, 16 May 2024 20:38:05 +1000 Subject: [PATCH 3/4] add tests --- tests/test_open.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_open.py b/tests/test_open.py index fce07234..f486a781 100755 --- a/tests/test_open.py +++ b/tests/test_open.py @@ -91,6 +91,38 @@ def test_open_multiple(self): self.assertEqual(src.gw.nbands, 2) self.assertTrue(src.gw.has_band_dim) self.assertTrue(src.gw.has_band_coord) + self.assertEqual(src.band.values.tolist(), [1, 2]) + + with gw.open( + [ + l8_224078_20200518_B2, + l8_224078_20200518_B2, + l8_224078_20200518_B2, + ], + stack_dim='band', + ) as src: + self.assertEqual(src.band.values.tolist(), [1, 2, 3]) + + with gw.open( + [ + l8_224078_20200518_B2, + l8_224078_20200518_B2, + l8_224078_20200518, + ], + stack_dim='band', + ) as src: + self.assertEqual(src.band.values.tolist(), [1, 2, 3, 4, 5]) + + with gw.open( + [ + l8_224078_20200518_B2, + l8_224078_20200518_B2, + l8_224078_20200518_B2, + ], + stack_dim='band', + band_names=['a', 'b', 'c'], + ) as src: + self.assertEqual(src.band.values.tolist(), ['a', 'b', 'c']) def test_open_multiple_same(self): with gw.open( From 844e0507a73aeb53c0e419e9e32cc1a4c124e69a Mon Sep 17 00:00:00 2001 From: jgrss Date: Sat, 18 May 2024 13:07:47 +1000 Subject: [PATCH 4/4] add pip flag --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bbd88264..f973f94a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,8 +41,8 @@ jobs: pip install -U pip setuptools wheel pip install numpy GDAL_VERSION=$(gdal-config --version | awk -F'[.]' '{print $1"."$2}') - pip install GDAL==$GDAL_VERSION --no-cache-dir - pip install arosics + pip install GDAL==$GDAL_VERSION --no-cache-dir --no-binary=gdal + pip install arosics --no-deps - name: Install GeoWombat run: | pip install ".[stac,web,coreg,perf,tests]"