csiro-coasts / emsarray

xarray extension that supports EMS model formats
BSD 3-Clause "New" or "Revised" License
13 stars 2 forks source link

Handle case where transect line does not intersect dataset geometry #119

Closed mx-moth closed 10 months ago

mx-moth commented 1 year ago

Currently a cryptic error message is raised when constructing the distance bounds data array.

Detecting this case is as simple as intersecting the line with the dataset, or checking if the Transect.segments list is empty.

The solution is either to generate an empty transect plot, or to raise a EmptyTransect error. Neither option is clearly preferable to the other.

mx-moth commented 1 year ago

Constructing a DataArray where the first dimension has a size of 0 is possible if you do it explicitly:

xarray.DataArray(
    data=numpy.zeros(shape=(0, 2)),
    dims=['segment', 'bounds'],
)
mx-moth commented 1 year ago

Constructing a multi-dimensional DataArray from an iterator with size 0 can be done properly if everything is explicit:

xarray.DataArray(
    data=numpy.fromiter(
        ((left, right) for left, right in self.segments),
        count=len(self.segments),
        dtype=numpy.dtype((float, 2)),
    ),
    dims=['segments', 'bounds'],
)