R/copy_input.R

Defines functions copy_input

#' copy_input
#' 
#' Function to copy input files to their destination folders
#'  
#' @param x Filepath or data frame containing the mapping of files to be deleted
#' @param sourcepath Path to folder containing all input files
#' @param suffix suffix that might be part of input names that should be deleted
#' @param move If TRUE files will be moved instead of copied (default=FALSE)
#' @export
#' @author Jan Philipp Dietrich, David Klein


copy_input <- function(x, sourcepath, suffix=NULL, move=FALSE) {
  if(!requireNamespace("magclass", quietly = TRUE)) stop("The package magclass is required for copying files!")
  if(is.character(x)) {
    if(!file.exists(x)) stop("Cannot find file mapping!")
    map <- read.csv(x, sep = ";", stringsAsFactors = FALSE)
  } else {
    map <- x
  }
  x <- map$file
  names(x) <- map$destination
  if(move) {
    cat("\nStart moving input files:\n")
  } else {
    cat("\nStart copying input files:\n")
  }
  
  nmax <- max(nchar(x))
  for(i in 1:length(x)) {
    outputpath <- path(names(x)[i],x[i])
    if(file.exists(outputpath)) file.remove(outputpath)
    inputpath <- paste0(sourcepath,"/",x[i])
    if(!file.exists(inputpath)) {
      inputpath <- Sys.glob(sub("^(.*)\\.[^\\.]*$", paste0(sourcepath,"/\\1_",suffix,".*"), x[i]))
      if(length(inputpath)>1) {
        stop("Problem determining the proper input path for file", x[i], "(more than one possible path found)")
      } else if(length(inputpath)==0) {
        warning("File ", x[i]," seems to be missing!")
        cat("   ",format(x[i],width=nmax)," ->  FAILED!\n")
        next
      }
    }
    magclass::copy.magpie(inputpath, outputpath, round=8)
    if(move & !(i != length(x) &  (x[i] %in% x[i+1:length(x)]))) {
      file.remove(inputpath)
    }
    cat("   ",format(inputpath,width=nmax)," -> ",outputpath, "\n")
  }
  cat("\n")
}
pik-piam/lucode documentation built on June 10, 2020, 6:55 p.m.