Skip to content
Open
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
3 changes: 1 addition & 2 deletions scout/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@


class LogConfig:
"""Configure the logger
"""
"""Configure the logger"""

@staticmethod
def configure_logging():
Expand Down
4,435 changes: 977 additions & 3,458 deletions scout/ecm_prep.py

Large diffs are not rendered by default.

1,704 changes: 1,704 additions & 0 deletions scout/ecm_prep_vars.py

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions scout/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import numpy_financial as npf
from datetime import datetime
from scout.plots import run_plot
from scout.config import FilePaths as fp
from scout.config import Config
from scout.config import Config, FilePaths as fp
from scout.utils import PrintFormat as fmt
import warnings


Expand Down Expand Up @@ -5122,9 +5122,6 @@ def main(opts: argparse.NameSpace): # noqa: F821
of key results to an output JSON.
"""

# Set function that only prints message when in verbose mode
verboseprint = print if opts.verbose else lambda *a, **k: None

# Raise numpy errors as exceptions
numpy.seterr('raise')
# Initialize user opts variable (elements: S-S calculation method;
Expand Down Expand Up @@ -5368,7 +5365,7 @@ def main(opts: argparse.NameSpace): # noqa: F821
# Reset measure fuel split attribute to imported values
m.eff_fs_splt = meas_eff_fs_data
# Print data import message for each ECM if in verbose mode
verboseprint("Imported ECM '" + m.name + "' competition data")
fmt.verboseprint(opts.verbose, f"Imported ECM {m.name} competition data", "info")

# Import total absolute heating and cooling energy use data, used in
# removing overlaps between supply-side and demand-side heating/cooling
Expand Down Expand Up @@ -5455,12 +5452,12 @@ def main(opts: argparse.NameSpace): # noqa: F821
try:
elec_carb = elec_cost_carb['CO2 intensity of electricity']['data']
elec_cost = elec_cost_carb['End-use electricity price']['data']
fmt = True # Boolean for indicating data key substructure
format_data = True # Boolean for indicating data key substructure
except KeyError:
# Data are structured as in the site_source_co2_conversions files
elec_carb = elec_cost_carb['electricity']['CO2 intensity']['data']
elec_cost = elec_cost_carb['electricity']['price']['data']
fmt = False
format_data = False

# Determine regions and building types used by active measures for
# aggregating onsite generation data
Expand Down Expand Up @@ -5501,7 +5498,7 @@ def variable_depth_dict(): return defaultdict(variable_depth_dict)
else:
bt_bin = 'commercial'
# Get CO2 intensity and electricity cost data and convert units
if fmt: # Data (and data structure) from emm_region files
if format_data: # Data (and data structure) from emm_region files
# Convert Mt/TWh to Mt/MMBtu
carbtmp = {k: elec_carb[cz].get(k, 0)/3.41214e6
for k in elec_carb[cz].keys()}
Expand Down
16 changes: 10 additions & 6 deletions scout/run_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from pathlib import Path
from scout.config import LogConfig, Config, FilePaths as fp
from scout.ecm_prep_args import ecm_args
from scout.ecm_prep import Utils, main as ecm_prep_main
from scout.ecm_prep import ECMPrepHelper, main as ecm_prep_main
from scout.utils import JsonIO
from scout import run
from argparse import ArgumentParser
import logging
Expand Down Expand Up @@ -122,15 +123,18 @@ def run_batch(self):
ecm_prep_main(ecm_prep_opts)

# Run run.main() for each yml in the group, set custom results directories
run_setup = Utils.load_json(fp.GENERATED / "run_setup.json")
run_setup = JsonIO.load_json(fp.GENERATED / "run_setup.json")
ecm_files_list = self.get_ecm_files(yml_grp)
for ct, config in enumerate(yml_grp):
# Set all ECMs inactive
run_setup = Utils.update_active_measures(run_setup,
to_inactive=ecm_prep_opts.ecm_files)
run_setup = ECMPrepHelper.update_active_measures(
run_setup,
to_inactive=ecm_prep_opts.ecm_files
)
# Set yml-specific ECMs active
run_setup = Utils.update_active_measures(run_setup, to_active=ecm_files_list[ct])
Utils.dump_json(run_setup, fp.GENERATED / "run_setup.json")
run_setup = ECMPrepHelper.update_active_measures(run_setup,
to_active=ecm_files_list[ct])
JsonIO.dump_json(run_setup, fp.GENERATED / "run_setup.json")
run_opts = self.get_run_opts(config)
logger.info(f"Running run.py for {config}")
run.main(run_opts)
Expand Down
84 changes: 84 additions & 0 deletions scout/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import json
import numpy
import logging
from pathlib import Path, PurePath


class JsonIO:
@staticmethod
def load_json(filepath: Path) -> dict:
"""Loads data from a .json file

Args:
filepath (pathlib.Path): filepath of .json file

Returns:
dict: .json data as a dict
"""
with open(filepath, 'r') as handle:
try:
data = json.load(handle)
except ValueError as e:
raise ValueError(f"Error reading in '{filepath}': {str(e)}") from None
return data

@staticmethod
def dump_json(data, filepath: Path):
"""Export data to .json file

Args:
data: data to write to .json file
filepath (pathlib.Path): filepath of .json file
"""
with open(filepath, "w") as handle:
json.dump(data, handle, indent=2, cls=MyEncoder)


class MyEncoder(json.JSONEncoder):
"""Convert numpy arrays to list for JSON serializing."""

def default(self, obj):
"""Modify 'default' method from JSONEncoder."""
# Case where object to be serialized is numpy array
if isinstance(obj, numpy.ndarray):
return obj.tolist()
if isinstance(obj, PurePath):
return str(obj)
# All other cases
else:
return super(MyEncoder, self).default(obj)


class PrintFormat:
"""Class for customizing print messages."""

@staticmethod
def custom_showwarning(message, category, filename, lineno, file=None, line=None):
"""Define a custom warning message format."""
# Other message details suppressed because error location and type are not relevant
print(message)

@staticmethod
def verboseprint(verbose, msg, log_type, logger=None):
"""Print input message when the code is run in verbose mode.

Args:
verbose (boolean): Indicator of verbose mode
msg (string): Message to print to console when in verbose mode
logger: Logger instance to use for logging
"""
if not verbose:
return
if not logger:
logger = logging.getLogger(__name__)

if log_type == "info":
logger.info(msg)
elif log_type == "warning":
logger.warning(msg)
elif log_type == "error":
logger.error(msg)

@staticmethod
def format_console_list(list_to_format):
return [f" {elem}\n" for elem in list_to_format]
Loading
Loading