Skip to content

Commit 59a588b

Browse files
committed
Create alfasim_sdk.result_reader.reader.Results.get_profile_time_set
1 parent f5842fe commit 59a588b

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/alfasim_sdk/result_reader/reader.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ def _read_profile(self, profile_key: str, index: int) -> Curve:
144144
),
145145
)
146146

147+
def _read_profile_time_set(self, profile_key: str) -> Array:
148+
"""
149+
Create a profile's timeset from the results.
150+
"""
151+
metadata = self.metadata
152+
profile_metadata = metadata.profiles[profile_key]
153+
profile_time_set_key = profile_metadata["time_set_key"]
154+
time_set_key = ("profile_id", profile_time_set_key)
155+
time_sets = read_time_sets(metadata, [time_set_key])
156+
time_set = time_sets[time_set_key]
157+
return Array(values=time_set, unit=metadata.time_sets_unit, category="time")
158+
147159
def get_positional_trend_curve(
148160
self,
149161
property_name: str,
@@ -170,7 +182,10 @@ def get_positional_trend_curve(
170182
return self._read_trend(trend_key)
171183

172184
msg = [
173-
f"Can not locate '{property_name}' trend for element '{element_name}' at position '{position_m}'.\nFound positional trends:",
185+
(
186+
f"Can not locate '{property_name}' trend for element '{element_name}'"
187+
f" at position '{position_m}'.\nFound positional trends:"
188+
),
174189
*map(str, self.list_positional_trends()),
175190
]
176191
raise RuntimeError("\n- ".join(msg))
@@ -189,7 +204,10 @@ def get_overall_trend_curve(self, property_name: str, element_name: str) -> Curv
189204
return self._read_trend(trend_key)
190205

191206
msg = [
192-
f"Can not locate overall '{property_name}' trend for element '{element_name}'.\nFound overall trends:",
207+
(
208+
f"Can not locate overall '{property_name}' trend for element"
209+
f" '{element_name}'.\nFound overall trends:"
210+
),
193211
*map(str, self.list_overall_trends()),
194212
]
195213
raise RuntimeError("\n- ".join(msg))
@@ -254,25 +272,43 @@ def list_global_trends(self) -> Sequence[GlobalTrendMetadata]:
254272
if (trend_metadata["network_element_name"] is None)
255273
]
256274

257-
def get_profile_curve(
258-
self, property_name: str, element_name: str, index: int
259-
) -> Curve:
275+
def _get_profile_key(self, property_name: str, element_name: str) -> str:
260276
"""
261-
Return a profile curve at a given time step index.
277+
Return the profile key (used to access the profile data in low level) for the
278+
given property and element.
262279
"""
263280
metadata = self.metadata
264281
for profile_key, profile_metadata in metadata.profiles.items():
265282
if (profile_metadata["property_id"] == property_name) and (
266283
profile_metadata["network_element_name"] == element_name
267284
):
268-
return self._read_profile(profile_key, index)
285+
return profile_key
269286

270287
msg = [
271-
f"Can not locate '{property_name}' profile for element '{element_name}'.\nFound profiles:",
288+
(
289+
f"Can not locate '{property_name}' profile for element '{element_name}'."
290+
f"\nFound profiles:"
291+
),
272292
*map(str, self.list_profiles()),
273293
]
274294
raise RuntimeError("\n- ".join(msg))
275295

296+
def get_profile_curve(
297+
self, property_name: str, element_name: str, index: int
298+
) -> Curve:
299+
"""
300+
Return a profile curve at a given time step index.
301+
"""
302+
profile_key = self._get_profile_key(property_name, element_name)
303+
return self._read_profile(profile_key, index)
304+
305+
def get_profile_time_set(self, property_name: str, element_name: str) -> Array:
306+
"""
307+
Return the timeset for a given profile.
308+
"""
309+
profile_key = self._get_profile_key(property_name, element_name)
310+
return self._read_profile_time_set(profile_key)
311+
276312
def list_profiles(self) -> Sequence[ProfileMetadata]:
277313
"""
278314
List the collected profiles (and how many timesteps are present).

tests/results/test_result_reader.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy
44
import pytest
55
from barril.curve.curve import Curve
6+
from barril.units import Array
67
from barril.units import Scalar
78

89
from alfasim_sdk.result_reader.reader import GlobalTrendMetadata
@@ -71,6 +72,11 @@ def test_profiles(results: Results) -> None:
7172
pressure_final_2.image.GetValues(),
7273
)
7374

75+
time_set = results.get_profile_time_set("pressure", "Conexão 1")
76+
assert isinstance(time_set, Array)
77+
assert time_set.GetUnit() == "s"
78+
assert len(time_set) == 14
79+
7480

7581
def test_global_trends(results: Results) -> None:
7682
assert list(map(str, results.list_global_trends())) == ["timestep"]

0 commit comments

Comments
 (0)