Bug: lake outlet ordering picks wrong tributary
.frs_connected_spawning line 1385:
ORDER BY s2.waterbody_key, s2.downstream_route_measure ASC
Should be:
ORDER BY s2.waterbody_key, s2.wscode_ltree, s2.localcode_ltree, s2.downstream_route_measure
DRM ordering picks an arbitrary segment on any BLK with the smallest measure. wscode ordering picks the actual network-topological outlet of the lake.
Also line 1392: PARTITION BY lo.blue_line_key should be PARTITION BY lo.waterbody_key so the cumulative distance calculation stays within one lake's downstream trace.
Example
waterbody_key 329064462 (BULK) spans 10 BLKs. Current code picks BLK 360504780 DRM 0 (wrong tributary). Correct ordering picks BLK 360846413 DRM 9718 (actual lake outlet). Downstream trace from the wrong outlet misses 7+ km of spawning habitat.
Proven
Corrected query on BULK SK spawning: 18.88 km → 24.41 km (bcfishpass 24.38 km). From -22.6% to +0.1%.
-- Corrected outlet selection
SELECT DISTINCT ON (s2.waterbody_key)
s2.waterbody_key, s2.blue_line_key, s2.downstream_route_measure
FROM fresh.streams s2
INNER JOIN fresh.streams_habitat hr ON s2.id_segment = hr.id_segment
WHERE hr.species_code = 'SK' AND hr.rearing IS TRUE
ORDER BY s2.waterbody_key, s2.wscode_ltree, s2.localcode_ltree, s2.downstream_route_measure
BULK regression also noted
BABL SK spawning -13.6% likely the same bug. ADMS +2.6% works because the rearing lake has only one outlet BLK.
Versions
- fresh: 0.13.4
- bcfishpass: v0.5.0
- link: 0.1.0
Bug: lake outlet ordering picks wrong tributary
.frs_connected_spawningline 1385:Should be:
DRM ordering picks an arbitrary segment on any BLK with the smallest measure. wscode ordering picks the actual network-topological outlet of the lake.
Also line 1392:
PARTITION BY lo.blue_line_keyshould bePARTITION BY lo.waterbody_keyso the cumulative distance calculation stays within one lake's downstream trace.Example
waterbody_key 329064462 (BULK) spans 10 BLKs. Current code picks BLK 360504780 DRM 0 (wrong tributary). Correct ordering picks BLK 360846413 DRM 9718 (actual lake outlet). Downstream trace from the wrong outlet misses 7+ km of spawning habitat.
Proven
Corrected query on BULK SK spawning: 18.88 km → 24.41 km (bcfishpass 24.38 km). From -22.6% to +0.1%.
BULK regression also noted
BABL SK spawning -13.6% likely the same bug. ADMS +2.6% works because the rearing lake has only one outlet BLK.
Versions