Skip to content
Merged

Dev #35

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
8c69477
update mrip vignette
Jul 7, 2025
35cebf3
finish mrip vignette with new data query functions
stephanie-owen Jul 7, 2025
5d5d765
Merge branch 'dev' into stephanie_dev
atyrell3 Jul 8, 2025
b1c1f0b
Merge pull request #23 from NEFSC/stephanie_dev
atyrell3 Jul 8, 2025
44d65cd
update mrip vignette
atyrell3 Jul 8, 2025
a6b1aea
update netcdf metadata, update mrip vignette
atyrell3 Jul 9, 2025
8293bd6
Merge pull request #24 from NEFSC/abby_dev
stephanie-owen Jul 9, 2025
bc34138
updates to install pkg and edab utils vignettes
Jul 9, 2025
f21c2f5
resolve merge conflict
Jul 9, 2025
b767cf4
update mrip vignette
atyrell3 Jul 9, 2025
7e81f73
add units to netcdf
atyrell3 Jul 10, 2025
8cb8b4d
update condition documentation
atyrell3 Jul 14, 2025
c168fd8
Merge pull request #25 from NEFSC/abby_dev
stephanie-owen Jul 14, 2025
21957e8
update condition function, format output for soe/esps, add ability to…
atyrell3 Jul 18, 2025
54f1f83
update condition vignette
atyrell3 Jul 18, 2025
5c9fd1f
Merge pull request #26 from NEFSC/abby_dev
stephanie-owen Jul 18, 2025
8f15f4d
test spatial indicator code
Jul 21, 2025
dae567b
add more flexibility to mrip catch pull, improve error handling and m…
atyrell3 Jul 21, 2025
6c2a108
add ecodata hms data
stephanie-owen Jul 21, 2025
540ea24
pull hms landings data from mrip
Jul 22, 2025
56f8958
test hms data formatting
atyrell3 Jul 22, 2025
af770ac
format hms mrip
Jul 22, 2025
71ef4fb
spatial indicator vignette, test sst code
Jul 23, 2025
eb7c5e6
Merge branch 'stephanie_dev' of https://github.com/NEFSC/READ-EDAB-NE…
Jul 23, 2025
b5ad029
add make_2d_summary_ts to test sst code
Jul 23, 2025
394b0bd
fix edab utils vignette
Jul 23, 2025
7b8afb4
test condition indicator
Jul 23, 2025
5e13c28
sst test with edab_utils using OISST daily and ltm
Jul 24, 2025
1f01dc6
attempt mrip data pull by region
Jul 24, 2025
34c8e94
resolve some merge conflicts
atyrell3 Jul 24, 2025
690d8dd
undo merge conflict fix
atyrell3 Jul 24, 2025
4203ed7
merging
atyrell3 Jul 24, 2025
bd5cd80
actually sync files with dev
atyrell3 Jul 24, 2025
25dc1ee
Merge pull request #28 from NEFSC/abby_dev
stephanie-owen Jul 25, 2025
2506bb4
resolve merge conflicts
Jul 25, 2025
6808255
attempt to fix mrip code
Jul 25, 2025
c9f48a5
update spatial vignette with sst example
Jul 25, 2025
295dd23
merge conflict
Jul 28, 2025
5a64b24
clean up mrip functions
atyrell3 Jul 28, 2025
0f2e57e
fix catch naming
atyrell3 Jul 28, 2025
763f1fe
mid and na mrip pulls
Jul 28, 2025
c3aa34a
mrip pulls for mid and north atlantic hms
Jul 31, 2025
ff20817
Merge pull request #29 from NEFSC/abby_dev
stephanie-owen Jul 31, 2025
6394f66
merge conflict
Jul 31, 2025
7c1db33
merge conflict
Jul 31, 2025
06b2116
Merge pull request #27 from NEFSC/stephanie_dev
stephanie-owen Jul 31, 2025
2593fa1
clean up mrip code
Jul 31, 2025
c67cd15
Merge pull request #30 from NEFSC/stephanie_dev
atyrell3 Jul 31, 2025
7720e8c
trying to resolve merge conflict
stephanie-owen Jul 31, 2025
5e39a72
merging
stephanie-owen Aug 1, 2025
b4ead5f
Merge pull request #31 from NEFSC/stephanie_dev
stephanie-owen Aug 1, 2025
bdaac7b
improve error handling, add mrip tests
atyrell3 Aug 4, 2025
26b73f9
testing hms mrip pull
atyrell3 Aug 7, 2025
d6f4f16
rerun north atl pulls
Aug 7, 2025
dffa614
merge conflict
Aug 7, 2025
3db4c64
create demo script
atyrell3 Aug 12, 2025
a45f2f6
clean up documentation
atyrell3 Aug 12, 2025
98c348c
finish merging
atyrell3 Aug 12, 2025
4a08f21
Merge pull request #33 from NEFSC/stephanie_dev
atyrell3 Aug 12, 2025
67a900c
delete file
atyrell3 Aug 12, 2025
5b6d5fe
format rec hms
Aug 12, 2025
3989da9
merge dev verion
atyrell3 Aug 12, 2025
5994254
Merge branch 'dev' into abby_dev
atyrell3 Aug 12, 2025
f0bcb34
Merge pull request #32 from NEFSC/abby_dev
atyrell3 Aug 12, 2025
a884e3f
Merge branch 'dev' into stephanie_dev
atyrell3 Aug 12, 2025
1d4c16f
Merge pull request #34 from NEFSC/stephanie_dev
atyrell3 Aug 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 2 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ export(create_coldpool_extent)
export(create_coldpool_index)
export(create_coldpool_persistence)
export(create_gsi)
export(create_mrip_trips)
export(create_prop_sp_trips)
export(create_rec_trips)
export(create_spatial_indicator)
export(create_sst)
export(create_template)
export(create_total_rec_catch)
export(create_total_rec_landings)
export(create_total_mrip)
export(create_wcr)
export(esp_csv_to_nc)
export(find_files)
Expand All @@ -21,12 +20,10 @@ export(format_indicator)
export(format_tbl_data)
export(get_mrip_catch)
export(get_mrip_trips)
export(get_trip_files)
export(nc_to_raster)
export(plot_condition)
export(plot_risk)
export(plt_indicator)
export(read_rec_catch)
export(rpt_card_table)
export(save_catch)
export(save_trips)
Expand Down
203 changes: 131 additions & 72 deletions R/create_condition_indicator.R
Original file line number Diff line number Diff line change
@@ -1,71 +1,93 @@


