|
| 1 | +"""Surface/block-model export logic for GeologicalModel (see API.md). |
| 2 | +
|
| 3 | +Extracted from GeologicalModel to separate export/visualization data prep |
| 4 | +from feature-container orchestration. GeologicalModel's @public_api methods |
| 5 | +(``get_fault_surfaces``, ``get_stratigraphic_surfaces``, ``get_block_model``, |
| 6 | +``save``) stay defined directly on the class -- their __qualname__ is part |
| 7 | +of the CI-checked stable API surface -- and delegate to the staticmethods |
| 8 | +here. |
| 9 | +""" |
| 10 | + |
| 11 | +import pathlib |
| 12 | + |
| 13 | +from ...geometry import StructuredGrid |
| 14 | +from ...utils import getLogger |
| 15 | + |
| 16 | +logger = getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class ModelExporter: |
| 20 | + @staticmethod |
| 21 | + def get_fault_surfaces(model, faults=None): |
| 22 | + if faults is None: |
| 23 | + faults = [] |
| 24 | + surfaces = [] |
| 25 | + if len(faults) == 0: |
| 26 | + faults = model.fault_names() |
| 27 | + |
| 28 | + for f in faults: |
| 29 | + surfaces.extend(model.get_feature_by_name(f).surfaces([0], model.bounding_box)) |
| 30 | + return surfaces |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def get_stratigraphic_surfaces(model, units=None, bottoms=True): |
| 34 | + if units is None: |
| 35 | + units = [] |
| 36 | + ## TODO change the stratigraphic column to its own class and have methods to get the relevant surfaces |
| 37 | + surfaces = [] |
| 38 | + units = [] |
| 39 | + if model.stratigraphic_column is None: |
| 40 | + return [] |
| 41 | + units = model.stratigraphic_column.get_isovalues() |
| 42 | + units_for_group = {} |
| 43 | + for name, u in units.items(): |
| 44 | + if u['group'] not in model: |
| 45 | + logger.warning(f"Group {u['group']} not found in model") |
| 46 | + continue |
| 47 | + if u['group'] not in units_for_group: |
| 48 | + units_for_group[u['group']] = [] |
| 49 | + u['name'] = name |
| 50 | + units_for_group[u['group']].append(u) |
| 51 | + for group, us in units_for_group.items(): |
| 52 | + feature = model.get_feature_by_name(group) |
| 53 | + values = [u['value'] for u in us] |
| 54 | + colours = [u['colour'] for u in us] |
| 55 | + names = [u['name'] for u in us] |
| 56 | + surfaces.extend( |
| 57 | + feature.surfaces(values, model.bounding_box, name=names, colours=colours) |
| 58 | + ) |
| 59 | + |
| 60 | + return surfaces |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def get_block_model(model, name='block model'): |
| 64 | + # NOTE: bounding_box.structured_grid() returns loop_common's |
| 65 | + # interpolation-support StructuredGrid (no properties dict); use |
| 66 | + # LoopStructural's own geometry StructuredGrid for storing values. |
| 67 | + grid = StructuredGrid( |
| 68 | + origin=model.bounding_box.origin, |
| 69 | + step_vector=model.bounding_box.step_vector, |
| 70 | + nsteps=model.bounding_box.nsteps, |
| 71 | + name=name, |
| 72 | + ) |
| 73 | + |
| 74 | + grid.cell_properties['stratigraphy'] = model.evaluate_model( |
| 75 | + model.rescale(model.bounding_box.cell_centres()) |
| 76 | + ) |
| 77 | + return grid, model.stratigraphic_ids() |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + def save( |
| 81 | + model, |
| 82 | + filename: str, |
| 83 | + block_model: bool = True, |
| 84 | + stratigraphic_surfaces=True, |
| 85 | + fault_surfaces=True, |
| 86 | + stratigraphic_data=True, |
| 87 | + fault_data=True, |
| 88 | + ): |
| 89 | + path = pathlib.Path(filename) |
| 90 | + extension = path.suffix |
| 91 | + parent = path.parent |
| 92 | + name = path.stem |
| 93 | + stratigraphic_surfaces = model.get_stratigraphic_surfaces() |
| 94 | + if fault_surfaces: |
| 95 | + for s in model.get_fault_surfaces(): |
| 96 | + ## geoh5 can save everything into the same file |
| 97 | + if extension == ".geoh5" or extension == '.omf': |
| 98 | + s.save(filename) |
| 99 | + else: |
| 100 | + s.save(f'{parent}/{name}_{s.name}{extension}') |
| 101 | + if stratigraphic_surfaces: |
| 102 | + for s in model.get_stratigraphic_surfaces(): |
| 103 | + if extension == ".geoh5" or extension == '.omf': |
| 104 | + s.save(filename) |
| 105 | + else: |
| 106 | + s.save(f'{parent}/{name}_{s.name}{extension}') |
| 107 | + if block_model: |
| 108 | + grid, _ids = model.get_block_model() |
| 109 | + if extension == ".geoh5" or extension == '.omf': |
| 110 | + grid.save(filename) |
| 111 | + else: |
| 112 | + grid.save(f'{parent}/{name}_block_model{extension}') |
| 113 | + if stratigraphic_data and model.stratigraphic_column is not None: |
| 114 | + for group in model.stratigraphic_column: |
| 115 | + if group == "faults": |
| 116 | + continue |
| 117 | + for data in model.__getitem__(group).get_data(): |
| 118 | + if extension == ".geoh5" or extension == '.omf': |
| 119 | + data.save(filename) |
| 120 | + else: |
| 121 | + data.save(f'{parent}/{name}_{group}_data{extension}') |
| 122 | + if fault_data: |
| 123 | + for f in model.fault_names(): |
| 124 | + for d in model.__getitem__(f).get_data(): |
| 125 | + if extension == ".geoh5" or extension == '.omf': |
| 126 | + |
| 127 | + d.save(filename) |
| 128 | + else: |
| 129 | + d.save(f'{parent}/{name}_{group}{extension}') |
0 commit comments