Skip to content

Reject 0D/1D DataArray in to_geotiff with the clear "Expected 2D or 3D" ValueError#3604

Merged
brendancol merged 3 commits into
xarray-contrib:mainfrom
brendancol:deep-sweep-error-handling-geotiff-2026-07-02
Jul 2, 2026
Merged

Reject 0D/1D DataArray in to_geotiff with the clear "Expected 2D or 3D" ValueError#3604
brendancol merged 3 commits into
xarray-contrib:mainfrom
brendancol:deep-sweep-error-handling-geotiff-2026-07-02

Conversation

@brendancol

Copy link
Copy Markdown
Contributor

Moves the "Expected 2D or 3D array" rank check in to_geotiff up front, ahead of backend dispatch and georef resolution. A 0D or 1D DataArray used to reach coords_to_transform (which reads dims[-2]) before the check and die with IndexError: tuple index out of range from _coords.py. A numpy array of the same rank, and a 4D DataArray, already got the clear ValueError; the guard now covers the DataArray case too.

The eager TIFF, .vrt, and GPU write paths all resolved georef before the check, so all three showed the bug. The guard sits before dispatch, so one check fixes every path. It reads ndim via getattr(data, "ndim", None), so numpy, cupy, dask, and DataArray inputs are covered, and a None input still falls through to the existing message.

Changes:

  • _writers/eager.py: early ndim guard in to_geotiff before GPU/VRT dispatch.
  • tests/test_edge_cases.py: 0D and 1D DataArray cases (TIFF and .vrt) asserting the ValueError type and message.

Checked: the new tests fail with IndexError before the change and pass after; the existing numpy 0D/1D/4D tests still pass unchanged; wider write-path suites are green (685 passed).

Found by the error-handling sweep.

A 0D/1D DataArray reached coords_to_transform (which reads dims[-2])
before the ndim check and raised an opaque IndexError from _coords.py,
while a numpy array of the same rank and a 4D DataArray already got the
clear 'Expected 2D or 3D array, got ND' ValueError. Hoist the check
ahead of backend dispatch and georef resolution so the eager TIFF, .vrt,
and GPU write paths all reject the same shapes identically.
@brendancol brendancol merged commit f0275b6 into xarray-contrib:main Jul 2, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant