Skip to content

1.5 — Load already-saved EDisGo grids in setup_grid (topology/ layout + config reset) #670

Description

@joda9
  • File: edisgo/run/tasks/grid.pytask_setup_grid (~line 64)
  • Branch: fix/setup-grid-saved-edisgo

Problem. setup_grid always assumes a raw ding0 dir
(EDisGo(ding0_grid=...)). For an already-saved EDisGo object (CSVs in a
topology/ subfolder + configs.json) it fails with
KeyError: ['mv_grid_id','lv_grid_id','in_building'] not in index. After loading,
a stale configs.json (older schema) then raises
KeyError: 'mv_max_voltage_deviation' downstream (in select_cable).

Change. Detect the topology/ layout, load via import_edisgo_from_files,
and reset config to current defaults. (Two coupled aspects — format detection +
config reset; review as two commits if preferred.)

@@ def task_setup_grid
     """
+    import os
+
     from edisgo import EDisGo
 
     grid_cfg = ctx.raw_config.get("grid", {})
@@
     if legacy_ding0_grids is None:
         legacy_ding0_grids = grid_cfg.get("legacy_ding0_grids", False)
 
-    if edisgo is None:
+    # Detect grid format: a saved EDisGo object has its CSVs in a
+    # 'topology' subfolder (+ configs.json) and must be loaded via
+    # import_edisgo_from_files. A raw ding0 grid has the CSVs directly
+    # in the directory and is loaded via the ding0 importer.
+    is_saved_edisgo = os.path.isdir(os.path.join(str(ding0_path), "topology"))
+
+    if is_saved_edisgo:
+        from edisgo.edisgo import import_edisgo_from_files
+
+        ctx.logger.info(
+            f"setup_grid: '{ding0_path}' looks like a saved EDisGo object "
+            f"(topology/ subfolder) -> loading via import_edisgo_from_files."
+        )
+        from edisgo.tools.config import Config
+
+        edisgo = import_edisgo_from_files(
+            edisgo_path=str(ding0_path),
+            import_topology=True,
+        )
+        edisgo.legacy_grids = False
+        # The saved configs.json can stem from an older eDisGo version with a
+        # different config schema (e.g. missing 'mv_max_voltage_deviation').
+        # Reset to the current defaults so downstream tasks find all keys.
+        edisgo._config = Config()
+    elif edisgo is None:
         edisgo = EDisGo(
             ding0_grid=str(ding0_path),
             legacy_ding0_grids=legacy_ding0_grids,
         )
     else:
         edisgo.import_ding0_grid(
             path=str(ding0_path), legacy_ding0_grids=legacy_ding0_grids
         )

Verify. Pointing grid.ding0_path at a saved-EDisGo dir loads the topology
and the pipeline reaches import_generators; raw ding0 dirs still load unchanged.

Part of #665.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions