diff --git a/bincio/extract/metrics.py b/bincio/extract/metrics.py index 08d0b35..67ef765 100644 --- a/bincio/extract/metrics.py +++ b/bincio/extract/metrics.py @@ -377,16 +377,21 @@ def _elevation( # Some devices (e.g. Apple Watch) record exactly 0.0 for the initial samples # while waiting for barometric/GPS lock, then jump to the real altitude. - # Detect this by checking for a leading near-zero run followed by a large - # jump: skip those zeros and seed the accumulator from the first real value. - # Safety: if the activity genuinely stays near sea level, no value in the - # series will exceed `threshold`, so `start` stays 0 — unchanged behaviour. + # Only activate when there are at least 2 consecutive near-zero leading + # values — a single 0.0 is a legitimate sea-level starting point. start = 0 if abs(elevations[0]) < 0.5: - for i, e in enumerate(elevations): - if abs(e) > threshold: - start = i + n_leading = 0 + for e in elevations: + if abs(e) < 0.5: + n_leading += 1 + else: break + if n_leading > 1: + for i, e in enumerate(elevations): + if abs(e) > threshold: + start = i + break gain = loss = 0.0 committed = elevations[start] diff --git a/tests/test_writer.py b/tests/test_writer.py index 410788e..84fc0f0 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -67,7 +67,7 @@ def _dummy_metrics(**overrides): elevation_gain_m=100.0, elevation_loss_m=95.0, avg_speed_kmh=10.0, max_speed_kmh=20.0, avg_hr_bpm=None, max_hr_bpm=None, - avg_cadence_rpm=None, avg_power_w=None, max_power_w=None, + avg_cadence_rpm=None, avg_power_w=None, np_power_w=None, max_power_w=None, bbox=None, start_latlng=None, end_latlng=None, mmp=None, best_efforts=None, best_climb_m=None, ) @@ -208,6 +208,7 @@ def test_build_summary_required_fields(): max_hr_bpm=None, avg_cadence_rpm=None, avg_power_w=None, + np_power_w=None, max_power_w=None, bbox=None, start_latlng=None,