Skip to content

Commit d53fa46

Browse files
committed
fix: update thicknesses in stratigraphic colum after thickness calculator runs
1 parent a535f4f commit d53fa46

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

loopstructural/gui/map2loop_tools/thickness_calculator_widget.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Widget for thickness calculator."""
22

33
import os
4+
import pandas as pd
45

56
from PyQt5.QtWidgets import QMessageBox, QWidget
67
from qgis.core import QgsMapLayerProxyModel
@@ -325,8 +326,61 @@ def _run_calculator(self):
325326
thicknesses = result.get('thicknesses')
326327
lines = result.get('lines')
327328
location_tracking = result.get('location_tracking')
328-
if thicknesses is not None:
329-
addGeoDataFrameToproject(thicknesses, "Thickness Results")
329+
# If thicknesses were calculated, update the stratigraphic column units
330+
try:
331+
if thicknesses is not None and getattr(self, 'data_manager', None):
332+
# Prefer median thickness if available, fallback to mean
333+
thickness_col = (
334+
'ThicknessMedian'
335+
if 'ThicknessMedian' in getattr(thicknesses, 'columns', [])
336+
else (
337+
'ThicknessMean'
338+
if 'ThicknessMean' in getattr(thicknesses, 'columns', [])
339+
else None
340+
)
341+
)
342+
if thickness_col is not None:
343+
for _, row in thicknesses.iterrows():
344+
unit_name = row.get('name') or row.get('UNITNAME')
345+
if not unit_name:
346+
continue
347+
try:
348+
value = row.get(thickness_col)
349+
except Exception:
350+
value = None
351+
# Skip invalid values (e.g. -1 means not calculated)
352+
try:
353+
is_invalid = pd.isna(value) or float(value) == -1
354+
except Exception:
355+
is_invalid = value is None
356+
if is_invalid:
357+
continue
358+
# Find unit in stratigraphic column and update thickness
359+
try:
360+
strat_col = self.data_manager.get_stratigraphic_column()
361+
unit = strat_col.get_unit_by_name(unit_name)
362+
if unit is not None:
363+
unit.thickness = float(value)
364+
except Exception as err:
365+
# Log but don't fail the widget
366+
try:
367+
if getattr(self, '_debug', None):
368+
self._debug.plugin.log(
369+
message=f"Failed to update stratigraphic unit thickness for {unit_name}: {err}",
370+
log_level=2,
371+
)
372+
except Exception:
373+
pass
374+
# Notify any stratigraphic column callbacks
375+
try:
376+
if getattr(self.data_manager, 'stratigraphic_column_callback', None):
377+
self.data_manager.stratigraphic_column_callback()
378+
except Exception:
379+
pass
380+
except Exception:
381+
pass
382+
# if thicknesses is not None:
383+
# addGeoDataFrameToproject(thicknesses, "Thickness Results")
330384
if lines is not None:
331385
addGeoDataFrameToproject(lines, "Thickness Lines")
332386
if location_tracking is not None:
@@ -388,5 +442,6 @@ def get_parameters(self):
388442
if self.data_manager and hasattr(self.data_manager, 'get_stratigraphic_unit_names')
389443
else None
390444
),
445+
'debug_manager': self._debug,
391446
}
392447
return params

0 commit comments

Comments
 (0)