Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jgrss/set band names #317

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
13 changes: 9 additions & 4 deletions src/geowombat/backends/xarray_.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ def reduce_func(
xr.where(left != tmp_nodata, left, right),
)


# Open all the data pointers
data_arrays = [
open_rasterio(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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_:
Expand Down Expand Up @@ -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
Expand All @@ -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]})
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
26 changes: 25 additions & 1 deletion src/geowombat/backends/xarray_rasterio_.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import typing as T
import warnings
from functools import singledispatch
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading