R/read_files.R

Defines functions read_files

Documented in read_files

#' Read LiCOR Files
#'
#' This function reads LiCOR files generated by a LiCOR 7500 analyzer and organizes
#' them into lists based on the specified patterns.
#'
#' @description
#' Generate a list of LiCOR files used in the NEE (Net Ecosystem Exchange) or
#' ET (Evapotranspiration) measurements.
#'
#' @usage
#' read_files(path, photo = "photo", resp = "resp", ambient = "a")
#'
#' @param path A character string representing the path to the folder containing
#' the LiCOR files to be analyzed.
#' @param photo A character string representing the pattern to identify
#' photosynthesis measurement files.
#' @param resp A character string representing the pattern to identify respiration
#' measurement files.
#' @param ambient A character string representing the pattern to identify ambient
#' flux measurement files.
#'
#' @return A list containing three elements: 'photo_names' for photosynthesis
#' files, 'ambient_names' for ambient flux files, and 'resp_names' for respiration
#' files.
#'
#' @details
#' This function reads LiCOR files based on the specified patterns for photosynthesis,
#' respiration, and ambient flux measurements.
#'
#' If the 'path' parameter is missing, the function will prompt the user to set the
#' working directory to the folder containing the LiCOR files.
#'
#'
#' @examples
#' # Example 1: Read LiCOR files from the current working directory interactively.
#' # li_cor_data <- read_files()
#'
#' # Example 2: Read LiCOR files from a specified directory with custom file patterns.
#' # li_cor_data <- read_files("path/to/files",
#' # photo = "photosynth",
#' # resp = "resp_flux",
#' # ambient = "amb")
#'
#' @export
read_files <- function(path, photo = "photo", resp = "resp", ambient = "a") {

  if (missing(path)) {
 readline("Please set the working directory to the folder\n that contains the LiCOR files to be analyzed.\n Do so with the upcoming prompt. Note that you must choose a file in the folder that\n you want to set as the working directory. \nPlease press 'return' to continue.")
    files <- list.files(dirname(file.choose()), full.names = TRUE)
  } else {
    files <- list.files(path, full.names = TRUE)
  }

  photo.names <- grep(paste0("[^", resp, "].txt$"),
                      grep(paste0("[^_", ambient, "]\\.txt$"),
                           files, value = TRUE), value = TRUE)
  # Identify ambient flux measurement files
  # ambient.names <- grep(paste0(ambient, ".txt"), files, value = TRUE)
  ambient_pattern <- paste0(ambient, ".txt$")
  ambient.names <- grep(ambient_pattern, files, value = TRUE)

  # Identify respiration measurement files
  # resp.names <- grep(paste0(resp, ".txt"), files, value = TRUE)
  respiration_pattern <- paste0(resp, ".txt$")
  resp.names <- grep(respiration_pattern, files, value = TRUE)
  # Check if matching files exist
  if (length(photo.names) == 0) {
    message("No matching photo files found.")
  }
  if (length(resp.names) == 0) {
    message("No matching resp files found.")
  }
  if (length(ambient.names) == 0) {
    message("No matching ambient files found. First second of photo files will be used.")
  }


  licor_files <- list(photo_names = photo.names,
                      ambient_names = ambient.names,
                      resp_names = resp.names)
  invisible(licor_files)
}
PaulESantos/co2fluxtent documentation built on Nov. 7, 2023, 7:59 p.m.