Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions db_eplusout_reader/processing/esofile_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@
# The schedule name is parsed but does not take part in variable identity.
HEADER_PATTERN = re.compile(r"^(\d+),(\d+),(.*?)(?:,(.*?) ?\[| ?\[)(.*?)\] !(\w*)(?:,(.*))?")

# e.g. ' Version 25.2.0-cf7368216c'
VERSION_PATTERN = re.compile(r"Version (\d+)\.(\d+)\.(\d+)")

# Files from EnergyPlus 8.9 onwards declare a sixth standard item (annual).
ANNUAL_ITEM_MIN_VERSION = (8, 9, 0)


def get_eso_file_version(raw_version):
"""Return eso file version as an integer (i.e.: 860, 890)."""
version = raw_version.strip()
start = version.index(" ")
return int(version[(start + 1) : (start + 6)].replace(".", ""))
"""Return eso file version as a (major, minor, patch) tuple (i.e.: (8, 9, 0), (25, 2, 0)).

A tuple compares correctly across one- and two-digit major versions,
which a concatenated integer does not ('25.2.0' -> 252 < 890).
"""
match = VERSION_PATTERN.search(raw_version)
if match is None:
raise InvalidLineSyntax("Cannot parse EnergyPlus version: '{}'!".format(raw_version))
return tuple(int(part) for part in match.groups())


def get_eso_file_timestamp(timestamp):
Expand Down Expand Up @@ -314,7 +325,7 @@ def read_file(file):
"""Read raw EnergyPlus output file."""
# process first few standard lines, ignore timestamp
version, _ = process_statement_line(next(file))
last_standard_item_id = 6 if version >= 890 else 5
last_standard_item_id = 6 if version >= ANNUAL_ITEM_MIN_VERSION else 5

# Skip standard reporting frequencies
for _ in range(last_standard_item_id):
Expand Down
7 changes: 6 additions & 1 deletion db_eplusout_reader/processing/esofile_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def get_month_n_days_from_cumulative(monthly_cumulative_days):

def find_num_of_days_annual(ann_num_of_days, rp_num_of_days):
"""Use runperiod data to calculate number of days for each annual period."""
if not ann_num_of_days:
# design-day environments report no annual rows
return []
days = rp_num_of_days[0] // len(ann_num_of_days)
return [days for _ in ann_num_of_days]

Expand Down Expand Up @@ -110,7 +113,9 @@ def set_start_date(orig, refs):
timestep_to_monthly_dates = {k: dates[k] for k in dates if k in [TS, H, D, M]}
if timestep_to_monthly_dates:
for frequency in (M, A, RP):
if frequency in dates:
# A design-day environment declares annual variables but never
# reports an annual row, so its annual dates can be empty.
if dates.get(frequency):
dates[frequency] = set_start_date(dates[frequency], timestep_to_monthly_dates)
return dates

Expand Down
17 changes: 17 additions & 0 deletions tests/test_db_esofile_single_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,20 @@ def test_get_results_with_single_env_eso_path(self, single_env_eso_path):
class TestValidateTime:
def test_validate_time_no_boundaries(self, single_env_eso_file):
assert single_env_eso_file._validate_time(datetime(2019, 1, 1), None, None) is True


class TestTwoDigitMajorVersion:
"""Regression for issue #32: EnergyPlus 10.0+ version strings were mis-parsed."""

@pytest.fixture(scope="class")
def modern_eso_file(self, tmp_path_factory):
header = _HEADER.replace("Version 9.2.0-921312fa1d", "Version 25.2.0-cf7368216c")
path = tmp_path_factory.mktemp("modern") / "modern.eso"
path.write_text(header + _SINGLE_ENV_BODY)
return DBEsoFile.from_path(str(path))

def test_frequencies(self, modern_eso_file):
assert modern_eso_file.frequencies == [H, D, M, A, RP]

