Skip to content

Commit

Permalink
Use FAR instead of DEP to determine gear_onboard
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentAntoine committed Apr 4, 2024
1 parent c9a1ca8 commit 285dc41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
24 changes: 20 additions & 4 deletions datascience/src/pipeline/queries/monitorfish/current_catches.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ ordered_deps AS (
ircs,
external_identification,
trip_number,
value->'gearOnboard' AS gear_onboard,
(value->>'departureDatetimeUtc')::timestamptz AS departure_datetime_utc,
ROW_NUMBER() OVER(PARTITION BY cfr ORDER BY (value->>'departureDatetimeUtc')::timestamptz DESC) as rk
FROM public.logbook_reports
Expand All @@ -30,8 +29,7 @@ last_deps AS (
ircs,
external_identification,
departure_datetime_utc,
trip_number,
gear_onboard
trip_number
FROM ordered_deps
WHERE rk=1
),
Expand All @@ -53,6 +51,8 @@ catches AS (
r.cfr,
r.report_id,
(jsonb_array_elements(r.value -> 'hauls'))->>'gear' as gear,
((jsonb_array_elements(r.value -> 'hauls'))->>'mesh')::DOUBLE PRECISION as mesh,
(jsonb_array_elements(r.value -> 'hauls'))->>'dimensions' as dimensions,
jsonb_array_elements((jsonb_array_elements(r.value -> 'hauls'))->'catches')->>'species' as species,
(jsonb_array_elements((jsonb_array_elements(r.value -> 'hauls'))->'catches')->>'weight')::DOUBLE PRECISION as weight,
jsonb_array_elements((jsonb_array_elements(r.value -> 'hauls'))->'catches')->>'faoZone' as fao_area
Expand All @@ -76,6 +76,20 @@ summed_catches AS (
SUM(weight) as weight
FROM catches
GROUP BY cfr, species, gear, fao_area
),

gear_onboard AS (
SELECT
cfr,
jsonb_agg(DISTINCT
jsonb_build_object(
'gear', gear,
'mesh', mesh,
'dimensions', dimensions
)
) AS gear_onboard
FROM catches
GROUP BY cfr
)

SELECT
Expand All @@ -85,7 +99,7 @@ SELECT
last_logbook_reports.last_logbook_message_datetime_utc,
departure_datetime_utc,
trip_number,
gear_onboard,
go.gear_onboard,
species,
gear,
fao_area,
Expand All @@ -95,3 +109,5 @@ FULL OUTER JOIN last_deps
ON last_logbook_reports.cfr = last_deps.cfr
LEFT JOIN summed_catches
ON last_logbook_reports.cfr = summed_catches.cfr
LEFT JOIN gear_onboard go
ON last_logbook_reports.cfr = go.cfr
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def current_segments() -> pd.DataFrame:
],
"trip_number": ["20210001", "20210002"],
"gear_onboard": [
[{"gear": "OTM", "mesh": 80.0}],
[{"gear": "OTB", "mesh": 80.0}],
[{"gear": "OTM", "mesh": 80.0, "dimensions": None}],
[{"gear": "OTB", "mesh": 80.0, "dimensions": None}],
],
"species_onboard": [
[
Expand Down

0 comments on commit 285dc41

Please sign in to comment.