R/sen_match_date.R

Defines functions sen_match_date

Documented in sen_match_date

#' functions to match the closest local sentinel image to a date
#'
#' This function allows you to merge several GEDI files
#' @param sen_inpath characterstring to folder with all files that should be merged (2B level)
#' @param date date as character string or date YYYY-mm-dd
#'
#' @keywords date sentinel match
#' @export
#' @examples
#' sen_match_date()
#'
#' @import rGEDI

# date <- "2019-04-22"
# date <- "2019-06-25"
# sen_inpath <- "C:/Users/Alice/Uni/Projekte/GEDI/data/Sentinel"

sen_match_date <- function(sen_inpath, date){

  ########################################################################################
  ### Settings
  ########################################################################################
  date_form <- as.Date(date)

  sen_dirs <- list.dirs(path = sen_inpath, recursive = F, full.names = F)

  sen_date_lst <- lapply(setNames(sen_dirs, sen_dirs), function(i){
    # i <- sen_dirs[[1]]
    splt <- strsplit(i, "_")
    chr_date <- substr(splt[[1]][7], start = 1, stop = 8)
    sen_date <- as.Date(strptime(chr_date, format = "%Y%m%d"))
    return(sen_date)
  })
  sen_date_v <- do.call(c, sen_date_lst)
  match_dir <- which(abs(sen_date_v-date_form) == min(abs(sen_date_v-date_form)))
  return(match_dir)
  }
envima/GEDItools documentation built on July 25, 2020, 5:13 p.m.