R/get_rainfall_for_polygons.R

Defines functions get_rainfall_for_polygons

Documented in get_rainfall_for_polygons

#' Extract the rainfall for a \code{data.frame} of polygons
#'
#' @description This function builds on the results of \code{slide_dates_in_polygon} which returns a df
#' where each row is a polygon with a slide that is uniquely indentifiable by its \code{poly_id} and the
#' \code{date}
#'
#'
#' @importFrom doParallel registerDoParallel
#' @importFrom foreach foreach %dopar%
#' @importFrom future availableCores
#'
#'
#'
#' @param landslidepoints_in_poly Object of type \code{sf} as returned by \code{slide_dates_in_polygon}
#'
#'
#' @export

get_rainfall_for_polygons = function(landslidepoints_in_poly,
                                     days_back = 5,
                                     funs = c("mean"),
                                     nc_var = "precipitation",
                                     parallel = T,
                                     ncores = NULL,
                                     data_path = "\\\\projectdata.eurac.edu/projects/Proslide/PREC_GRIDS_updated/") {
  # if parallel
  if (parallel) {

    # the number of cores
    if (is.null(ncores)) {
      nc = availableCores() - 4
    } else{
      nc = ncores
    }

    # setup the workers
    registerDoParallel(nc)


    # run the function
    res = foreach(
      i = 1:nrow(landslidepoints_in_poly),
      .combine = rbind,
      .packages = c("rainfallR",
                    "dplyr",
                    "magrittr",
                    "stringr")
    ) %dopar% {
      # get the date for the slides in that slope unit
      date = landslidepoints_in_poly[i,]$date

      # the spatial object is the slope unit in the for loop
      spatial.obj = landslidepoints_in_poly[i, ]

      # lets look back 5 days
      days_back = days_back

      r = ex_rainfall(
        data_path = data_path,
        spatial.obj = spatial.obj,
        fun = funs,
        date = date,
        nc_var = nc_var,
        days_back = days_back
      )

      r
    }

    return(res)

  }

}
RobinKohrs/rainfallR documentation built on Oct. 3, 2021, 1:42 a.m.