Skip to content

Commit

Permalink
documentation examples updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaichas committed Jun 13, 2020
1 parent 359a167 commit fcff032
Show file tree
Hide file tree
Showing 32 changed files with 529 additions and 148 deletions.
4 changes: 2 additions & 2 deletions R/calc_age2length.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
#' columns to include the length bin information.
#'
#' @examples
#'
#' \dontrun{
#' directory <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' load(file.path(directory, "outputSETASrun_truth.RData"))
#'
#' lenout <- calc_age2length(
#' structn=result$structn, resn=result$resn, nums=result$nums,
#' biolprm = result$biolprm, fgs = result$fgs,
#' CVlenage = 0.1, remove.zeroes=TRUE)
#'
#' }
calc_age2length <- function(structn, resn, nums,
biolprm, fgs, maxbin = 150,
CVlenage = 0.1, remove.zeroes = TRUE) {
Expand Down
20 changes: 0 additions & 20 deletions R/calc_avgwtstage2age.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,6 @@
#' with the other functions
#'
#' @export
#'
#' @examples
#' dir <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' file_nc="outputSETAS.nc"
#' fgs=load_fgs(dir = dir, "functionalGroups.csv")
#' file_init="INIT_VMPA_Jan2015.nc"
#' bps=load_bps(dir = dir, fgs, file_init)
#' select_groups=fgs$Name[fgs$IsTurnedOn > 0]
#' select_variable="Nums"
#' box.info=load_box(dir = dir, file_bgm="VMPA_setas.bgm")
#' bboxes=get_boundary(box.info)
#' #when calc_stage2age is run in the run_truth, it will need to have the nums
#' #data frame and the bioprm already read in:
#' nums_data <- load_nc(dir = dir,
#' file_nc="outputSETAS.nc",
#' bps=bps, fgs=fgs, select_groups=select_groups,
#' select_variable = "Nums",
#' check_acronyms = TRUE, bboxes = bboxes)
#' biolprm <- load_biolprm(dir, file_biolprm="VMPA_setas_biol_fishing_Trunk.prm")
#' YOY <- load_yoy(dir, file_yoy="outputSETASYOY.txt")

## ACTUAL FUNCTION ##
calc_avgwtstage2age <- function(wtagecl, annages, fgs) {
Expand Down
2 changes: 1 addition & 1 deletion R/calc_biomass_age.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' # Set up the example with input files
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' fgs <- load_fgs(d, "functionalGroups.csv")
#' bps <- load_bps(dir = d, fgs = fgs,
#' bps <- load_bps(dir = d, fgs = "functionalGroups.csv",
#' file_init = paste0(tail(strsplit(d, "/")[[1]], 1), ".nc"))
#' runprm <- load_runprm(d, "VMPA_setas_run_fishing_F_Trunk.xml")
#' biolprm <- load_biolprm(dir = d,
Expand Down
2 changes: 1 addition & 1 deletion R/calc_pred_diet.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#' @examples
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' fgs <- load_fgs(d, "functionalGroups.csv")
#' bps <- load_bps(dir = d, fgs = fgs,
#' bps <- load_bps(dir = d, fgs = "functionalGroups.csv",
#' file_init = paste0(tail(strsplit(d, "/")[[1]], 1), ".nc"))
#' runprm <- load_runprm(d, "VMPA_setas_run_fishing_F_Trunk.xml")
#' dietcomp <- load_diet_comp(dir = d, file_diet = "outputSETASDietCheck.txt",
Expand Down
2 changes: 1 addition & 1 deletion R/calc_stage2age.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#' file_nc="outputSETAS.nc"
#' fgs=load_fgs(dir = dir, "functionalGroups.csv")
#' file_init="INIT_VMPA_Jan2015.nc"
#' bps=load_bps(dir = dir, fgs, file_init)
#' bps=load_bps(dir = dir, "functionalGroups.csv", file_init)
#' select_groups=fgs$Name[fgs$IsTurnedOn > 0]
#' select_variable="Nums"
#' box.info=load_box(dir = dir, file_bgm="VMPA_setas.bgm")
Expand Down
3 changes: 2 additions & 1 deletion R/calc_timestep2time.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
#' is returned.
#'
#' @examples
#' \dontrun{
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' runprm <-
#' load(file = file.path(d, "outputSETASrun_truth.RData"))
#' test <- calc_timestep2time(dir = d, data = result$biomass_ages,
#' file_runprm = "VMPA_setas_run_fishing_F_Trunk.xml")
#' rm(test)
#'
#' }
calc_timestep2time <- function(dir = getwd(), data, file_runprm){
if (!is.null(dir)) file_runprm <- file.path(dir, file_runprm)

Expand Down
5 changes: 3 additions & 2 deletions R/load_bioind.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#'
#' @examples
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' file <- "outputSETAScatch.txt"
#' test <- load_catch(dir = d, file_catch = file)
#' file <- "outputSETASBiomIndx.txt"
#' fgs <- load_fgs(dir = d, "functionalGroups.csv")
#' test <- load_bioind(dir = d, file_bioind = file, fgs = fgs)
load_bioind <- function(dir, file_bioind, fgs, verbose = FALSE) {
file.bioind <- file.path(dir, file_bioind)
truebio <- read.table(file.bioind, header = TRUE)
Expand Down
7 changes: 3 additions & 4 deletions R/load_bps.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#' Extracts the names of the epibenthic biomasspools
#' from the initial conditions file.
#'
#' Use \code{fgs} \code{data.frame} as read in by \code{\link{load_fgs}}
#' Use \code{file_fgs} \code{data.frame} (this function will call \code{load_fgs})
#' to get the biomass pool information.
#'
#' @template dir
#' @template fgs
#' @template file_fgs
#' @template file_init
#'
#' @author Alexander Keth
Expand All @@ -17,8 +17,7 @@
#'
#' @examples
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' fgs <- load_fgs(d, "functionalGroups.csv")
#' bps <- load_bps(dir = d, fgs = fgs, file_init = "INIT_VMPA_Jan2015.nc")
#' bps <- load_bps(dir = d, fgs = "functionalGroups.csv", file_init = "INIT_VMPA_Jan2015.nc")
#'
load_bps <- function(dir = getwd(), fgs, file_init){
if (strsplit(file_init, "\\.")[[1]][2] != "nc") {
Expand Down
6 changes: 4 additions & 2 deletions R/load_catch.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#'
#' @examples
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' file <- "outputSETAScatch.txt"
#' test <- load_catch(dir = d, file_catch = file)
#' file <- "outputSETASCatch.txt"
#' fgs <- load_fgs(dir = d, "functionalGroups.csv")
#' test <- load_catch(dir = d, fgs = fgs, file_catch = file)
load_catch <- function(dir, file_catch, fgs, verbose = FALSE) {
file.catch <- file.path(dir, file_catch)
catchbio <- read.table(file.catch, header = TRUE)
catchbio <- catchbio[, -grep("TsAct", colnames(catchbio))]

fgs <- fgs[fgs$IsTurnedOn > 0,]
fishedlookup <- fgs[fgs$IsFished > 0,]

names(catchbio)[match(fishedlookup$Code,names(catchbio))] <- fishedlookup$Name
Expand Down
2 changes: 1 addition & 1 deletion R/load_fisheries.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#'
#' @examples
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' fleets <- load_fisheries(d, "fisheries.csv")
#' fleets <- load_fisheries(d, "SETasFisheries.csv")
#' rm(fleets)
#'
load_fisheries <- function(dir = getwd(), file_fish){
Expand Down
61 changes: 30 additions & 31 deletions R/load_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ load_meta <- function(dir = getwd(), scenario, verbose = FALSE) {
warning(paste("The function load_meta does not allow dir = NULL\n",
"and dir will be set to\n", getwd(), "\nas determined using getwd()"))
}
data <- list()
mdata <- list()

# Main nc file
grepnc <- paste0("^output", scenario, "\\.nc")
grepnc <- paste0(scenario, "\\.nc")
file <- dir(dir, pattern = grepnc, full.names = TRUE)
connection <- RNetCDF::open.nc(file)
on.exit(RNetCDF::close.nc(connection))

# Get model name from the scenario argument
data$modelname <- scenario
mdata$modelname <- scenario

# Get Atlantis version number
file.log <- dir(dir, pattern = "^log", full.names = TRUE)
Expand All @@ -51,14 +51,14 @@ load_meta <- function(dir = getwd(), scenario, verbose = FALSE) {
"load_meta will use the following log file:\n", tail(file.log, 1)))
file.log <- tail(file.log, 1)
}
if (!file.exists(file.log)) {
if (length(file.log)==0) {
warning(paste("The log file does not exist in\n", dir, "\n",
"and consequently the metadata will list the version number",
"as NA."))
data$atlantisversion <- NA
mdata$atlantisversion <- NA
} else {
data$atlantisversion <- readLines(file.log, n = 2)[2]
data$atlantisversion <- gsub("#", "", data$atlantisversion)
mdata$atlantisversion <- readLines(file.log, n = 2)[2]
mdata$atlantisversion <- gsub("#", "", mdata$atlantisversion)
}

# Get number of years, timestep, output interval,
Expand All @@ -81,20 +81,20 @@ load_meta <- function(dir = getwd(), scenario, verbose = FALSE) {
warning(paste("The .prm file does not exist in\n", dir, "\n",
"and consequently the metadata will list the number of years,",
"timestep, and timestepunit as NA."))
data$toutstart <- NA
data$toutinc <- NA
data$toutfinc <- NA
data$tstop <- NA
data$nyears <- NA
data$timestep <- NA
data$timestepunit <- NA
data$outputstep <- NA
data$outputstepunit <- NA
data$hemisphere <- NA
data$nspp <- NA
data$nfleet <- NA
mdata$toutstart <- NA
mdata$toutinc <- NA
mdata$toutfinc <- NA
mdata$tstop <- NA
mdata$nyears <- NA
mdata$timestep <- NA
mdata$timestepunit <- NA
mdata$outputstep <- NA
mdata$outputstepunit <- NA
mdata$hemisphere <- NA
mdata$nspp <- NA
mdata$nfleet <- NA
} else { # Only go here if the prm file is available
data <- append(data,
mdata <- append(mdata,
load_runprm(dir = NULL, file.prm))
} # Close if for prm file

Expand All @@ -108,18 +108,17 @@ load_meta <- function(dir = getwd(), scenario, verbose = FALSE) {
warning(paste("The .bgm file does not exist in\n", dir, "\n",
"and consequently the metadata will list the number",
"of boxes and\n maximum depth as NA."))
data$nbox <- NA
data$depthmax <- NA
mdata$nbox <- NA
mdata$depthmax <- NA
} else { # Only go here if the bgm file is available
bgm <- readLines(file.bgm)
data$nbox <- grep("nbox", bgm, value = TRUE)
data$nbox <- gsub("nbox|[[:space:]]+", "", data$nbox)
data$nbox <- as.numeric(data$nbox)
mdata$nbox <- grep("nbox", bgm, value = TRUE)
mdata$nbox <- gsub("nbox|[[:space:]]+", "", mdata$nbox)

# Get maximum depth
data$depthmax <- grep("maxwcbotz", bgm, value = TRUE)
data$depthmax <- gsub("maxwcbotz|[[:space:]]+", "", data$depthmax)
data$depthmax <- as.numeric(data$depthmax)
mdata$depthmax <- grep("maxwcbotz", bgm, value = TRUE)
mdata$depthmax <- gsub("maxwcbotz|[[:space:]]+", "", mdata$depthmax)
mdata$depthmax <- as.numeric(mdata$depthmax)
} # Close if for bgm file

# Get the size of the study area in kilometers^2
Expand All @@ -130,8 +129,8 @@ load_meta <- function(dir = getwd(), scenario, verbose = FALSE) {
area <- volume[dim(volume)[1], , ] / dz[dim(dz)[1], 1, 1]
# If the assumption of the lowest box having a height of 1 m is false you
# would need to divide by the sum of meters of sediment here
data$area <- sum(area[2:dim(area)[1], 1]) / 1000000
data$areaunit <- "km^2"
mdata$area <- sum(area[2:dim(area)[1], 1]) / 1000000
mdata$areaunit <- "km^2"

invisible(data)
invisible(mdata)
}
2 changes: 1 addition & 1 deletion R/load_nc.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#' @examples
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' fgs <- load_fgs(d, "functionalGroups.csv")
#' bps <- load_bps(dir = d, fgs = fgs, file_init = "INIT_VMPA_Jan2015.nc")
#' bps <- load_bps(dir = d, "functionalGroups.csv", file_init = "INIT_VMPA_Jan2015.nc")
#' test <- load_nc(dir = d, file_nc = "outputSETASCATCH.nc",
#' fgs = fgs, bps = bps,
#' select_variable = "Catch",
Expand Down
37 changes: 13 additions & 24 deletions R/load_nc_annage.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@
#' @keywords gen
#' @author Alexander Keth, modified by Sarah Gaichas
#'
#' @examples
#' d <- system.file("extdata", "INIT_VMPA_Jan2015", package = "atlantisom")
#' fgs <- load_fgs(d, "functionalGroups.csv")
#' bps <- load_bps(dir = d, fgs = fgs, file_init = "INIT_VMPA_Jan2015.nc")
#' test <- load_nc(dir = d, file_nc = "outputSETASCATCH.nc",
#' fgs = fgs, bps = bps,
#' select_variable = "Catch",
#' select_groups = "Pisciv_T_Fish",
#' check_acronyms = TRUE)
#' str(test)
#' rm(test)
#'
#' @importFrom magrittr %>%
#' @export
Expand Down Expand Up @@ -65,7 +54,7 @@ load_nc_annage <- function(dir = getwd(), file_nc, file_fish, bps, fgs, biolprm,

# Check input of select_variable as only one value is allowed
select_variable <- match.arg(select_variable, several.ok = FALSE)

# select_variable gets renamed for fishery data, save input
input_select_variable <- select_variable

Expand Down Expand Up @@ -140,7 +129,7 @@ load_nc_annage <- function(dir = getwd(), file_nc, file_fish, bps, fgs, biolprm,
dplyr::mutate(maxage = nagecl * ageperagecl) %>%
dplyr::mutate(name = fgs$Name[match(code, fgs$Code)]) %>%
dplyr::filter(!is.na(maxage))

#limit to groups that have ages
select_groups <- select_groups[select_groups %in% maxage$name]

Expand Down Expand Up @@ -179,7 +168,7 @@ load_nc_annage <- function(dir = getwd(), file_nc, file_fish, bps, fgs, biolprm,
final_agecl <- maxage$maxage[
sapply(final_species, function(x) which(x == maxage$name))]

# Get final fleets for full age structure fishery output
# Get final fleets for full age structure fishery output
if(input_select_variable %in% c("Catch", "Discard")){
final_fleet <- fleetnames$Code[sapply(
lapply(select_variable, grepl, x = search_clean), any)]
Expand Down Expand Up @@ -249,22 +238,22 @@ load_nc_annage <- function(dir = getwd(), file_nc, file_fish, bps, fgs, biolprm,

int_fs <- final_species
int_fa <- final_agecl

if(input_select_variable %in% c("Catch", "Discard")){
# how many unique fleets per select_group in search_clean?
# lookup of species and fleet names
splitfleets <- search_clean %>%
str_replace(paste0("\\d+_*",input_select_variable,"_"), "-")
sp_fleet <- as.data.frame(unique(splitfleets)) %>%
rename(spfleet = "unique(splitfleets)") %>%
separate(spfleet, c("species", "fleet"), sep = "-")
separate(spfleet, c("species", "fleet"), sep = "-")
# lookup of number of fleets per species
sp_nfleet <- as.data.frame(table(sp_fleet$species)) %>%
rename(species = Var1, nfleet = Freq)
# for indexing fleets
int_ff <- sp_nfleet$nfleet
}

if (length(at_data3d) >= 1) {
# Remove biomasspools if selected variable is "N"!
if (select_variable == "N") {
Expand Down Expand Up @@ -322,18 +311,18 @@ load_nc_annage <- function(dir = getwd(), file_nc, file_fish, bps, fgs, biolprm,
}

# Order of the data in value column = "atoutput".
# 1. species --> rep each with the number of
# 1. species --> rep each with the number of
# ageclasses * fleets and n_timesteps * boxes
# 2. age --> rep each (1:maxage for each species) with n_timesteps * boxes
# 3. timestep --> rep each timestep (1:n_timesteps)
# with the number of boxes and final_agecl
# (num ages per species)
# 4. polygon --> rep boxes times n_timesteps * final_agecl
# (num ages per species)
# 5. fleet -->
# 5. fleet -->

if(input_select_variable %in% c("Nums", "Weight")){

result2d <- data.frame(species = unlist(sapply(
X = mapply(FUN = rep, x = int_fs, each = int_fa, SIMPLIFY = FALSE,
USE.NAMES = FALSE),
Expand All @@ -349,18 +338,18 @@ load_nc_annage <- function(dir = getwd(), file_nc, file_fish, bps, fgs, biolprm,
stringsAsFactors = F)
if (select_variable == "N") result2d$layer <- n_layers - 1
}

#should now work properly for multiple fleets
if(input_select_variable %in% c("Catch", "Discard")){
result2d <- data.frame(species = unlist(sapply(X = mapply(FUN = rep, x = int_fs,
result2d <- data.frame(species = unlist(sapply(X = mapply(FUN = rep, x = int_fs,
each = (int_fa * int_ff),SIMPLIFY = FALSE,USE.NAMES = FALSE),
FUN = rep, each = length(boxes) * n_timesteps, simplify = FALSE)),
agecl = unlist(sapply(X = sapply(X = rep(int_fa, int_ff), FUN = seq, from = 1,
by = 1, simplify = FALSE, USE.NAMES = FALSE),
FUN = rep, each = (length(boxes) * n_timesteps), simplify = FALSE)),
polygon = unlist(sapply(X = n_timesteps * int_fa * int_ff,
FUN = rep, x = boxes, simplify = FALSE, USE.NAMES = FALSE)),
fleet = unlist(sapply(X = mapply(FUN = rep, x = sp_fleet$fleet,
fleet = unlist(sapply(X = mapply(FUN = rep, x = sp_fleet$fleet,
each = (rep(int_fa, int_ff)),SIMPLIFY = FALSE,USE.NAMES = FALSE),
FUN = rep, each = (length(boxes) * n_timesteps), simplify = FALSE)),
time = unlist(sapply(X = int_fa * int_ff , FUN = rep, x = rep(0:(n_timesteps - 1),
Expand All @@ -369,7 +358,7 @@ load_nc_annage <- function(dir = getwd(), file_nc, file_fish, bps, fgs, biolprm,
stringsAsFactors = F)
#if (select_variable == "N") result2d$layer <- n_layers - 1
}

}

# Combine dataframes if necessary!
Expand Down
Loading

0 comments on commit fcff032

Please sign in to comment.