From e073b80d3ca38c0e85356edca6facee655c80b25 Mon Sep 17 00:00:00 2001 From: Tokarzewski Date: Tue, 17 Nov 2026 14:46:29 +0100 Subject: [PATCH] Skip result lines with an undeclared report id A result line whose id has no output slot for the current frequency raised a bare KeyError and stopped the whole file loading. Such lines are now skipped and logged at debug level. Fixes #39 Co-Authored-By: Claude Opus 5.5 --- .../processing/esofile_reader.py | 15 ++++++++-- tests/test_esofile_reader.py | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/db_eplusout_reader/processing/esofile_reader.py b/db_eplusout_reader/processing/esofile_reader.py index 02f769a..6246c20 100644 --- a/db_eplusout_reader/processing/esofile_reader.py +++ b/db_eplusout_reader/processing/esofile_reader.py @@ -1,5 +1,6 @@ """Parse the header and body of an EnergyPlus .eso file into raw output data.""" +import logging import re from collections import defaultdict, namedtuple from datetime import datetime @@ -14,6 +15,8 @@ from db_eplusout_reader.processing.esofile_time import EsoTimestamp from db_eplusout_reader.processing.raw_eso_data import RawOutputData +logger = logging.getLogger(__name__) + ENVIRONMENT_LINE = 1 TIMESTEP_OR_HOURLY_LINE = 2 DAILY_LINE = 3 @@ -308,8 +311,16 @@ def read_body(eso_file, highest_frequency_id, header, all_ids): ) else: # current line represents a result, replace nan values from the last step - res = float(line[0]) - raw_outputs.outputs[frequency][line_id][-1] = res + slots = raw_outputs.outputs.get(frequency, {}) + if line_id in slots: + slots[line_id][-1] = float(line[0]) + else: + # id not declared for this frequency, see issue #39 + logger.debug( + "Skipping result line with undeclared id %s (%s).", + line_id, + frequency, + ) except ValueError: if "End of Data" in raw_line: diff --git a/tests/test_esofile_reader.py b/tests/test_esofile_reader.py index 3ac6f5f..48e32ca 100644 --- a/tests/test_esofile_reader.py +++ b/tests/test_esofile_reader.py @@ -115,3 +115,31 @@ def test_duplicate_variable_with_and_without_schedule(self, tmp_path): outputs = result[0].outputs["hourly"] assert outputs[275] == [21.0] assert outputs[276] == [22.0] + + +class TestUndeclaredReportId: + """Result lines whose id has no slot for the current frequency (issue #39).""" + + def test_unknown_id_is_skipped(self, tmp_path): + eso = tmp_path / "unknown_id.eso" + body = _VALID_BODY.replace("7,20.0\n", "7,20.0\n99999,42.0\n") + eso.write_text(_HEADER + body) + result = process_eso_file(str(eso)) + assert result[0].outputs["hourly"][7] == [20.0] + assert 99999 not in result[0].outputs["hourly"] + + def test_id_declared_for_another_frequency_is_skipped(self, tmp_path): + # Id 7 is declared hourly; a copy of it after a daily step has no slot. + eso = tmp_path / "wrong_frequency.eso" + header = _HEADER + ( + "8,7,Environment,Site Day Type Index [] !Daily " + "[Value,Min,Hour,Minute,Max,Hour,Minute]\n" + ) + body = _VALID_BODY.replace( + "7,20.0\n", + "7,20.0\n3,1, 1, 1, 0,Tuesday\n8,3.0,3.0,1,60,3.0,1,60\n7,99.0\n", + ) + eso.write_text(header + body) + result = process_eso_file(str(eso)) + assert result[0].outputs["hourly"][7] == [20.0] + assert result[0].outputs["daily"][8] == [3.0]