Repository navigation
fix: keep auxiliary coordinates of dense containers in netcdf round trip - #1024
Open
MaykThewessen wants to merge 1 commit into
Open
MaykThewessen wants to merge 1 commit into
MaykThewessen wants to merge 1 commit into
Conversation
read_netcdf dropped every non-index coordinate except MultiIndex levels to keep out the scalar coordinates of other containers that xarray attaches when selecting one container's data variables. That also dropped the auxiliary coordinates of dense variables, constraints, expressions and parameters, such as the levels on a flat dimension that v1 recommends instead of a MultiIndex. Only drop scalar coordinates owned by another container, matched on the longest prefix since names may contain dashes. Coordinates with dimensions only attach to the container whose dimensions they share. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MaykThewessen
added a commit
to MaykThewessen/linopy
that referenced
this pull request
Oct 8, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merging this PR will not alter performance
Comparing Footnotes
|
5 tasks done
This branch has not been deployed
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.
Note
AI-assisted. Claude wrote the code and tests, and ran the checks listed below. I reviewed the diff.
Changes proposed in this Pull Request
read_netcdfdropped the auxiliary (non-index) coordinates of every dense variable, constraint, named expression and ofModel.parameters, also for plain naive values. With v1 recommending a flat dimension with levels as auxiliary coordinates instead of aMultiIndex, a round trip through netcdf (and sosolve(remote=...)) loses those levels. Frozen constraints already kept theirs.The drop in
get_prefixwas there because selecting one container's data variables from the merged dataset attaches the scalar coordinates of all other containers (e.g.constraints-c0-snapshotafterx.sel(snapshot=0) >= 1). Coordinates with dimensions can only attach to the container whose dimensions they share, so the read now drops only scalar coordinates that belong to another container, matched on the longest prefix because names may contain-. This also restores a container's own scalar coordinates, and makes the separateMultiIndexlevel bookkeeping inget_prefixunnecessary.Overlap with #1021: both touch
read_netcdf/get_prefixinlinopy/io.pyand merge without textual conflicts. The tests of both pass in either merge order. #1025 (dash-named parameters) also touchesget_prefix: it adds anowner()lookup by longest container prefix, and whichever of #1024 and #1025 lands second should use it in place of the prefix split here, otherwise a dash-named parameter can take the scalar coords of another parameter (details in #1025).Checklist
AGENTS.md).doc.doc/release_notes.rstof the upcoming release is included.What was checked (AI-generated)
test_model_to_netcdf_aux_coordsintest/test_io.py: a flatsnapshotdimension withtime(datetime) andperiodaux coords on a variable, a dense constraint, a named expression and a parameter, plus a constraintc-firstfromx.sel(snapshot=0)whose scalar coords would leak intocunder a plain prefix match. Assertsassert_model_equal, which compares coordinates. Fails on master and passes on the branch, under both legacy and v1 semantics.MultiIndexlevels; probing a file shows the coords xarray attaches on selection are the scalar ones of other containers.test/test_io.py,test/test_csr.pyand the other netcdf users (test_sos_reformulation,test_indicator_constraints,test_dtypes,test_scaling,test_piecewise_constraints,test_fix_relax) pass: 1864 passed. The 6 failures areTestSignParameterSOS2 cases intest_piecewise_constraints.py(HiGHS here has no SOS support) and fail identically with master'sio.py.ruff check,ruff format --checkclean;mypyreports no errors in the changed files (2 existing ones insolvers.pyandremote/oetc.py).fix/netcdf-tz-aware-coords): no textual conflicts, andtest/test_io.pyplustest/test_csr.pypass on the merge (1048 passed). With both merged, dense tz-aware aux coords on a variable, constraint and expression come back with their zone (datetime64[ns, Europe/Amsterdam]).-(e.g.m.parameters["my-param"]) is silently dropped byread_netcdfon master, becausehas_prefixsplits on the last-. Separate fix.🤖 Generated with Claude Code