Skip to content

Commit d03949e

Browse files
committed
fix: adding omf export
1 parent 4368eb6 commit d03949e

File tree

5 files changed

+122
-15
lines changed

5 files changed

+122
-15
lines changed

LoopStructural/datatypes/_point.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def save(self, filename: Union[str, io.StringIO], ext=None):
6767
for k, v in self.properties.items():
6868
df[k] = v
6969
df.to_csv(filename, index=False)
70+
elif ext == 'omf':
71+
from LoopStructural.export.omf_wrapper import add_pointset_to_omf
72+
73+
add_pointset_to_omf(self, filename)
7074
else:
7175
raise ValueError(f'Unknown file extension {ext}')
7276

@@ -154,5 +158,9 @@ def save(self, filename):
154158
for k, v in self.properties.items():
155159
df[k] = v
156160
df.to_csv(filename)
161+
elif ext == 'omf':
162+
from LoopStructural.export.omf_wrapper import add_pointset_to_omf
163+
164+
add_pointset_to_omf(self, filename)
157165
else:
158166
raise ValueError(f'Unknown file extension {ext}')

LoopStructural/datatypes/_surface.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional
33
import numpy as np
44
import io
5+
from pathlib import Path
56

67

78
@dataclass
@@ -128,9 +129,8 @@ def from_dict(cls, d, flatten=False):
128129
d.get('cell_properties', None),
129130
)
130131

131-
def save(self, filename, ext=None):
132-
133-
print(filename, ext)
132+
def save(self, filename, replace_spaces=True, ext=None):
133+
filename = filename.replace(' ', '_') if replace_spaces else filename
134134
if isinstance(filename, (io.StringIO, io.BytesIO)):
135135
if ext is None:
136136
raise ValueError('Please provide an extension for StringIO')
@@ -139,14 +139,12 @@ def save(self, filename, ext=None):
139139
filename = str(filename)
140140
if ext is None:
141141
ext = filename.split('.')[-1].lower()
142-
143142
if ext == 'json':
144143
import json
145144

146145
with open(filename, 'w') as f:
147146
json.dump(self.to_dict(), f)
148147
elif ext == 'vtk':
149-
print(filename)
150148
self.vtk().save(filename)
151149
elif ext == 'obj':
152150
import meshio
@@ -179,5 +177,9 @@ def save(self, filename, ext=None):
179177
for k, v in self.properties.items():
180178
df[k] = v
181179
df.to_csv(filename, index=False)
180+
elif ext == 'omf':
181+
from LoopStructural.export.omf_wrapper import add_surface_to_omf
182+
183+
add_surface_to_omf(self, filename)
182184
else:
183185
raise ValueError(f"Extension {ext} not supported")
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
try:
2+
import omf
3+
except ImportError:
4+
raise ImportError(
5+
"You need to install the omf package to use this feature. "
6+
"You can install it with: pip install --pre omf"
7+
)
8+
import numpy as np
9+
10+
11+
def get_project(filename):
12+
try:
13+
project = omf.load(filename)
14+
except FileNotFoundError:
15+
project = omf.Project(name='LoopStructural Model')
16+
return project
17+
18+
19+
def get_cell_attributes(loopobject):
20+
attributes = []
21+
if loopobject.cell_properties:
22+
attributes += [
23+
omf.NumericAttribute(name=k, array=v, location="faces")
24+
for k, v in loopobject.cell_properties.items()
25+
]
26+
return attributes
27+
28+
29+
def get_point_attributed(loopobject):
30+
attributes = []
31+
if loopobject.properties:
32+
attributes += [
33+
omf.NumericAttribute(name=k, array=v, location="vertices")
34+
for k, v in loopobject.properties.items()
35+
]
36+
return attributes
37+
38+
39+
def add_surface_to_omf(surface, filename):
40+
41+
attributes = []
42+
attributes += get_cell_attributes(surface)
43+
attributes += get_point_attributed(surface)
44+
surface = omf.Surface(
45+
vertices=surface.vertices,
46+
triangles=surface.triangles,
47+
attributes=attributes,
48+
name=surface.name,
49+
)
50+
project = get_project(filename)
51+
52+
project.elements += [surface]
53+
omf.save(project, filename, mode='w')
54+
55+
56+
def add_pointset_to_omf(points, filename):
57+
58+
attributes = []
59+
attributes += get_point_attributed(points)
60+
61+
points = omf.PointSet(
62+
vertices=points.locations,
63+
attributes=attributes,
64+
name=points.name,
65+
)
66+
67+
project = get_project(filename)
68+
project.elements += [points]
69+
omf.save(project, filename, mode='w')
70+
71+
72+
def add_structured_grid_to_omf(grid, filename):
73+
print('Open Mining Format cannot store structured grids')
74+
return
75+
# attributes = []
76+
# attributes += get_cell_attributes(grid)
77+
# attributes += get_point_attributed(grid)
78+
79+
# vol = omf.TensorGridBlockModel(
80+
# name=grid.name,
81+
# tensor_u=np.ones(grid.nsteps[0]).astype(float),
82+
# tensor_v=np.ones(grid.nsteps[0]).astype(float),
83+
# tensor_w=np.ones(grid.nsteps[0]).astype(float),
84+
# origin=grid.origin,
85+
# attributes=attributes,
86+
# )
87+
# project = get_project(filename)
88+
# project.elements += [vol]
89+
# omf.save(project, filename, mode='w')

