7
7
from typing import Dict , Any , Optional , List
8
8
import numpy as np
9
9
10
- from gempy .core .data .surface_points import SurfacePointsTable
11
- from gempy .core .data .orientations import OrientationsTable
12
- from gempy .core .data .structural_frame import StructuralFrame
13
- from gempy .core .data .grid import Grid
14
- from gempy .core .data .geo_model import GeoModel
15
10
from .schema import SurfacePoint , Orientation , GemPyModelJson
16
11
17
12
18
13
class JsonIO :
19
14
"""Class for handling JSON I/O operations for GemPy models."""
20
15
21
16
@staticmethod
22
- def load_model_from_json (file_path : str ) -> GeoModel :
17
+ def load_model_from_json (file_path : str ):
23
18
"""
24
19
Load a GemPy model from a JSON file.
25
20
@@ -29,34 +24,81 @@ def load_model_from_json(file_path: str) -> GeoModel:
29
24
Returns:
30
25
GeoModel: A new GemPy model instance
31
26
"""
27
+ # Import here to avoid circular imports
28
+ from gempy .core .data .geo_model import GeoModel
29
+ from gempy .core .data .grid import Grid
30
+ from gempy .core .data .structural_frame import StructuralFrame
31
+ from gempy_engine .core .data import InterpolationOptions
32
+ from gempy .API .map_stack_to_surfaces_API import map_stack_to_surfaces
33
+
32
34
with open (file_path , 'r' ) as f :
33
35
data = json .load (f )
34
36
35
37
# Validate the JSON data against our schema
36
38
if not JsonIO ._validate_json_schema (data ):
37
39
raise ValueError ("Invalid JSON schema" )
38
40
41
+ # Get surface names from series data
42
+ surface_names = []
43
+ for series in data ['series' ]:
44
+ surface_names .extend (series ['surfaces' ])
45
+
46
+ # Create id to name mapping
47
+ id_to_name = {i : name for i , name in enumerate (surface_names )}
48
+
39
49
# Load surface points and orientations
40
- surface_points = JsonIO ._load_surface_points (data ['surface_points' ])
41
- orientations = JsonIO ._load_orientations (data ['orientations' ])
50
+ surface_points = JsonIO ._load_surface_points (data ['surface_points' ], id_to_name )
51
+ orientations = JsonIO ._load_orientations (data ['orientations' ], id_to_name )
52
+
53
+ # Create structural frame
54
+ structural_frame = StructuralFrame .from_data_tables (surface_points , orientations )
42
55
43
- # TODO: Load other components
44
- raise NotImplementedError ("Only surface points and orientations loading is implemented" )
56
+ # Create grid
57
+ grid = Grid (
58
+ extent = data ['grid_settings' ]['regular_grid_extent' ],
59
+ resolution = data ['grid_settings' ]['regular_grid_resolution' ]
60
+ )
61
+
62
+ # Create interpolation options
63
+ interpolation_options = InterpolationOptions (
64
+ range = 1.7 , # Default value
65
+ c_o = 10 , # Default value
66
+ mesh_extraction = True , # Default value
67
+ number_octree_levels = 1 # Default value
68
+ )
69
+
70
+ # Create GeoModel
71
+ model = GeoModel (
72
+ name = data ['metadata' ]['name' ],
73
+ structural_frame = structural_frame ,
74
+ grid = grid ,
75
+ interpolation_options = interpolation_options
76
+ )
77
+
78
+ # Map series to surfaces
79
+ for series in data ['series' ]:
80
+ map_stack_to_surfaces (model , {series ['name' ]: series ['surfaces' ]})
81
+
82
+ return model
45
83
46
84
@staticmethod
47
- def _load_surface_points (surface_points_data : List [SurfacePoint ]) -> SurfacePointsTable :
85
+ def _load_surface_points (surface_points_data : List [SurfacePoint ], id_to_name : Dict [ int , str ]) :
48
86
"""
49
87
Load surface points from JSON data.
50
88
51
89
Args:
52
90
surface_points_data (List[SurfacePoint]): List of surface point dictionaries
91
+ id_to_name (Dict[int, str]): Mapping from surface IDs to names
53
92
54
93
Returns:
55
94
SurfacePointsTable: A new SurfacePointsTable instance
56
95
57
96
Raises:
58
97
ValueError: If the data is invalid or missing required fields
59
98
"""
99
+ # Import here to avoid circular imports
100
+ from gempy .core .data .surface_points import SurfacePointsTable
101
+
60
102
# Validate data structure
61
103
required_fields = {'x' , 'y' , 'z' , 'id' , 'nugget' }
62
104
for i , sp in enumerate (surface_points_data ):
@@ -79,32 +121,36 @@ def _load_surface_points(surface_points_data: List[SurfacePoint]) -> SurfacePoin
79
121
80
122
# Create name_id_map from unique IDs
81
123
unique_ids = np .unique (ids )
82
- name_id_map = {f"surface_ { id } " : id for id in unique_ids }
124
+ name_id_map = {id_to_name [ id ] : id for id in unique_ids }
83
125
84
126
# Create SurfacePointsTable
85
127
return SurfacePointsTable .from_arrays (
86
128
x = x ,
87
129
y = y ,
88
130
z = z ,
89
- names = [f"surface_ { id } " for id in ids ],
131
+ names = [id_to_name [ id ] for id in ids ],
90
132
nugget = nugget ,
91
133
name_id_map = name_id_map
92
134
)
93
135
94
136
@staticmethod
95
- def _load_orientations (orientations_data : List [Orientation ]) -> OrientationsTable :
137
+ def _load_orientations (orientations_data : List [Orientation ], id_to_name : Dict [ int , str ]) :
96
138
"""
97
139
Load orientations from JSON data.
98
140
99
141
Args:
100
142
orientations_data (List[Orientation]): List of orientation dictionaries
143
+ id_to_name (Dict[int, str]): Mapping from surface IDs to names
101
144
102
145
Returns:
103
146
OrientationsTable: A new OrientationsTable instance
104
147
105
148
Raises:
106
149
ValueError: If the data is invalid or missing required fields
107
150
"""
151
+ # Import here to avoid circular imports
152
+ from gempy .core .data .orientations import OrientationsTable
153
+
108
154
# Validate data structure
109
155
required_fields = {'x' , 'y' , 'z' , 'G_x' , 'G_y' , 'G_z' , 'id' , 'nugget' , 'polarity' }
110
156
for i , ori in enumerate (orientations_data ):
@@ -139,7 +185,7 @@ def _load_orientations(orientations_data: List[Orientation]) -> OrientationsTabl
139
185
140
186
# Create name_id_map from unique IDs
141
187
unique_ids = np .unique (ids )
142
- name_id_map = {f"surface_ { id } " : id for id in unique_ids }
188
+ name_id_map = {id_to_name [ id ] : id for id in unique_ids }
143
189
144
190
# Create OrientationsTable
145
191
return OrientationsTable .from_arrays (
@@ -149,18 +195,18 @@ def _load_orientations(orientations_data: List[Orientation]) -> OrientationsTabl
149
195
G_x = G_x ,
150
196
G_y = G_y ,
151
197
G_z = G_z ,
152
- names = [f"surface_ { id } " for id in ids ],
198
+ names = [id_to_name [ id ] for id in ids ],
153
199
nugget = nugget ,
154
200
name_id_map = name_id_map
155
201
)
156
202
157
203
@staticmethod
158
- def save_model_to_json (model : GeoModel , file_path : str ) -> None :
204
+ def save_model_to_json (model , file_path : str ) -> None :
159
205
"""
160
206
Save a GemPy model to a JSON file.
161
207
162
208
Args:
163
- model (GeoModel) : The GemPy model to save
209
+ model: The GemPy model to save
164
210
file_path (str): Path where to save the JSON file
165
211
"""
166
212
# TODO: Implement saving logic
0 commit comments