def test_annual_results(self, modern_eso_file):
assert modern_eso_file.get_results(_VARIABLE, A).first_array == [23.5]
50 changes: 48 additions & 2 deletions tests/test_eso_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
``any_eso_file`` / ``any_eso_path`` / ``any_sql_path`` fixtures.
"""

from db_eplusout_reader import Variable, get_results
from db_eplusout_reader.constants import D, H, M
from datetime import datetime

import pytest

from db_eplusout_reader import DBEsoFileCollection, Variable, get_results
from db_eplusout_reader.constants import RP, TS, A, D, H, M

from .conftest import TEST_FILES_DIR


class TestEsoAcrossVersions:
Expand All @@ -25,6 +31,46 @@ def test_get_results_monthly(self, any_eso_file):
results = any_eso_file.get_results(variables, M)
assert len(results.time_series) == 12

def test_frequencies(self, any_eso_file):
# Raised KeyError: '' for EnergyPlus 10.0+ files before issue #32 was fixed.
assert any_eso_file.frequencies == [TS, H, D, M, RP]


class TestAnnualOutputs:
"""EnergyPlus 26.2 file with an !Annual variable (issue #32).

Generated with EnergyPlus 26.2 from resources/252_1ZoneUncontrolledAnnualOutputs.idf
after the 25.2 -> 26.1 -> 26.2 transitions (which only change the Version
object) and the Chicago TMY3 weather file in resources/. The model runs two
design days and a 2018-2019 run period.
"""

_VARIABLE = Variable("ZONE ONE", "Zone Mean Air Temperature", "C")

@pytest.fixture(scope="class")
def collection(self):
path = TEST_FILES_DIR / "262_1ZoneUncontrolledAnnualOutputs.eso"
return DBEsoFileCollection.from_path(str(path))

@pytest.fixture(scope="class")
def run_period(self, collection):
return next(f for f in collection if "RUN PERIOD" in f.environment_name.upper())

def test_frequencies(self, run_period):
assert run_period.frequencies == [D, A, RP]

def test_annual_results(self, run_period):
results = run_period.get_results(self._VARIABLE, A)
assert results.time_series == [datetime(2018, 1, 1), datetime(2019, 1, 1)]
assert len(results.first_array) == 2
assert run_period.n_days[A] == [365, 365]

def test_design_days_have_no_annual_rows(self, collection):
design_days = [f for f in collection if "RUN PERIOD" not in f.environment_name.upper()]
assert len(design_days) == 2
for eso_file in design_days:
assert eso_file.n_days[A] == []


class TestSqlAcrossVersions:
def test_get_results_hourly(self, any_sql_path):
Expand Down
20 changes: 17 additions & 3 deletions tests/test_esofile_reader_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,30 @@


class TestStatementLine:
def test_get_eso_file_version(self):
assert get_eso_file_version(" Version 9.2.0-921312fa1d") == 920
@pytest.mark.parametrize(
"raw_version, expected",
[
(" Version 8.6.0-198c6a3cff", (8, 6, 0)),
(" Version 9.2.0-921312fa1d", (9, 2, 0)),
(" Version 23.1.0-87ed9199d4", (23, 1, 0)),
(" Version 25.2.0-cf7368216c", (25, 2, 0)),
(" Version 26.2.0-4bd7a1f26f", (26, 2, 0)),
],
)
def test_get_eso_file_version(self, raw_version, expected):
assert get_eso_file_version(raw_version) == expected

def test_two_digit_major_sorts_after_single_digit(self):
assert get_eso_file_version(" Version 23.1.0-87ed9199d4") > (8, 9, 0)
assert get_eso_file_version(" Version 8.6.0-198c6a3cff") < (8, 9, 0)

def test_get_eso_file_timestamp(self):
timestamp = get_eso_file_timestamp(" YMD=2020.11.10 11:31")
assert timestamp == datetime(2020, 11, 10, 11, 31)

def test_process_statement_line(self):
version, timestamp = process_statement_line(_STATEMENT)
assert version == 920
assert version == (9, 2, 0)
assert timestamp == datetime(2020, 11, 10, 11, 31)


Expand Down
Loading
Loading