Skip to content

fix(writers): accept fmt as an alias for to_netcdf format - #3453

Open
Manny7717 wants to merge 1 commit into
pytroll:mainfrom
Manny7717:fix/cf-fmt-alias
Open

fix(writers): accept fmt as an alias for to_netcdf format#3453
Manny7717 wants to merge 1 commit into
pytroll:mainfrom
Manny7717:fix/cf-fmt-alias

Conversation

@Manny7717

Copy link
Copy Markdown

What

Fixes #2685: save_datasets(writer='cf', fmt='nc', ...) raised TypeError: Dataset.to_netcdf() got an unexpected keyword argument 'fmt'. The fmt kwarg was forwarded verbatim into xarray's Dataset.to_netcdf(), which expects format, not fmt.

Change

CFWriter.save_datasets now pops fmt from the forwarded kwargs and passes it through as format, mapping the common 'nc' shorthand to 'NETCDF4'. Passing both fmt and format with different values raises a clear error.

Tests

  • New test_save_array_fmt_nc_alias: saves with fmt='nc' and verifies the file round-trips. Fails on the previous implementation (TypeError; verified) and passes with the fix.
  • Full test_cf.py: 32 passed. ruff clean.

save_datasets() forwarded the fmt kwarg straight into xarray's
Dataset.to_netcdf(), which does not accept it (TypeError: got an
unexpected keyword argument 'fmt'). Accept fmt as an alias for
xarray's format kwarg, mapping the common 'nc' shorthand to
'NETCDF4'. Closes pytroll#2685
@djhoese

djhoese commented Sep 12, 2026

Copy link
Copy Markdown
Member

I'm confused by this pull request and even @mraspaud's original issue. Where is the format kwarg supposed to go? Why was it specified in @mraspaud's use case? Was he trying to pass the netcdf model to use like in xarray's to_netcdf?

https://docs.xarray.dev/en/latest/generated/xarray.Dataset.to_netcdf.html#xarray.Dataset.to_netcdf

Because in the issue Martin was passing format="nc" which is not an accepted value to that function in xarray anyway.

Regardless, why does Satpy need to use fmt instead of format? Also, maybe we shouldn't pass arbitrary kwargs through to xarray with **kwargs but instead have an netcdf_kwargs= or xarray_kwargs or to_netcdf_kwargs that gets passed along?

CC @pnuu @mraspaud @gerritholl and maybe @sfinkens (I think you did CF writer stuff?)

@codecov

codecov Bot commented Sep 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 96.33%. Comparing base (17bf08d) to head (116a299).
⚠️ Report is 39 commits behind head on main.

Files with missing lines Patch % Lines
satpy/writers/cf_writer.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3453      +/-   ##
==========================================
- Coverage   96.34%   96.33%   -0.02%     
==========================================
  Files         465      465              
  Lines       59151    59153       +2     
==========================================
- Hits        56990    56983       -7     
- Misses       2161     2170       +9     
Flag Coverage Δ
behaviourtests 3.58% <0.00%> (-0.01%) ⬇️
unittests 96.42% <92.85%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pnuu

pnuu commented Sep 12, 2026

Copy link
Copy Markdown
Member

The original original issue was in Trollflow2, where we pass the format among **kwargs to scn.save_dataset() along with other kwargs that are not pop'd. I think we should be more explicit there what are passed to Satpy.

Some relevant points in Trollflow2:

I actually don't follow anymore what's happening in the Trollflow2 code, and one of my goals with the Claude subscription would be to simplify it as much as possible. Remove all in-place product list operations and such.

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.

Format keyword "nc" for save_dataset issues an error

3 participants