11import geoh5py
22import geoh5py .workspace
33import numpy as np
4- from LoopStructural . datatypes import ValuePoints , VectorPoints
4+ import pandas as pd
55
6+ from LoopStructural .datatypes import ValuePoints , VectorPoints
67
7- def add_surface_to_geoh5 (filename , surface , overwrite = True , groupname = "Loop" ):
8+ def add_group_to_geoh5 (filename , groupname = "Loop" , parent = None , overwrite = True ):
89 with geoh5py .workspace .Workspace (filename ) as workspace :
10+
911 group = workspace .get_entity (groupname )[0 ]
12+ if group and overwrite :
13+ group .allow_delete = True
14+ workspace .remove_entity (group )
1015 if not group :
1116 group = geoh5py .groups .ContainerGroup .create (
1217 workspace , name = groupname , allow_delete = True
1318 )
19+ if parent is not None :
20+ parent = workspace .get_entity (parent )[0 ]
21+ if parent :
22+ parent .add_children (group )
23+ return group .uid
24+ def add_surface_to_geoh5 (filename , surface , overwrite = True , group = "Loop" ):
25+ with geoh5py .workspace .Workspace (filename ) as workspace :
26+ group = workspace .get_entity (group )[0 ]
27+ if not group :
28+ group = geoh5py .groups .ContainerGroup .create (
29+ workspace , name = group , allow_delete = True
30+ )
1431 if surface .name in workspace .list_entities_name .values ():
1532 existing_surf = workspace .get_entity (surface .name )
1633 existing_surf [0 ].allow_delete = True
@@ -32,6 +49,7 @@ def add_surface_to_geoh5(filename, surface, overwrite=True, groupname="Loop"):
3249
3350def add_points_to_geoh5 (filename , point , overwrite = True , groupname = "Loop" ):
3451 with geoh5py .workspace .Workspace (filename ) as workspace :
52+
3553 group = workspace .get_entity (groupname )[0 ]
3654 if not group :
3755 group = geoh5py .groups .ContainerGroup .create (
@@ -52,15 +70,73 @@ def add_points_to_geoh5(filename, point, overwrite=True, groupname="Loop"):
5270 data ['vz' ] = {'association' : "VERTEX" , "values" : point .vectors [:, 2 ]}
5371
5472 if isinstance (point , ValuePoints ):
55- data ['val ' ] = {'association' : "VERTEX" , "values" : point .values }
73+ data ['values ' ] = {'association' : "VERTEX" , "values" : point .values }
5674 point = geoh5py .objects .Points .create (
5775 workspace ,
5876 name = point .name ,
5977 vertices = point .locations ,
6078 parent = group ,
6179 )
6280 point .add_data (data )
81+
82+ def overwrite_object (workspace , name , overwrite ):
83+ if name in workspace .list_entities_name .values ():
84+ existing_entity = workspace .get_entity (name )
85+ existing_entity [0 ].allow_delete = True
86+ if overwrite :
87+ workspace .remove_entity (existing_entity [0 ])
6388
89+ def add_points_from_df (filename , df , name = 'pointset' , overwrite = True , columns = None , groupname = "Loop" , x_col = 'X' , y_col = 'Y' , z_col = 'Z' ):
90+ """
91+ Add points to a geoh5 file from a pandas DataFrame. The DataFrame must have columns 'name', 'X', 'Y', 'Z' for the point locations.
92+ Additional columns can be added as data associated with the points.
93+ Parameters
94+ ----------
95+ filename: str
96+ Path to the geoh5 file.
97+ df: pandas.DataFrame
98+ DataFrame containing point data. Must have columns 'name', 'X', 'Y', 'Z'. Additional columns will be added as data.
99+ overwrite: bool, optional
100+ Whether to overwrite existing points with the same name. Default is True.
101+ columns: list of str, optional
102+ List of columns in the DataFrame to add as data. If None, all columns except 'name', 'X', 'Y', 'Z' will be added. Default is None.
103+
104+ """
105+ if columns is None :
106+ columns = df .columns .tolist ()
107+ if x_col not in columns or y_col not in columns or z_col not in columns :
108+ raise ValueError ("DataFrame must contain 'name', 'X', 'Y', 'Z' columns. " \
109+ "Specify the column names using x_col, y_col, z_col parameters if they are different." )
110+ with geoh5py .workspace .Workspace (filename ) as workspace :
111+ if groupname :
112+ group = workspace .get_entity (groupname )
113+ group = group [0 ] if group else None
114+ if not group :
115+ group = geoh5py .groups .ContainerGroup .create (
116+ workspace , name = groupname , allow_delete = True ,
117+ )
118+
119+ location = np .array (df [[x_col , y_col , z_col ]].values ) # shape (n,3)
120+
121+ overwrite_object (workspace , name , overwrite )
122+
123+
124+ pts = geoh5py .objects .Points .create (
125+ workspace ,
126+ name = name ,
127+ vertices = location ,
128+ parent = group ,
129+ )
130+ data = {}
131+ for col in columns :
132+ if col in ['name' , x_col , y_col , z_col ]:
133+ continue
134+ data [col ] = {"association" : "VERTEX" , "values" : np .array (df [col ]).flatten ()}
135+
136+
137+ if data :
138+ pts .add_data (data )
139+
64140
65141def add_structured_grid_to_geoh5 (filename , structured_grid , overwrite = True , groupname = "Loop" ):
66142 with geoh5py .workspace .Workspace (filename ) as workspace :
0 commit comments