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: 1 addition & 1 deletion powersimdata/input/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__all__ = ['abstract_grid', 'change_table', 'csv_reader',
'grid', 'helpers', 'mat_reader', 'profiles', 'scaler',
'grid', 'helpers', 'profiles', 'scaler', 'scenario_grid',
'usa_tamu_model']
16 changes: 11 additions & 5 deletions powersimdata/input/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import warnings

from powersimdata.input.usa_tamu_model import TAMU
from powersimdata.input.mat_reader import REISEMATReader
from powersimdata.input.scenario_grid import FromREISE, FromREISEjl
from powersimdata.input.grid_fields \
import AbstractGridField, Branch, Bus, DCLine, GenCost, Plant, Storage, Sub

Expand All @@ -16,10 +16,9 @@ def __init__(self, interconnect, source='usa_tamu', engine='REISE'):

:param list interconnect: interconnect name(s).
:param str source: model used to build the network.
:param str engine: engine used to run scenario, if using MATReader.
:param str engine: engine used to run scenario, if using ScenarioGrid.
:raises TypeError: if source and engine are not both strings.
:raises ValueError: if model or engine does not exist.
:raises NotImplementedError: if engine is not yet built (see REISE.jl).
"""
if not isinstance(source, str):
raise TypeError('source must be a string')
Expand All @@ -31,9 +30,9 @@ def __init__(self, interconnect, source='usa_tamu', engine='REISE'):
data = TAMU(interconnect)
elif os.path.splitext(source)[1] == '.mat':
if engine == 'REISE':
data = REISEMATReader(source)
data = FromREISE(source)
elif engine == 'REISE.jl':
raise NotImplementedError
data = FromREISEjl(source)
else:
raise ValueError('Unknown engine %s!' % engine)

Expand Down Expand Up @@ -163,8 +162,15 @@ def _univ_eq(ref, test):
other_keys = other.fields[k].data.keys()
assert self_keys == other_keys
for subkey in self_keys:
# REISE will modify gencost and some gen columns
if subkey == 'gencost':
continue
self_data = self.fields[k].data[subkey]
other_data = other.fields[k].data[subkey]
if subkey == 'gen':
excluded_cols = ['ramp_10', 'ramp_30']
self_data = self_data.drop(excluded_cols, axis=1)
other_data = other_data.drop(excluded_cols, axis=1)
_univ_eq(self_data, other_data)
elif isinstance(v, Bus):
# MOST changes BUS_TYPE for buses with DC Lines attached
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
add_interconnect_to_grid_data_frames)


class MATReader(AbstractGrid):
"""MATLAB file reader
class ScenarioGrid(AbstractGrid):
"""File reader for MAT files for scenarios which were run on the server.

"""
def __init__(self, filename):
Expand All @@ -34,18 +34,7 @@ def _set_data_loc(self, filename):
else:
self.data_loc = filename

def _build_network(self):
"""Defines how to interpret the MAT file data to build a network.
Not implemented for MATReader, but must be defined for subclasses.
"""
pass


class REISEMATReader(MATReader):
"""MATLAB file reader, for MAT files created by REISE/MATPOWER

"""
def _build_network(self):
def _read_network(self):
data = loadmat(self.data_loc, squeeze_me=True, struct_as_record=False)
mpc = data['mdi'].mpc
try:
Expand All @@ -64,6 +53,7 @@ def _build_network(self):

# bus
self.bus, _ = frame('bus', mpc.bus, mpc.busid)
self.bus.drop(columns=['bus_id'], inplace=True)

# plant
self.plant, plant_storage = frame('plant',
Expand Down Expand Up @@ -125,6 +115,29 @@ def _build_network(self):
# interconnect
self.interconnect = self.sub.interconnect.unique().tolist()

def _build_network(self):
"""Defines how to interpret the MAT file data to build a network.
Not implemented for ScenarioGrid, but must be defined for subclasses.
"""
pass


class FromREISE(ScenarioGrid):
"""MATLAB file reader, for MAT files created by REISE/MATPOWER

"""
def _build_network(self):
self._read_network()
reindex_model(self)
add_information_to_model(self)


class FromREISEjl(ScenarioGrid):
"""MATLAB file reader, for MAT files created (& converted) by REISE.jl

"""
def _build_network(self):
self._read_network()
add_information_to_model(self)


Expand Down Expand Up @@ -319,10 +332,17 @@ def parse_gencost_row(row):
def add_information_to_model(grid):
"""Makes a standard grid.

:param powersimdata.input.MATReader grid: grid with missing information.
:param powersimdata.input.ScenarioGrid grid: grid with missing information.
"""
grid.bus.drop(columns=['bus_id'], inplace=True)

add_interconnect_to_grid_data_frames(grid)
add_zone_to_grid_data_frames(grid)
add_coord_to_grid_data_frames(grid)

grid.plant = grid.plant.join(grid.heat_rate_curve)


def reindex_model(grid):
def reset_id():
return lambda x: grid.bus.index[x - 1]

Expand All @@ -334,9 +354,6 @@ def reset_id():
'from_bus_id'] = grid.dcline['from_bus_id'].apply(reset_id())
grid.dcline[
'to_bus_id'] = grid.dcline['to_bus_id'].apply(reset_id())

add_interconnect_to_grid_data_frames(grid)
add_zone_to_grid_data_frames(grid)
add_coord_to_grid_data_frames(grid)

grid.plant = grid.plant.join(grid.heat_rate_curve)
if not grid.storage['gen'].empty:
grid.storage[
'gen'].bus_id = grid.storage['gen'].bus_id.apply(reset_id())
2 changes: 1 addition & 1 deletion powersimdata/input/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from powersimdata.input.usa_tamu_model import check_interconnect
from powersimdata.input.helpers import add_column_to_data_frame
from powersimdata.input.mat_reader import format_gencost, link
from powersimdata.input.scenario_grid import format_gencost, link
from powersimdata.input.grid import Grid
from powersimdata.input.usa_tamu_model import TAMU

Expand Down