Skip to content

Commit

Permalink
io: fix netcdf IO for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianHofmann committed Jun 14, 2024
1 parent 8294530 commit 4171bec
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Release Notes
Upcoming Version
----------------

* The writing and reading from netcdf files was fixed to correctly handle the model `parameters` field.

Version 0.3.10
--------------
Expand Down
4 changes: 2 additions & 2 deletions linopy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ def with_prefix(ds, prefix):
if m.objective.value is not None:
objective = objective.assign_attrs(value=m.objective.value)
obj = [with_prefix(objective, "objective")]
params = [with_prefix(m.parameters, "params")]
params = [with_prefix(m.parameters, "parameters")]

scalars = {k: getattr(m, k) for k in m.scalar_attrs}
ds = xr.merge(vars + cons + obj + params, combine_attrs="drop_conflicts")
Expand Down Expand Up @@ -1008,7 +1008,7 @@ def get_prefix(ds, prefix):
m.objective.sense = objective.attrs.pop("sense")
m.objective._value = objective.attrs.pop("value", None)

m.parameters = get_prefix(ds, "parameter")
m.parameters = get_prefix(ds, "parameters")

for k in m.scalar_attrs:
setattr(m, k, ds.attrs.get(k))
Expand Down
2 changes: 2 additions & 0 deletions test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def model():

m.add_objective(2 * x + 3 * y)

m.parameters["param"] = xr.DataArray([1, 2, 3, 4], dims=["x"])

return m


Expand Down

0 comments on commit 4171bec

Please sign in to comment.