FluxCal: Calculate CO2 and CH4 gas fluxes

Description Usage Arguments Value Examples

View source: R/FluxCal.R

Description

A function to calculate CO2 and CH4 gas fluxes from the data loaded by the function LoadLGR or LoadOther. It takes a time cue data frame (argument df_cue), either created by the function SelCue or prepared by the user following the format of example files "Time_&_Ta_1.csv" and "Time_&_Ta_2.csv" at https://github.com/junbinzhao/FluxCalR/tree/master/inst/extdata to separate the measurements and then calculate the fluxes for all the measurements at once. Note that the header for the time cue column must be either "Start" or "End". Based on the time cues and window width provided for the calculation, the function will automatically scan over data that cover 1.5x (default) length of the window and calculate the fluxes based on the best linear regression (i.e., largest R2). After the calculations are done, a graph with regression lines plotted on the CO2 and/or CH4 concentration time series can be drawn for checkup purposes.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
FluxCal(
  data,
  win,
  vol,
  area,
  pa = 1,
  cal = "CO2_CH4",
  df_cue,
  cue_type = "End",
  other = NULL,
  df_Ta = NULL,
  f = NULL,
  ext = 1.5,
  metric = "R2",
  output = "Flux_output.csv",
  digits = 3,
  check_plot = TRUE
)

Arguments

data

A dataframe generated by the function LoadLGR or LoadOther.

win

A number indicates the window width for the flux calculation, unit: minutes.

vol

A number indicates volume of the chamber; unit: dm^3 or l.

area

A number indicates base area of the chamber; unit: m^2.

pa

A number indicates the air pressure during measurements; unit: atm. Default: 1.

cal

A string, either "CO2_CH4" (default),"CO2" or "CH4", indicates which gas flux it is calculated for.

df_cue

A data frame that includes "Start" and/or "End" time (HH:MM:SS) of each measurement. The header for the time must be either "Start" or "End". This data frame can either be created by the function SelCue or be prepared by the user (see example files "Time_&_Ta_1.csv" and "Time_&_Ta_2.csv" at https://github.com/junbinzhao/FluxCalR/tree/master/inst/extdata).

cue_type

A string, either "Start", "End" (default) or "Start_End", indicates if start, end or both time in the data frame assigned to df_cue will be used as the cues . When "Start_End" is chosen, both "Start" and "End" columns have to be present in the data frame for df_cue and the flux with the largest R2 within the range will be calculated without considering the argument ext.

other

A vector of strings indicates the names of other columns in the data frame for the df_cue argument that need to be passed along to the final output data frame. Default: NULL.

df_Ta

A data frame contains a column "Ta" with the air temperature values (ideally, this is temperature measured inside of the chamber during the flux measurement; unit: degree C. This can be the same data frame as in df_cue. See example files "Time_&_Ta_1.csv" and "Time_&_Ta_2.csv" at https://github.com/junbinzhao/FluxCalR/tree/master/inst/extdata). Note the row number of the data frame must be the same as the number of flux measurements. Default: NULL, then the temperature used is either the average ambient air temperature measured by the LGR analyzer (column "AmbT_C") or, if the data measured by other analyzers, Ta input from function LoadOther.

f

A number indicates the sampling (recording) frequency of the data (unit: second). If not provided (default), the frequency is computed based on the difference of timestamps of the first two rows in the data.

ext

A number indicates a range of how many times of the window width (win) should the calculation scan through to choose the regression with the largest R2. Default: 1.5. This argument is ignored when df_cue is "Start_End".

metric

A string to indicate the evaluation metric for selecting the regression. Possible options: "R2" (default) and "RMSE".

output

A string includes output directory and file name (.csv) to export the calculated fluxes. Default: a file named "Flux_output.csv" with calculated fluxes will be created under the current work directory. FALSE, do not create a file.

digits

An integer indicates the number of decimal digits to be used for the calculated fluxes and slopes.

check_plot

A logic value indicates whether a checking plot should be drawn after the calculation. Default: TRUE.

Value

A data frame with 9 columns, including number of measurement ("Num"), date of measurement ("Date"), start and end time for each flux calculation ("Start" and "End"), gas name ("Gas", either CO2 or CH4), slope ("Slope") and R2 ("R2") of the regressions, air temperature used for calculation ("Ta") and the calculated fluxes ("Flux", unit: umol m-2 s-1). As default, a copy of the data frame will be saved as "Flux_output.csv" under the work directory (or as what is provided in the argument output) and a graph with regression lines plotted on the CO2 and/or CH4 concentration time series will pop up for checkup purposes.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
## Not run: 
library(FluxCalR)
#### data from LGR
# get the directory of the example LGR raw data
example_data1 <- system.file("extdata", "Flux_example_1_LGR.txt", package = "FluxCalR")
example_data1

# load the data
Flux_lgr <- LoadLGR(file = example_data1,
                    time_format = "mdy_HMS")

# manually select the end of each measurement as time cues
time_cue <- SelCue(Flux_lgr,flux = "CO2",cue = "End",save = FALSE)

# calculate the fluxes over a 3-minute window using the manually selected cues
Flux_output1 <- FluxCal(data = Flux_lgr,
                        win = 3,
                        vol = 208, area = 0.26,
                        df_cue = time_cue,
                        cue_type = "End",
                        output = FALSE) # don't create a output file
Flux_output1

## input the time cues from a prepared file and calculate the fluxes over a 3-minute window
# get directory of the example file with time cues and Ta
Example_cue1 <- system.file("extdata", "Time_&_Ta_1.csv", package = "FluxCalR")
Time_Ta1 <- read.csv(Example_cue1)
# this is how this file should look like
head(Time_Ta1)
Flux_output2 <- FluxCal(data = Flux_lgr,
                        win = 3,
                        vol = 208, area = 0.26,
                        df_cue = Time_Ta1,
                        cue_type = "Start_End", # use both start and end time as time cues
                        other = c("Plot","Light_Dark"), # pass other columns into the final output
                        df_Ta = Time_Ta1) # use separately measured air temperature for calculation
Flux_output2

#### data from other sources
# get the directory of the example data
example_data2 <- system.file("extdata", "Flux_example_2_other.csv", package = "FluxCalR")
example_data2

# load the data
Flux_other <- LoadOther(file = example_data2,
                        time = "Date_time",
                        time_format = "mdy_HMS",
                        CO2 = "CO2_PPM",
                        Ta = "Tem_C")

## input the time cues from a prepared file and calculate the fluxes over a 3-minute window
# get directory of the example file with time cues and Ta
Example_cue2 <- system.file("extdata", "Time_&_Ta_2.csv", package = "FluxCalR")
Time_Ta2 <- read.csv(Example_cue2)
head(Time_Ta2)
Flux_output3 <- FluxCal(data = Flux_other,
                        cal = "CO2", # only calculate CO2 flux
                        win = 3,
                        vol = 208, area = 0.26,
                        df_cue = Time_Ta2,
                        cue_type = "Start",
                        other = c("Plot","Light_Dark"),
                        output = FALSE) # don't create a output file
Flux_output3

## End(Not run)

junbinzhao/R-Package-FluxCalR documentation built on May 25, 2021, 5:49 a.m.