From 4171bece2b6d89294d681cd1cfaa24625433d889 Mon Sep 17 00:00:00 2001 From: Fabian Date: Fri, 14 Jun 2024 10:19:45 +0200 Subject: [PATCH] io: fix netcdf IO for parameters --- doc/release_notes.rst | 1 + linopy/io.py | 4 ++-- test/test_io.py | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/release_notes.rst b/doc/release_notes.rst index 4ce400b7..e77cdacc 100644 --- a/doc/release_notes.rst +++ b/doc/release_notes.rst @@ -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 -------------- diff --git a/linopy/io.py b/linopy/io.py index 779d3636..3d8cda02 100644 --- a/linopy/io.py +++ b/linopy/io.py @@ -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") @@ -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)) diff --git a/test/test_io.py b/test/test_io.py index 8bca1a0b..6def28c3 100644 --- a/test/test_io.py +++ b/test/test_io.py @@ -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