Skip to content

Commit 8ef62c4

Browse files
authored
Rename bulk config cell indices (equinor#13907)
We wish to pivot from the li, lj, lk format for grid cells to i, j, k in the ert code. The li, lj, lk format originates in the smspec file format with regards to local grid refinement. The "name" to "well" column mapping is also resolved in this change, meaning we will refer to the column as well in the code in accord with the user configuration. The wrapper around make_summary_key is responsible for translating these arguments to the parameter naming used in resfo-utilities.
1 parent 00b0112 commit 8ef62c4

2 files changed

Lines changed: 37 additions & 23 deletions

File tree

src/ert/config/_observations.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from __future__ import annotations
22

3-
import inspect
43
import logging
54
import re
65
from collections.abc import Mapping, Sequence
76
from datetime import datetime
87
from enum import StrEnum
98
from pathlib import Path
10-
from typing import Annotated, Any, Literal, Self, assert_never, get_args, get_type_hints
9+
from typing import Annotated, Any, Literal, Self, assert_never, get_type_hints
1110

1211
import numpy as np
1312
import pandas as pd
@@ -94,6 +93,33 @@ def strip_dataframe_whitespaces(df: pl.DataFrame) -> pl.DataFrame:
9493
)
9594

9695

96+
def _make_summary_key(
97+
keyword: str,
98+
number: str | None = None,
99+
well: str | None = None,
100+
nx: str | None = None,
101+
ny: str | None = None,
102+
lgr_name: str | None = None,
103+
i: str | None = None,
104+
j: str | None = None,
105+
k: str | None = None,
106+
) -> str:
107+
def _int_or_none(s: str | None) -> int | None:
108+
return int(s) if s is not None else None
109+
110+
return make_summary_key(
111+
keyword=keyword,
112+
number=_int_or_none(number),
113+
name=well,
114+
nx=_int_or_none(nx),
115+
ny=_int_or_none(ny),
116+
lgr_name=lgr_name,
117+
li=_int_or_none(i),
118+
lj=_int_or_none(j),
119+
lk=_int_or_none(k),
120+
)
121+
122+
97123
class SummaryObservation(_SummaryValues):
98124
error_mode: ErrorModes | None = Field(default=None, exclude=True)
99125
error_min: float | None = Field(default=None, exclude=True)
@@ -219,12 +245,6 @@ def from_bulk_config_dict(
219245

220246
required_csv_columns = ["keyword", "value", "error", "date"]
221247

222-
make_summary_key_optional_args = [
223-
name
224-
for name, param in inspect.signature(make_summary_key).parameters.items()
225-
if param.default is not inspect.Parameter.empty
226-
]
227-
228248
csv_file = observation_dict.get("VALUES")
229249
if csv_file is None:
230250
raise _missing_value_error(context, "VALUES")
@@ -241,9 +261,10 @@ def from_bulk_config_dict(
241261
) from err
242262
csv_df = strip_dataframe_whitespaces(csv_df)
243263

244-
# Rename 'well' column to 'name' to match make_summary_key parameter names
245-
if "well" in csv_df.columns:
246-
csv_df = csv_df.rename({"well": "name"})
264+
make_summary_key_optional_args = set(get_type_hints(_make_summary_key)) - {
265+
"keyword", # keyword is not optional
266+
"return", # return is not a parameter
267+
}
247268

248269
for col in csv_df.columns:
249270
if col not in {
@@ -319,18 +340,13 @@ def from_bulk_config_dict(
319340
for i, row in enumerate(csv_df.iter_rows(named=True)):
320341
kw = validate_nonempty_string(row["keyword"], "keyword", context)
321342

322-
arg_type_hints = get_type_hints(make_summary_key)
323343
optional_kw_args = {
324-
kw_arg: validate_int(val, kw)
325-
if (val := row.get(kw_arg)) is not None
326-
and int in get_args(arg_type_hints.get(kw_arg))
327-
else val
328-
for kw_arg in make_summary_key_optional_args
344+
kw_arg: row.get(kw_arg) for kw_arg in make_summary_key_optional_args
329345
}
330346
try:
331-
summary_key = make_summary_key(
347+
summary_key = _make_summary_key(
332348
keyword=kw,
333-
**optional_kw_args, # type: ignore[arg-type]
349+
**optional_kw_args,
334350
)
335351
except InvalidSummaryKeyError as e:
336352
raise _ill_configured_summary_key(kw, csv_file, i + 1, e) from e
@@ -342,7 +358,7 @@ def from_bulk_config_dict(
342358
date = row["date"]
343359
standardized_date = _parse_date(date).isoformat()
344360

345-
loc_values = well_localization.get(row.get("name"), {})
361+
loc_values = well_localization.get(row.get("well"), {})
346362
shape_id = cls.get_shape_id(
347363
loc_values.get("east"),
348364
loc_values.get("north"),

tests/ert/unit_tests/config/test_bulk_summary_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,7 @@ def test_that_fully_populated_csv_does_not_crash_given_arbitrary_keyword(keyword
469469
WELL OP1 {};
470470
};
471471
"""
472-
csv_columns = (
473-
"well, keyword, value, error, date, number, nx, ny, lgr_name, li, lj, lk"
474-
)
472+
csv_columns = "well, keyword, value, error, date, number, nx, ny, lgr_name, i, j, k"
475473
csv_row = f"OP1, {keyword}, 1e6, 1.0, 2012-02-01, 10, 1, 1, foo, 1, 1, 1"
476474
csv_content_ = f"{csv_columns}\n{csv_row}"
477475

0 commit comments

Comments
 (0)