Skip to content

Commit

Permalink
fix: Adding plexos stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pesap committed Oct 18, 2024
1 parent f97922b commit 259a4f3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/r2x/parser/plexos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,10 +1059,10 @@ def _set_unit_capacity(self, record):
-----
At this point of the code, all the properties should be either a single value, or a `SingleTimeSeries`
"""
record["active_power_limits"] = self._get_active_power_limits(record)
if not (availability := record.get("available")):
logger.warning("Unit {} is not activated on the model.", record["name"])
return
record["active_power_limits"] = self._get_active_power_limits(record)

# Set availability, rating, storage_capacity as multiplier of availability/'units'
if (
Expand Down Expand Up @@ -1108,7 +1108,7 @@ def _set_unit_capacity(self, record):
return record

def _get_active_power_limits(self, record) -> MinMax:
assert record["base_power"]
assert record["base_power"] is not None
if active_power_min := record.get("min_rated_capacity"):
if isinstance(active_power_min, SingleTimeSeries):
active_power_min = np.min(active_power_min.data)
Expand Down Expand Up @@ -1259,6 +1259,12 @@ def _data_file_handler(
logger.debug("Simplified time series heat rate for {}", record_name)
return parsed_file["value"].median()

# PATCH: It is not common to have a Max Capacity time series. But if it is the case, we just get the
# median as well
if property_name == "Max Capacity":
logger.trace("Simplified Max Capacity time series for {}", record_name)
return parsed_file["value"].median()

if "year" not in parsed_file.columns:
parsed_file = parsed_file.with_columns(year=self.year)

Expand Down Expand Up @@ -1294,6 +1300,9 @@ def _parse_data_file(
if "year" in parsed_file.columns:
parsed_file = pl_filter_year(parsed_file, year=self.year)

if parsed_file.is_empty():
logger.warning("No time series data specified for year filter. Year passed {}", self.year)

if "name" in parsed_file.columns:
cols = [col.lower() for col in [record_name, property_name, variable_name] if col]
parsed_file = parsed_file.filter(pl.col("name").str.to_lowercase().is_in(cols))
Expand Down Expand Up @@ -1364,7 +1373,7 @@ def _parse_property_data(self, record_data: list[dict[str, Any]]):
prop_name = record["property_name"]
prop_value = record["property_value"]
timeslice = record["tag_timeslice"]
unit = record["property_unit"].replace("$", "usd")
unit = record["property_unit"].replace("$", "usd") if record["property_unit"] else None
mapped_property_name = self.property_map.get(prop_name, prop_name)
unit = get_pint_unit(unit)
property_unit_map[mapped_property_name] = unit
Expand Down Expand Up @@ -1446,9 +1455,9 @@ def _handle_record(self, record: dict[str, Any], prop_name, prop_value, unit):
logger.trace("Parsing standard property")
value = self._parse_value(prop_value, unit=unit)
case {"text": str(), "text_class_name": ClassEnum.DataFile}:
data_file_value = (
self._data_file_handler(record["name"], prop_name, record["text"]) or prop_value
)
data_file_value = self._data_file_handler(record["name"], prop_name, record["text"])
if data_file_value is None:
data_file_value = prop_value
value = self._parse_value(
value=data_file_value, variable_name=mapped_property_name, unit=unit
)
Expand Down Expand Up @@ -1489,10 +1498,12 @@ def _handle_record(self, record: dict[str, Any], prop_name, prop_value, unit):
value = self._parse_value(value, variable_name=mapped_property_name, unit=unit)
case {"tag_datafile": str()}:
record["text"] = self._get_nested_object_data(record["tag_datafile_object_id"])
data_file = self._data_file_handler(record["name"], prop_name, fpath_str=str(record["text"]))
if data_file is None:
data_file = prop_value
value = self._parse_value(data_file, variable_name=mapped_property_name, unit=unit)
data_file_value = self._data_file_handler(
record["name"], prop_name, fpath_str=str(record["text"])
)
if data_file_value is None:
data_file_value = prop_value
value = self._parse_value(data_file_value, variable_name=mapped_property_name, unit=unit)
case {"tag_timeslice": str()}:
value = self._parse_value(prop_value, variable_name=mapped_property_name, unit=unit)
case _:
Expand Down
8 changes: 8 additions & 0 deletions src/r2x/parser/plexos_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ def parse_data_file(column_type: DATAFILE_COLUMNS, data_file):
data_file = parse_ts_nm(data_file)
case column_type.TS_YM:
data_file = parse_ts_ym(data_file)
case column_type.TS_YMDPV:
data_file = parse_ts_ymdpv(data_file)
case column_type.TS_YMDH:
data_file = parse_ts_ymdh(data_file)
case column_type.TS_NYMDV:
Expand Down Expand Up @@ -271,6 +273,12 @@ def parse_ts_ym(data_file):
return data_file


def parse_ts_ymdpv(data_file):
data_file = data_file.with_columns(hour=pl.col("period"))
data_file = data_file.with_columns(pl.col("hour").cast(pl.Int8))
return data_file


def parse_ts_nmdp(data_file):
data_file = data_file.with_columns(hour=pl.col("period"))
data_file = data_file.with_columns(pl.col("hour").cast(pl.Int8))
Expand Down

0 comments on commit 259a4f3

Please sign in to comment.