Skip to content

Commit

Permalink
Use 32bit floats for surfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dafeda committed Sep 19, 2023
1 parent d1ae20d commit 18d26aa
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/ert/config/surface_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import TYPE_CHECKING, List

import numpy as np
import xarray as xr
import xtgeo
from typing_extensions import Self
Expand Down Expand Up @@ -67,7 +68,9 @@ def from_config_list(cls, surface: List[str]) -> Self:
assert out_file is not None
assert base_surface is not None
try:
surf = xtgeo.surface_from_file(base_surface, fformat="irap_ascii")
surf = xtgeo.surface_from_file(
base_surface, fformat="irap_ascii", dtype=np.float32
)
except Exception as err: # pylint: disable=broad-exception-caught
raise ConfigValidationError.with_context(
f"Could not load surface {base_surface!r}", surface
Expand Down Expand Up @@ -99,7 +102,9 @@ def read_from_runpath(self, run_path: Path, real_nr: int) -> xr.Dataset:
f"'{self.name}' in file {file_name}: "
"File not found\n"
)
surface = xtgeo.surface_from_file(file_path, fformat="irap_ascii")
surface = xtgeo.surface_from_file(
file_path, fformat="irap_ascii", dtype=np.float32
)

da = xr.DataArray(
surface.values,
Expand Down
5 changes: 4 additions & 1 deletion src/ert/storage/local_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Union
from uuid import UUID

import numpy as np
import xtgeo

from ert.config import ExtParamConfig, Field, GenKwConfig, SurfaceConfig
Expand Down Expand Up @@ -58,7 +59,9 @@ def parameter_info(self) -> Dict[str, Any]:

def get_surface(self, name: str) -> xtgeo.RegularSurface:
return xtgeo.surface_from_file(
str(self.mount_point / f"{name}.irap"), fformat="irap_ascii"
str(self.mount_point / f"{name}.irap"),
fformat="irap_ascii",
dtype=np.float32,
)

@property
Expand Down
2 changes: 1 addition & 1 deletion src/ert/storage/migration/block_fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def _migrate_surface(
surface = surfaces[block.name]
except KeyError:
surface = surfaces[block.name] = xtgeo.surface_from_file(
config.base_surface_path, fformat="irap_ascii"
config.base_surface_path, fformat="irap_ascii", dtype=np.float32
)
array = data_file.load(block, np.prod(surface.dimensions)).reshape(
surface.dimensions
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/analysis/test_es_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ def sample_prior(nx, ny):
surf_prior = ens_prior.load_parameters("TOP", list(range(ensemble_size)))
for i in range(ensemble_size):
_prior_init = xtgeo.surface_from_file(
f"surface/surf_init_{i}.irap", fformat="irap_ascii"
f"surface/surf_init_{i}.irap", fformat="irap_ascii", dtype=np.float32
)
np.testing.assert_array_equal(surf_prior[i], _prior_init.values.data)

Expand Down
4 changes: 3 additions & 1 deletion tests/unit_tests/config/test_surface_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def test_runpath_roundtrip(tmp_path, storage, surface):

# compare contents
# Data is saved as 'irap_ascii', which means that we only keep 6 significant digits
actual_surface = xtgeo.surface_from_file(tmp_path / "output", fformat="irap_ascii")
actual_surface = xtgeo.surface_from_file(
tmp_path / "output", fformat="irap_ascii", dtype=np.float32
)
np.testing.assert_allclose(
actual_surface.values, surface.values, rtol=0, atol=1e-06
)
Expand Down
7 changes: 6 additions & 1 deletion tests/unit_tests/storage/test_parameter_sample_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def test_surface_param_update(tmpdir):
values=values)
surf.to_file("surf.irap", fformat="irap_ascii")
surf_fs = xtgeo.surface_from_file("surf.irap", fformat="irap_ascii")
surf_fs = xtgeo.surface_from_file("surf.irap", fformat="irap_ascii", dtype=np.float32)
a, b, c, *_ = surf_fs.values.data.ravel()
output = [a * x**2 + b * x + c for x in range(10)]
Expand Down Expand Up @@ -748,6 +748,9 @@ def test_surface_param_update(tmpdir):
.T
)

assert prior_param.dtype == np.float32
assert posterior_param.dtype == np.float32

assert np.linalg.det(np.cov(prior_param[:3])) > np.linalg.det(
np.cov(posterior_param[:3])
)
Expand All @@ -758,6 +761,7 @@ def test_surface_param_update(tmpdir):
surf = xtgeo.surface_from_file(
f"simulations/realization-{realizations_to_test[0]}/iter-1/surf.irap",
fformat="irap_ascii",
dtype=np.float32,
)

assert base_surface.ncol == surf.ncol
Expand All @@ -772,6 +776,7 @@ def test_surface_param_update(tmpdir):
surf2 = xtgeo.surface_from_file(
f"simulations/realization-{realizations_to_test[1]}/iter-1/surf.irap",
fformat="irap_ascii",
dtype=np.float32,
)

assert not (surf.values == surf2.values).any()
Expand Down

0 comments on commit 18d26aa

Please sign in to comment.