1use crate::id::{IDCollection, define_id_type};
6use crate::units::{Dimensionless, Year};
7use anyhow::{Context, Result};
8use indexmap::{IndexMap, IndexSet};
9use itertools::Itertools;
10use serde::de::Error;
11use serde::{Deserialize, Serialize};
12use serde_string_enum::DeserializeLabeledStringEnum;
13use std::fmt::Display;
14use std::iter;
15
16define_id_type! {Season}
17define_id_type! {TimeOfDay}
18
19#[derive(Hash, Eq, PartialEq, Ord, PartialOrd, Clone, Debug)]
21pub struct TimeSliceID {
22 pub season: Season,
24 pub time_of_day: TimeOfDay,
26}
27
28#[cfg(test)]
30impl From<&str> for TimeSliceID {
31 fn from(value: &str) -> Self {
32 let (season, time_of_day) = value
33 .split(".")
34 .collect_tuple()
35 .expect("Time slice not in form season.time_of_day");
36 TimeSliceID {
37 season: season.into(),
38 time_of_day: time_of_day.into(),
39 }
40 }
41}
42
43impl Display for TimeSliceID {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 write!(f, "{}.{}", self.season, self.time_of_day)
46 }
47}
48
49impl<'de> Deserialize<'de> for TimeSliceID {
50 fn deserialize<D>(deserialiser: D) -> std::result::Result<Self, D::Error>
51 where
52 D: serde::Deserializer<'de>,
53 {
54 let s: &str = Deserialize::deserialize(deserialiser)?;
55 let (season, time_of_day) = s.split('.').collect_tuple().ok_or_else(|| {
56 D::Error::custom(format!(
57 "Invalid input '{s}': Should be in form season.time_of_day"
58 ))
59 })?;
60 Ok(Self {
61 season: season.into(),
62 time_of_day: time_of_day.into(),
63 })
64 }
65}
66
67impl Serialize for TimeSliceID {
68 fn serialize<S>(&self, serialiser: S) -> std::result::Result<S::Ok, S::Error>
69 where
70 S: serde::Serializer,
71 {
72 serialiser.collect_str(self)
73 }
74}
75
76#[derive(PartialEq, Eq, Hash, Clone, Debug)]
78pub enum TimeSliceSelection {
79 Annual,
81 Season(Season),
83 Single(TimeSliceID),
85}
86
87impl TimeSliceSelection {
88 pub fn level(&self) -> TimeSliceLevel {
90 match self {
91 Self::Annual => TimeSliceLevel::Annual,
92 Self::Season(_) => TimeSliceLevel::Season,
93 Self::Single(_) => TimeSliceLevel::DayNight,
94 }
95 }
96
97 pub fn iter<'a>(
99 &'a self,
100 time_slice_info: &'a TimeSliceInfo,
101 ) -> Box<dyn Iterator<Item = (&'a TimeSliceID, Year)> + 'a> {
102 let ts_info = time_slice_info;
103 match self {
104 Self::Annual => Box::new(ts_info.iter()),
105 Self::Season(season) => {
106 Box::new(ts_info.iter().filter(move |(ts, _)| ts.season == *season))
107 }
108 Self::Single(ts) => Box::new(iter::once((ts, ts_info.time_slices[ts]))),
109 }
110 }
111
112 pub fn iter_at_level<'a>(
123 &'a self,
124 time_slice_info: &'a TimeSliceInfo,
125 level: TimeSliceLevel,
126 ) -> Option<Box<dyn Iterator<Item = (Self, Year)> + 'a>> {
127 if level > self.level() {
128 return None;
129 }
130
131 let ts_info = time_slice_info;
132 let iter: Box<dyn Iterator<Item = _>> = match self {
133 Self::Annual => match level {
134 TimeSliceLevel::Annual => Box::new(iter::once((Self::Annual, Year(1.0)))),
135 TimeSliceLevel::Season => Box::new(
136 ts_info
137 .seasons
138 .iter()
139 .map(|(season, duration)| (season.clone().into(), *duration)),
140 ),
141 TimeSliceLevel::DayNight => Box::new(
142 ts_info
143 .time_slices
144 .iter()
145 .map(|(ts, duration)| (ts.clone().into(), *duration)),
146 ),
147 },
148 Self::Season(season) => match level {
149 TimeSliceLevel::Season => {
150 Box::new(iter::once((self.clone(), ts_info.seasons[season])))
151 }
152 TimeSliceLevel::DayNight => Box::new(
153 ts_info
154 .time_slices
155 .iter()
156 .filter(move |(ts, _)| &ts.season == season)
157 .map(|(ts, duration)| (ts.clone().into(), *duration)),
158 ),
159 TimeSliceLevel::Annual => unreachable!(),
160 },
161 Self::Single(time_slice) => Box::new(iter::once((
162 time_slice.clone().into(),
163 ts_info.time_slices[time_slice],
164 ))),
165 };
166
167 Some(iter)
168 }
169}
170
171impl From<TimeSliceID> for TimeSliceSelection {
172 fn from(value: TimeSliceID) -> Self {
173 Self::Single(value)
174 }
175}
176
177impl From<Season> for TimeSliceSelection {
178 fn from(value: Season) -> Self {
179 Self::Season(value)
180 }
181}
182
183impl Display for TimeSliceSelection {
184 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185 match self {
186 Self::Annual => write!(f, "annual"),
187 Self::Season(season) => write!(f, "{season}"),
188 Self::Single(ts) => write!(f, "{ts}"),
189 }
190 }
191}
192
193#[derive(
195 PartialEq, PartialOrd, Copy, Clone, Debug, DeserializeLabeledStringEnum, strum::EnumIter,
196)]
197pub enum TimeSliceLevel {
198 #[string = "daynight"]
200 DayNight,
201 #[string = "season"]
203 Season,
204 #[string = "annual"]
206 Annual,
207}
208
209#[derive(PartialEq, Debug)]
211pub struct TimeSliceInfo {
212 pub times_of_day: IndexSet<TimeOfDay>,
214 pub seasons: IndexMap<Season, Year>,
216 pub time_slices: IndexMap<TimeSliceID, Year>,
218}
219
220impl Default for TimeSliceInfo {
221 fn default() -> Self {
223 let id = TimeSliceID {
224 season: "all-year".into(),
225 time_of_day: "all-day".into(),
226 };
227 let time_slices = [(id.clone(), Year(1.0))].into_iter().collect();
228
229 Self {
230 seasons: iter::once((id.season, Year(1.0))).collect(),
231 times_of_day: iter::once(id.time_of_day).collect(),
232 time_slices,
233 }
234 }
235}
236
237impl TimeSliceInfo {
238 #[allow(clippy::doc_markdown)]
239 pub fn get_time_slice_id_from_str(&self, time_slice: &str) -> Result<TimeSliceID> {
243 let (season, time_of_day) = time_slice
244 .split('.')
245 .collect_tuple()
246 .context("Time slice must be in the form season.time_of_day")?;
247 let season = self
248 .seasons
249 .get_id(season)
250 .with_context(|| format!("{season} is not a known season"))?;
251 let time_of_day = self
252 .times_of_day
253 .get_id(time_of_day)
254 .with_context(|| format!("{time_of_day} is not a known time of day"))?;
255
256 Ok(TimeSliceID {
257 season: season.clone(),
258 time_of_day: time_of_day.clone(),
259 })
260 }
261
262 pub fn get_selection(&self, time_slice: &str) -> Result<TimeSliceSelection> {
266 if time_slice.eq_ignore_ascii_case("annual") {
267 Ok(TimeSliceSelection::Annual)
268 } else if time_slice.contains('.') {
269 let time_slice = self.get_time_slice_id_from_str(time_slice)?;
270 Ok(TimeSliceSelection::Single(time_slice))
271 } else {
272 let season = self
273 .seasons
274 .get_id(time_slice)
275 .with_context(|| format!("'{time_slice}' is not a valid season"))?
276 .clone();
277 Ok(TimeSliceSelection::Season(season))
278 }
279 }
280
281 pub fn iter_ids(&self) -> indexmap::map::Keys<'_, TimeSliceID, Year> {
283 self.time_slices.keys()
284 }
285
286 pub fn iter_seasons(&self) -> indexmap::map::Keys<'_, Season, Year> {
288 self.seasons.keys()
289 }
290
291 pub fn iter(&self) -> impl Iterator<Item = (&TimeSliceID, Year)> {
293 self.time_slices
294 .iter()
295 .map(|(ts, duration)| (ts, *duration))
296 }
297
298 pub fn iter_selections_at_level(
303 &self,
304 level: TimeSliceLevel,
305 ) -> Box<dyn Iterator<Item = TimeSliceSelection> + '_> {
306 match level {
307 TimeSliceLevel::Annual => Box::new(iter::once(TimeSliceSelection::Annual)),
308 TimeSliceLevel::Season => {
309 Box::new(self.seasons.keys().cloned().map(TimeSliceSelection::Season))
310 }
311 TimeSliceLevel::DayNight => {
312 Box::new(self.iter_ids().cloned().map(TimeSliceSelection::Single))
313 }
314 }
315 }
316
317 pub fn iter_selection_share<'a>(
329 &'a self,
330 selection: &'a TimeSliceSelection,
331 level: TimeSliceLevel,
332 ) -> Option<impl Iterator<Item = (TimeSliceSelection, Dimensionless)> + use<>> {
333 let selections = selection.iter_at_level(self, level)?.collect_vec();
335
336 let total_duration: Year = selections.iter().map(|(_, duration)| *duration).sum();
338
339 let iter = selections
341 .into_iter()
342 .map(move |(selection, duration)| (selection, duration / total_duration));
343 Some(iter)
344 }
345
346 pub fn length_for_selection(&self, selection: &TimeSliceSelection) -> Result<Year> {
348 let length: Year = selection.iter(self).map(|(_, duration)| duration).sum();
349 Ok(length)
350 }
351
352 pub fn calculate_share<'a>(
366 &'a self,
367 selection: &'a TimeSliceSelection,
368 level: TimeSliceLevel,
369 value: Dimensionless,
370 ) -> Option<impl Iterator<Item = (TimeSliceSelection, Dimensionless)> + use<>> {
371 let iter = self
372 .iter_selection_share(selection, level)?
373 .map(move |(selection, share)| (selection, value * share));
374 Some(iter)
375 }
376}
377
378#[cfg(test)]
379mod tests {
380 use super::*;
381 use crate::units::UnitType;
382 use itertools::assert_equal;
383 use rstest::{fixture, rstest};
384
385 #[fixture]
386 fn time_slices1() -> [TimeSliceID; 2] {
387 [
388 TimeSliceID {
389 season: "winter".into(),
390 time_of_day: "day".into(),
391 },
392 TimeSliceID {
393 season: "summer".into(),
394 time_of_day: "night".into(),
395 },
396 ]
397 }
398
399 #[fixture]
400 fn time_slice_info1(time_slices1: [TimeSliceID; 2]) -> TimeSliceInfo {
401 TimeSliceInfo {
402 seasons: [("winter".into(), Year(0.5)), ("summer".into(), Year(0.5))]
403 .into_iter()
404 .collect(),
405 times_of_day: ["day".into(), "night".into()].into_iter().collect(),
406 time_slices: time_slices1.map(|ts| (ts, Year(0.5))).into_iter().collect(),
407 }
408 }
409
410 #[fixture]
411 fn time_slice_info2() -> TimeSliceInfo {
412 let time_slices = [
413 TimeSliceID {
414 season: "winter".into(),
415 time_of_day: "day".into(),
416 },
417 TimeSliceID {
418 season: "winter".into(),
419 time_of_day: "night".into(),
420 },
421 TimeSliceID {
422 season: "summer".into(),
423 time_of_day: "day".into(),
424 },
425 TimeSliceID {
426 season: "summer".into(),
427 time_of_day: "night".into(),
428 },
429 ];
430 TimeSliceInfo {
431 times_of_day: ["day".into(), "night".into()].into_iter().collect(),
432 seasons: [("winter".into(), Year(0.5)), ("summer".into(), Year(0.5))]
433 .into_iter()
434 .collect(),
435 time_slices: time_slices
436 .iter()
437 .map(|ts| (ts.clone(), Year(0.25)))
438 .collect(),
439 }
440 }
441
442 #[rstest]
443 fn test_ts_selection_iter_annual(
444 time_slice_info1: TimeSliceInfo,
445 time_slices1: [TimeSliceID; 2],
446 ) {
447 assert_equal(
448 TimeSliceSelection::Annual.iter(&time_slice_info1),
449 time_slices1.iter().map(|ts| (ts, Year(0.5))),
450 );
451 }
452
453 #[rstest]
454 fn test_ts_selection_iter_season(
455 time_slice_info1: TimeSliceInfo,
456 time_slices1: [TimeSliceID; 2],
457 ) {
458 assert_equal(
459 TimeSliceSelection::Season("winter".into()).iter(&time_slice_info1),
460 iter::once((&time_slices1[0], Year(0.5))),
461 );
462 }
463
464 #[rstest]
465 fn test_ts_selection_iter_single(
466 time_slice_info1: TimeSliceInfo,
467 time_slices1: [TimeSliceID; 2],
468 ) {
469 let ts = time_slice_info1
470 .get_time_slice_id_from_str("summer.night")
471 .unwrap();
472 assert_equal(
473 TimeSliceSelection::Single(ts).iter(&time_slice_info1),
474 iter::once((&time_slices1[1], Year(0.5))),
475 );
476 }
477
478 fn assert_selection_equal<I, T>(actual: Option<I>, expected: Option<Vec<(&str, T)>>)
479 where
480 T: UnitType,
481 I: Iterator<Item = (TimeSliceSelection, T)>,
482 {
483 let Some(actual) = actual else {
484 assert!(expected.is_none());
485 return;
486 };
487
488 let ts_info = time_slice_info2();
489 let expected = expected
490 .unwrap()
491 .into_iter()
492 .map(move |(sel, frac)| (ts_info.get_selection(sel).unwrap(), frac));
493 assert_equal(actual, expected);
494 }
495
496 #[rstest]
497 #[case(TimeSliceSelection::Annual, TimeSliceLevel::Annual, Some(vec![("annual", Year(1.0))]))]
498 #[case(TimeSliceSelection::Annual, TimeSliceLevel::Season, Some(vec![("winter", Year(0.5)), ("summer", Year(0.5))]))]
499 #[case(TimeSliceSelection::Annual, TimeSliceLevel::DayNight,
500 Some(vec![("winter.day", Year(0.25)), ("winter.night", Year(0.25)), ("summer.day", Year(0.25)), ("summer.night", Year(0.25))]))]
501 #[case(TimeSliceSelection::Season("winter".into()), TimeSliceLevel::Annual, None)]
502 #[case(TimeSliceSelection::Season("winter".into()), TimeSliceLevel::Season, Some(vec![("winter", Year(0.5))]))]
503 #[case(TimeSliceSelection::Season("winter".into()), TimeSliceLevel::DayNight,
504 Some(vec![("winter.day", Year(0.25)), ("winter.night", Year(0.25))]))]
505 #[case(TimeSliceSelection::Single("winter.day".into()), TimeSliceLevel::Annual, None)]
506 #[case(TimeSliceSelection::Single("winter.day".into()), TimeSliceLevel::Season, None)]
507 #[case(TimeSliceSelection::Single("winter.day".into()), TimeSliceLevel::DayNight, Some(vec![("winter.day", Year(0.25))]))]
508 fn test_ts_selection_iter_at_level(
509 time_slice_info2: TimeSliceInfo,
510 #[case] selection: TimeSliceSelection,
511 #[case] level: TimeSliceLevel,
512 #[case] expected: Option<Vec<(&str, Year)>>,
513 ) {
514 let actual = selection.iter_at_level(&time_slice_info2, level);
515 assert_selection_equal(actual, expected);
516 }
517
518 #[rstest]
519 #[case(TimeSliceSelection::Annual, TimeSliceLevel::Annual, Some(vec![("annual", Dimensionless(8.0))]))]
520 #[case(TimeSliceSelection::Annual, TimeSliceLevel::Season, Some(vec![("winter", Dimensionless(4.0)), ("summer", Dimensionless(4.0))]))]
521 #[case(TimeSliceSelection::Annual, TimeSliceLevel::DayNight,
522 Some(vec![("winter.day", Dimensionless(2.0)), ("winter.night", Dimensionless(2.0)), ("summer.day", Dimensionless(2.0)), ("summer.night", Dimensionless(2.0))]))]
523 #[case(TimeSliceSelection::Season("winter".into()), TimeSliceLevel::Annual, None)]
524 #[case(TimeSliceSelection::Season("winter".into()), TimeSliceLevel::Season, Some(vec![("winter", Dimensionless(8.0))]))]
525 #[case(TimeSliceSelection::Season("winter".into()), TimeSliceLevel::DayNight,
526 Some(vec![("winter.day", Dimensionless(4.0)), ("winter.night", Dimensionless(4.0))]))]
527 #[case(TimeSliceSelection::Single("winter.day".into()), TimeSliceLevel::Annual, None)]
528 #[case(TimeSliceSelection::Single("winter.day".into()), TimeSliceLevel::Season, None)]
529 #[case(TimeSliceSelection::Single("winter.day".into()), TimeSliceLevel::DayNight, Some(vec![("winter.day", Dimensionless(8.0))]))]
530 fn test_calculate_share(
531 time_slice_info2: TimeSliceInfo,
532 #[case] selection: TimeSliceSelection,
533 #[case] level: TimeSliceLevel,
534 #[case] expected: Option<Vec<(&str, Dimensionless)>>,
535 ) {
536 let actual = time_slice_info2.calculate_share(&selection, level, Dimensionless(8.0));
537 assert_selection_equal(actual, expected);
538 }
539}