#' Create species condition indicator
#'
#' This function calculates species condition from a survdat data pull.
#' This function calculates species condition from a survdat data pull.
#' Methods derived from Laurel Smith (https://github.com/Laurels1/Condition/blob/master/R/RelConditionEPU.R)
#'
#' @param data data table. NEFSC survey data generated by 'survdat::get_survdat_data(channel, getBio = T, getLengths = T)'
#' @param LWparams L-W parameters from Wigley et al. 2003 from NEesp2::LWparams
#' @param species.codes data table. Species codes and names from NEesp2::species.codes
#'
#' @param data data table. NEFSC survey data generated by 'survdat::get_survdat_data(channel, getBio = TRUE, getLengths = TRUE)'
#' @param LWparams data table. L-W parameters. Default is from Wigley et al. 2003 from NEesp2::LWparams. Alternative parameters could be used, as long as they were formatted the same as the Wigley parameters.
#' @param species.codes data table. Species codes (SVSPP) and common names from NEesp2::species.codes
#' @param by_EPU logical. If TRUE, calculates condition by EPUs specified in the input data, if FALSE, calculates condition for all data combined.
#' @param by_sex logical. If TRUE, calculates condition by sex. If FALSE, calculates condition across sexes.
#' @param length_break numeric vector. If not NULL, will calculate condition by length breaks specified in the vector. User must specify minimum and maximum lengths in this parameter, e.g., c(0, 20, 70). If NULL, will not calculate by length groupings.
#' @param output character. If "soe", returns a data frame of species condition for the State of the Ecosystem report. If "esp", returns a data frame for ESPs. If "full", returns a data frame of all calculated values. *Setting by_sex = TRUE or length_break to any value will always return a full dataframe*
#' @importFrom magrittr %>%
#' @return Returns R object `condition`, a data frame of species condition by EPU with scale.
#' @return Returns a data frame of species condition
#' @export

