From 259a4f380d792d821e7e0b6ad235453380a9976d Mon Sep 17 00:00:00 2001 From: pesap Date: Fri, 18 Oct 2024 11:32:57 -0600 Subject: [PATCH] fix: Adding plexos stuff --- src/r2x/parser/plexos.py | 31 +++++++++++++++++++++---------- src/r2x/parser/plexos_utils.py | 8 ++++++++ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/r2x/parser/plexos.py b/src/r2x/parser/plexos.py index 4338e2a2..08c99b62 100644 --- a/src/r2x/parser/plexos.py +++ b/src/r2x/parser/plexos.py @@ -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 ( @@ -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) @@ -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) @@ -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)) @@ -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 @@ -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 ) @@ -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 _: diff --git a/src/r2x/parser/plexos_utils.py b/src/r2x/parser/plexos_utils.py index 7408bed2..1008222e 100644 --- a/src/r2x/parser/plexos_utils.py +++ b/src/r2x/parser/plexos_utils.py @@ -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: @@ -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))