Preserve CF time coordinate in read_netcdf#21
Merged
Conversation
read_netcdf stored coordinate and variable arrays in Float64-typed Dicts,
so a CF time axis written by write_netcdf(...; time=t) — which NCDatasets
decodes to DateTime — raised a convert error on read. Widen the
coordinates/variables containers to Any so the decoded time is preserved
(data["coordinates"]["time"]::Vector{DateTime}) alongside the Float64
spatial coordinates. Adds a CF-time round-trip test.
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
read_netcdfcrashed on any NetCDF file carrying a CF time coordinate — including files produced by our ownwrite_netcdf(...; time=t). NCDatasets decodes a CF time axis (units = "seconds since ...",calendar) toDateTime, butread_netcdfstored coordinates/variables inFloat64-typedDicts, so the assignment raised:Decoding to
DateTimeis the correct CF behavior; the bug was discarding it. This widens the"coordinates"/"variables"containers toAnyso the decoded time is preserved rather than coerced —data["coordinates"]["time"]::Vector{DateTime}— while spatial coordinates still come back asFloat64.Changes
src/io.jl—read_netcdf:coordinates/variablesdicts are nowAny-valued; docstring updated to describe the time-decoding behavior.test/io.jl— newread_netcdf preserves CF time coordinateround-trip test (write withtime=3600.0, assertVector{DateTime}decoding to1970-01-01T01:00:00, spatial coord stillFloat64).CHANGELOG.md— Fixed entry.Testing
TEST_GROUP=iopasses 306/306 locally, including the new test.Note (not in this PR)
grid_from_netcdfhas the same root cause: its auto dimension-inference doesFloat64.(Array(ds[name]))over every coordinate, so a CF time axis crashes it too, and it would then mishandle the time dimension on data variables. Fixing that to load a CF-time file into a spatial regular grid needs time-slice selection (a small design decision) and is left for a separate change.