|
#------------------------------------------------------------------------------ |
|
# helper functions |
|
|
|
#' @title my custom `cut` function |
|
cut_bis <- function(x, breaks, labels) as.character(cut(x, |
|
breaks = breaks, labels = labels, |
|
include.lowest = TRUE, right = FALSE |
|
)) |
|
|
|
#' @title extract the minutes |
|
#' @param x numeric vector |
|
retrieve_mins <- function(x) { |
|
x <- abs(x) |
|
# convert to degree-minute |
|
degrees <- as.integer(x) |
|
mins <- as.integer(round(60 * (x - degrees))) |
|
return(mins) |
|
} |
|
|
|
#------------------------------------------------------------------------------ |
|
# GFCM statistical grid |
|
|
|
gfcm_lims <- c("xmin" = -6, "ymin" = 30, "xmax" = 42, "ymax" = 47.5) |
|
|
|
# rectangle labels for latitudes |
|
gfcm_lat_breaks <- seq(gfcm_lims["ymin"], gfcm_lims["ymax"], .5) |
|
gfcm_lat_labels <- sprintf("%02d", 0:34) # 1:(length(gfcm_lat_breaks) - 1) - 1 |
|
|
|
# rectangle labels for longitudes |
|
gfcm_lon_breaks <- seq(gfcm_lims["xmin"], gfcm_lims["xmax"], .5) |
|
gfcm_col <- LETTERS[1:10] # from “A” to “J” |
|
gfcm_lon_lab <- paste0(rep(gfcm_col, each = 10), rep(0:9, times = length(gfcm_col))) |
|
gfcm_lon_labels <- gfcm_lon_lab[!gfcm_lon_lab %in% paste0("J", 6:9)] # they are ommitted |
|
|
|
#' @title identify GFCM Statistical grid |
|
identify_gfcm_rect <- function(LONGITUDES, LATITUDES) { |
|
|
|
lat <- cut_bis(LATITUDES, breaks = gfcm_lat_breaks, labels = gfcm_lat_labels) |
|
lon <- cut_bis(LONGITUDES, breaks = gfcm_lon_breaks, labels = gfcm_lon_labels) |
|
res <- paste0(lat, lon) |
|
|
|
bad_coords <- |
|
LATITUDES < gfcm_lims["ymin"] | gfcm_lims["ymax"] < LATITUDES | |
|
LONGITUDES < gfcm_lims["xmin"] | gfcm_lims["xmax"] < LONGITUDES |
|
if (any(bad_coords)) { |
|
warning("NAs produced because GPS coordinates out of GFCM defined ranges!") |
|
res[bad_coords] <- NA_character_ |
|
} |
|
return(res) |
|
} |
|
|
|
#------------------------------------------------------------------------------ |
|
# ICES statistical rectangles |
|
|
|
ices_lims <- c("xmin" = -44, "ymin" = 36, "xmax" = 68.5, "ymax" = 85.5) |
|
|
|
# rectangle labels for latitudes |
|
ices_lat_breaks <- seq(ices_lims["ymin"], ices_lims["ymax"], .5) |
|
ices_lat_labels <- sprintf("%02d", 1:99) # 1:(length(ices_lat_breaks) - 1) - 1 |
|
|
|
# rectangle labels for longitudes |
|
ices_lon_breaks <- ices_lims["xmin"]:(ices_lims["xmax"] + .5) # because it’s 1° longitude |
|
ices_col <- LETTERS[c(1:8, 10:13)] # from “A” to “M” without letter “I” |
|
ices_lon_lab <- paste0(rep(ices_col, each = 10), rep(0:9, times = length(ices_col))) |
|
ices_lon_labels <- ices_lon_lab[!ices_lon_lab %in% c("M9", paste0("A", 4:9))] # they are ommitted |
|
|
|
#' @title identify ICES statistical subrectangles from GPS coordinates |
|
#' @param subrect boolean; if TRUE return subrectangle |
|
identify_ices_rect <- function(LONGITUDES, LATITUDES, subrect = FALSE) { |
|
|
|
# rectangle identification |
|
lat <- cut_bis(LATITUDES, breaks = ices_lat_breaks, labels = ices_lat_labels) |
|
lon <- cut_bis(LONGITUDES, breaks = ices_lon_breaks, labels = ices_lon_labels) |
|
res <- paste0(lat, lon) |
|
|
|
# subrectangle identification ------------------------- |
|
if (isTRUE(subrect)) { |
|
lat_mins <- retrieve_mins(LATITUDES) |
|
lon_mins <- retrieve_mins(LONGITUDES) |
|
|
|
# segmenting latitudes every 10’, labels is chosen to easily calculate subrect |
|
lat_seg <- as.integer(cut_bis(lat_mins, breaks = seq(0, 60, 10), labels = rep(1:3, times = 2))) |
|
# segmenting longitudes every 20’, labels is chosen to easily calculate subrect |
|
lon_seg <- as.integer(cut_bis(lon_mins, breaks = seq(0, 60, 20), labels = 0:2)) |
|
# change hemisphere (western to eastern) |
|
lon_seg_bis <- ifelse(LONGITUDES < 0, 2L - lon_seg, lon_seg) |
|
|
|
sub_rect <- lat_seg + 3L * lon_seg_bis |
|
res <- paste0(res, sub_rect) |
|
} |
|
|
|
# finish ---------------------------------------------- |
|
bad_coords <- |
|
LATITUDES < ices_lims["ymin"] | ices_lims["ymax"] < LATITUDES | |
|
LONGITUDES < ices_lims["xmin"] | ices_lims["xmax"] < LONGITUDES |
|
if (any(bad_coords)) { |
|
warning("NAs produced because GPS coordinates out of ICES defined ranges!") |
|
res[bad_coords] <- NA_character_ |
|
} |
|
return(res) |
|
} |