Skip to content

feat(netcdf output)!: Produce netCDF-files that follow CF conventions - #549

Open
mpvginde wants to merge 5 commits into
mainfrom
refactor/cf-aligned-netcdf
Open

feat(netcdf output)!: Produce netCDF-files that follow CF conventions#549
mpvginde wants to merge 5 commits into
mainfrom
refactor/cf-aligned-netcdf

Conversation

@mpvginde

Copy link
Copy Markdown
Contributor

Currently the netCDF output produced by inference is quite minimal in terms of coordinate metadata.

image

Some issues:

  • latitude and longitude are not marked as coordinate variables
  • one single time coordinate
    • may lead to loss of information if used together with write_initial_state: false.
    • prevents use of xr.open_mfdatasets()
  • generally no CF conventions are followed.

This PR updates the netCDF writing, bringing the netCDF-output in line with CF conventions. Which in term provides better xarray-handling

image

As a contributor to the Anemoi framework, please ensure that your changes include unit tests, updates to any affected dependencies and documentation, and have been tested in a parallel setting (i.e., with multiple GPUs). As a reviewer, you are also responsible for verifying these aspects and requesting changes if they are not adequately addressed. For guidelines about those please refer to https://anemoi.readthedocs.io/en/latest/

By opening this pull request, I affirm that all authors agree to the Contributor License Agreement.

@gmertes gmertes changed the title refactor(netcdf-output)!: Produce netCDF-files that follow CF conventions feat(netcdf output)!: Produce netCDF-files that follow CF conventions Jul 14, 2026

@gmertes gmertes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I'll defer to others with more knowledge of CF conventions to review that part. We may also want to present this at ATS just for visibility, there may be other users relying on the current behaviour.

Comment thread src/anemoi/inference/testing/checks.py Outdated
@HCookie HCookie moved this from To be triaged to Now In Progress in Anemoi-dev Jul 14, 2026
@gmertes

gmertes commented Jul 15, 2026

Copy link
Copy Markdown
Member

Note to maintainers: let's do a release before merging.

@gmertes
gmertes requested a review from aaron-hopkinson July 15, 2026 09:24
if reference_date := getattr(self.context, "reference_date", None):
self.reference_date = reference_date
# start date of the forecast
reference_date = getattr(self.context, "reference_date", None) or state["date"]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmertes , I guess here the wrong reference_date can be set if the context doesn't contain reference_date (when does this happen?) and write_initial_state=False. I guess in that case state["date"] contains the valid time of the first timestep?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More tech debt 🥰

The getattr can be replaced with a simple self.context.reference_date: after the recent Runner refactor, the context (runner) will always have the reference_date attribute (see here), but it can be None.

It will be None when no date is set in the config, and then we rely on the input to find the date in the input file. The input will attach the date it found to the state, and indeed that's where we then find it.

The logic is messy and could be improved, but I don't think there is a scenario where the reference date can be wrong or diverge.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, makes sense now. See some thoughts on improving here: #551

@gmertes gmertes added ATS Approved Approved by ATS and removed ATS Approval needed labels Jul 16, 2026
@HCookie HCookie added the enhancement New feature or request label Jul 22, 2026

@aaron-hopkinson aaron-hopkinson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor changes (typos).

I think we need to include a global attribute that states the version of the conventions that the data adheres to, see:
https://cfconventions.org/Data/cf-conventions/cf-conventions-1.13/cf-conventions.html#identification-of-conventions

self.reference_date_var.standard_name = "forecast_reference_time"
self.reference_date_var.long_name = "start time of forecast"
self.reference_date_var.units = f"seconds since {EPOCH}"
self.reference_date_var.calender = CALENDAR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

Suggested change
self.reference_date_var.calender = CALENDAR
self.reference_date_var.calendar = CALENDAR

self.time_var.axis = "T"

# forecast period / lead time
# time dimension auxilary coordinate

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# time dimension auxilary coordinate
# time dimension auxiliary coordinate

self.period_var.units = "seconds"

# latitude / longitude
# values dimension auxilary coordinates

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# values dimension auxilary coordinates
# values dimension auxiliary coordinates

@github-project-automation github-project-automation Bot moved this from Now In Progress to Under Review in Anemoi-dev Jul 22, 2026
@aaron-hopkinson

Copy link
Copy Markdown
Contributor

@mpvginde Just out of interest, have you tried passing data outputted from this change through https://github.com/cedadev/cf-checker? That would be a good way of checking for CF compliance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ATS Approved Approved by ATS enhancement New feature or request

Projects

Status: Under Review

Development

Successfully merging this pull request may close these issues.

4 participants