R/Load.R

Defines functions LoadOther LoadLGR

Documented in LoadLGR LoadOther

#' @title Load LGR data
#'
#' @description A function used to load raw data from LGR Ultraportable Gas Analyzers to be called in the functions
#' \code{\link{SelCue}} and \code{\link{FluxCal}}. This function removes the extra lines without data at the end of the data file output
#' by LGR analyzer and convert the timestamps into POSIXct for further processing.
#'
#' @param file A string contains the directory path and file name of the raw data.
#' The data should be provided as what is exported from the LGR analyzer.
#' @param time_format A string indicates the format of timestamps. Default: "mdy_HMS".
#' It also takes the format "dmy_HMS" or "ymd_HMS", depending on the timestamps output from the analyzer.
#' @param Ta_ignore A logic value indicates whether air temperature in the dataset is ignored.
#' Default: FALSE. If this is set to "TRUE", the column name of the air temperature will not be checked, but
#' air temperature data will have to be added to the argument \code{df_Ta} in the function \code{\link{FluxCal}}.
#' @return A data frame with all data in the right format for the functions \code{\link{SelCue}} and \code{\link{FluxCal}}.
#'
#' @examples
#' library(FluxCalR)
#' # get the directory of the example LGR raw data
#' example_data <- system.file("extdata", "Flux_example_1_LGR.txt", package = "FluxCalR")
#' # load the data
#' Flux <- LoadLGR(example_data)
#'
#' @export
## Function for loading data from LGR ----------
LoadLGR <- function(file,
                    time_format = "mdy_HMS",
                    Ta_ignore = FALSE
){
  # argument check
  time_format <- match.arg(time_format,c("mdy_HMS","dmy_HMS","ymd_HMS"))
  # Load the raw data after deleting the text massages at the beginning and the end of each raw data file generate by LGR
  flux <- utils::read.table(file, sep= ",", header = TRUE,skip = 1,fill = TRUE,stringsAsFactors = FALSE)
  flux <- subset(flux,!is.na(flux[,2])) # remove the comments generated by LGR
  # replace the time with "lubridated" time with different format
  flux$Time <- try(suppressWarnings(lubridate::parse_date_time(flux$Time,orders = time_format)),
                   silent = TRUE)
  # error if time column is note correctly referred to
  if (class(flux$Time)[1]=="try-error" | is.na(flux$Time)[1]){
    stop("Error: check if the 'time_format' argument is correct!")
  }

  # check the CO2 column names
  flux$X.CO2.d_ppm <- try(flux[,"X.CO2.d_ppm"],silent = TRUE)
  # if column name is not found, try find "wet" CO2 instead
  if (class(flux$X.CO2.d_ppm[1])=="character" | class(flux$X.CO2.d_ppm[1])=="try-error"){
    flux$X.CO2.d_ppm <- try(flux[,"X.CO2._ppm"],silent = TRUE)
  }
  # error if column name is not found
  if (class(flux$X.CO2.d_ppm[1])=="character" | class(flux$X.CO2.d_ppm[1])=="try-error"){
    stop("Column names do not match! Use 'LoadOther()' function to load data.")
  }

  # check the CH4 column names
  flux$X.CH4.d_ppm <- try(flux[,"X.CH4.d_ppm"],silent = TRUE)
  # if column name is not found, try find "wet" CH4 instead
  if (class(flux$X.CH4.d_ppm[1])=="character" | class(flux$X.CH4.d_ppm[1])=="try-error"){
    flux$X.CH4.d_ppm <- try(flux[,"X.CH4._ppm"],silent = TRUE)
  }
  # error if column name is not found
  if (class(flux$X.CH4.d_ppm[1])=="character" | class(flux$X.CH4.d_ppm[1])=="try-error"){
    stop("Column names do not match! Use 'LoadOther()' function to load data.")
  }

  # check the air temperature column name if it is not ignored
  if (Ta_ignore == FALSE) {
    flux$AmbT_C <- try(flux[,"AmbT_C"],silent = TRUE)
    # error if column name is not found
    if (class(flux$AmbT_C[1])=="character" | class(flux$AmbT_C[1])=="try-error"){
      stop("Column names do not match! Use 'LoadOther()' function to load data.")
    }
  }

  return(flux)
}
##----------------------

