|
| 1 | +import warnings |
| 2 | +from pathlib import Path |
| 3 | +from typing import Literal, TypedDict |
| 4 | + |
| 5 | +from natsort import natsorted |
| 6 | +from ruamel.yaml import YAML |
| 7 | + |
| 8 | +from ert.cli.main import ErtCliError |
| 9 | +from ert.config import ErtConfig, Observation |
| 10 | +from ert.plugins import ErtRuntimePlugins |
| 11 | + |
| 12 | + |
| 13 | +class YamlObservation(TypedDict): |
| 14 | + date: str |
| 15 | + value: float |
| 16 | + error: float |
| 17 | + |
| 18 | + |
| 19 | +class SummaryDict(TypedDict): |
| 20 | + key: str |
| 21 | + observations: list[YamlObservation] |
| 22 | + |
| 23 | + |
| 24 | +YamlDict = dict[Literal["smry"], list[SummaryDict]] |
| 25 | + |
| 26 | + |
| 27 | +class YamlConverter: |
| 28 | + TARGET_FILE = "summary_observations.yaml" |
| 29 | + |
| 30 | + def __init__(self, observations: list[Observation]) -> None: |
| 31 | + summary_observations = [ |
| 32 | + o for o in observations if o.type == "summary_observation" |
| 33 | + ] |
| 34 | + if not summary_observations: |
| 35 | + raise ErtCliError("No summary observations in configuration.\nExiting ...") |
| 36 | + |
| 37 | + self.summary_observations = summary_observations |
| 38 | + |
| 39 | + def _summary_to_yaml_dict(self) -> YamlDict: |
| 40 | + summary_keys: set[str] = {o.key for o in self.summary_observations} |
| 41 | + summary_list: list[SummaryDict] = [] |
| 42 | + for key in natsorted(summary_keys): |
| 43 | + observations_with_key = [ |
| 44 | + o for o in self.summary_observations if o.key == key |
| 45 | + ] |
| 46 | + chronological_observations = sorted( |
| 47 | + observations_with_key, key=lambda o: o.date |
| 48 | + ) |
| 49 | + # Round dates without HH/MM/SS to just date |
| 50 | + for o in chronological_observations: |
| 51 | + if o.date.endswith("T00:00:00"): |
| 52 | + o.date = o.date.split("T")[0] |
| 53 | + obs_dicts: list[YamlObservation] = [ |
| 54 | + { |
| 55 | + "date": o.date, |
| 56 | + "value": o.value, |
| 57 | + "error": o.error, |
| 58 | + } |
| 59 | + for o in chronological_observations |
| 60 | + ] |
| 61 | + summary_dict: SummaryDict = {"key": key, "observations": obs_dicts} |
| 62 | + summary_list.append(summary_dict) |
| 63 | + return {"smry": summary_list} |
| 64 | + |
| 65 | + def export_yaml(self) -> None: |
| 66 | + yaml = YAML() |
| 67 | + yaml_dict = self._summary_to_yaml_dict() |
| 68 | + try: |
| 69 | + with Path(self.TARGET_FILE).open("x", encoding="utf-8") as f: |
| 70 | + yaml.dump(yaml_dict, f) |
| 71 | + except FileExistsError as error: |
| 72 | + raise ErtCliError( |
| 73 | + f"A file with name '{self.TARGET_FILE}' already exists. " |
| 74 | + "Will not overwrite it and exit instead." |
| 75 | + ) from error |
| 76 | + print(f"Successfully wrote summary observations to '{self.TARGET_FILE}'.") |
| 77 | + |
| 78 | + |
| 79 | +def convert_summary_to_yaml(config: str, site_plugins: ErtRuntimePlugins) -> None: |
| 80 | + with warnings.catch_warnings(): |
| 81 | + warnings.filterwarnings(action="ignore") |
| 82 | + ert_config = ErtConfig.with_plugins(site_plugins).from_file(config) |
| 83 | + |
| 84 | + observations = ert_config.observation_declarations |
| 85 | + |
| 86 | + yaml_exporter = YamlConverter( |
| 87 | + observations=observations, |
| 88 | + ) |
| 89 | + yaml_exporter.export_yaml() |
0 commit comments