knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
options(width = 100)

Background

Static chamber is one of the most commonly used approach for measuring greenhouse gas flux (e.g., CO2 and CH4) from ecosystems (e.g., stem/soil respiration, CH4 emission, etc.) for its easy implementation, high accuracy and low cost. To perform the measurements, commercial infrared gas analyzers (IRGA) are usually used to record the changes of greenhouse gas concentrations within a chamber that covers an area of interest (e.g., soil surface) at a certain temporal resolution (e.g., 1s). However, to calculate the fluxes from the measured gas concentrations can be tedious and subjective considering that the original dataset usually includes large amount of unwanted records for the periods between the measurements and that the window for calculating each flux has to be defined manually. Therefore, a tool that can efficiently calculate the concentration changes into fluxes in a more objective fashion is needed.

What does the package do?

The FluxCalR package directly takes the raw data exported by a gas analyzer (e.g. LGR Ultraportable Gas Analyzers) after chamber measurements and calculate fluxes of multiple measurements at once from the data file. Specifically, users can use the function LoadLGR() or LoadOther() to load the raw data file that includes the gas concentration records. Then, create a separate data frame that includes the time cues that each measurement starts and/or ends corresponding to the time in the raw data file. Based on the time cues, the fluxes of all the measurements can be calculated and exported using the function FluxCal(). The package has several features that facilitate the process of flux calculation:

Examples of using the package

Example 1: Calculate fluxes from LGR raw data (manually selected time cues)

First, we'll need to load the raw data file exported from LGR into the R program by the function LoadLGR(). The users just need to assign the directory (including the file name) and timestamp format. Then the function will take care the rest process and convert the data file into a data frame that can be used in the next steps. Here, we take the example data file "Flux_example_1_LGR.txt" comes with the package.

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 # check the directory

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

After loading the data, one can use the function SelCue() to manually identify the data for each of the measured fluxes within the entire dataset. After executing the function, a interactive graph of CO2 (or CH4, see the argument flux) concentration time series will pop up for the user to click on the corresponding points (either end or start of the measurements). Selecting the End (see the argument cue) points are recommended here because they are usually associated with sudden drops or increases in the gas concentrations when removing the chamber, making them easier to be identified than the starting points.

# manually select the end of each measurement as time cues
time_cue <- SelCue(Flux_lgr,flux = "CO2",cue = "End",save = F)
knitr::include_graphics("images/timecue.png") # add time cue graph with limited width
time_cue <- read.csv("Time_cue.csv")
# this is how the time cue data frame looks like
time_cue

After having the time cues saved as a data frame 'time_cue', we can do the calculations now using the function FluxCal()! In the function, there are 5 arguments that have to be assigned by the users without default: data (the data frame we got from LoadLGR()), win (the window size for flux calculation, unit: minute),vol (chamber volumn in l),area (chamber base area in m^2) and df_cue (the data frame we got from SelCue()). After the function is executed with the arguments assigned, the calculation will be done and a check-up graph with all the regression lines drawn on top of the gas concentration time series will pop up by default for the user to visually check if there is any mistake occured to the calcualtions.

# 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) # no output file
knitr::include_graphics("images/output.png") # add output graph with limited width
Flux_output1 <- FluxCal(data = Flux_lgr,
                        win = 3,
                        vol = 208,area = 0.26,
                        df_cue = time_cue,
                        cue_type = "End",
                        check_plot = FALSE,
                        output = FALSE) # no output file

This is how the output data frame looks like and the last column ("Flux") includes the calculated flux rates:

head(Flux_output1)

Example 2: Calculate fluxes from LGR raw data (load self-prepared time cues file)

In the second example, we use the same dataset as in Example 1 from LGR analyzer ('Flux_lgr'), but use a user self-prepared time cue file. A little background: depending on the protocols of the chamber measurements in the field, one may have recorded the starting and/or ending time of each measurement taken. This record can be entered into an excel sheet in the format following the time cue examples (files "Time_&Ta_1.csv" and "Time&_Ta_2.csv" at https://github.com/junbinzhao/FluxCalR/tree/master/inst/extdata) and loaded into R as the time cue data frame that assigned to the argument df_cue. If both the starting and ending time are recorded, the user can change the cue_type to "Start_End", so that the calculations will be restricted to the period betweenthem and best linear regression with the length of win will be used for flux calculation. In addition, if there are other meta data come along with each measurement (e.g. plot ID), they can also be added to the time cue file and pass to the calculated flux data frame by assigning the column names to the other argument.

# input the time cues from a prepared file and calculate the fluxes over a 3-minute window
Example_cue1 <- system.file("extdata", 
                            "Time_&_Ta_1.csv", # directory of the file with time cues and Ta
                            package = "FluxCalR") 
Time_Ta1 <- read.csv(Example_cue1)
# this is how the time cue file looks like
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 of each measurement as cues
                        other = c("Plot","Light_Dark"), # also pass other columns into the final output
                        df_Ta = Time_Ta1, # use separately measured air temperature for calculation
                        check_plot = FALSE, # don't show check-up plot
                        output = FALSE) # no ouput file
head(Flux_output2)

Example 3: Calculate fluxes from raw data of other analyzers

In the third example, we'll use a dataset exported from another analyzer. The logics behind the workflow is the same as in the Example 2, except that it uses LoadOther() to load the data instead. When loading the data, users need to assign the column names of the time and gas concentrations (CO2 and/or CH4) and ambient temperature in the dataset to the corresponding argument.

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

# 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
Example_cue2 <- system.file("extdata", 
                            "Time_&_Ta_2.csv", # directory of the file with time cues and Ta
                            package = "FluxCalR") 
Time_Ta2 <- read.csv(Example_cue2)
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"),
                        check_plot = FALSE, # don't show check-up plot
                        output = FALSE) # don't create a output file
head(Flux_output3)

Note that in the examples, all the arguments that control the output file (save in the function SelCue() and output in the function FluxCal()) have been set to 'FALSE' for the convenience of building the vignette file. However, to keep track of the time cue selections and reproduce the calculations, these arguments are recommended to keep as default ('TURE') or as designated directory.

For more details on the functions, please check the help documents (?LoadLGR(),?LoadOther(),?SelCue(),?FluxCal()).

To report problems, seek support or contribute, please contact the author Junbin Zhao (junbinzhao1985@gmail.com). Requests/suggestions for new features are also welcome.



junbinzhao/R-Package-FluxCalR documentation built on Dec. 11, 2020, 10:49 a.m.