Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store xtgeo.RegularSurface as bytestream #761

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [#755](https://github.com/equinor/webviz-subsurface/pull/755) - Updated existing and added new tests for the Drogon dataset.

### Changed
- [#761](https://github.com/equinor/webviz-subsurface/pull/761) - Store `xtgeo.RegularSurface` as bytestream instead of serializing to `json`.

## [0.2.5] - 2021-09-03
### Added
- [#733](https://github.com/equinor/webviz-subsurface/pull/733) - Added plugin to visualize well logs from files using [videx-welllog](https://github.com/equinor/videx-wellog).
Expand Down
41 changes: 12 additions & 29 deletions webviz_subsurface/_models/surface_set_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import List, Tuple, Callable, Optional, Any, Dict
from pathlib import Path
import warnings
import json
import io

import numpy as np
Expand Down Expand Up @@ -132,12 +131,15 @@ def calculate_statistical_surface(
# When portable check if the surface has been stored
# if not calculate
try:
surface = save_statistical_surface(sorted(list(df["path"])), calculation)
surface_stream = save_statistical_surface(
sorted(list(df["path"])), calculation
)
except OSError:
surface = save_statistical_surface_no_store(
surface_stream = save_statistical_surface_no_store(
sorted(list(df["path"])), calculation
)
return surface_from_json(json.load(surface))

return xtgeo.surface_from_file(surface_stream, fformat="irap_binary")

def webviz_store_statistical_calculation(
self,
Expand Down Expand Up @@ -230,7 +232,9 @@ def save_statistical_surface_no_store(
surface = xtgeo.RegularSurface(
ncol=1, nrow=1, xinc=1, yinc=1
) # 1's as input is required
return io.BytesIO(surface_to_json(surface).encode())
stream = io.BytesIO()
surface.to_file(stream, fformat="irap_binary")
return stream


@webvizstore
Expand All @@ -252,7 +256,9 @@ def save_statistical_surface(fns: List[str], calculation: str) -> io.BytesIO:
surface = xtgeo.RegularSurface(
ncol=1, nrow=1, xinc=1, yinc=1
) # 1's as input is required
return io.BytesIO(surface_to_json(surface).encode())
stream = io.BytesIO()
surface.to_file(stream, fformat="irap_binary")
return stream


# pylint: disable=too-many-return-statements
Expand All @@ -275,26 +281,3 @@ def get_statistical_surface(
return xtgeo.RegularSurface(
ncol=1, nrow=1, xinc=1, yinc=1
) # 1's as input is required


def surface_to_json(surface: xtgeo.RegularSurface) -> str:
"""Returns a json represention of a Xtgeo surface instance"""
return json.dumps(
{
"ncol": surface.ncol,
"nrow": surface.nrow,
"xori": surface.xori,
"yori": surface.yori,
"rotation": surface.rotation,
"xinc": surface.xinc,
"yinc": surface.yinc,
"values": surface.values.copy().filled(np.nan).tolist(),
}
)


def surface_from_json(surfaceobj: dict) -> xtgeo.RegularSurface:
"""Returns a Xtgeo surface instance from a json surface representation"""
surface = xtgeo.RegularSurface(**surfaceobj)
surface.values = surfaceobj["values"]
return surface