#' Reads DBEM files
#'
#' This function loads the DBEM files from DROBO. You have to be connected to the UBC network
#' and have access to the CORU-DROBO in order to use the function. Note that no user/id will be
#' required but the path wont be found.
#'
#' @param taxon_key is the species to load as taxon key number.
#' @param year expects a year of sequence of years to load the data from 1951 to 2099
#' @param model is the ESM models to load; GFDL; IPSL, MPI. For all models select "All"
#' @param rcp expects "26" for RCP 2.6-low emission scenario and "85" for RCP 8.5-high emission scenario
#' @param data_type select Catch or Abd data
#' @param path Expects the head computer path before DROBO. Note: do not include /
#' @param my_path If you have a completely different path for the data. Note that you still have to respect "model/rcp/taxonkey/" structure
#' @return A tidy table -tiddy- with the DBEM data selected
#' @export
#'
read_dbem <- function(taxon_key,
year,
model = "All",
rcp,
data_type,
path,
my_path = FALSE
){
# ----------------#
# Packages needed
# ----------------#
library <- c("tibble","dplyr")
lapply(library, require, character.only = TRUE)
# ----------------#
# Set paths
# ----------------#
if(my_path == TRUE){
if(file.exists(path) == "FALSE"){
print(paste("Oh-oh, looks like your path is wrong. Path:",path))
stop()
}
if(model == "All"){
D_Path <- c(paste(path,"/GFDL",rcp,"/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep=""),
paste(path,"/IPSL",rcp,"/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep=""),
paste(path,"/MPI",rcp,"/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep="")
)
}else{
D_Path <- paste(path,"/",model,rcp,"/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep="")
}
}else{ # Else from my_path = T
# Double check path provided is correct
dbem_path <- paste(path,"/DATA/DATA/DBEM/",sep="")
# dbem_path <- paste(path,sep="")
if(file.exists(dbem_path) == "FALSE"){
print(paste("Oh-oh, looks like your path is wrong. Path:",dbem_path))
stop()
}
if(model == "All"){
D_Path <- c(paste(dbem_path,"/GFDL",rcp,"F1/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep=""),
paste(dbem_path,"/IPSL",rcp,"F1/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep=""),
paste(dbem_path,"/MPI",rcp,"F1/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep="")
)
}else{
D_Path <- paste(dbem_path,"/DBEM/",model,rcp,"F1/",taxon_key,"/",taxon_key,data_type,year,".txt",
sep="")
}
} # end else of my_path
# ----------------#
#### Importing data
# ----------------#
# Step to make sure data for that species exists
if(file.exists(D_Path[1])){
cur <- lapply(D_Path, FUN=data.table::fread, na.strings="NA")
}else{
print(paste("Oh-oh, we have no data for for taxon key",taxon_key))
print(D_Path)
df <- tibble()
return(df)
}
if(length(cur)>0){
cur <- cur[sapply(cur, function(d) nrow(d) >= 1)]
colnames <- c("index", "value")
cur <- lapply(cur, setNames, colnames)
df <- bind_rows(cur, .id = "column_label")
if(nrow(df)>0){
frame_key <- tibble(column_label = seq(1,length(unlist(year)),1),
"year"=year) %>%
mutate(column_label=as.character(column_label))
df <- left_join(df, frame_key,
by="column_label") %>%
dplyr::select(-column_label)
df <- df %>% mutate(data_type=data_type,
taxon_key = taxon_key,
model = model,
rcp = rcp
)
}
} else {
df <- tibble()
}
# Function result
# ----------------#
return(df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.