Skip to content

Commit

Permalink
Move type parsing for genkw from enkf_main to gen_kw_config
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-eq committed Jan 24, 2025
1 parent 02df6ee commit 1f604fe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
18 changes: 16 additions & 2 deletions src/ert/config/gen_kw_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,23 @@ def write_to_runpath(
f" is of size {len(self.transform_functions)}, expected {array.size}"
)

def parse_value(value: float | int | str) -> float | int | str:
if isinstance(value, float | int):
return value
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return value

data = dict(
zip(array["names"].values.tolist(), array.values.tolist(), strict=False)
zip(
array["names"].values.tolist(),
[parse_value(i) for i in array.values],
strict=False,
)
)

log10_data = {
Expand All @@ -358,7 +373,6 @@ def write_to_runpath(
template = template.replace(f"<{key}>", f"{value:.6g}")
with open(run_path / target_file, "w", encoding="utf-8") as f:
f.write(template)

if log10_data:
return {self.name: data, f"LOG10_{self.name}": log10_data}
else:
Expand Down
19 changes: 2 additions & 17 deletions src/ert/enkf_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,9 @@ def _value_export_json(
if len(values) == 0:
return

def parse_value(value: float | int | str) -> float | int | str:
if isinstance(value, float | int):
return value
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return value

# Hierarchical
json_out: dict[str, float | dict[str, float | int | str]] = {
key: {
inner_key: parse_value(inner_value)
for inner_key, inner_value in param_map.items()
}
for key, param_map in values.items()
json_out: dict[str, float | dict[str, float]] = {
key: dict(param_map.items()) for key, param_map in values.items()
}

# Disallow NaN from being written: ERT produces the parameters and the only
Expand Down

0 comments on commit 1f604fe

Please sign in to comment.