Skip to content

Preserve CF time coordinate in read_netcdf#21

Merged
mmbell merged 1 commit into
mainfrom
fix/read-netcdf-cf-time
Jul 15, 2026
Merged

Preserve CF time coordinate in read_netcdf#21
mmbell merged 1 commit into
mainfrom
fix/read-netcdf-cf-time

Conversation

@mmbell

@mmbell mmbell commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

read_netcdf crashed on any NetCDF file carrying a CF time coordinate — including files produced by our own write_netcdf(...; time=t). NCDatasets decodes a CF time axis (units = "seconds since ...", calendar) to DateTime, but read_netcdf stored coordinates/variables in Float64-typed Dicts, so the assignment raised:

MethodError: Cannot `convert` an object of type Dates.DateTime to an object of type Float64

Decoding to DateTime is the correct CF behavior; the bug was discarding it. This widens the "coordinates" / "variables" containers to Any so the decoded time is preserved rather than coerced — data["coordinates"]["time"]::Vector{DateTime} — while spatial coordinates still come back as Float64.

Changes

  • src/io.jlread_netcdf: coordinates/variables dicts are now Any-valued; docstring updated to describe the time-decoding behavior.
  • test/io.jl — new read_netcdf preserves CF time coordinate round-trip test (write with time=3600.0, assert Vector{DateTime} decoding to 1970-01-01T01:00:00, spatial coord still Float64).
  • CHANGELOG.md — Fixed entry.

Testing

TEST_GROUP=io passes 306/306 locally, including the new test.

Note (not in this PR)

grid_from_netcdf has the same root cause: its auto dimension-inference does Float64.(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.

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

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/io.jl 83.22% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mmbell
mmbell merged commit fdf285c into main Jul 15, 2026
25 checks passed
@mmbell
mmbell deleted the fix/read-netcdf-cf-time branch July 15, 2026 02:10
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