Skip to content

add_variables drops non-dimension coordinates since #1078 #1084

Open

Description

@MaykThewessen

Note

Drafted with AI assistance. The repro was run on both commits below.

Version Checks

  • I have confirmed this bug exists on the latest release of Linopy.
  • I have confirmed this bug exists on the current master branch of Linopy (b1d6a2e).

Issue Description

Since #1078, add_variables drops non-dimension coordinates of an xr.Coordinates. The new fast path _variables_on (linopy/model.py:1016) builds its grid with _coords_to_dict, which keeps only dimension coordinates. The coordinates are gone before any constraint or netcdf code runs. This undoes #1024, whose test still passes because both sides of the round trip lack the coordinate.

@FabianHofmann I can open a small fix PR: fall back to the old path in _variables_on when coords carries non-dimension coordinates, and add an assert "time" in x.coords to the #1024 test. OK?

Reproducible Example

import numpy as np, xarray as xr, linopy

coords = xr.Coordinates({"snapshot": np.arange(4), "period": ("snapshot", [2020, 2020, 2030, 2030])})
x = linopy.Model().add_variables(lower=0, coords=coords, name="x")
print(list(x.coords))
# b1d6a2e9 (#1078): ['snapshot']
# 81891602 (before): ['snapshot', 'period']

Expected Behavior

x.coords keeps period, as before #1078.

Installed Versions

linopy master b1d6a2e, Python 3.12.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions