|
| 1 | +# coding=utf8 |
| 2 | +""" |
| 3 | +Copyright (C) 2025 Laurent Courty |
| 4 | +
|
| 5 | +This program is free software; you can redistribute it and/or |
| 6 | +modify it under the terms of the GNU General Public License |
| 7 | +as published by the Free Software Foundation; either version 2 |
| 8 | +of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU General Public License for more details. |
| 14 | +""" |
| 15 | + |
| 16 | +from datetime import datetime, timedelta |
| 17 | +from typing import Dict, Self |
| 18 | +from copy import deepcopy |
| 19 | + |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +from itzi.providers.base import RasterOutputProvider, VectorOutputProvider |
| 23 | +from itzi.data_containers import SimulationData, DrainageNetworkData |
| 24 | + |
| 25 | + |
| 26 | +class MemoryRasterOutputProvider(RasterOutputProvider): |
| 27 | + """Save rasters in memory as numpy arrays.""" |
| 28 | + |
| 29 | + def initialize(self, config: Dict) -> Self: |
| 30 | + """Initialize output provider with simulation configuration.""" |
| 31 | + self.out_map_names = config["out_map_names"] |
| 32 | + |
| 33 | + self.output_maps_dict = {k: [] for k in self.out_map_names.keys()} |
| 34 | + return self |
| 35 | + |
| 36 | + def write_array(self, array: np.ndarray, map_key: str, sim_time: datetime | timedelta) -> None: |
| 37 | + """Save simulation data for current time step.""" |
| 38 | + self.output_maps_dict[map_key].append((deepcopy(sim_time), array.copy())) |
| 39 | + |
| 40 | + def finalize(self, final_data: SimulationData) -> None: |
| 41 | + """Finalize outputs and cleanup.""" |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +class MemoryVectorOutputProvider(VectorOutputProvider): |
| 46 | + """Save drainage simulation outputs in memory.""" |
| 47 | + |
| 48 | + def initialize(self, config: Dict | None = None) -> Self: |
| 49 | + """Initialize output provider with simulation configuration.""" |
| 50 | + self.drainage_data = [] |
| 51 | + |
| 52 | + return self |
| 53 | + |
| 54 | + def write_vector( |
| 55 | + self, drainage_data: DrainageNetworkData, sim_time: datetime | timedelta |
| 56 | + ) -> None: |
| 57 | + """Save simulation data for current time step.""" |
| 58 | + self.drainage_data.append((deepcopy(sim_time), deepcopy(drainage_data))) |
| 59 | + |
| 60 | + def finalize(self, drainage_data: DrainageNetworkData) -> None: |
| 61 | + """Finalize outputs and cleanup.""" |
| 62 | + pass |
0 commit comments