R/read_dbem_ens.R

Defines functions read_dbem_ens

Documented in read_dbem_ens

#' Reads DBEM files from the ensemble member modeling with the GFDL
#'
#' This function loads the DBEM files from the GFDL 8.5 ensemble member from the CORU-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 user/id will not 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". Note that for now model is set to GFDL since is the only one we have
#' @param rcp expects "26" for RCP 2.6-low emission scenario and "85" for RCP 8.5-high emission scenario. For now set to 85 as we do not have other
#' @param ensemble Expects the ensemble number. Note, it wont load all ensembles at a time.
#' @param data_type expects Catch or Abd data
#' @param path Expects the computer head path before DROBO. Do not include /
#' @return A tibble with the data
#' @export
#' 
read_dbem_ens <- function(taxon_key,
                          year,
                          model = "GFDL",
                          rcp = 85,
                          ensemble,
                          data_type,
                          path,
                          my_path = FALSE
){
  
  # ----------------#
  # Functions needed
  # ----------------#
  library <- c("data.table","dplyr")
  lapply(library, require, character.only = TRUE)
  
  # ----------------#
  #  Error messages for global variables
  # ----------------#
  
  if(model != "GFDL" |
     rcp != 85 | 
     ensemble > 112 |
     ensemble < 102){
    print("Oh-oh, some variables are wrong. We only have ten ensemble members (102 to 111) results for the GFDL under the RCP 8.5 scenario from the years 1951 to 2099.")
    stop()
    return(df)
  }
  
  # ----------------#
  # Set paths
  # ----------------##
  
  # Double check path provided is correct
  
  if(my_path == TRUE){
    
    D_Path <- paste(path,"mpa0F1ENS",ensemble,"/",taxon_key,"/",taxon_key,data_type,year,".txt",
                    sep="")
    
  }else{
    
    # Main path where I store my data
    if(Sys.info()[7] == "carmelia"){
      dbem_path <- "/Volumes/HALL2000/Data/DBEM"
    }
    if(Sys.info()[7] == "jepa88"){
      dbem_path <- "Z:/Data/DBEM"
    }
    if(Sys.info()[7] == "hall1000"){
      dbem_path <- "/Volumes/DATA/DATA/DBEM"
    }
    
    D_Path <- paste(dbem_path,"/mpa0F1ENS",ensemble,"/",taxon_key,"/",taxon_key,data_type,year,".txt",
                    sep="")
  }
  
  if(file.exists(dbem_path) == "FALSE"){
    print(paste("Oh-oh, looks like your path is wrong. Path:",dbem_path))
    stop()
  }
  #----------------------------#

  
  
  # ----------------#
  #### Importing data
  # ----------------#
  
  # Step to make sure data for that species in that year 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))
    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") %>%
        select(-column_label)
      
      df <- df %>% mutate(data_type=data_type,
                          taxon_key = taxon_key,
                          ensemble = ensemble
      )
    }
  } else {
    df <- tibble()
  }
  
  # Function result
  # ----------------#
  
  return(df)
}
jepa/dbemImport documentation built on March 18, 2021, 2:34 p.m.