Skip to content

Commit 8cb4d7b

Browse files
committed
WIP
1 parent 1ee55b2 commit 8cb4d7b

File tree

4 files changed

+123
-72
lines changed

4 files changed

+123
-72
lines changed

deepfield/field/base_component.py

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def __init__(self, name=None, section=None, kw=None, custom_loader=None, postpro
4343
self._binary_section = binary_section
4444
self._binary_process = binary_process
4545

46-
def _load_value(self, data, path_to_results, basename, logger):
47-
__import__('ipdb').set_trace()
46+
def _load_value(self, data, binary_data, logger):
4847
if self._binary_file is not None:
49-
val = self._load_ecl_binary_value(path_to_results, basename, logger)
48+
val = self._load_ecl_binary_value(binary_data, logger)
5049
else:
5150
val = None
5251
if val is not None:
5352
self._value = val
5453
return self
54+
__import__('pdb').set_trace()
5555
if self._custom_loader is not None:
5656
self._value = self._custom_loader(data)
5757
return self
@@ -62,21 +62,30 @@ def _load_value(self, data, path_to_results, basename, logger):
6262
self._value = self._not_present
6363
return self
6464

65-
def _load_ecl_binary_value(self, path_to_results, basename, logger):
66-
path = get_single_path(path_to_results, basename + self._binary_file, logger)
67-
if path is None:
65+
def _load_ecl_binary_value(self, binary_data, logger):
66+
if binary_data is None:
6867
return None
69-
attrs = [self._binary_section]
70-
sections = read_ecl_bin(path, attrs, logger=logger)
71-
if self._binary_section in sections:
72-
val = sections[self._binary_section]
73-
if self._binary_process is not None:
74-
return self._binary_process(val)
75-
else:
68+
if self._binary_file not in binary_data:
7669
return None
70+
file_data = binary_data[self._binary_file]
71+
val = file_data.find_unique(self._binary_section)
72+
if val is None:
73+
return None
74+
if self._binary_process is not None:
75+
return self._binary_process(file_data[val].value)
76+
return file_data[val].value
7777

78-
def load(self, data, path_to_results, basename, logger):
79-
self._load_value(data, path_to_results, basename, logger)
78+
def load(self, data, binary_data, logger):
79+
self._load_value(data, binary_data, logger)
80+
81+
@property
82+
def value(self):
83+
"""The value property."""
84+
return self._value
85+
86+
@value.setter
87+
def value(self, value):
88+
self._value = value
8089

8190
MAX_STRLEN = 40
8291

@@ -100,18 +109,20 @@ class BaseComponent:
100109
"""Base class for components of geological model."""
101110

102111
_attributes_to_load = []
103-
def __init__(self, *args, **kwargs):
104-
_ = args
112+
def __init__(self, dump=None, field=None):
113+
self._field = None
114+
if dump is not None:
115+
self._attributes = dump['attributes']
116+
self.field = dump['field']
117+
self._state = dump['state']
118+
return None
119+
self._attributes = []
105120
self._state = State()
106-
self._class_name = kwargs.pop('class_name', self.__class__.__name__)
107-
self._data = {}
108-
if 'field' in kwargs:
109-
self.field = kwargs['field']
110-
else:
111-
self.field = None
112-
for k, v in kwargs.items():
113-
if k != 'field':
114-
setattr(self, k, v)
121+
# self._class_name = kwargs.pop('class_name', self.__class__.__name__)
122+
self.field = field
123+
# for k, v in kwargs.items():
124+
# if k != 'field':
125+
# setattr(self, k, v)
115126

116127
@property
117128
def field(self):
@@ -130,7 +141,7 @@ def field(self, field):
130141
@property
131142
def attributes(self):
132143
"""Array of attributes."""
133-
return tuple(self._data.keys())
144+
return tuple((attr.name for attr in self._attributes))
134145

135146
@property
136147
def empty(self):
@@ -147,7 +158,7 @@ def values(self):
147158

148159
def items(self):
149160
"""Returns pairs of attribute's names and data."""
150-
return self._data.items()
161+
return ((attr.name, attr.value) for attr in self._attributes)
151162

152163
@property
153164
def state(self):
@@ -157,11 +168,11 @@ def state(self):
157168
@property
158169
def class_name(self):
159170
"""Name of the component."""
160-
return self._class_name
171+
return self.__class__.__name__
161172

162-
@class_name.setter
163-
def class_name(self, v):
164-
self._class_name = v
173+
# @class_name.setter
174+
# def class_name(self, v):
175+
# self._class_name = v
165176

166177
def empty_like(self):
167178
"""Get an empty component with the same state and the structure of embedded BaseComponents (if any)."""
@@ -187,18 +198,28 @@ def del_state(self, *args):
187198
return self
188199

189200
def __getattr__(self, key):
190-
if key.upper() in self._data:
191-
return self._data[key.upper()]
201+
for attr in self._attributes:
202+
if key.upper() == attr.name:
203+
return attr.value
192204
raise AttributeError("{} has no attribute {}".format(self.class_name, key))
205+
def dump_dict(self):
206+
return {
207+
'attributes': deepcopy(self._attributes),
208+
'field': self.field,
209+
'state': self.state
210+
}
193211

194212
def __getitem__(self, key):
195213
return getattr(self, key)
196214

197215
def __setattr__(self, key, value):
198216
if (key[0] == '_') or (key in dir(self)):
199217
return super().__setattr__(key, value)
200-
self._data[key.upper()] = value
201-
return self
218+
for att in self._attributes:
219+
if key == att.name:
220+
att.value = value
221+
return None
222+
raise AttributeError(f'{self.class_name} has no attribute {key}.')
202223

203224
def __setitem__(self, key, value):
204225
return setattr(self, key, value)
@@ -335,11 +356,11 @@ def load(self, path_or_buffer, **kwargs):
335356
return self._get_fmt_loader(fmt)(path_or_buffer, **kwargs)
336357
return self._read_buffer(path_or_buffer, **kwargs)
337358

338-
def load(self, data, path_to_results, basename, logger):
359+
def load(self, data, binary_data, logger):
339360
"""Load data."""
340361
self._attributes = deepcopy(self._attributes_to_load)
341362
for attr in self._attributes:
342-
attr.load(data, path_to_results, basename, logger)
363+
attr.load(data, binary_data, logger)
343364

344365

345366
def _load_ecl_binary(self, path_to_results, **kwargs):

deepfield/field/base_tree.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class BaseTree(BaseComponent):
3636
"""
3737

3838
def __init__(self, node=None, nodeclass=None, **kwargs):
39+
__import__('pdb').set_trace()
3940
super().__init__(**kwargs)
4041
nodeclass = BaseTreeNode if nodeclass is None else nodeclass
4142
self._root = nodeclass(name='FIELD', ntype="group",
@@ -54,22 +55,6 @@ def copy(self):
5455
node_copy.parent = copy[node.parent.name]
5556
return copy
5657

57-
@property
58-
def field(self):
59-
return self._field()
60-
61-
@field.setter
62-
def field(self, field):
63-
"""Set field to which component belongs."""
64-
if isinstance(field, ref) or field is None:
65-
self._field = field
66-
return self
67-
self._field = ref(field)
68-
if hasattr(self, 'root'):
69-
for node in self:
70-
node.field = field
71-
return self
72-
7358
@property
7459
def root(self):
7560
"""Tree root."""

deepfield/field/field.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from .utils import get_single_path
3434
from .wells import Wells
3535
import resdp
36+
import resdp.binary
3637

3738
ACTOR = None
3839

@@ -458,17 +459,20 @@ def _load_data(self, raise_errors=False, include_binary=True):
458459

459460
data = resdp.load(pathlib.Path(self.path))
460461
self._data = data
462+
if include_binary:
463+
self._binary_data = resdp.binary.load(pathlib.Path(self.path))
464+
else:
465+
self._binary_data = None
461466

462-
path_to_results = os.path.join(os.path.dirname(self.path), 'RESULTS')
463467
for comp in self._components:
464-
getattr(self, comp).load(self._data, path_to_results, self.basename, self._logger)
468+
getattr(self, comp).load(self._data, self._binary_data, self._logger)
465469

466470
# loaders = self._get_loaders(self._config)
467471
# tnav_ascii_parser(self._path, loaders, self._logger, encoding=self._encoding,
468472
# raise_errors=raise_errors)
469473
#
470-
# self.grid = specify_grid(self.grid)
471-
# self.grid.create_vtk_grid()
474+
self.grid = specify_grid(self.grid)
475+
self.grid.create_vtk_grid()
472476
#
473477
# if 'MINPV' in self.grid.attributes:
474478
# if 'ACTNUM' in self.grid.state.binary_attributes:

deepfield/field/grids.py

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,58 @@ class Grid(SpatialComponent):
2020
Attribute(
2121
kw='DIMENS',
2222
section='RUNSPEC',
23-
binary_file='.EGRID',
23+
binary_file='EGRID',
2424
binary_section='GRIDHEAD',
2525
binary_process=binary_utils.gridhead_to_dimens
26+
),
27+
Attribute(
28+
kw='ACTNUM',
29+
section='GRID',
30+
binary_file='EGRID',
31+
binary_section='ACTNUM',
32+
binary_process=lambda val: val.astype(bool)
33+
),
34+
Attribute(
35+
kw='ZCORN',
36+
section='GRID',
37+
binary_file='EGRID',
38+
binary_section='ZCORN',
39+
),
40+
Attribute(
41+
kw='COORD',
42+
section='GRID',
43+
binary_file='EGRID',
44+
binary_section='COORD'
45+
),
46+
Attribute(
47+
kw='DX',
48+
section='GRID',
49+
),
50+
Attribute(
51+
kw='DY',
52+
section='GRID',
53+
),
54+
Attribute(
55+
kw='DZ',
56+
section='GRID',
57+
),
58+
Attribute(
59+
kw='TOPS',
60+
section='GRID',
2661
)
2762
]
2863

2964
def __init__(self, *args, **kwargs):
65+
__import__('pdb').set_trace()
3066
super().__init__(*args, **kwargs)
3167
self._vtk_grid = vtk.vtkUnstructuredGrid()
3268
self._vtk_locator = None
3369
self._actnum_ids = None
3470
self.to_spatial()
35-
if 'MAPAXES' not in self:
36-
setattr(self, 'MAPAXES', np.array([0, 1, 0, 0, 1, 0]))
37-
if 'ACTNUM' not in self and 'DIMENS' in self:
38-
self.actnum = np.ones(self.dimens, dtype=bool)
71+
# if 'MAPAXES' not in self:
72+
# setattr(self, 'MAPAXES', np.array([0, 1, 0, 0, 1, 0]))
73+
# if 'ACTNUM' not in self and 'DIMENS' in self:
74+
# self.actnum = np.ones(self.dimens, dtype=bool)
3975

4076
@property
4177
def vtk_grid(self):
@@ -115,7 +151,10 @@ def get_xyz(self, ijk=None):
115151
@property
116152
def origin(self):
117153
"""Grid axes origin relative to the map coordinates."""
118-
return np.array([self.mapaxes[2], self.mapaxes[3], self.tops.ravel()[0]])
154+
if 'MAPAXES' in self.attributes:
155+
return np.array([self.mapaxes[2], self.mapaxes[3], self.tops.ravel()[0]])
156+
else:
157+
return np.array([0, 0, 0])
119158

120159
@property
121160
def cell_centroids(self):
@@ -227,25 +266,27 @@ def _to_spatial(self, attr, **kwargs):
227266
"""Spatial order 'F' transformations."""
228267
_ = kwargs
229268
data = getattr(self, attr)
269+
dimens_vals = self.dimens.values.reshape(-1)
230270
if isinstance(data, np.ndarray) and data.ndim == 1:
231271
if attr in ['ACTNUM', 'DX', 'DY', 'DZ']:
232-
data = data.reshape(self.dimens, order='F')
272+
data = data.reshape(dimens_vals, order='F')
233273
elif attr == 'TOPS':
234-
if data.size == np.prod(self.dimens):
235-
data = data.reshape(self.dimens, order='F')
274+
if data.size == np.prod(dimens_vals):
275+
data = data.reshape(dimens_vals, order='F')
236276
else:
237-
data = data.reshape(self.dimens[:2], order='F')
277+
data = data.reshape(dimens_vals[:2], order='F')
238278
elif attr == 'COORD':
239-
nx, ny, nz = self.dimens
279+
nx, ny, nz = dimens_vals
240280
data = data.reshape(-1, 6)
241281
data = data.reshape((nx + 1, ny + 1, 6), order='F')
242282
elif attr == 'ZCORN':
243-
nx, ny, nz = self.dimens
283+
nx, ny, nz = dimens_vals
244284
data = data.reshape((2, nx, 2, ny, 2, nz), order='F')
245285
data = np.moveaxis(data, range(6), (3, 0, 4, 1, 5, 2))
246286
data = data.reshape((nx, ny, nz, 8), order='F')
247287
else:
248288
return self
289+
__import__('pdb').set_trace()
249290
setattr(self, attr, data)
250291
return self
251292

@@ -549,7 +590,7 @@ def specify_grid(grid):
549590
"""
550591
if not isinstance(grid, (CornerPointGrid, OrthogonalGrid)):
551592
if ('DX' in grid) and ('DY' in grid) and ('DZ' in grid):
552-
grid = OrthogonalGrid(**dict(grid.items()), field=grid.field)
593+
grid = OrthogonalGrid(dump=grid.dump_dict())
553594
else:
554-
grid = CornerPointGrid(**dict(grid.items()), field=grid.field)
595+
grid = CornerPointGrid(dump=grid.dump_dict())
555596
return grid

0 commit comments

Comments
 (0)