Skip to content

Conversation

@ecomodeller
Copy link
Member

@ecomodeller ecomodeller commented Jan 16, 2026

Summary

  • Add Geometry0D class for time-series-only data
  • Make dims a derived property from geometry.spatial_dims
  • Add reduce() methods to geometries for spatial aggregations
  • Add deprecation warnings for v4.0 migration path
  • Use @cached_property for dims and _has_time_axis for performance optimization

Breaking changes

  • dims parameter deprecated (FutureWarning) - dimensions now derived from geometry
  • squeeze() method deprecated (FutureWarning) - use isel() instead
  • Non-equidistant grid selections now raise ValueError immediately (DFS format compliance)
  • GeometryUndefined deprecated - use Geometry0D

Historical Context: The dims Parameter

The dims parameter was introduced in January 2022 (commit 1133864f) with a guessing heuristic:

if dims is None:  # This is not very robust, but is probably a reasonable guess
    if data.ndim == 1:
        self.dims = ("t",)
    elif data.ndim == 2:
        self.dims = ("t", "x")
    # ... etc

The original comment "This is not very robust, but is probably a reasonable guess" acknowledged this was always a heuristic, not a proper solution.

The Core Problem

The dims parameter was copied from xarray's API (for xarray integration), but created a false impression:

  • xarray supports arbitrary dimension names: ("lat", "lon", "depth", "ensemble_member")
  • DFS format does NOT: dimensions are fixed by file structure
    • Dfs0/Grid1D: time, x
    • Grid2D: time, y, x
    • Grid3D: time, z, y, x
    • DFSU: time, element

Allowing users to pass custom dims created an illusion of possibilities not supported by the DFS format. Custom dims wouldn't persist through write/read cycles.

The Solution

This PR completes the work started in March 2024 to make dims truly derivable:

  • March 2024 (aa1afc15): Started delegating dims to geometry classes
  • January 2026 (04e1e2d9, a7931470): Made dims a derived property, removed parameter
  • This PR: Cache the derived property for performance, fix return types

Dimensions are now always derived from geometry, eliminating the illusion that they could be arbitrary and ensuring they correctly represent the actual DFS file structure.

- Add Geometry0D class for time-series-only data (dfs0)
- Add reduce() methods to geometries for spatial aggregations
- Make dims a derived property from geometry.spatial_dims
- Rename default_dims to spatial_dims across all geometries
- Add deprecation warnings for v4.0 migration:
  - dims parameter usage
  - squeeze() method
  - Non-equidistant isel() operations
  - GeometryUndefined instantiation
- Update spectral geometries with proper spatial_dims

BREAKING CHANGES (with deprecation warnings):
- dims parameter is deprecated, dimensions derived from geometry
- squeeze() method deprecated, use isel() instead
- Non-equidistant grid selections will error in v4.0
- GeometryUndefined deprecated, use Geometry0D
Replace FutureWarning + GeometryUndefined pattern with immediate
ValueError. This fails fast when users attempt non-equidistant
selections that cannot be written to DFS format, avoiding late
surprises in their workflows.

- Grid2D.isel(), Grid3D.isel(), Grid3D._index_to_Grid3D(),
  Grid3D._geometry_for_layers() now raise ValueError immediately
- Remove deprecation warning from GeometryUndefined.__init__
  (class kept for backwards compatibility but no longer instantiated)
- Update tests to expect ValueError instead of FutureWarning
- Add reduce() method to _Geometry base class
- Rename spatial_dims to dims across all geometry classes
- Remove axis_name from Grid1D and axis_names from Grid2D
- Grid1D.isel returns Geometry0D instead of GeometryUndefined
- Update tests to use Geometry0D and expect "x" dimension from Grid1D
- Remove deprecated squeeze() and dims parameter usage from tests
Performance improvements and type corrections:
- Use @cached_property for dims and _has_time_axis in DataArray
- Eliminates redundant computation in hot paths (isel, aggregate, etc.)
- dims accessed 36+ times across codebase, now computed only once
- Fix return types for geometry.reduce() methods (Grid2D, Grid3D)
- Update base _Geometry.reduce() to return _Geometry for covariance
- Remove dims= from squeeze(), isel(), aggregate(), and quantile() methods
- Dims are now always derived from geometry via @cached_property
- Update docstring to mark dims parameter as deprecated
- All 755 tests pass - dims derivation from geometry works correctly
- Remove dfsu, dfs0, dfs2, and nc files from notebooks/
- Add gitignore rules to prevent committing notebook outputs
@ecomodeller ecomodeller marked this pull request as ready for review January 17, 2026 14:53
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.

2 participants