55
66from LoopStructural .datatypes import ValuePoints , VectorPoints
77
8-
9- def add_surface_to_geoh5 (filename , surface , overwrite = True , groupname = "Loop" ):
8+ def add_group_to_geoh5 (filename , groupname = "Loop" , parent = None , overwrite = True ):
109 with geoh5py .workspace .Workspace (filename ) as workspace :
10+
1111 group = workspace .get_entity (groupname )[0 ]
12+ if group and overwrite :
13+ group .allow_delete = True
14+ workspace .remove_entity (group )
1215 if not group :
1316 group = geoh5py .groups .ContainerGroup .create (
1417 workspace , name = groupname , allow_delete = True
1518 )
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+ )
1631 if surface .name in workspace .list_entities_name .values ():
1732 existing_surf = workspace .get_entity (surface .name )
1833 existing_surf [0 ].allow_delete = True
@@ -34,6 +49,7 @@ def add_surface_to_geoh5(filename, surface, overwrite=True, groupname="Loop"):
3449
3550def add_points_to_geoh5 (filename , point , overwrite = True , groupname = "Loop" ):
3651 with geoh5py .workspace .Workspace (filename ) as workspace :
52+
3753 group = workspace .get_entity (groupname )[0 ]
3854 if not group :
3955 group = geoh5py .groups .ContainerGroup .create (
@@ -54,56 +70,73 @@ def add_points_to_geoh5(filename, point, overwrite=True, groupname="Loop"):
5470 data ['vz' ] = {'association' : "VERTEX" , "values" : point .vectors [:, 2 ]}
5571
5672 if isinstance (point , ValuePoints ):
57- data ['val ' ] = {'association' : "VERTEX" , "values" : point .values }
73+ data ['values ' ] = {'association' : "VERTEX" , "values" : point .values }
5874 point = geoh5py .objects .Points .create (
5975 workspace ,
6076 name = point .name ,
6177 vertices = point .locations ,
6278 parent = group ,
6379 )
6480 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 ])
6588
66- def add_points_from_df (filename , df , overwrite = True , child = None , parent = None ,
67- normal_cols = ['nx' ,'ny' ,'nz' ]):
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." )
68110 with geoh5py .workspace .Workspace (filename ) as workspace :
69- entities = workspace .get_entity (child )
70- child_name = child
71- child = entities [0 ] if entities else None
72- if not child :
73- child = geoh5py .groups .ContainerGroup .create (
74- workspace , name = child_name , allow_delete = True ,
75- )
76- if parent :
77- parent .add_children (child )
78-
79- for _ , row in df .iterrows ():
80- name = row ['name' ]
81- loc = np .array ([[row ['X' ], row ['Y' ], row ['Z' ]]]) # shape (1,3)
82-
83- # remove existing entity if present and overwrite requested
84- if name in workspace .list_entities_name .values ():
85- existing = workspace .get_entity (name )
86- if existing :
87- existing [0 ].allow_delete = True
88- if overwrite :
89- workspace .remove_entity (existing [0 ])
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+
90123
91- pts = geoh5py .objects .Points .create (
92- workspace ,
93- name = name ,
94- vertices = loc ,
95- parent = child ,
96- )
97-
98- # build data dict from normal_cols (and any other columns you want)
99- data = {}
100- for col in normal_cols :
101- if col in row and not pd .isna (row [col ]):
102- # association must be "VERTEX" and values length must match vertices (1)
103- data [col ] = {"association" : "VERTEX" , "values" : np .array ([row [col ]])}
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+
104136
105- if data :
106- pts .add_data (data )
137+ if data :
138+ pts .add_data (data )
139+
107140
108141def add_structured_grid_to_geoh5 (filename , structured_grid , overwrite = True , groupname = "Loop" ):
109142 with geoh5py .workspace .Workspace (filename ) as workspace :
0 commit comments