Skip to content

Commit 6db2023

Browse files
Adding the function to add dataframe as pts with nesting in geohy5.py
1 parent 367019f commit 6db2023

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

LoopStructural/export/geoh5.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,47 @@ def add_points_to_geoh5(filename, point, overwrite=True, groupname="Loop"):
6161
)
6262
point.add_data(data)
6363

64+
def add_points_from_df(filename, df, overwrite=True, child = None, parent = None,
65+
normal_cols=['nx','ny','nz']):
66+
with geoh5py.workspace.Workspace(filename) as workspace:
67+
entities = workspace.get_entity(child)
68+
child_name = child
69+
child = entities[0] if entities else None
70+
if not child:
71+
child = geoh5py.groups.ContainerGroup.create(
72+
workspace, name=child_name, allow_delete=True,
73+
)
74+
if parent:
75+
parent.add_children(child)
76+
77+
for _, row in df.iterrows():
78+
name = row['name']
79+
loc = np.array([[row['X'], row['Y'], row['Z']]]) # shape (1,3)
80+
81+
# remove existing entity if present and overwrite requested
82+
if name in workspace.list_entities_name.values():
83+
existing = workspace.get_entity(name)
84+
if existing:
85+
existing[0].allow_delete = True
86+
if overwrite:
87+
workspace.remove_entity(existing[0])
88+
89+
pts = geoh5py.objects.Points.create(
90+
workspace,
91+
name=name,
92+
vertices=loc,
93+
parent=child,
94+
)
95+
96+
# build data dict from normal_cols (and any other columns you want)
97+
data = {}
98+
for col in normal_cols:
99+
if col in row and not pd.isna(row[col]):
100+
# association must be "VERTEX" and values length must match vertices (1)
101+
data[col] = {"association": "VERTEX", "values": np.array([row[col]])}
102+
103+
if data:
104+
pts.add_data(data)
64105

65106
def add_structured_grid_to_geoh5(filename, structured_grid, overwrite=True, groupname="Loop"):
66107
with geoh5py.workspace.Workspace(filename) as workspace:

0 commit comments

Comments
 (0)