species_condition <- function(data,
LWparams = NEesp2::LWparams,
species.codes = NEesp2::species.codes,
by_EPU = TRUE) {
species_condition <- function(data,
LWparams = NEesp2::LWparams,
species.codes = NEesp2::species.codes,
by_EPU = TRUE,
by_sex = FALSE,
length_break = NULL,
output = "soe") {
if (by_sex |
!is.null(length_break)) {
if (output != "full") {
message("You asked to group results by sex and/or length ; data will not be formatted for SOE or ESP output.")
}
output <- "full"
}

# add 0 to length_break if needed
if (!0 %in% length_break & !is.null(length_break)) {
length_break <- c(0, length_break)
}

if(by_EPU) {
survey.data <- data %>%
if (by_EPU) {
survey.data <- data %>%
dplyr::left_join(NEesp2::strata_epu_key)
} else {
survey.data <- data |>
dplyr::mutate(EPU = "UNIT")
}


#Change sex = NA to sex = 0
fall <- survey.data %>%
dplyr::filter(SEASON == 'FALL') %>%
dplyr::mutate(sex = dplyr::if_else(is.na(SEX), '0', as.character(SEX)))

# Change sex = NA to sex = 0
fall <- survey.data %>%
dplyr::filter(SEASON == "FALL") %>%
dplyr::mutate(sex = dplyr::if_else(is.na(SEX), "0", as.character(SEX)))

# filter LWparams to fall only, add in male/female if data is "combined"
LWfall <- LWparams %>%
dplyr::filter(SEASON == 'FALL')
LWfall <- LWparams %>%
dplyr::filter(SEASON == "FALL")

add_sexes <- LWfall |>
dplyr::group_by(SpeciesName) |>
dplyr::mutate(count = dplyr::n()) |>
dplyr::filter(count == 1) |>
dplyr::ungroup() |>
dplyr::select(-Gender) |>
dplyr::full_join(tibble::tibble(count = 1,
Gender = c("Male", "Female")),
relationship = "many-to-many") |>
dplyr::full_join(
tibble::tibble(
count = 1,
Gender = c("Male", "Female")
),
relationship = "many-to-many"
) |>
dplyr::select(-count)

new_dat <- dplyr::bind_rows(LWfall, add_sexes) |>
dplyr::arrange(SpeciesName)
#Add SEX for Combined gender back into Wigley at all data (loses 4 Gender==Unsexed):

# Add SEX for Combined gender back into Wigley at all data (loses 4 Gender==Unsexed):
LWpar_sexed <- new_dat |>
dplyr::mutate(sex = dplyr::case_when(Gender == "Combined" | Gender == "Unsexed" ~ as.character(0),
Gender == "Male" ~ as.character(1),
Gender == "Female" ~ as.character(2),
TRUE ~ NA))

LWpar_spp <- LWpar_sexed %>%
dplyr::mutate(sex = dplyr::case_when(
Gender == "Combined" | Gender == "Unsexed" ~ as.character(0),
Gender == "Male" ~ as.character(1),
Gender == "Female" ~ as.character(2),
TRUE ~ NA
))

LWpar_spp <- LWpar_sexed %>%
dplyr::mutate(SVSPP = as.numeric(LW_SVSPP))
#Join survdat data with LW data
mergedata <- dplyr::left_join(fall, LWpar_spp, by= c('SEASON', 'SVSPP', 'sex'))
#filters out values without losing rows with NAs:
mergewt <- dplyr::filter(mergedata, is.na(INDWT) | INDWT<900)
mergewtno0 <- dplyr::filter(mergewt, is.na(INDWT) | INDWT>0.004)
mergelenno0 <- dplyr::filter(mergewtno0, is.na(LENGTH) | LENGTH>0)

# Join survdat data with LW data
mergedata <- dplyr::left_join(fall, LWpar_spp, by = c("SEASON", "SVSPP", "sex"))

# filters out values without losing rows with NAs:
mergewt <- dplyr::filter(mergedata, is.na(INDWT) | INDWT < 900)
mergewtno0 <- dplyr::filter(mergewt, is.na(INDWT) | INDWT > 0.004)
mergelenno0 <- dplyr::filter(mergewtno0, is.na(LENGTH) | LENGTH > 0)
mergelen <- dplyr::filter(mergelenno0, !is.na(LENGTH))
mergeindwt <- dplyr::filter(mergelen, !is.na(INDWT))
mergeLW <- dplyr::filter(mergeindwt, !is.na(lna))
Expand All @@ -76,44 +98,81 @@ species_condition <- function(data,
# !is.na(lna),
# INDWT < 900 | INDWT > 0.004,
# LENGTH > 0)

###########################################
### Calculate species condition ###

condcalc <- dplyr::mutate(mergeLW,
predwt = (exp(lna))*LENGTH^b,
RelCond = INDWT/predwt) |>
dplyr::filter(is.na(RelCond) | RelCond<300) %>%
predwt = (exp(lna)) * LENGTH^b,
RelCond = INDWT / predwt
) |>
dplyr::filter(is.na(RelCond) | RelCond < 300) %>%
dplyr::group_by(SVSPP, SEX) %>%
dplyr:: mutate(mean = mean(RelCond), sd = sd(RelCond)) |>
dplyr::mutate(mean = mean(RelCond), sd = sd(RelCond)) |>
dplyr::ungroup() |>
# might want to update this outlier removal eventually
dplyr::filter(RelCond < (mean+(2*sd)) & RelCond > (mean-(2*sd))) |>
dplyr::filter(is.na(sex) | sex != 4) %>%
dplyr::filter(RelCond < (mean + (2 * sd)) & RelCond > (mean - (2 * sd))) |>
dplyr::filter(is.na(sex) | sex != 4) %>%
dplyr::mutate(sexMF = sex)

cond.epu <- dplyr::left_join(condcalc, species.codes, by= c('SVSPP'))

#Summarize annually by EPU
condition <- cond.epu %>%
dplyr::group_by(Species, EPU, YEAR) %>%
dplyr::summarize(MeanCond = mean(RelCond),
nCond = dplyr::n()

cond.epu <- dplyr::left_join(condcalc, species.codes, by = c("SVSPP"))

# Summarize annually -- parameterized groupings
grouping_vars <- c("Species", "YEAR", "EPU")

if (by_sex) {
grouping_vars <- c(grouping_vars, "sexMF")
}

if (!is.null(length_break)) {
cond.epu <- cond.epu |>
dplyr::mutate(
length_group = cut(LENGTH, breaks = length_break, include.lowest = TRUE)
)
grouping_vars <- c(grouping_vars, "length_group")
}

grouped_condition <- cond.epu |>
dplyr::group_by(!!!rlang::syms(grouping_vars))

condition <- grouped_condition %>%
dplyr::summarize(
MeanCond = mean(RelCond),
nCond = dplyr::n()
) |>
dplyr::ungroup() |>
dplyr::filter(nCond >= 3) %>%
dplyr::add_count(Species, EPU) %>%
dplyr::filter(n >= 20) %>%
dplyr::select(Species, EPU, YEAR, MeanCond, nCond) %>%
dplyr::group_by(Species, EPU) |>
dplyr::mutate(sd = sd(MeanCond, na.rm = TRUE),
variance = var(MeanCond, na.rm = TRUE),
INDICATOR_NAME = "mean condition") %>%
dplyr::rename(DATA_VALUE = MeanCond) %>%
dplyr::filter(nCond >= 3) |>
# select columns
dplyr::select(dplyr::all_of(c(grouping_vars, "MeanCond", "nCond"))) |>
# group again, without YEAR
dplyr::group_by(!!!rlang::syms(grouping_vars[-which(grouping_vars == "YEAR")])) |>
# filter to only species with 20+ years of data
dplyr::mutate(n = dplyr::n()) |>
dplyr::filter(n >= 20) |>
dplyr::select(-n) |>
# calculate sd and variance across years
dplyr::mutate(
sd = sd(MeanCond, na.rm = TRUE),
variance = var(MeanCond, na.rm = TRUE),
INDICATOR_NAME = "mean condition"
) %>%
# dplyr::rename(DATA_VALUE = MeanCond) %>%
dplyr::ungroup()


# format for different outputs
if (output == "soe") {
condition <- condition |>
dplyr::select(YEAR, Species, EPU, MeanCond) |>
dplyr::rename(
Var = Species,
Time = YEAR,
Value = MeanCond
) |>
dplyr::mutate(Units = "MeanCond")
} else if (output == "esp") {
condition <- condition |>
dplyr::select(Species, EPU, YEAR, MeanCond, INDICATOR_NAME) |>
dplyr::rename(DATA_VALUE = MeanCond)
}
return(condition)
}



34 changes: 28 additions & 6 deletions R/create_netcdf.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,32 @@
#' @export

esp_csv_to_nc <- function(
data,
fname
) {
data,
fname) {
data <- data |>
dplyr::rename(creator_name = CONTACT) |> # rename for metadata requirements
dplyr::mutate(
long_name = INDICATOR_NAME,
title = paste(INTENDED_ESP_NAME, "ESP"),
Conventions = "CF-1.11, COARDS, ACDD-1.3",
Metadata_Conventions = "Unidata Dataset Discovery v1.0",
creator_email = "nefsc.esp.leads@noaa.gov",
creator_type = "person",
acknowledgements = "The data are sponsored by NOAA and may be freely distributed",
institution = "DOC | NOAA | National Marine Fisheries Service | Fisheries Northeast Fisheries Science Center",
program = "Ecosystem Dynamics and Assessment Branch",
publisher_type = "institution",
publisher_name = "Northeast Fisheries Science Center",
publisher_url = "https://www.fisheries.noaa.gov/about/northeast-fisheries-science-center",
publisher_email = "nefsc.erddap@noaa.gov",
contributor_name = "Ecosystem Dynamics and Assessment Branch",
contributor_type = "group",
contributor_url = "https://www.fisheries.noaa.gov/new-england-mid-atlantic/ecosystems/northeast-ecosystem-dynamics-and-assessment-our-research",
contributor_institution = "DOC | NOAA Fisheries | Northeast Fisheries Science Center",
naming_authority = "gov.noaa.nefsc",
license = "The data may be used and redistributed for free but is not intended for legal use, since it may contain inaccuracies. Neither the data, contributor, NEFSC, NOAA, nor the United States Government, nor any of their employees or contractors, makes any warranty, expressed or implied, including warranties of marketability and fitness for a particular purpose, or assumes any legal liability for the accuracy, completeness, or usefulness of this information."
)

var.index <- data |>
dplyr::select(INDICATOR_NAME, UNITS) |>
dplyr::distinct()
Expand Down Expand Up @@ -118,10 +141,9 @@ esp_csv_to_nc <- function(

## input data = long format indicator time series of all variables

# data <- AKesp::get_esp_data("Black Sea Bass") |>
# dplyr::filter(INDICATOR_NAME != "BSB_Winter_Bottom_Temperature_North")
# data <- AKesp::get_esp_data("Black Sea Bass")
#
# esp_csv_to_nc(data = data, fname = here::here("data-raw", "bsb_example3.nc"))
# esp_csv_to_nc(data = data, fname = here::here("data-raw", paste0(Sys.Date(), "_bsb_example.nc")))

# Check the contents of the NetCDF file

Expand Down
Loading