Skip to content

Commit

Permalink
Save fields as 32 bit float
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindeide committed Sep 6, 2023
1 parent 069e19a commit ce2c823
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/ert/_c_wrappers/enkf/config/field_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def mask(self):


def field_transform(
data: npt.NDArray[np.double], transform_name: Optional[str]
) -> npt.NDArray[np.double]:
data: npt.NDArray[np.float32], transform_name: Optional[str]
) -> npt.NDArray[np.float32]:
if transform_name is None:
return data
return TRANSFORM_FUNCTIONS[transform_name](data)
Expand Down
10 changes: 6 additions & 4 deletions src/ert/storage/field_utils/field_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def read_field(
field_name: str,
mask: npt.NDArray[np.bool_],
shape: Shape,
) -> np.ma.MaskedArray[Any, np.dtype[np.double]]:
) -> np.ma.MaskedArray[Any, np.dtype[np.float32]]:
path = Path(field_path)
ext = path.suffix
if ext == ".roff":
Expand All @@ -125,17 +125,19 @@ class _Path:
results = import_roff(_Path(str(field_path)), field_name)
values = results["values"]
elif ext == ".grdecl":
values = read_grdecl_3d_property(path, field_name, shape, dtype=np.double)
values = read_grdecl_3d_property(path, field_name, shape, dtype=np.float32)
elif ext == ".bgrdecl":
values = import_bgrdecl(field_path, field_name, shape)
else:
raise ValueError(f'Could not read {field_path}. Unrecognized suffix "{ext}"')

if np.issubdtype(values.dtype, np.floating) and values.dtype == np.float64:
# RMS can only work with 32 bit roff files
values = values.astype(np.float32)
return np.ma.MaskedArray(data=values, mask=mask, fill_value=np.nan) # type: ignore


def save_field(
field: np.ma.MaskedArray[Any, np.dtype[np.double]],
field: np.ma.MaskedArray[Any, np.dtype[np.float32]],
field_name: str,
output_path: _PathLike,
file_format: str,
Expand Down
10 changes: 6 additions & 4 deletions src/ert/storage/field_utils/grdecl_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def read_grdecl_3d_property(
keyword: str,
dimensions: Tuple[int, int, int],
dtype: npt.DTypeLike = float,
) -> npt.NDArray[np.double]:
) -> npt.NDArray[np.float32]:
"""
Read a 3d grid property from a grdecl file, see open_grdecl for description
of format.
Expand Down Expand Up @@ -195,7 +195,7 @@ def import_bgrdecl(
file_path: Union[str, os.PathLike[str]],
field_name: str,
dimensions: Tuple[int, int, int],
) -> Optional[npt.NDArray[np.double]]:
) -> Optional[npt.NDArray[np.float32]]:
field_name = field_name.strip()
with open(file_path, "rb") as f:
for entry in ecl_data_io.lazy_read(f):
Expand All @@ -205,14 +205,16 @@ def import_bgrdecl(
if np.issubdtype(values.dtype, np.integer):
values = values.astype(np.int32)
else:
values = values.astype(np.float64)
values = values.astype(np.float32)
return values.reshape(dimensions, order="F") # type: ignore
return None


# pylint: disable=too-many-arguments
def export_grdecl(
values: Union[np.ma.MaskedArray[Any, np.dtype[np.double]], npt.NDArray[np.double]],
values: Union[
np.ma.MaskedArray[Any, np.dtype[np.float32]], npt.NDArray[np.float32]
],
file_path: Union[str, os.PathLike[str]],
param_name: str,
binary: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion src/ert/storage/field_utils/roff_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def export_roff(
data: np.ma.MaskedArray[Any, np.dtype[np.double]],
data: np.ma.MaskedArray[Any, np.dtype[np.float32]],
filelike: Union[TextIO, BinaryIO, _PathLike],
parameter_name: str,
binary: bool = True,
Expand Down

0 comments on commit ce2c823

Please sign in to comment.