2022-10: THIS IS STILL A TRIAL AND ERROR - NOT (YET) PART OF THE PRODUCTION FLOW

library(data.table)
library(sf)
library(mapdeck)
source("~/r/Pakkar2/ramb/TOPSECRET.R")
library(tidyverse)
options(ggplot2.continuous.colour="viridis")
options(ggplot2.continuous.fill="viridis")
library(lubridate)
library(ramb)
library(omar)
# source("rb_R-functions/functions.R")
con <- connect_mar()
YEARS <- 2021:2009
VIDs <- 1976
D1 <- paste0(min(YEARS), "-01-01")
D2 <- paste0(max(YEARS), "-12-31")
vessels <-
  omar::vid_registry(con) %>%
  # no dummy vessels
  filter(!vid %in% c(0, 1, 3:5),
         !is.na(vessel)) |> 
  collect(n = Inf)
# replace with the "proper" wrapper function
gears <-
  tbl_mar(con, "ops$einarhj.gid_orri_plus") %>% 
  select(gid = veidarfaeri, gclass = gid2) %>% 
  collect(n = Inf) %>% 
  mutate(#description = ifelse(gid == 92, "G.halibut net", description),
    gid = as.integer(gid),
    gclass = as.integer(gclass))

Logbooks

PROCESSING STEPS:

  1. Get and merge logbook and landings data
  • gear correction part of the process
  1. Lump some gears

  2. Cap effort and end of action

  3. Mesh size “corrections”

  4. Set gear width proxy

  5. Set gear class

  6. Match vid with mobileid in stk

  7. Add vessel information - only needed for ICES datacall - pending

  8. Add metier - only needed for ICES datacall - pending

  9. ICES rectangles - only needed for ICES datacall - pending

  10. Anonymize vid - only needed for ICES datacall - pending

lb <- 
  rb_logbook(con, YEARS = YEARS) %>% 
  # lump gear
  mutate(gid = case_when(gid %in% c(10L, 12L) ~ 4L,       # purse seines
                         gid %in% c(18L, 39L) ~ 18L,      # traps
                         TRUE ~ gid)) %>% 
  # make the rest a negative number
  mutate(gid = ifelse(is.na(gid), -666L, gid)) %>% 
  # "skip" these also in downstream processing
  mutate(gid = ifelse(gid %in% c(4, 12, 42), -666, gid)) %>% 
  # lump dredges into one single gear
  mutate(gid = ifelse(gid %in% c(15, 37, 38, 40), 15, gid)) |> 
  rb_cap_effort() %>% 
  rb_std_meshsize() %>% 
  rb_gearwidth_proxy() %>% 
  left_join(gears %>% select(gid, gclass),
            by = "gid") |> 
  # get the mobileid
  #  Here we get into the problem that we have multiple mid for a vid
  #  ergo: repeated records in the logbooks
  left_join(tbl_mar(con, "ops$einarhj.MID_VID_ICELANDIC_20220418") %>% 
              select(mid, vid) %>% 
              collect(n = Inf),
            by = "vid")
lb |> 
  count(vid) |> 
  arrange(n) |> 
  view()

VIDS <- lb |> group_by(vid) |> mutate(n = n()) |> ungroup() |> 
  filter(n >= 5, vid %in% vessels$vid) |> pull(vid) |> unique() |> sort()

ABOVE NEEDS TO BE FIXED, here is a count of mobileid per vessel:

tbl_mar(con, "ops$einarhj.MID_VID_ICELANDIC_20220418") %>% 
  select(mid, vid) %>% 
  collect(n = Inf) %>% 
  count(vid) %>% 
  count(n)

AIS data

PROCESSING STEPS:

X. whacky track X. Somewhere along the chain - do not extrapolate between trips

hb <- gisland::gl_read_is_harbours()
rb_whacky_speed <-  function(lon, lat, time, criteria = rb_kn2ms(25)) {
  x1 <- traipse::track_speed(lon, lat, time)
  x2 <- dplyr::lead(x1)
  dplyr::if_else(x1 > criteria | is.na(x2), TRUE, FALSE, TRUE)
}
# note skip 1:3 here - in a full workflow go from one
for(v in 4:200) { #length(VIDS)) {
  print(paste(v, VIDS[v]))
  ramb:::rb_read_trails(con, VID = VIDS[v], YEARS = YEARS) |> 
    arrange(vid, time) |> 
    distinct(vid, time, .keep_all = TRUE) |> 
    # create trips
    st_as_sf(coords = c("lon", "lat"),
             crs = 4326,
             remove = FALSE) |> 
    st_join(hb %>% select(hid)) %>% 
    st_drop_geometry() %>% 
    group_by(vid) %>%
    # cruise id (aka tripid), negative values: in harbour
    mutate(.cid = ramb::rb_trip(!is.na(hid)),) %>%
    ungroup() |> 
    group_by(vid, .cid) |> 
    mutate(trip.pings = n(),
           whacky = ifelse(.cid > 0 & trip.pings > 1,
                           rb_whacky_speed(lon, lat, time),
                           FALSE)) |> 
    filter(!whacky) |> 
    # redo: in case second point is also a problem
    mutate(whacky = ifelse(.cid > 0 & trip.pings > 1,
                           rb_whacky_speed(lon, lat, time),
                           FALSE)) |> 
    filter(!whacky) |> 
    select(-whacky) |> 
    mutate(speed = ifelse(is.na(speed) & .cid > 0 & trip.pings > 1,
                          traipse::track_speed(lon, lat, time),
                          speed)) |> 
    ungroup() |> 
    write_rds(paste0("~/r/Pakkar2/ramb/trails/trails-", 
                     str_pad(VIDS[v],width = 4, side = "left", pad = "0"),
                     ".rds"))
}
ais |> 
  filter(!whacky,
         .cid > 0,
         trip.pings > 10) |> 
  mutate(speed = ifelse(speed > 12, 12, speed)) |> 
  ramb:::rb_mapdeck()