#' @title Load data from other analyzers
#'
#' @description Function used to load raw data output from other similar analyzers (e.g. LICOR-7810) to be called
#' in the functions \code{\link{SelCue}} and \code{\link{FluxCal}}. This function extract the data (i.e., CO2 and/or CH4 concentration and ambient
#' temperature) required to calculate the fluxes and converts the timestamps into POSIXct for further processing.
#'
#' @param file A string contains the directory path and file name of the raw data.
#' @param time A string indicates column name of the timestamps in the data file.
#' @param time_format A string indicates the format of timestamps. Default: "mdy_HMS".
#' Other format, "dmy_HMS" or "ymd_HMS", can also be taken, depending on the data timestamp from the analyzer.
#' In case that the date and time are recorded separately, "HMS" can be used here and add the date to the "date_ms" argument.
#' @param date_ms A string indicates date of the measurements in the format of \emph{"mm/dd/yyyy"}.
#' This argument is used only when "HMS" has assigned to the argument \code{time_format}.
#' @param CO2 A string indicates column name of the CO2 concentrations in the data file. Default: no CO2 is measured.
#' Note that at least one of \code{CO2} and \code{CH4} argument has to be specified.
#' @param CH4 A string indicates column name of the CH4 concentrations in the data file. Default: no CH4 is measured.
#' Note that at least one of \code{CO2} and \code{CH4} argument has to be specified.
#' @param Ta A string indicates column name of ambient air temperature in the data file. If not measured, a constant
#' value can be used. Default: 25 (unit: degree C).
#' @param skip A integer indicates rows to be skipped at the beginning of the data file. Default: 0, no rows will be skipped.
#' @param sep A string indicates delimiter that separates columns. Default: ",".
#' @param ... Other arguments used in the function \code{\link[utils]{read.table}}.
#'
#' @return A dataframe with all data in the right format for functions \code{\link{SelCue}} and \code{\link{FluxCal}}.
#'
#' @examples
#' library(FluxCalR)
#' # get the directory of the example data
#' example_data <- system.file("extdata", "Flux_example_2_other.csv", package = "FluxCalR")
#' # load the data
#' Flux <- LoadOther(file = example_data,
#'                   time = "Date_time",
#'                   time_format = "mdy_HMS",
#'                   CO2 = "CO2_PPM",
#'                   Ta = "Tem_C")
#'
#' @export
## Function for loading data from other sources ----------
LoadOther <- function(file,
                      time,
                      time_format="mdy_HMS",
                      date_ms = NULL,
                      CO2 = NULL,
                      CH4 = NULL,
                      Ta = 25,
                      skip = 0,
                      sep = ",",
                      ...
)
{
  # argument check
  time_format <- match.arg(time_format,c("mdy_HMS","dmy_HMS","ymd_HMS","HMS"))
  # define the pipe from the package "magrittr"
  `%>%` <- magrittr::`%>%`
  # Load the raw data after deleting the text massages at the beginning and the end of each raw data file
  flux1 <- utils::read.table(file, sep= sep, header = TRUE,skip = skip,fill = TRUE,stringsAsFactors = FALSE,...)
  flux1 <- subset(flux1,!is.na(flux1[,2])) # remove the comments generated by analyzer
  # replace the time with "lubridated" time with different format
  if (time_format=="HMS"){ # if only time is provided, then combine the time with date first
    Time <- try(paste0(date_ms,"_",flux1[,time]),silent = TRUE) # if only time is provided, then combine the time with date first
    Time <- try(lubridate::mdy_hms(Time),silent = TRUE)
  } else { # if it is full date format
    Time <- suppressWarnings(try(lubridate::parse_date_time(flux1[,time],
                                                            orders = time_format),
                                 silent = TRUE))
  }

  # error if time column is note correctly referred to
  if (class(Time)[1]=="try-error" | is.na(Time)[1]){
    stop("Error: check if the 'time', 'time_format' and/or 'date_ms' arguments are correct!")
  }
  flux <- data.frame(Time)
  # error if no column of CO2 or CH4 is specified
  if (is.null(CO2) & is.null(CH4)){
    stop("Error: at least one column for CO2 or CH4 has to be specified!")
  }
  # add a column as CO2 concentration
  if (!is.null(CO2)){
    X.CO2.d_ppm <- try(flux1[,CO2],silent = TRUE)
    # error if column name is not found
    if (class(X.CO2.d_ppm[1])=="try-error" | class(X.CO2.d_ppm[1])=="character"){
      stop("Error: check if column name for CO2 is correct!")
    }
    flux <- data.frame(flux,X.CO2.d_ppm)
  }
  # add a column as CH4 concentration
  if (!is.null(CH4)){
    X.CH4.d_ppm <- try(flux1[,CH4],silent = TRUE)
    # error if column name is not found
    if (class(X.CH4.d_ppm[1])=="try-error" | class(X.CH4.d_ppm[1])=="character"){
      stop("Error: check if column name for CH4 is correct!")
    }
    flux <- data.frame(flux,X.CH4.d_ppm)
  }
  # add a column as ambient air temperature
  if (assertthat::is.string(Ta)){
    AmbT_C <- flux1[,Ta]
  } else {
    AmbT_C <- Ta
  }
  flux <- data.frame(flux,AmbT_C)

  return(flux)
}
junbinzhao/R-Package-FluxCalR documentation built on May 25, 2021, 5:49 a.m.