@@ -173,12 +173,13 @@ def load_model_from_json(file_path: str):
173
173
interpolation_options = interpolation_options
174
174
)
175
175
176
- # Set the metadata with proper dates
176
+ # Set the metadata with proper dates and defaults
177
+ metadata = data .get ('metadata' , {})
177
178
model_meta = GeoModelMeta (
178
- name = model_name ,
179
- creation_date = data . get ( ' metadata' , {}) .get ('creation_date' , current_date ),
180
- last_modification_date = data . get ( ' metadata' , {}) .get ('last_modification_date' , current_date ),
181
- owner = data . get ( ' metadata' , {}) .get ('owner' , "GemPy Modeller" )
179
+ name = metadata . get ( 'name' , model . meta . name ), # Use model's name if available
180
+ creation_date = metadata .get ('creation_date' , current_date ), # Set current date as default
181
+ last_modification_date = metadata .get ('last_modification_date' , current_date ), # Set current date as default
182
+ owner = metadata .get ('owner' , "GemPy Modeller" ) # Set default owner
182
183
)
183
184
model .meta = model_meta
184
185
@@ -331,13 +332,16 @@ def save_model_to_json(model, file_path: str) -> None:
331
332
model: The GemPy model to save
332
333
file_path (str): Path where to save the JSON file
333
334
"""
334
- # Create JSON structure
335
+ # Get current date for default values
336
+ current_date = datetime .now ().strftime ("%Y-%m-%d" )
337
+
338
+ # Create JSON structure with metadata handling
335
339
json_data = {
336
340
"metadata" : {
337
- "name" : model .meta .name ,
338
- "creation_date" : model .meta .creation_date ,
339
- "last_modification_date" : model .meta .last_modification_date ,
340
- "owner" : model .meta .owner
341
+ "name" : model .meta .name if model . meta . name is not None else "GemPy Model" ,
342
+ "creation_date" : model .meta .creation_date if model . meta . creation_date is not None else current_date ,
343
+ "last_modification_date" : model .meta .last_modification_date if model . meta . last_modification_date is not None else current_date ,
344
+ "owner" : model .meta .owner if model . meta . owner is not None else "GemPy Modeller"
341
345
},
342
346
"surface_points" : [],
343
347
"orientations" : [],
@@ -364,10 +368,10 @@ def save_model_to_json(model, file_path: str) -> None:
364
368
# Get series and surface information
365
369
for group in model .structural_frame .structural_groups :
366
370
series_entry = {
367
- "name" : group .name ,
368
- "surfaces" : [element .name for element in group .elements ],
369
- "structural_relation" : group .structural_relation .name ,
370
- "colors" : [element .color for element in group .elements ]
371
+ "name" : str ( group .name ) ,
372
+ "surfaces" : [str ( element .name ) for element in group .elements ],
373
+ "structural_relation" : str ( group .structural_relation .name ) ,
374
+ "colors" : [str ( element .color ) for element in group .elements ]
371
375
}
372
376
json_data ["series" ].append (series_entry )
373
377
@@ -430,6 +434,9 @@ def _validate_json_schema(data: Dict) -> None:
430
434
# Set default nugget if not provided
431
435
if 'nugget' not in point :
432
436
point ['nugget' ] = 0.0 # Default nugget for surface points
437
+ # Set default id if not provided
438
+ if 'id' not in point :
439
+ point ['id' ] = 0 # Default id
433
440
434
441
# Validate orientations
435
442
for orientation in data ['orientations' ]:
@@ -443,6 +450,9 @@ def _validate_json_schema(data: Dict) -> None:
443
450
# Set default polarity if not provided
444
451
if 'polarity' not in orientation :
445
452
orientation ['polarity' ] = 1
453
+ # Set default id if not provided
454
+ if 'id' not in orientation :
455
+ orientation ['id' ] = 0 # Default id
446
456
447
457
# Validate grid settings
448
458
grid_settings = data ['grid_settings' ]
@@ -485,18 +495,34 @@ def _validate_json_schema(data: Dict) -> None:
485
495
if 'number_octree_levels' in data ['interpolation_options' ] and not isinstance (data ['interpolation_options' ]['number_octree_levels' ], int ):
486
496
raise ValueError ("Number of octree levels must be an integer" )
487
497
488
- # Validate metadata if present
489
- if 'metadata' in data :
490
- metadata = data ['metadata' ]
491
- if not isinstance (metadata , dict ):
492
- raise ValueError ("Metadata must be a dictionary" )
493
- if 'name' in metadata and not isinstance (metadata ['name' ], str ):
494
- raise ValueError ("Metadata name must be a string" )
495
- if 'creation_date' in metadata and not isinstance (metadata ['creation_date' ], str ):
496
- raise ValueError ("Metadata creation date must be a string" )
497
- if 'last_modification_date' in metadata and not isinstance (metadata ['last_modification_date' ], str ):
498
- raise ValueError ("Metadata last modification date must be a string" )
499
- if 'owner' in metadata and not isinstance (metadata ['owner' ], str ):
500
- raise ValueError ("Metadata owner must be a string" )
501
-
498
+ # Validate and set default metadata
499
+ if 'metadata' not in data :
500
+ data ['metadata' ] = {}
501
+
502
+ metadata = data ['metadata' ]
503
+ if not isinstance (metadata , dict ):
504
+ raise ValueError ("Metadata must be a dictionary" )
505
+
506
+ # Set default values for metadata
507
+ current_date = datetime .now ().strftime ("%Y-%m-%d" )
508
+ if 'name' not in metadata or metadata ['name' ] is None :
509
+ metadata ['name' ] = "GemPy Model"
510
+ elif not isinstance (metadata ['name' ], str ):
511
+ raise ValueError ("Metadata name must be a string" )
512
+
513
+ if 'creation_date' not in metadata or metadata ['creation_date' ] is None :
514
+ metadata ['creation_date' ] = current_date
515
+ elif not isinstance (metadata ['creation_date' ], str ):
516
+ raise ValueError ("Metadata creation_date must be a string" )
517
+
518
+ if 'last_modification_date' not in metadata or metadata ['last_modification_date' ] is None :
519
+ metadata ['last_modification_date' ] = current_date
520
+ elif not isinstance (metadata ['last_modification_date' ], str ):
521
+ raise ValueError ("Metadata last_modification_date must be a string" )
522
+
523
+ if 'owner' not in metadata or metadata ['owner' ] is None :
524
+ metadata ['owner' ] = "GemPy Modeller"
525
+ elif not isinstance (metadata ['owner' ], str ):
526
+ raise ValueError ("Metadata owner must be a string" )
527
+
502
528
return True
0 commit comments