library(dplyr)
library(rlang)
library(purrr)
library(here)
library(fs)
library(glue)
library(sf)
library(ggplot2)
library(plotly)
library(scales)
library(mapview)
library(DT)
library(rerddap)
library(lubridate)
library(readr)
library(stringr)
here = here::here
q_calcofi <- "CalCOFI Larvae Counts, Scientific Names"
dir_cache <- here("cache")
dir_img <- "/Volumes/GoogleDrive/My Drive/projects/nms-web/cinms/Images"
lrvcntSBtoSC_csv <- glue("{dir_cache}/erdCalCOFIlrvcntSBtoSC.csv")
stns_cinms_csv <- here("data/stns_cinms.csv")
plys_cinms_geo <- here("data/plys_cinms.geojson")
yr_min <- 1978
spp_csv <- here("data/spp.csv")
spp_grps_csv <- here("data/spp_grps.csv")
redo_grp_ply_csv <- F
# functions ----
convert_fig <- function(fig_beg, fig_end, redo=F){
stopifnot(file.exists(fig_beg), dir.exists(dirname(fig_end)))
#browser()
if (!file.exists(fig_end) | redo){
cmd <- glue("magick convert '{fig_beg}' -resize 800 -alpha on '{fig_end}'")
system(cmd)
paths_01 <- glue("{path_ext_remove(fig_end)}-{c(0,1)}.png")
if (all(file.exists(paths_01))){
file_copy(paths_01[2], fig_end, overwrite = T)
file_delete(paths_01)
}
}
}
get_nms_ply <- function(nms){
# eg get_nms_ply("cinms")
# get polygon for National Marine Sanctuary
nms_shp <- here(glue("data/shp/{nms}_py.shp"))
if (!file.exists(nms_shp)){
# download if needed
# https://sanctuaries.noaa.gov/library/imast_gis.html
nms_url <- glue("https://sanctuaries.noaa.gov/library/imast/{nms}_py2.zip")
nms_zip <- here(glue("data/{nms}.zip"))
shp_dir <- here("data/shp")
download.file(nms_url, nms_zip)
unzip(nms_zip, exdir = shp_dir)
file_delete(nms_zip)
}
# read and convert to standard geographic projection
read_sf(nms_shp) %>%
st_transform(4326)
}
figs <- list(
folder = "App F: LR (AppF.Q#.#)/AppendixF_Q12",
figure = c(
"App.F.12.17.Map_CalCOFI.tiff"))
figs_beg <- path(dir_img, figs$folder, figs$figure)
figs_end <- here(glue("docs/figures/{path_ext_remove(basename(figs_beg))}.png"))
walk2(figs_beg, figs_end, convert_fig)
knitr::include_graphics(figs_end)
Figure App.F.12.17. Location of spring season net samples by the California Cooperative Oceanic Fisheries Investigations (CalCOFI) used in analyses of abundance and trends in pelagic resources in two regions: Channel Islands National Marine Sanctuary region (red) includes CalCOFI stations inside and adjacent to CINMS; and Southern California Shelf (yellow) includes all CalCOFI stations over the shelf. Figure: A. Thompson/NOAA
figs <- list(
folder = "App F: LR (AppF.Q#.#)/AppendixF_Q12",
figure = c(
"App.F.12.18_CalCOFI_SPV.tiff"))
figs_beg <- path(dir_img, figs$folder, figs$figure)
figs_end <- here(glue("docs/figures/{path_ext_remove(basename(figs_beg))}.png"))
walk2(figs_beg, figs_end, convert_fig)
knitr::include_graphics(figs_end)
Figure App.F.12.18. Mean volume of small plankton collected in net samples during CalCOFI cruises at only sites in the Channel Islands National Marine Sanctuary region (top) and all sites over the Southern California Shelf (bottom) from 1978 to 2015 (sampling locations shown in Figure App.F.12.17). Horizontal lines show the mean (dashed line) ± 1.0 standard deviation (solid lines) over the full time series. Symbol at upper right indicates whether data over the last five years (green shaded areas) had a positive trend (), a negative trend (), or no trend (↔). Symbol at lower right indicates whether the mean over the past five years was greater than (+), less than (–), or within 1 standard deviation (●) of the mean of the full time series.. Figure App.C.4.15. Variation over time in percentage of stations from winter CalCOFI cruises with plastic micro-debris. Micro-debris was present in more than 50 percent of samples at each time period. Figure: Gilfillan et al. 2009
figs <- list(
folder = "App F: LR (AppF.Q#.#)/AppendixF_Q12",
figure = c(
"App.F.12.19b.CalCOFI_Med_ichthy.tiff",
"App.F.12.19a.CalCOFI_High_ichthy.tiff"))
figs_beg <- path(dir_img, figs$folder, figs$figure)
figs_end <- here(glue("docs/figures/{path_ext_remove(basename(figs_beg))}.png"))
walk2(figs_beg, figs_end, convert_fig)
knitr::include_graphics(figs_end)
Figure App.F.12.19. Relative abundance of key forage groups collected in net samples during spring CalCOFI cruises at sites in the Channel Islands National Marine Sanctuary (CINMS) region from 1978 to 2015. Forage is grouped by high (left panel) and medium (right panel) energy density. High energy taxa are Pacific sardines, northern anchovies, and and Myctophids. Medium- energy taxa are Pacific hake, shortbelly rockfish, and sanddabs. Although sardine were completely absent in net samples from 2011 to 2014 in the CINMS region, comparison with samples collected in the larger Southern California Shelf region reveal that sardine were at very low abundance but not completely absent from the region (sampling locations shown in Figure App.F12.17). Symbols on graph explained in the caption of Figure App.F12.18. Data source: CalCOFI; Figure: A. Thompson/NOAA
figs <- list(
folder = "App F: LR (AppF.Q#.#)/AppendixF_Q12",
figure = c(
"App.F.12.20.CalCOFI_squid.tiff"))
figs_beg <- path(dir_img, figs$folder, figs$figure)
figs_end <- here(glue("docs/figures/{path_ext_remove(basename(figs_beg))}.png"))
walk2(figs_beg, figs_end, convert_fig)
knitr::include_graphics(figs_end)
Figure App.F.12.20. Mean abundance of market squid (Doryteuthis opalescens) collected in net samples during CalCOFI cruises at only sites in the Channel Islands National Marine Sanctuary region (top) and all sites over the Southern California Shelf (bottom) from 1997 to 2015 (sampling locations shown in Figure App.F12.17). Symbols on graph explained in the caption of Figure App.F12.18. Data source: CalCOFI; Figure: A. Thompson/NOAA
figs <- list(
folder = "App F: LR (AppF.Q#.#)/AppendixF_Q15",
figure = c(
"App.F.15.5.CalCOFI_water_associated.tiff"))
figs_beg <- path(dir_img, figs$folder, figs$figure)
figs_end <- here(glue("docs/figures/{path_ext_remove(basename(figs_beg))}.png"))
walk2(figs_beg, figs_end, convert_fig)
knitr::include_graphics(figs_end)
Figure App.F.15.5. Relative abundance of cool-water (top panels) and warm-water (bottom panels) associated ichthyoplankton (fish larvae) groups collected in net samples during spring CalCOFI cruises at sites in the Channel Islands National Marine Sanctuary (CINMS) region (left panels) and over the Southern California Shelf (right panels) from 1978 to 2015. Sampling sites are shown on Figure App.F.12.17. Symbols on the graph are explained in the caption for Figure App.F.12.18. Data source: CalCOFI; Figure: A. Thompson/NOAA
figs <- list(
folder = "App F: LR (AppF.Q#.#)/AppendixF_Q15",
figure = c(
"App.F.15.6.CalCOFI_diversity.tiff"))
figs_beg <- path(dir_img, figs$folder, figs$figure)
figs_end <- here(glue("docs/figures/{path_ext_remove(basename(figs_beg))}.png"))
walk2(figs_beg, figs_end, convert_fig)
knitr::include_graphics(figs_end)
Figure App.F.15.6. Mean species richness (top panels) and mean Gini-Simpson diversity (bottom panels) of fish larvae (ichthyoplankton) collected in net samples during spring CalCOFI cruises at sites in the Channel Islands National Marine Sanctuary (CINMS) region (left panels) and over the Southern California Shelf (right panels) from 1978 to 2015. Species richness is the number of species present in a net sample. Gini-Simpson diversity (1-λ form) takes into account the number of species present, as well as the relative abundance of each species. Species richness tends to increase in El Niño years due to influx of central Pacific species to the shelf. Gini-Simpson diversity is high when individuals are well-distributed among species suggesting that the 2015 spike in richness was not due to rare species. Sampling sites are shown on Figure App.F.12.17. Symbols on the graph are explained in the caption for Figure App.F.12.18. Data source: CalCOFI; Figure: A. Thompson/NOAA
dir_create(dir_cache)
dataset_ids <- ed_search(
query = q_calcofi, which = "tabledap") %>% .$info %>%
arrange(dataset_id) %>%
pull(dataset_id)
for (i in 1:length(dataset_ids)){ # i = 12
id <- dataset_ids[i]
message(glue("{i %>% str_pad(2, '0', side='left')} of {length(dataset_ids)}: {id} - {Sys.time()}"))
d_csv <- glue("{dir_cache}/{id}.csv")
if (!file.exists(d_csv) & i != 12){
d <- tabledap(id)
write_csv(d, d_csv)
}
if (i == 12 & !file.exists(d_csv)){
browser()
tabledap(id, store=disk(path=dir_cache, overwrite = T))
# paste0(digest::digest(ky), ".", fmt) # https://github.com/ropensci/rerddap/blob/a4fd75a79179fe5c6c56d553a11940baecc78100/R/grid.R#L309
digest_csv <- glue("{dir_cache}/b3d5c9eb945013f5e878258049a923a3.csv")
file_move(digest_csv, d_csv)
}
}
# ~4.4 min ea * 31 datasets = 136.9 min
dir_info(dir_cache) %>%
mutate(
file = basename(path),
size = format(size)) %>%
select(file, size) %>%
datatable()
stns <- tabledap('erdCalCOFIstns') %>%
arrange(cruise, ship_code, order_occupied)
p_cinms <- get_nms_ply("cinms")
ymd_min = ymd(glue("{yr_min}-01-01"))
#info("erdCalCOFIlrvcntSBtoSC")
#flds <- c("calcofi_species_code", "station", "line", "longitude", "latitude", "net_location")
#d_lrvcntSBtoSC <- tabledap("erdCalCOFIlrvcntSBtoSC", fields=flds) #%>%
#tic()
if (!file.exists(stns_cinms_csv)){
tbl_stns_cinms <- read_csv(lrvcntSBtoSC_csv)
tbl_stns_cinms <- tbl_stns_cinms %>%
as_tibble() %>%
mutate(
stn_cruise_ship_order = glue("{cruise}-{ship_code}-{order_occupied}"),
lon = as.numeric(longitude), # rng: -179.82, -77.23
lat = as.numeric(latitude), # rng: 0.01, 51.09
dtime = as_datetime(time),
line_station = glue("{line} {station}")) %>%
select(-longitude, -latitude, -time) %>%
filter(
dtime >= ymd_min, # n: 2,416,384 -> 1,501,280
# select only starboard samples from the bongo nets
net_location == "starboard") %>% # n: 2,416,384 -> 1,792,640
arrange(cruise, ship_code, order_occupied) # x 25
# core stations
linestation_core = c(
"76.7 49", "76.7 51", "76.7 55", "76.7 60","76.7 70","76.7 80","76.7 90", "76.7 100","80 51",
"80 55","80 60","80 70","80 80", "80 90","80 100",
"81.8 46.9","83.3 40.6", "83.3 42", "83.3 51", "83.3 55", "83.3 60","83.3 70","83.3 80", "83.3 90", "83.3 100","83.3 110",
"86.7 33", "86.7 35", "86.7 40", "86.7 45", "86.7 50", "86.7 55","86.7 60","86.7 70", "86.7 80", "86.7 90", "86.7 100", "86.7 110",
"90 28", "90 30", "90 35","90 37", "90 45", "90 53", "90 60", "90 70", "90 80", "90 90", "90 100", "90 110", "90 120",
"93.3 26.7", "93.3 28", "93.3 30", "93.3 35", "93.3 40","93.3 45", "93.3 50","93.3 60","93.3 70", "93.3 80", "93.3 90", "93.3 100","93.3 110","93.3 120")
# CINMS stations
linestation_cinms = c(
"76.7 49", "76.7 51", "76.7 55", "76.7 60","80 51", "80 55",
"80 60","81.8 46.9","83.3 40.6", "83.3 42", "83.3 51", "83.3 55", "83.3 60",
"86.7 33", "86.7 35", "86.7 40", "86.7 45", "86.7 50", "86.7 55","86.7 60","90 28", "90 30", "90 35",
"90 37", "90 45", "90 53","90 60", "93.3 26.7", "93.3 28", "93.3 30", "93.3 35", "93.3 40",
"93.3 45", "93.3 50","93.3 60")
# SB stations
linestation_sb <- c("80 55","80 51","81.8 46.9","83.3 55","83.3 51","83.3 42","83.3 40.6")
tbl_stns_cinms <- tbl_stns_cinms %>%
mutate(
is_core = line_station %in% linestation_core,
is_cinms = line_station %in% linestation_cinms,
is_sb = line_station %in% linestation_sb)
# View(tbl_stns_cinms)
write_csv(tbl_stns_cinms, stns_cinms_csv)
}
tbl_stns_cinms <- read_csv(stns_cinms_csv)
pts_stns <- tbl_stns_cinms %>%
filter(is_core) %>%
group_by(stn_cruise_ship_order) %>%
summarise(
lon = mean(lon),
lat = mean(lat),
line_station = first(line_station),
is_core = first(is_core),
is_cinms = first(is_cinms),
is_sb = first(is_sb)) %>%
st_as_sf(coords=c("lon", "lat"), crs=4326, remove=F)
if (!file.exists(plys_cinms_geo)){
ply_cinms <- pts_stns %>%
filter(is_cinms) %>%
st_union() %>% st_convex_hull()
ply_core <- pts_stns %>%
filter(is_core) %>%
st_union() %>% st_convex_hull()
ply_sb <- pts_stns %>%
filter(is_sb) %>%
st_union() %>% st_convex_hull()
plys <- tibble(
ply_code = c("CINMS", "SoCal Shelf", "SoCal"),
geometry = c(ply_sb, ply_cinms, ply_core)) %>%
st_as_sf()
write_sf(plys, plys_cinms_geo, delete_dsn=T)
}
plys <- read_sf(plys_cinms_geo)
mapviewOptions(
basemaps = c("Esri.OceanBasemap", "Stamen.TonerLite"))
mapview(plys) +
pts_stns