diff --git a/NAMESPACE b/NAMESPACE index edb3f6e..d56a857 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(es50_base) export(es50_timeblock) +export(es_intervals) export(fetch_mpa) export(fetch_obis) export(fetch_obis_country) @@ -9,7 +10,10 @@ export(has_mpa) export(has_obis) export(list_mpa) export(list_obis) +export(mpa_es50) +export(mpa_knit_obs) export(mpa_match) +export(mpa_timeblock) export(obis_as_sf) export(obis_columns) export(obis_strict_match) diff --git a/R/es50.R b/R/es50.R index e672eb5..2645dc3 100644 --- a/R/es50.R +++ b/R/es50.R @@ -1,33 +1,241 @@ +#' Compute ES50 for a sf table of MPAs +#' +#' @export +#' @param mpa sf table of MPAs, possibly with a column of 'obs'. If that column +#' is missing we add it by calling \code{fetch_obis} which could take some time. +#' @param verbose logical, output helpful messages +#' @return the same mpa row with 'total_records', 'total_phyla', 'total_species', +#' 'es50_overall' and possibly 'obs' if it doesn't already exist +mpa_es50 <- function(mpa = read_mpa("Cuba"), verbose = interactive()){ + + if (!inherits(mpa, "sf")) stop("mpa input must be of 'sf' class") + + if (!("obs" %in% colnames(mpa))){ + if (verbose) cat("fetching OBIS observation data\n") + obs <- fetch_obis(mpa, form = "sf") + mpa <- mpa_knit_obs(mpa, obs) + } + + mpa |> + dplyr::group_by(.data$WDPAID) |> + dplyr::group_map(es50_base, .keep = TRUE, verbose = verbose) |> + dplyr::bind_rows() +} + + +#' Compute ES50 at timesteps for a sf table of MPAs +#' +#' @export +#' @param mpa sf table of MPAs, possibly with a column of 'obs'. If that column +#' is missing we add it by calling \code{fetch_obis} which could take some time. +#' @param ages numeric, age(s) to compute es50 for +#' @param step_size numeric of years in each step of analysis +#' @param verbose logical, output helpful messages +#' @return the same mpa row with 'diversity' column, and possibly 'obs' if it doesn't already exist +mpa_timeblock <- function(mpa = read_mpa("Cuba"), + ages = c(10, 20), + step_size = 10, + verbose = interactive()){ + + if (!inherits(mpa, "sf")) stop("mpa input must be of 'sf' class") + + if (!("obs" %in% colnames(mpa))){ + if (verbose) cat("fetching OBIS observation data\n") + obs <- fetch_obis(mpa, form = "sf") + mpa <- mpa_knit_obs(mpa, obs) + } + + + + es <- lapply(seq_len(nrow(mpa)), + function(i){ + es_intervals(mpa |> dplyr::slice(i), + ages = ages, + step_size = step_size, + verbose = verbose) + }) + + geom_ix <- which_geometry(mpa) + mpa |> + dplyr::mutate(diversity = es, .before = dplyr::all_of(geom_ix)) +} + + + +#' Calculate es50 metric for an mpa for specific time windows +#' +#' @param mpa a single row from wdpa table containing one mpa with metadata, obs and geometry column +#' If the obs column is not present then it is fetched from OBIS +#' @param key the wdpaid of the mpa (ignored) +#' @param ages numeric, one or more 'ages' relative to 'STATUS_YR' to compute es50 for +#' @param step_size numeric duration (in years) for each age (interval = age + time_step) +#' @param sample_size numeric see \code{\link[vegan]{rarefy}} \code{sample} argument. 50 computes ES50. +#' @param verbose logical, if TRUE output helpful messages +#' @return tibble of ES statistics +#' @export +es_intervals <- function(mpa, key, + ages = c(10, 20), # make plural + step_size = 10, + sample_size = 50, + verbose = interactive()) { + + if (nrow(mpa) > 1) stop("one row at a time please") + + samp <- as.numeric(sample_size[1]) + if (verbose) cat(sprintf("es%i_intervals WDPAID: %s", samp[1], mpa$WDPAID[1]), "\n") + + if (!("obs" %in% colnames(mpa))){ + if (verbose) cat("fetching OBIS observations...\n") + # request the records based upon geometry column + records <- tryCatch( + { + #robis::occurrence(geometry = sf::st_as_text(sf::st_convex_hull(mpa[[geom_ix[1]]]))) + fetch_obis(mpa, form = 'sf') + }, + error = function(e){ + message('Error in es50_timeblock') + print(e) + return(NULL) + }) + mpa <- mpa_knit_obs(mpa, records) + } + + interval_one <- function(mpa, + age = 10, + step_size = 10, + samp = 50, + verbose = interactive()){ + + # can't find ES50 in the future, so return + if ((mpa$STATUS_YR + age) > as.numeric(format(Sys.Date(),format="%Y"))) { + if (verbose) cat(paste(mpa$WDPAID, "MPA is too young, age:", age, "\n", sep=" ")) + + #geom_ix <- which_geometry(mpa) + + #mpa <- mpa %>% + # dplyr::mutate("age_{age}" := NA, .before = geom_ix) + r <- dplyr::tibble( + WDPAID = mpa$WDPAID, + age = age, + "es{samp}_age" := NA_real_, + "species_age" := NA_real_, + "phylum_age" := NA_real_, + "records_age" := NA_real_) + return(r) + } + + #filter occurrence records for bin calculation + #if (age == 0) { + # #maybe (step_+size - 1) + # age_year <- mpa$STATUS_YR + step_size[1] - 1 + # + # age_records <- mpa$obs[[1]] %>% + # dplyr::filter(dplyr::between(.data$date_year, mpa$STATUS_YR, age_year)) + # + #} else { + bin_start <- mpa$STATUS_YR + age + + bin_end <- bin_start + step_size[1] - 1 + + age_records <- mpa$obs[[1]] %>% + dplyr::filter(dplyr::between(.data$date_year, bin_start, bin_end)) + #} + + #compute ES{samp} for filtered occurrence records by bin + if (nrow(age_records) > 0) { + # there is no "Count" field + species_counts <- aggregate(as.integer(age_records$individualCount), + by=list(Category=age_records$scientificName), + FUN=sum) + + phylum_counts <- aggregate(as.integer(age_records$individualCount), + by=list(Category=age_records$phylum), + FUN=sum) + + if (nrow(species_counts) >= samp[1]) { + + if (verbose) cat(sprintf("%s calculating es%i, age: %i", mpa$WDPAID, samp[1], age[1]),"\n") + + es <- vegan::rarefy(as.integer(species_counts$x), samp[1]) + + + r <- dplyr::tibble( + WDPAID = mpa$WDPAID, + age = age, + "es{samp}_age" := es, + "species_age" := nrow(phylum_counts), + "phylum_age" := nrow(phylum_counts), + "records_age" := sum(species_counts$x)) + } else { + if (verbose) cat(paste(mpa$WDPAID, "Fewer than 50 species records, age:", age, "\n", sep=" ")) + + r <- dplyr::tibble( + WDPAID = mpa$WDPAID, + age = age, + "es{samp}_age" := NA_real_, + "species_age" := NA_real_, + "phylum_age" := NA_real_, + "records_age" := NA_real_) + } + } else { + if (verbose) cat(paste(mpa$WDPAID, "No records, age:", age, "\n", sep=" ")) + r <- dplyr::tibble( + WDPAID = mpa$WDPAID, + age = age, + "es{samp}_age" := NA_real_, + "species_age" := NA_real_, + "phylum_age" := NA_real_, + "records_age" := NA_real_) + } + + return(r) + } # interval_one + + + lapply(ages, + function(age){ + interval_one(mpa, age = age, step_size = step_size, + samp = samp, verbose = verbose) + }) |> + dplyr::bind_rows() +} + + #' Record number of records and attempt an overall es50 calculation #' +#' @seealso \code{\link{mpa_es50}} #' @export #' @param mpa a single row of an mpa records table #' @param key the wdpaid of the mpa -#' @param records an object containing obis records #' @param verbose logical, if TRUE output helpful messages #' @return the same mpa row with total_records, total_phyla, total_species, es50_overall #' or NULL if an error occurs -#' -es50_base <- function(mpa, key, records, verbose = interactive()) { +es50_base <- function(mpa, key, verbose = interactive()) { - if (verbose) cat(key$WDPAID[1], "\n") + if (verbose) cat("es50_base WDPAID: ", mpa$WDPAID[1], "\n") # which column is likely to contain geometry? geom_ix <- which_geometry(mpa) - # request the records based upon geometry column - records <- tryCatch( - { - robis::occurrence(geometry = sf::st_as_text(sf::st_convex_hull(mpa[[geom_ix[1]]]))) - }, - error = function(e){ - message('Error in es50_base') - print(e) - return(NULL) - }) - + if (!("obs" %in% colnames(mpa))){ + if (verbose) cat("fetching OBIS observations...\n") + # request the records based upon geometry column + records <- tryCatch( + { + #robis::occurrence(geometry = sf::st_as_text(sf::st_convex_hull(mpa[[geom_ix[1]]]))) + fetch_obis(mpa, form = 'sf') + }, + error = function(e){ + message('Error in es50_base') + print(e) + return(NULL) + }) + mpa <- mpa_knit_obs(mpa, records) + } else { + records <- mpa$obs[[1]] + } - if (!rlang::is_empty(records)) { + if (!rlang::is_empty(records) && nrow(records) > 0) { if ("individualCount" %in% colnames(records)) { @@ -51,19 +259,26 @@ es50_base <- function(mpa, key, records, verbose = interactive()) { dplyr::mutate(species_count_all = nrow(species_count), phylum_count_all = nrow(phylum_count), records_all = sum(species_count$x), - .before = geom_ix) + .before = dplyr::all_of(geom_ix)) if (nrow(species_count) >= 50){ total_es50 = vegan::rarefy(as.integer(species_count$x),50) geom_ix <- which_geometry(mpa) mpa <- mpa %>% dplyr::mutate(total_es50 = total_es50, - .before = geom_ix) + .before = dplyr::all_of(geom_ix)) } + } else { + mpa <- mpa %>% + dplyr::mutate(total_es50 = NA_real_, + species_count_all = 0, + phylum_count_all = 0, + records_all = 0, + .before = dplyr::all_of(geom_ix)) } - if (verbose) cat("\n") + #if (verbose) cat("\n") return(mpa) } @@ -71,47 +286,69 @@ es50_base <- function(mpa, key, records, verbose = interactive()) { #' Calculate es50 metric for an mpa for specific time points #' -#' @param mpa a row from wdpa table containing one mpa with metadata and geometry column +#' @param mpa a row from wdpa table containing one mpa with metadata, obs and geometry column +#' If the obs column is not present then it is fetched from OBIS #' @param key the wdpaid of the mpa -#' @param records an object containing obis records -#' @param age numeric, age to compute es50 for -#' @param step_size numeric of years in each step of analysis +#' @param age numeric, ages to compute es50 for +#' @param step_size numeric duration in each step of analysis #' @param verbose logical, if TRUE output helpful messages #' @return single row table containing same columns as mpa but with added timeblock columns #' #' @export -es50_timeblock <- function(mpa, key, records, age, step_size, verbose = interactive()) { - - if ((mpa$STATUS_YR + age) > format(as.Date(Sys.Date()), format="%Y")) { +es50_timeblock <- function(mpa, key, + age = 10, # make plural + step_size = 10, + verbose = interactive()) { + if (verbose) cat("es50_base WDPAID: ", mpa$WDPAID[1], "\n") + + if (!("obs" %in% colnames(mpa))){ + if (verbose) cat("fetching OBIS observations...\n") + # request the records based upon geometry column + records <- tryCatch( + { + #robis::occurrence(geometry = sf::st_as_text(sf::st_convex_hull(mpa[[geom_ix[1]]]))) + fetch_obis(mpa, form = 'sf') + }, + error = function(e){ + message('Error in es50_timeblock') + print(e) + return(NULL) + }) + mpa <- mpa_knit_obs(mpa, records) + } + + # can't find ES50 in the future + if ((mpa$STATUS_YR + age) > as.numeric(format(Sys.Date(),format="%Y"))) { if (verbose) cat(paste(mpa$WDPAID, "MPA is too young, age:", age, "\n", sep=" ")) geom_ix <- which_geometry(mpa) mpa <- mpa %>% - dplyr::mutate("age_{age}" := NA, .before = geom_ix) + dplyr::mutate("age_{age}" := NA, .before = dplyr::all_of(geom_ix)) return(mpa) } #filter occurrence records for bin calculation if (age == 0) { - - age_year <- mpa$STATUS_YR + 19 - - age_records <- records %>% + # why 19? maybe (step_+size - 1) + age_year <- mpa$STATUS_YR + step_size[1] - 1 # 19 + + age_records <- mpa$obs[[1]] %>% dplyr::filter(dplyr::between(.data$date_year, mpa$STATUS_YR, age_year)) } else { bin_start <- mpa$STATUS_YR + age - bin_end <- bin_start + 19 + bin_end <- bin_start + step_size[1] - 1 # 19 - age_records <- records %>% + age_records <- mpa$obs[[1]] %>% dplyr::filter(dplyr::between(.data$date_year, bin_start, bin_end)) } #compute es50 for filtered occurrence records by bin if (nrow(age_records) > 0) { + # there is no "Count" field species_counts <- aggregate(age_records$Count, by=list(Category=age_records$scientificName), FUN=sum) @@ -121,6 +358,7 @@ es50_timeblock <- function(mpa, key, records, age, step_size, verbose = interact FUN=sum) if (nrow(species_counts) >= 50) { + if (verbose) cat(paste(mpa$WDPAID, "calculating es50, age:", age, "\n", sep=" ")) es_50 <- vegan::rarefy(as.integer(species_counts$x), 50) @@ -133,11 +371,11 @@ es50_timeblock <- function(mpa, key, records, age, step_size, verbose = interact "species_age_{age}" := nrow(species_counts), "phylum_age_{age}" := nrow(phylum_counts), "records_age_{age}" := sum(species_counts$x), - .before = geom_ix) + .before = dplyr::all_of(geom_ix)) return(mpa) } else { - if (verbose) cat(paste(mpa$WDPAID, "Less than 50 species records, age:", age, "\n", sep=" ")) + if (verbose) cat(paste(mpa$WDPAID, "Fewer than 50 species records, age:", age, "\n", sep=" ")) geom_ix <- which_geometry(mpa) @@ -146,7 +384,7 @@ es50_timeblock <- function(mpa, key, records, age, step_size, verbose = interact "species_age_{age}" := NA_real_, "phylum_age_{age}" := NA_real_, "records_age_{age}" := NA_real_, - .before = geom_ix) + .before = dplyr::all_of(geom_ix)) } } else { if (verbose) cat(paste(mpa$WDPAID, "No records, age:", age, "\n", sep=" ")) @@ -158,7 +396,7 @@ es50_timeblock <- function(mpa, key, records, age, step_size, verbose = interact "species_age_{age}" := NA_real_, "phylum_age_{age}" := NA_real_, "records_age_{age}" := NA_real_, - .before = geom_ix) + .before = dplyr::all_of(geom_ix)) } return(mpa) diff --git a/R/mpa.R b/R/mpa.R index 056142c..8f84e9f 100644 --- a/R/mpa.R +++ b/R/mpa.R @@ -18,6 +18,38 @@ random_points <- function(x = read_mpa("Cuba"), } +#' Given a set of MPAs and observations (like obis), add a list columns of +#' obs to each row of MPAs that contain observations. Rows of mpa that have no +#' obs will have an empty tibble. +#' +#' @export +#' @param mpa sf table of MPAs +#' @param obs sf table of observations, like OBIS +#' @return sf table with added 'obs' column +mpa_knit_obs <- function(mpa = read_mpa("Cuba"), + obs = fetch_obis("Cuba", form = "sf")){ + if (!inherits(obs, "sf")){ + stop("obs must inherit from sf") + } + # a sparse list of indices - one element per row of mpa + ix <- sf::st_contains(mpa, obs) + geom_ix <- which_geometry(mpa) + mpa |> + dplyr::mutate( + obs = lapply(seq_along(ix), + function(i){ + if (length(ix[[i]])>0){ + obs |> + dplyr::slice(ix[[i]]) + } else { + obs |> + dplyr::slice(0) + } + }), + .before = dplyr::all_of(geom_ix)) +} + + #' Given a set of MPAs and observations determine which observations belong to which MPA #' #' @export diff --git a/R/obis.R b/R/obis.R index e9d9cda..0e5b92c 100644 --- a/R/obis.R +++ b/R/obis.R @@ -63,10 +63,15 @@ obis_as_sf <- function(x, #' We fetch the convex hull to simplify the fetch, but that may allow some observations that are outside MPAs. #' If "convex hull" then we allow these to be returned. If "strict" then we filter for those observations that are in #' the MPAs (in mean within or on the boundary). +#' @param counts character, either "at_least_one" which forces \code{individualCount} to be at least 1 +#' or 'as_is' which allows for NA values. Philosophy, if it is observed there must have been at least one. +#' @param form character, either 'table' (default) or 'sf' #' @return tibble of OBIS observations, possibly empty fetch_obis <- function(x = read_mpa("Cuba"), combine = TRUE, - policy = c("convex hull", "strict")[1]) { + policy = c("convex hull", "strict")[1], + counts = c("as_is", "at_least_one")[2], + form = c("tibble", "sf")[1]) { # Fetch one row (one MPA) @@ -107,10 +112,17 @@ fetch_obis <- function(x = read_mpa("Cuba"), dplyr::bind_rows() } + if ((nrow(r) > 0) && (tolower(counts[1]) == 'at_least_one')){ + r$individualCount <- as.integer(r$individualCount) + ix <- is.na(r$individualCount) + r$individualCount[ix] <- 1L + } + if (tolower(policy[1]) == "strict"){ r <- obis_strict_match(x, r) } + if (tolower(form[1]) == "sf") r <- obis_as_sf(r) r } @@ -120,7 +132,6 @@ fetch_obis <- function(x = read_mpa("Cuba"), #' #' @export #' @param name character, the name of the country to fetch -#' @param name character, the name to save the MPA under (no spaces please!) #' @param overwrite logical, if TRUE allow existing files to be overwritten #' @param ... other arguments for obis_fetch_mpa #' @return tibble of OBIS observations, possibly empty @@ -145,19 +156,19 @@ fetch_obis_country <- function(name = "Cuba", #' @param what character, by default all values are returned, but specify a subset with #' a character vector of desired columns (variables) #' @param form character, either 'table' (default) or 'sf' -#' @param ... further arguments for \code{\link{obis_as_sf}} in piarricular \code{crs} +#' @param ... further arguments for \code{\link{obis_as_sf}} in particular \code{crs} #' @return data table or sf object read_obis <- function(name = "Cuba", path = rappdirs::user_data_dir("robis"), form = c("table", "sf")[1], what = "all", ...){ - filename <- file.path(path, sprintf("%s.csv.gz", name)) + filename <- file.path(path, sprintf("%s.tsv.gz", name)) if (!file.exists(filename)){ stop("file not found:", filename) } - x <- data.table::fread(filename, quote="") %>% + x <- data.table::fread(filename, quote="", fill = TRUE) %>% dplyr::as_tibble() if (tolower(form[1]) == "sf"){ @@ -184,7 +195,7 @@ write_obis <- function(x, name, if(!dir.exists(path)) stop("robis output path doesn't exist:", path) } - filename <- file.path(path, sprintf("%s.csv.gz", name)) + filename <- file.path(path, sprintf("%s.tsv.gz", name)) if (file.exists(filename) && overwrite == FALSE){ stop("file already exists:", filename) } diff --git a/inst/scripts/example_country_analysis.R b/inst/scripts/example_country_analysis.R index ee1e8c9..1ed6000 100644 --- a/inst/scripts/example_country_analysis.R +++ b/inst/scripts/example_country_analysis.R @@ -1,5 +1,3 @@ - - library(mpatools) library(dplyr) @@ -14,30 +12,43 @@ if (!has_mpa(country)) { mpa <- read_mpa(country) } - -# for testing on a small number of mpas, skip for running the analysis on a whole country -# we can't know ahead of time if we are going to get a column named 'geom' or 'geometry' -# select only MPAs that are polygons (no points or other non-areal objects) -# group by MPA -# compute es_50 by group -# bind results into one dataframe (one row per MPA) -# drop the geometry to leave a bare table -x <- mpa %>% - head(5) %>% - dplyr::select(WDPAID, - NAME, - IUCN_CAT, - REP_AREA, - REP_M_AREA, - STATUS, - STATUS_YR) - -geom_ix <- which_geometry(x) -x <- x %>% - dplyr::filter(sapply(x[[geom_ix]], function(x) inherits(x, "MULTIPOLYGON"))) %>% - dplyr::group_by(WDPAID) %>% - dplyr::group_map(es50_base, .keep=TRUE) %>% - #dplyr::group_map(es50_timeblock, ages, .keep=TRUE) %>% - dplyr::bind_rows() %>% - sf::st_drop_geometry() +mpa <- mpa_timeblock(mpa, + ages = c(-20, 10, 10, 20), + step_size = 10) + +div <- mpa |> + dplyr::pull(diversity) |> + dplyr::bind_rows() + +summary(div) + +View(div) + + + +# # for testing on a small number of mpas, skip for running the analysis on a whole country +# # we can't know ahead of time if we are going to get a column named 'geom' or 'geometry' +# # select only MPAs that are polygons (no points or other non-areal objects) +# # group by MPA +# # compute es_50 by group +# # bind results into one dataframe (one row per MPA) +# # drop the geometry to leave a bare table +# x <- mpa %>% +# head(5) %>% +# dplyr::select(WDPAID, +# NAME, +# IUCN_CAT, +# REP_AREA, +# REP_M_AREA, +# STATUS, +# STATUS_YR) +# +# geom_ix <- which_geometry(x) +# x <- x %>% +# dplyr::filter(sapply(x[[geom_ix]], function(x) inherits(x, "MULTIPOLYGON"))) %>% +# dplyr::group_by(WDPAID) %>% +# dplyr::group_map(es50_base, .keep=TRUE) %>% +# #dplyr::group_map(es50_timeblock, ages, .keep=TRUE) %>% +# dplyr::bind_rows() %>% +# sf::st_drop_geometry() diff --git a/man/es50_base.Rd b/man/es50_base.Rd index e4e0620..5b88ef2 100644 --- a/man/es50_base.Rd +++ b/man/es50_base.Rd @@ -4,15 +4,13 @@ \alias{es50_base} \title{Record number of records and attempt an overall es50 calculation} \usage{ -es50_base(mpa, key, records, verbose = interactive()) +es50_base(mpa, key, verbose = interactive()) } \arguments{ \item{mpa}{a single row of an mpa records table} \item{key}{the wdpaid of the mpa} -\item{records}{an object containing obis records} - \item{verbose}{logical, if TRUE output helpful messages} } \value{ @@ -22,3 +20,6 @@ the same mpa row with total_records, total_phyla, total_species, es50_overall \description{ Record number of records and attempt an overall es50 calculation } +\seealso{ +\code{\link{mpa_es50}} +} diff --git a/man/es50_timeblock.Rd b/man/es50_timeblock.Rd index 10bb62b..5e61e1b 100644 --- a/man/es50_timeblock.Rd +++ b/man/es50_timeblock.Rd @@ -4,18 +4,17 @@ \alias{es50_timeblock} \title{Calculate es50 metric for an mpa for specific time points} \usage{ -es50_timeblock(mpa, key, records, age, step_size, verbose = interactive()) +es50_timeblock(mpa, key, age = 10, step_size = 10, verbose = interactive()) } \arguments{ -\item{mpa}{a row from wdpa table containing one mpa with metadata and geometry column} +\item{mpa}{a row from wdpa table containing one mpa with metadata, obs and geometry column +If the obs column is not present then it is fetched from OBIS} \item{key}{the wdpaid of the mpa} -\item{records}{an object containing obis records} +\item{age}{numeric, ages to compute es50 for} -\item{age}{numeric, age to compute es50 for} - -\item{step_size}{numeric of years in each step of analysis} +\item{step_size}{numeric duration in each step of analysis} \item{verbose}{logical, if TRUE output helpful messages} } diff --git a/man/es_intervals.Rd b/man/es_intervals.Rd new file mode 100644 index 0000000..7c509f1 --- /dev/null +++ b/man/es_intervals.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/es50.R +\name{es_intervals} +\alias{es_intervals} +\title{Calculate es50 metric for an mpa for specific time windows} +\usage{ +es_intervals( + mpa, + key, + ages = c(10, 20), + step_size = 10, + sample_size = 50, + verbose = interactive() +) +} +\arguments{ +\item{mpa}{a single row from wdpa table containing one mpa with metadata, obs and geometry column +If the obs column is not present then it is fetched from OBIS} + +\item{key}{the wdpaid of the mpa (ignored)} + +\item{ages}{numeric, one or more 'ages' relative to 'STATUS_YR' to compute es50 for} + +\item{step_size}{numeric duration (in years) for each age (interval = age + time_step)} + +\item{sample_size}{numeric see \code{\link[vegan]{rarefy}} \code{sample} argument. 50 computes ES50.} + +\item{verbose}{logical, if TRUE output helpful messages} +} +\value{ +tibble of ES statistics +} +\description{ +Calculate es50 metric for an mpa for specific time windows +} diff --git a/man/fetch_obis.Rd b/man/fetch_obis.Rd index 3b3f750..cf89d7d 100644 --- a/man/fetch_obis.Rd +++ b/man/fetch_obis.Rd @@ -7,7 +7,9 @@ fetch_obis( x = read_mpa("Cuba"), combine = TRUE, - policy = c("convex hull", "strict")[1] + policy = c("convex hull", "strict")[1], + counts = c("as_is", "at_least_one")[2], + form = c("tibble", "sf")[1] ) } \arguments{ @@ -22,6 +24,11 @@ out if \code{policy} is 'strict'.} We fetch the convex hull to simplify the fetch, but that may allow some observations that are outside MPAs. If "convex hull" then we allow these to be returned. If "strict" then we filter for those observations that are in the MPAs (in mean within or on the boundary).} + +\item{counts}{character, either "at_least_one" which forces \code{individualCount} to be at least 1 +or 'as_is' which allows for NA values. Philosophy, if it is observed there must have been at least one.} + +\item{form}{character, either 'table' (default) or 'sf'} } \value{ tibble of OBIS observations, possibly empty diff --git a/man/fetch_obis_country.Rd b/man/fetch_obis_country.Rd index 565a2f9..203a9b4 100644 --- a/man/fetch_obis_country.Rd +++ b/man/fetch_obis_country.Rd @@ -7,7 +7,7 @@ fetch_obis_country(name = "Cuba", overwrite = TRUE, ...) } \arguments{ -\item{name}{character, the name to save the MPA under (no spaces please!)} +\item{name}{character, the name of the country to fetch} \item{overwrite}{logical, if TRUE allow existing files to be overwritten} diff --git a/man/mpa_es50.Rd b/man/mpa_es50.Rd new file mode 100644 index 0000000..742ba1f --- /dev/null +++ b/man/mpa_es50.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/es50.R +\name{mpa_es50} +\alias{mpa_es50} +\title{Compute ES50 for a sf table of MPAs} +\usage{ +mpa_es50(mpa = read_mpa("Cuba"), verbose = interactive()) +} +\arguments{ +\item{mpa}{sf table of MPAs, possibly with a column of 'obs'. If that column +is missing we add it by calling \code{fetch_obis} which could take some time.} + +\item{verbose}{logical, output helpful messages} +} +\value{ +the same mpa row with 'total_records', 'total_phyla', 'total_species', + 'es50_overall' and possibly 'obs' if it doesn't already exist +} +\description{ +Compute ES50 for a sf table of MPAs +} diff --git a/man/mpa_knit_obs.Rd b/man/mpa_knit_obs.Rd new file mode 100644 index 0000000..35db1b7 --- /dev/null +++ b/man/mpa_knit_obs.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mpa.R +\name{mpa_knit_obs} +\alias{mpa_knit_obs} +\title{Given a set of MPAs and observations (like obis), add a list columns of +obs to each row of MPAs that contain observations. Rows of mpa that have no +obs will have an empty tibble.} +\usage{ +mpa_knit_obs(mpa = read_mpa("Cuba"), obs = fetch_obis("Cuba", form = "sf")) +} +\arguments{ +\item{mpa}{sf table of MPAs} + +\item{obs}{sf table of observations, like OBIS} +} +\value{ +sf table with added 'obs' column +} +\description{ +Given a set of MPAs and observations (like obis), add a list columns of +obs to each row of MPAs that contain observations. Rows of mpa that have no +obs will have an empty tibble. +} diff --git a/man/mpa_timeblock.Rd b/man/mpa_timeblock.Rd new file mode 100644 index 0000000..e963b53 --- /dev/null +++ b/man/mpa_timeblock.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/es50.R +\name{mpa_timeblock} +\alias{mpa_timeblock} +\title{Compute ES50 at timesteps for a sf table of MPAs} +\usage{ +mpa_timeblock( + mpa = read_mpa("Cuba"), + ages = c(10, 20), + step_size = 10, + verbose = interactive() +) +} +\arguments{ +\item{mpa}{sf table of MPAs, possibly with a column of 'obs'. If that column +is missing we add it by calling \code{fetch_obis} which could take some time.} + +\item{ages}{numeric, age(s) to compute es50 for} + +\item{step_size}{numeric of years in each step of analysis} + +\item{verbose}{logical, output helpful messages} +} +\value{ +the same mpa row with 'diversity' column, and possibly 'obs' if it doesn't already exist +} +\description{ +Compute ES50 at timesteps for a sf table of MPAs +} diff --git a/man/read_obis.Rd b/man/read_obis.Rd index 5e763cb..19d8fb9 100644 --- a/man/read_obis.Rd +++ b/man/read_obis.Rd @@ -22,7 +22,7 @@ read_obis( \item{what}{character, by default all values are returned, but specify a subset with a character vector of desired columns (variables)} -\item{...}{further arguments for \code{\link{obis_as_sf}} in piarricular \code{crs}} +\item{...}{further arguments for \code{\link{obis_as_sf}} in particular \code{crs}} } \value{ data table or sf object