LoopStructural/modelling/core/geological_model.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ def get_stratigraphic_surfaces(self, units: List[str] = [], bottoms: bool = True
18101810
def get_block_model(self, name='block model'):
18111811
grid = self.bounding_box.structured_grid(name=name)
18121812

1813-
grid.properties_cell['stratigraphy'] = self.evaluate_model(
1813+
grid.cell_properties['stratigraphy'] = self.evaluate_model(
18141814
self.bounding_box.cell_centers(), scale=False
18151815
)
18161816
return grid, self.stratigraphic_ids()
@@ -1831,19 +1831,19 @@ def save(
18311831
if fault_surfaces:
18321832
for s in self.get_fault_surfaces():
18331833
## geoh5 can save everything into the same file
1834-
if extension == ".geoh5":
1834+
if extension == ".geoh5" or extension == '.omf':
18351835
s.save(filename)
18361836
else:
18371837
s.save(f'{name}_{s.name}.{extension}')
18381838
if stratigraphic_surfaces:
18391839
for s in self.get_stratigraphic_surfaces():
1840-
if extension == ".geoh5":
1840+
if extension == ".geoh5" or extension == '.omf':
18411841
s.save(filename)
18421842
else:
18431843
s.save(f'{name}_{s.name}.{extension}')
18441844
if block_model:
18451845
grid, ids = self.get_block_model()
1846-
if extension == ".geoh5":
1846+
if extension == ".geoh5" or extension == '.omf':
18471847
grid.save(filename)
18481848
else:
18491849
grid.save(f'{name}_block_model.{extension}')
@@ -1852,15 +1852,16 @@ def save(
18521852
for group in self.stratigraphic_column.keys():
18531853
if group == "faults":
18541854
continue
1855-
for series in self.stratigraphic_column[group].keys():
1856-
if extension == ".geoh5":
1857-
self.__getitem__(series).save(filename)
1855+
for data in self.__getitem__(group).get_data():
1856+
if extension == ".geoh5" or extension == '.omf':
1857+
data.save(filename)
18581858
else:
1859-
self.__getitem__(series).save(f'{name}_{series}.{extension}')
1859+
data.save(f'{name}_{group}_data.{extension}')
18601860
if fault_data:
18611861
for f in self.fault_names():
18621862
for d in self.__getitem__(f).get_data():
1863-
if extension == ".geoh5":
1863+
if extension == ".geoh5" or extension == '.omf':
1864+
18641865
d.save(filename)
18651866
else:
18661867
d.save(f'{name}_{group}.{extension}')

LoopStructural/modelling/features/_geological_feature.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __setitem__(self, key, item):
9090
def set_model(self, model):
9191
self.model = model
9292

93-
def evaluate_value(self, pos: np.ndarray, ignore_regions=False) -> np.ndarray:
93+
def evaluate_value(self, pos: np.ndarray, ignore_regions=False, fillnan=None) -> np.ndarray:
9494
"""
9595
Evaluate the scalar field value of the geological feature at the locations
9696
specified
@@ -122,6 +122,13 @@ def evaluate_value(self, pos: np.ndarray, ignore_regions=False) -> np.ndarray:
122122
logger.error(f"Unable to evaluate value for {self.name}")
123123
else:
124124
v[mask] = self.interpolator.evaluate_value(evaluation_points[mask, :])
125+
if fillnan == 'nearest':
126+
import scipy.spatial as spatial
127+
128+
nanmask = np.isnan(v)
129+
tree = spatial.cKDTree(evaluation_points[~nanmask, :])
130+
_d, i = tree.query(evaluation_points[nanmask, :])
131+
v[nanmask] = v[~nanmask][i]
125132
return v
126133

127134
def evaluate_gradient(self, pos: np.ndarray, ignore_regions=False) -> np.ndarray:

0 commit comments

Comments
 (0)