Skip to content

Commit 5f97bf3

Browse files
committed
Fix Handling of TimeSeriesData
1 parent eba1ec4 commit 5f97bf3

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

flixopt/calculation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def __init__(
6161
"""
6262
self.name = name
6363
if flow_system.used_in_calculation:
64-
logging.warning(f'FlowSystem {flow_system} is already used in a calculation. '
65-
f'Creating a copy for Calculation "{self.name}".')
64+
logger.warning(
65+
f'FlowSystem {flow_system} is already used in a calculation. '
66+
f'Creating a copy of the FlowSystem for Calculation "{self.name}".'
67+
)
6668
flow_system = flow_system.copy()
6769

6870
if active_timesteps is not None:

flixopt/core.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ class TimeSeriesData(xr.DataArray):
5151

5252
__slots__ = () # No additional instance attributes - everything goes in attrs
5353

54-
def __init__(self, *args, aggregation_group: Optional[str] = None, aggregation_weight: Optional[float] = None,
55-
agg_group: Optional[str] = None, agg_weight: Optional[float] = None, **kwargs):
54+
def __init__(
55+
self,
56+
*args,
57+
aggregation_group: Optional[str] = None,
58+
aggregation_weight: Optional[float] = None,
59+
agg_group: Optional[str] = None,
60+
agg_weight: Optional[float] = None,
61+
**kwargs
62+
):
5663
"""
5764
Args:
5865
*args: Arguments passed to DataArray
@@ -84,6 +91,23 @@ def __init__(self, *args, aggregation_group: Optional[str] = None, aggregation_w
8491
# Always mark as TimeSeriesData
8592
self.attrs['__timeseries_data__'] = True
8693

94+
def fit_to_coords(
95+
self,
96+
coords: Dict[str, pd.Index],
97+
name: Optional[str] = None,
98+
) -> 'TimeSeriesData':
99+
"""Fit the data to the given coordinates. Returns a new TimeSeriesData object if the current coords are different."""
100+
if self.coords.equals(xr.Coordinates(coords)):
101+
return self
102+
103+
da = DataConverter.to_dataarray(self.data, coords=coords)
104+
return self.__class__(
105+
da,
106+
aggregation_group=self.aggregation_group,
107+
aggregation_weight=self.aggregation_weight,
108+
name=name if name is not None else self.name
109+
)
110+
87111
@property
88112
def aggregation_group(self) -> Optional[str]:
89113
return self.attrs.get('aggregation_group')

flixopt/flow_system.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,7 @@ def fit_to_model_coords(
343343
if isinstance(data, TimeSeriesData):
344344
try:
345345
data.name = name # Set name of previous object!
346-
return TimeSeriesData(
347-
DataConverter.to_dataarray(data, coords=coords),
348-
aggregation_group=data.aggregation_group, aggregation_weight=data.aggregation_weight
349-
).rename(name)
346+
return data.fit_to_coords(coords)
350347
except ConversionError as e:
351348
raise ConversionError(
352349
f'Could not convert time series data "{name}" to DataArray:\n{data}\nOriginal Error: {e}') from e

0 commit comments

Comments
 (0)