R/interpolation_wrapper.R

Defines functions interpolation_wrapper

Documented in interpolation_wrapper

#' Wrapper function around interpolate_variable()
#'
#' This function interpolates gear temperature to calculate cold pool area using all candidate interpolation methods and outputs rasters of temperature and estimates of Cold Pool Area. Output includes the best
#'
#' @param temp_data_path Filepath to temperature csv file generated by get_data()
#' @param proj_crs CRS string to use for interpolation as a character vector (CRS should have units of meters)
#' @param cell_resolution Interpolation grid cell dimension in meters.
#' @param select_years Optional. Select years to use for interpolation. Default NULL uses all years.
#' @param select_region Region for interpolation as a character string. Options = "ebs", "sebs", "nbs"
#' @param methods Optional character vector of methods to pass to interpolate_variable(). Valid choices: "NN", "IDW", "IDW4", "Exp", "Sph", "Bes", "Gau", "Cir", "Mat", "Ste", "Tps". If NULL, uses defaults.
#' @return Data frame of estimated cold pool area from multiple candidate interpolation methods.
#' @export

interpolation_wrapper <- function(temp_data_path,
                                  proj_crs,
                                  cell_resolution,
                                  select_years = NULL,
                                  interp_variable,
                                  select_region = "sebs",
                                  methods = NULL) {
  
  # temp_data_path = ebs_csv_path
  # proj_crs = proj_crs
  # cell_resolution = 5000 # 5x5 km grid resolution
  # select_years = 1982:2022
  # interp_variable = "gear_temperature"
  # select_region = "sebs"
  # methods = NULL

  if(!(class(temp_data_path) == "data.frame")) {
    
    temperature_df <- read.csv(file = temp_data_path,
                               stringsAsFactors = FALSE)
    
    names(temperature_df) <- tolower(names(temperature_df))
    
  }
  
  if(is.null(methods)) {
    methods <- c("NN", "IDW", "IDW4", "Exp", "Sph", "Bes", "Gau", "Cir", "Mat", "Ste", "Tps")
  }

  # Vector of years ----
  year_vec <- sort(unique(temperature_df$year))

  if(!is.null(select_years)) {
    year_vec <- year_vec[year_vec %in% select_years]
  }

  # Calculate cold pool area and generate rasters
  for(ii in 1:length(year_vec)) {

    cpa_year <- interpolate_variable(dat = dplyr::filter(temperature_df, year == year_vec[ii]),
                                     dat.year = year_vec[ii],
                                     in.crs = "EPSG:4326",
                                     interpolation.crs = proj_crs,
                                     cell.resolution = cell_resolution,
                                     lon.col = "longitude",
                                     lat.col = "latitude",
                                     var.col = interp_variable,
                                     nm = Inf,
                                     pre = paste0("_", toupper(interp_variable), "_", year_vec[ii]),
                                     select.region = select_region,
                                     methods = methods)

  }

}
afsc-gap-products/coldpool documentation built on Feb. 25, 2024, 9:44 p.m.