Skip to content

Fix read_geotiff_gpu dim mismatch on stripped multi-band TIFFs#1525

Merged
brendancol merged 1 commit intoxarray-contrib:mainfrom
brendancol:fix/gpu-stripped-multiband-dims
May 9, 2026
Merged

Fix read_geotiff_gpu dim mismatch on stripped multi-band TIFFs#1525
brendancol merged 1 commit intoxarray-contrib:mainfrom
brendancol:fix/gpu-stripped-multiband-dims

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

The stripped-fallback branch in read_geotiff_gpu hardcoded dims=['y','x'] even when the underlying CPU reader returned a 3-D (y, x, band) array for multi-band stripped files. That raised ValueError: dimensions ('y', 'x') must have the same length as the number of data dimensions, ndim=3.

The fix mirrors the tiled-branch logic already used a few lines later in the same function: when arr_gpu.ndim == 3 use ('y', 'x', 'band') and add a band coord; otherwise keep ('y', 'x').

Reproducer

import numpy as np, xarray as xr, tempfile, os
from xrspatial.geotiff import to_geotiff, read_geotiff_gpu

data = np.random.randint(0, 200, size=(64, 96, 3), dtype=np.uint8)
da = xr.DataArray(data, dims=['y','x','band'])
with tempfile.TemporaryDirectory() as d:
    p = os.path.join(d, 'wt.tif')
    to_geotiff(da, p, tiled=False)
    out = read_geotiff_gpu(p)              # raised ValueError before this fix
    assert out.dims == ('y','x','band')

Tests

xrspatial/geotiff/tests/test_gpu_stripped_multiband.py covers:

  • 3-band uint8 stripped TIFF round-trip
  • 2-band uint16 stripped TIFF round-trip
  • Single-band stripped sanity check (2-D path still works)

Tests use the same cupy + CUDA skip pattern as test_gpu_byteswap_1508.py.

$ pytest xrspatial/geotiff/tests/test_gpu_stripped_multiband.py -x -q
3 passed

$ pytest xrspatial/geotiff/tests/ -x -q -k "gpu and (stripped or multi or band)"
26 passed, 768 deselected

Context

Audit pass following PRs #1523 / #1524 on the geotiff GPU read path.

Test plan

  • New test module passes locally with cupy + CUDA available
  • Existing GPU stripped/multi/band tests still pass
  • CI green

The stripped fallback inside read_geotiff_gpu hardcoded dims=['y','x']
even when the CPU read returned a 3-D (y, x, band) array for multi-band
stripped files. That raised a ValueError about dimension count.

Mirror the tiled-branch logic: pick ('y', 'x', 'band') with a band coord
when the CPU array is 3-D, otherwise keep the 2-D ('y', 'x') path.

Adds xrspatial/geotiff/tests/test_gpu_stripped_multiband.py covering the
3-band uint8, 2-band uint16, and single-band sanity cases. Audit pass
following PRs xarray-contrib#1523/xarray-contrib#1524.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label May 8, 2026
@brendancol brendancol requested a review from Copilot May 8, 2026 22:11
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a shape/dimension mismatch in read_geotiff_gpu for stripped (non-tiled) multi-band GeoTIFFs. Previously, the stripped-file branch always constructed a 2-D DataArray with dims=['y', 'x'], even when the CPU reader returned a 3-D (y, x, band) array, causing an xarray ValueError. The change makes the stripped path mirror the already-correct tiled path behavior.

Changes:

  • Update read_geotiff_gpu stripped-file fallback to set dims=('y','x','band') and add a band coord when the uploaded GPU array is 3-D.
  • Add GPU regression tests covering stripped multi-band round-trips (uint8/uint16) and ensuring single-band remains 2-D.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
xrspatial/geotiff/__init__.py Fix stripped (non-tiled) read_geotiff_gpu DataArray construction to handle 3-D multi-band arrays consistently with the tiled path.
xrspatial/geotiff/tests/test_gpu_stripped_multiband.py Add regression tests for stripped multi-band GPU reads and a sanity check for single-band behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@brendancol brendancol merged commit 90167dd into xarray-contrib:main May 9, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants