####################################################################

# General function to load data for a specified MPA, data variable, and desired spatial scale
#   Constructs a filename based the specifications, opens the file, and filters to give the desired variable subset


# GENERAL FORMAT OF FILENAMES:
#   (note: the Shiny app will treat the working directory as the one where server.R is located)
#   For Shiny, filename = "../data/[variable_name]/[variable name]_[spatial_scale].csv"  
#   For prototype, filename = "CeNCOOS Data Views/dataview-prototype/data/[variable_name]/[variable name]_[spatial_scale].csv"  


# LIST OF DATA VARIABLE NAMES:
#   beuti : BEUTI Upwelling Index
#   enso: Multivariate ENSO Index v2 (NOAA)
#   cuti : CUTI Upwelling Index
#   kelp-biomass : Kelp Biomass (satellite)
#   pdo : NCEI PDO Index (NOAA)
#   roms-chl : Chlorophyll (West Coast ROMS Climate Run)
#   roms-npp : Net Primary Production (West Coast ROMS Climate Run)
#   roms-sst : Sea Surface Temperature (West Coast ROMS Climate Run)
#   sat-kd : KD490 for Turbidity (satellite/Kahru)
#   sat-npp : Net Primary Production (satellite/Kahru)
#   sat-sst : Sea Surface Temperature (satellite/Kahru)
#   wave-height : Significant Wave Height (CDIP MOP)
#   wind-direction : Wind Direction (COAMPS)
#   wind-speed : Wind Speed (COAMPS)

# LIST OF SPATIAL SCALES:
#   indiv-mpa
#   comb-mpas
#   bioregions

####################################################################


library(tidyverse)
library(lubridate)

####################################################################


# list("Sea Surface Temperature, Annual Mean" = "roms-sst-annualmean",
#      "Sea Surface Temperature, Annual Maximum" = "roms-sst-annualmax",
#      "Chlorophyll, Annual Mean" = "roms-chl-annualmean", 
#      "Significant Wave Hight" = "hs", 
#      "Upwelling (CUTI)" = "cuti", 
#      "Upwelling (BEUTI)" = "beuti")


####################################################################

loadVariable = function(mpa_name, data_variable, spatial_scale, site_ref = "../data/MPA_list.csv"){
  
  # build string of filepath from the input variables
  # load and filter the dataset (determine which bioregion the MPA is in)
  # return
  
  # assemble filepath
  # filepath = paste0("CeNCOOS Data Views/dataview-prototype/data/", data_variable, "/", data_variable, "_", spatial_scale, ".csv") # for test code
  filepath = paste0("../data/", data_variable, "/", data_variable, "_", spatial_scale, ".csv") # for shiny app
  
  # load data file
  datafile = read_csv(filepath)
  
  # load reference list
  mpa_list = read_csv(site_ref)
  
  # identify MPA bioregion
  bioregion_name = mpa_list$BIOREGION[mpa_list$MPA_NAME == mpa_name]
  
  # determine which scale of 
  
  if (spatial_scale == "indiv-mpas"){
    
    filtered_data = datafile %>% 
      filter(area == mpa_name)
    
  } else if (spatial_scale == "comb-mpas") {

    filtered_data = datafile %>%
      filter(area == paste0(bioregion_name, "_MPAs"))
    
  } else if (spatial_scale == "bioregions") {
    
    filtered_data = datafile %>%
      filter(area == bioregion_name)
  }
  
  # format everything as a date-time
  # should this be done at the processing stage or can I just make things into years here?
  
  if ("year" %in% names(filtered_data)) {
    
    filtered_data <- filtered_data %>%
      mutate(date = ymd(paste0(as.character(year), "0701"))) %>%
      select(-year)
  }
  

  return(filtered_data)
  
}
  
  
  
# test code
# loadVariable(mpa_name = "Pyramid Point SMCA", spatial_scale = "indiv-mpas",data_variable = "roms-sst-annualmean",
#              site_ref = "CeNCOOS Data Views/dataview-prototype/data/MPA_list.csv")

