Support NetCDF output on any AbstractVerticalCoordinate#5689
Conversation
The NCDatasets extension dispatched `vertical_coordinate_name`, `gather_vertical_dimensions`, and `default_vertical_dimension_attributes` only on the concrete `StaticVerticalDiscretization` and `MutableVerticalDiscretization`, so `NetCDFWriter` threw a `MethodError` on a grid whose vertical coordinate is any other `AbstractVerticalCoordinate` subtype (e.g. one defined in a downstream package). Widen the two non-static methods to dispatch on `AbstractVerticalCoordinate` and add a `vertical_coordinate_name(::AbstractVerticalCoordinate) = "r"` fallback. Such coordinates save their reference (Lagrangian) coordinate `r` (the shared `cᵃᵃᶠ`/`cᵃᵃᶜ` fields); physical height is reconstructed at read time from `r` and the coordinate transform, identical to the existing z-star path. `Static` keeps its own method, and `MutableVerticalDiscretization` behaviour is unchanged (`MVD <: AbstractVerticalCoordinate`). Verified by writing a NetCDF file from a `NetCDFWriter` on a grid whose vertical coordinate is a downstream `AbstractVerticalCoordinate` subtype. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add a unit test that a custom non-static `AbstractVerticalCoordinate` subtype (standing in for one defined in a downstream package) routes to the generic `r` fallback in `vertical_coordinate_name`, while `StaticVerticalDiscretization` still maps to `z` and `MutableVerticalDiscretization` still maps to `r`. The `r` NetCDF write path itself is covered end-to-end by `test_netcdf_rectilinear_mvd_output`, which now dispatches through the widened `::AbstractVerticalCoordinate` methods. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
🇬🇧
just a correction here, the reference is not always Lagrangian |
Per review: the reference coordinate `r` is only Lagrangian for z-star. A general `AbstractVerticalCoordinate` (e.g. a terrain-following σ coordinate) uses `r` as a fixed reference/computational coordinate. Drop "(Lagrangian)" from the now-generic comments and the NetCDF `long_name` attributes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Good catch — fixed in bbbfdd7. Dropped "(Lagrangian)" from the now-generic comments and the NetCDF |
I'll review this PR in just a second, but here's a question: why is |
Summary
NetCDFWritererrored with aMethodErroron any grid whose vertical coordinate is anAbstractVerticalCoordinateother than the two concrete built-ins. The NCDatasets extension dispatchedvertical_coordinate_name,gather_vertical_dimensions, anddefault_vertical_dimension_attributesonly onStaticVerticalDiscretizationandMutableVerticalDiscretization, so a third subtype matched no method and died at writer construction.This came up with a terrain-following σ coordinate defined downstream — Breeze.jl's
TerrainFollowingVerticalDiscretization, which<: AbstractVerticalCoordinate— but the fix is generic.Change
gather_vertical_dimensions,default_vertical_dimension_attributes) from::MutableVerticalDiscretizationto::AbstractVerticalCoordinate.vertical_coordinate_name(::AbstractVerticalCoordinate) = "r"fallback.Such coordinates save their reference coordinate
r(read from the sharedcᵃᵃᶠ/cᵃᵃᶜfields); physical height is reconstructed at read time fromrand the coordinate transform — identical to the existing z-star path.Why it's safe
MutableVerticalDiscretizationbehavior is unchanged:MVD <: AbstractVerticalCoordinateand the method bodies are byte-identical, so MVD now simply arrives via the supertype.StaticVerticalDiscretizationkeeps its own, more specific method (still writes physicalz).MutableVerticalDiscretization, so a static-terrain coordinate correctly skips it.Testing
test_netcdf_abstract_vertical_coordinate_name): a custom non-MVDAbstractVerticalCoordinateroutes to the"r"fallback, whileStatic→"z"andMVD→"r"still hold.test_netcdf_rectilinear_mvd_output(z-star) now exercises the widened::AbstractVerticalCoordinatemethods end-to-end (regression for therwrite path).NetCDFWriterconstructs and writes on a downstream terrain-following grid: referencerdimension + staggered fields present.🤖 Generated with Claude Code