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
2 changes: 2 additions & 0 deletions db_eplusout_reader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""A tool to fetch results from EnergyPlus output files."""

__version__ = "0.1.0"

from db_eplusout_reader.db_esofile import DBEsoFile, DBEsoFileCollection
Expand Down
2 changes: 2 additions & 0 deletions db_eplusout_reader/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Shared frequency and variable field constants."""

TS = "timestep"
H = "hourly"
D = "daily"
Expand Down
157 changes: 87 additions & 70 deletions db_eplusout_reader/db_esofile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Classes representing processed EnergyPlus .eso file output data."""

from db_eplusout_reader.constants import RP, TS, A, D, H, M
from db_eplusout_reader.exceptions import CollectionRequired, raise_if_missing
from db_eplusout_reader.processing.esofile_reader import Variable, process_eso_file
Expand All @@ -9,79 +11,80 @@


class DBEsoFile:
def __init__(self, environment_name, header, outputs, dates, n_days, days_of_week):
"""
Represents processed EnergyPlus eso file output data.
"""
Represents processed EnergyPlus eso file output data.

Results are stored in bins identified by output frequency.
Results are stored in bins identified by output frequency.

Parameters
----------
environment_name : str
A name of the environment.
header : dict of {str, dict of {int, Variable}}
Processed header dictionary.
outputs : dict of {str, dict of {int, list of float}}
Processed numeric outputs.
dates : dict of {str, datetime}
Parsed dates.
n_days : dict of {str, list of int}
Number of days for each step for monthly to runperiod frequencies.
days_of_week:
Day of week for each step for timestep to daily frequencies.

Example
-------
environment_name 'TEST (01-01:31-12)'
header {
'hourly' : {
Variable(key='B1:ZONE1', type='Zone Mean Air Temperature', units='C'): 322,
Variable(key='B1:ZONE2', type='Zone Air Relative Humidity', units='%'): 304
...
},
'daily' : {
Variable(key='B1:ZONE1', type='Zone Air Relative Humidity', units='%'): 521,
Variable(key='B1:ZONE2', type='Zone Air Relative Humidity', units='%'): 565
...
}
}
outputs {
'hourly' : {
322 : [17.906587634970627, 17.198486368112462, 16.653197201251096, ...]
304 : [0.006551864336487204, 0.0061786832466626095, 0.005800374315868216, ...]
...
},
'daily' : {
521 : [38.83017767728567, 48.74604212532369, 41.69013850729892, ...]
565 : [43.25127519033924, 55.42681891740626, 42.215387940031526, ...]
...
}
}
dates {
'hourly' : [
datetime.datetime(2002, 1, 1, 1, 0),
datetime.datetime(2002, 1, 1, 2, 0),
datetime.datetime(2002, 1, 1, 3, 0),
...
],
'daily' : [
datetime.datetime(2002, 1, 1, 0, 0),
datetime.datetime(2002, 1, 2, 0, 0),
datetime.datetime(2002, 1, 3, 0, 0),
...
}
n_days {
'monthly': [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
'annual': [365],
'runperiod': [365]
Parameters
----------
environment_name : str
A name of the environment.
header : dict of {str, dict of {int, Variable}}
Processed header dictionary.
outputs : dict of {str, dict of {int, list of float}}
Processed numeric outputs.
dates : dict of {str, datetime}
Parsed dates.
n_days : dict of {str, list of int}
Number of days for each step for monthly to runperiod frequencies.
days_of_week:
Day of week for each step for timestep to daily frequencies.

Example
-------
environment_name 'TEST (01-01:31-12)'
header {
'hourly' : {
Variable(key='B1:ZONE1', type='Zone Mean Air Temperature', units='C'): 322,
Variable(key='B1:ZONE2', type='Zone Air Relative Humidity', units='%'): 304
...
},
'daily' : {
Variable(key='B1:ZONE1', type='Zone Air Relative Humidity', units='%'): 521,
Variable(key='B1:ZONE2', type='Zone Air Relative Humidity', units='%'): 565
...
}
days_of_week {
'hourly': ['Tuesday', 'Tuesday', 'Tuesday', 'Tuesday', ...],
'daily': ['Tuesday', 'Wednesday', 'Thursday', 'Friday', ...],
}
outputs {
'hourly' : {
322 : [17.906587634970627, 17.198486368112462, 16.653197201251096, ...]
304 : [0.006551864336487204, 0.0061786832466626095, 0.005800374315868216, ...]
...
},
'daily' : {
521 : [38.83017767728567, 48.74604212532369, 41.69013850729892, ...]
565 : [43.25127519033924, 55.42681891740626, 42.215387940031526, ...]
...
}
}
dates {
'hourly' : [
datetime.datetime(2002, 1, 1, 1, 0),
datetime.datetime(2002, 1, 1, 2, 0),
datetime.datetime(2002, 1, 1, 3, 0),
...
],
'daily' : [
datetime.datetime(2002, 1, 1, 0, 0),
datetime.datetime(2002, 1, 2, 0, 0),
datetime.datetime(2002, 1, 3, 0, 0),
...
}
n_days {
'monthly': [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
'annual': [365],
'runperiod': [365]
}
days_of_week {
'hourly': ['Tuesday', 'Tuesday', 'Tuesday', 'Tuesday', ...],
'daily': ['Tuesday', 'Wednesday', 'Thursday', 'Friday', ...],
...
}

"""
"""

def __init__(self, environment_name, header, outputs, dates, n_days, days_of_week):
self.environment_name = environment_name
self.header = header
self.outputs = outputs
Expand All @@ -90,7 +93,8 @@ def __init__(self, environment_name, header, outputs, dates, n_days, days_of_wee
self.days_of_week = days_of_week

@classmethod
def _from_raw_outputs(cls, raw_outputs, year):
def from_raw_outputs(cls, raw_outputs, year):
"""Create a DBEsoFile from a single environment's RawOutputData."""
dates = convert_raw_date_data(raw_outputs.dates, raw_outputs.days_of_week, year)
n_days = get_n_days_from_cumulative(raw_outputs.cumulative_days)
return cls(
Expand All @@ -104,9 +108,10 @@ def _from_raw_outputs(cls, raw_outputs, year):

@classmethod
def from_path(cls, file_path, year=None):
"""Create a DBEsoFile from a path; raises CollectionRequired if multi-environment."""
all_raw_outputs = process_eso_file(file_path)
if len(all_raw_outputs) == 1:
return cls._from_raw_outputs(all_raw_outputs[0], year)
return cls.from_raw_outputs(all_raw_outputs[0], year)
raise CollectionRequired(
"Cannot process file {}. "
"as there are multiple environments included.\n"
Expand All @@ -117,6 +122,7 @@ def from_path(cls, file_path, year=None):

@property
def frequencies(self):
"""List available output frequencies, ordered from most to least granular."""
order = {TS: 0, H: 1, D: 2, M: 3, A: 4, RP: 5}
return sorted(list(self.header.keys()), key=lambda x: order[x])

Expand Down Expand Up @@ -249,15 +255,17 @@ def __init__(self, db_eso_files=None):

@classmethod
def from_path(cls, file_path, year=None):
"""Create a DBEsoFileCollection holding one DBEsoFile per environment in the file."""
all_raw_outputs = process_eso_file(file_path)
db_eso_files = []
for raw_outputs in all_raw_outputs:
db_eso_file = DBEsoFile._from_raw_outputs(raw_outputs, year)
db_eso_file = DBEsoFile.from_raw_outputs(raw_outputs, year)
db_eso_files.append(db_eso_file)
return cls(db_eso_files)

@property
def environment_names(self):
"""List environment names of the held DBEsoFile objects."""
return [ef.environment_name for ef in self._db_eso_files]

def __iter__(self):
Expand All @@ -271,28 +279,37 @@ def __contains__(self, item):
return item in self._db_eso_files

def append(self, item):
"""Append a DBEsoFile to the collection."""
self._db_eso_files.append(item)

def count(self):
"""Return the number of DBEsoFile objects in the collection."""
len(self._db_eso_files)

def index(self, item):
"""Return the index of the given DBEsoFile in the collection."""
return self._db_eso_files.index(item)

def extend(self, items):
"""Extend the collection with an iterable of DBEsoFile objects."""
self._db_eso_files.extend(items)

def insert(self, index, item):
"""Insert a DBEsoFile at the given index."""
self._db_eso_files.insert(index, item)

def pop(self, index):
"""Remove and return the DBEsoFile at the given index."""
return self._db_eso_files.pop(index)

def remove(self, item):
"""Remove the first occurrence of the given DBEsoFile from the collection."""
self._db_eso_files.remove(item)

def reverse(self):
"""Reverse the order of the collection in place."""
reversed(self._db_eso_files)

def sort(self, reverse):
"""Sort the collection in place by file name."""
self._db_eso_files.sort(key=lambda ef: ef.file_name, reverse=reverse)
3 changes: 3 additions & 0 deletions db_eplusout_reader/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""Exceptions raised while reading and processing EnergyPlus output files."""


class IncompleteFile(Exception):
"""Exception raised when the file is not complete."""

Expand Down
2 changes: 2 additions & 0 deletions db_eplusout_reader/get_results.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""High level entry point to extract results from .eso or .sql files."""

import os

from db_eplusout_reader.db_esofile import DBEsoFile, DBEsoFileCollection
Expand Down
4 changes: 4 additions & 0 deletions db_eplusout_reader/processing/esofile_reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Parse the header and body of an EnergyPlus .eso file into raw output data."""

import re
from collections import defaultdict, namedtuple
from datetime import datetime
Expand Down Expand Up @@ -222,13 +224,15 @@ def parse_annual_frequency():


def split_raw_line(raw_line):
"""Split a raw eso file body line into its line id and remaining fields."""
split_line = raw_line.split(",")
line_id = int(split_line[0])
line = split_line[1:]
return line_id, line


def process_frequency_line(line_id, line, all_raw_outputs, header, all_ids, raw_outputs):
"""Process a frequency line, returning the (possibly new) raw outputs and frequency."""
if line_id == ENVIRONMENT_LINE:
# initialize variables for current environment
environment_name = line[0].strip()
Expand Down
8 changes: 5 additions & 3 deletions db_eplusout_reader/processing/esofile_time.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Convert raw EnergyPlus timestamp data into standard datetime objects."""

import calendar
import logging
from collections import namedtuple
Expand Down Expand Up @@ -86,13 +88,13 @@ def check_year_increment(first_step_data, current_step_data):
def generate_datetime_dates(raw_dates, year):
"""Generate datetime index for a given period."""
dates = []
for i in range(0, len(raw_dates)):
for raw_date in raw_dates:
# based on the first, current and previous
# steps decide if the year should be incremented
if check_year_increment(raw_dates[0], raw_dates[i]):
if check_year_increment(raw_dates[0], raw_date):
year += 1
# year can be incremented automatically when converting to datetime
date = parse_eso_timestamp(year, *raw_dates[i])
date = parse_eso_timestamp(year, *raw_date)
dates.append(date)
return dates

Expand Down
6 changes: 6 additions & 0 deletions db_eplusout_reader/processing/raw_eso_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""Container holding raw, not-yet-dated numeric outputs parsed from an .eso file."""

from collections import defaultdict

from db_eplusout_reader.constants import RP, A, M


class RawOutputData:
"""Holds raw, not-yet-dated numeric outputs and dictionaries for a single environment."""

def __init__(self, environment_name, header, all_ids=None):
self.environment_name = environment_name
self.header = header
Expand All @@ -25,6 +29,7 @@ def __init__(self, environment_name, header, all_ids=None):
) = self.initialize_results_bins()

def initialize_results_bins(self):
"""Create empty outputs, dates, cumulative_days and days_of_week bins for all_ids."""
outputs = defaultdict(dict)
dates = {}
cumulative_days = {}
Expand All @@ -40,5 +45,6 @@ def initialize_results_bins(self):
return outputs, dates, cumulative_days, days_of_week

def initialize_next_outputs_step(self, frequency):
"""Append a nan placeholder to every output array for the given frequency."""
for value in self.outputs[frequency].values():
value.append(float("nan"))
7 changes: 7 additions & 0 deletions db_eplusout_reader/results_dict.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Dictionary-like container for extracted results and its table/csv export helpers."""

import csv
import sys
from collections import OrderedDict
Expand Down Expand Up @@ -48,25 +50,30 @@ def _items(self):

@property
def scalar(self):
"""Return the first value of the first variable's array."""
try:
return self._items[0][1][0]
except IndexError:
raise NoResults("Cannot get scalar value, first array is empty!")

@property
def first_array(self):
"""Return the numeric array of the first variable."""
return self._items[0][1]

@property
def first_variable(self):
"""Return the first Variable named tuple."""
return self._items[0][0]

@property
def variables(self):
"""Return all Variable named tuples."""
return [v[0] for v in self._items]

@property
def arrays(self):
"""Return all numeric arrays."""
return [v[1] for v in self._items]

def to_table(self, explode_header=True):
Expand Down
Loading
Loading