Code Monkey home page Code Monkey logo

gofluxyourself's Introduction

GoFluxYourself: A user-friendly way to calculate GHG fluxes yourself, regardless of user experience

One Package to rule them all

Non-steady state chambers are widely used for measuring soil greenhouse gas (GHG) fluxes, such as CO2, CH4, N2O, and water vapor (H2O). While linear regression (LM) is commonly used to estimate GHG fluxes, this method tends to underestimate the pre-deployment flux (f0). Indeed, a non-linearity is expected when gas concentration increases inside a closed chamber, due to changes in diffusion gradients between the soil and the air inside the chamber. In addition, lateral gas flow and leakage contribute to non-linearity. Many alternative to LM have been developed to try and provide a more accurate estimation of f0, for instance the method of Hutchinson and Mosier (HM) (1981), which was implemented in the HMR package by Pedersen et al., 2010. However, non-linear models have a tendency to largely overestimate some flux measurements, due to an exaggerated curvature. Therefore, the user is expected to decide which method is more appropriate for each flux estimate. With the advent of portable greenhouse gas analyzers (e.g. Los Gatos Research (ABB) laser gas analyzers), soil GHG fluxes have become much easier to measure, and more efficient ways to calculate these flux estimates are needed in order to process large amounts of data. A recent approach was developed by Hüppi et al., 2018 to restrict the curvature in the HM model for a more reliable flux estimate. In the HM model, the curvature is controlled by the kappa parameter. Hüppi et al. suggested the use of the kappa.max to limit the maximal curvature allowed in the model (see their R package gasfluxes, available on the CRAN). This procedure introduces less arbitrary decisions in the flux estimation process.

Previous software that have been developed to calculate GHG fluxes were limited in many aspects that this package is meant to overcome. Most were limited to the linear regression approach (e.g. LI-COR SoilFluxPro, Flux Puppy, and the R packages flux and FluxCalR), others were compatible with only one designated system (e.g. LI-COR SoilFluxPro, Flux Puppy), and some were impractical with large amounts of measurements (e.g. the R package HMR) or simply did not include data pre-processing (e.g. the R packages HMR, flux and gasfluxes).

This new R package, GoFluxYourself is meant to be “student proof”, meaning that no extensive knowledge or experience is needed to import raw data into R, chose the best model to calculate fluxes (LM, HM or no flux), quality check the results objectively and obtain high quality flux estimates. The package contains a wide range of functions that allows the user to import raw data from a variety of instruments (LI-COR, LGR, GAIA2TECH and Picarro); calculate fluxes from a variety of GHG (CO2, CH4, N2O, and H2O) with both linear (LM) and non-linear (HM) flux calculation methods; align instruments clocks after data import; interactively identify measurements (start and end time) if there are no automatic chamber recordings (e.g. LI-COR smart chamber); plot measurements for easy visual inspection; and quality check the measurements based on objective criteria and non-arbitrary thresholds.

Three R packages for the Elven-kings under the CRAN,
Seven for the Dwarf-lords in their halls of open software,
Nine for Mortal Men doomed to write scripts themselves,
One for the Dark Lady on her dark throne
In the Land of GitHub where the Shadows lie.
One Package to rule them all, One Package to find them,
One Package to bring them all and in the darkness bind them
In the Land of GitHub where the Shadows lie.

About the package

This package GoFluxYourself is meant to be “student proof”, meaning that no extensive knowledge or experience is needed to import raw data into R (except for knowing how to use R, of course), chose the best model to calculate fluxes (LM or HM, that is the question. -Shakespeare, 1603), quality check the results objectively (hence the no experience needed) and obtain high quality flux estimates from static chamber measurements (wonderful!).

The package contains a wide range of functions that lets the user import raw data from a variety of instruments:

After import, the user can chose from two methods for identification of measurements:

  • Manual identification of measurements - based on start.time, provided separately in an auxiliary file. The function obs.win() splits the imported data into a list of data frame (divided by UniqueID) and creates an observation window around the start.time to allow for a manual selection of the start and end points of each measurements, using the function click.peak.loop().
  • Automatic selection of measurements - based on automatic recordings of chamber opening and closing from an instrument such as the LI-COR Smart Chamber or the GAIATECH Automated ECOFlux chamber.

The function goFlux() calculates fluxes from a variety of greenhouse gases (CO2, CH4, N2O, and H2O) using both linear (LM) and non-linear (Hutchinson and Mosier) flux calculation methods.

Following flux calculation, the user can select the best flux estimate (LM or HM) based on objective criteria and non-arbitrary thresholds, using the best.flux() function:

  • Assumed non-linearity: If all criteria are respected, the best flux estimate is assumed to be the non-linear estimate from the Hutchinson and Mosier model.
  • G-factor: the g-factor is the ratio between the result of a non-linear flux calculation model (e.g. Hutchinson and Mosier; HM) and the result of a linear flux calculation model (Hüppi et al., 2018). With the best.flux() function, one can chose a limit at which the HM model is deemed to overestimate (f0). Recommended thresholds for the g-factor are <4 for a flexible threshold, <2 for a medium threshold, or <1.25 for a more conservative threshold. The default threshold is g.limit = 2. If the g-factor is above the specified threshold, the best flux estimate will switch to LM instead of HM.
  • Minimal Detectable Flux: The minimal detectable flux (MDF) is based on instrument precision and measurements time (Christiansen et al., 2015). Under the MDF, the flux estimate is considered null, but the function will not return a 0 to avoid heteroscedasticity of variances. There will simply be a comment in the columns “LM.diagnose” or “HM.diagnose” saying that there is “No detectable flux (MDF)”.
  • Kappa max: The parameter kappa determines the curvature of the non-linear regression in the Hutchinson and Mosier model. A large kappa, returns a strong curvature. A maximum threshold for this parameter, kappa-max, is calculated based on the minimal detectable flux (MDF), the linear flux estimate and the measurement time (Hüppi et al., 2018). The units of the kappa-max is s-1. This limit of kappa-max is included in the goFlux() function, so that the non-linear flux estimate cannot exceed this maximum curvature. In the function best.flux(), one can chose the linear flux estimate over the non-linear flux estimate based on the ratio between kappa and kappa-max. The ratio is expressed as a percentage, where 1 indicates HM.k = k.max, and 0.5 indicates HM.k = 0.5*k.max. The default setting is k.ratio = 1.
  • Statistically significant flux (p-value): This criteria is only applicable to the linear flux. Under a defined p-value, the linear flux estimate is deemed non-significant, i. e., no flux. The default threshold is p.val = 0.05.
  • Coefficient of determination (r2): This criteria is used as a warning of a potentially “bad measurement”. No best flux estimate is chosen based on r2, but a warning will be given in the column “quality.check” saying “Check plot (r2 < 0.6)”, for example. The default threshold is r2 = 0.6.
  • Relative Standard Error: The delta method is used to propagate the total error of the flux calculation (deltamethod() from the msm package). The standard error is then divided by the flux estimate to obtain the relative standard error (%). This criteria is used as a warning of a potentially “bad measurement”. No best flux estimate is chosen based on SE.rel, but a warning will be given in the column “quality.check” saying “Check plot (SE > 5%)”, for example. The default threshold is SE.rel = 5.
  • Intercept: If the initial gas concentration (C0) calculated for the non-linear flux estimate is more or less than 20% of the linear regression’s intercept, then a warning is issued in the “quality.check” column saying “Intercept out of bounds (HM)”. Alternatively, one can provide boundaries for the intercept, for example: intercept.lim = c(380, 420) for a true C0 of 400 ppm. If no limits are provided, only the non-linear regression’s intercept can be tested. If a limit is provided, both intercepts are tested.

Finally, after finding the best flux estimates, one can plot the results and visually inspect the measurements using the function flux.plot() and save the plots as pdf using flux2pdf().

How to use

Here is a simple example on how to use the package with a single raw file from LGR gas analyzers.

Installation

To install a package from GitHub, one must first install the package remotes from the CRAN:

if (!require("remotes", quietly = TRUE))
  install.packages("remotes")

Then, install the GoFluxYourself package from GitHub:

remotes::install_github("Qepanna/GoFluxYourself")

The functioning of the package depends on many other packages (data.table, dplyr, ggnewscale, ggplot2, graphics, grDevices, grid, gridExtra, lubridate, minpack.lm, msm, pbapply, plyr, purrr, rjson, rlist, SimDesign, stats, tibble, tidyr, utils), which will be installed when installing GoFluxYourself. If required, it is recommended to update any pre-installed packages.

Import raw data into R

library(GoFluxYourself)

# Get the example raw file from the package
file.path <- system.file("extdata", "LGR/example_LGR.txt", package = "GoFluxYourself")

# Import raw data from an LGR gas analyzer
?LGR_import
example_LGR_imp <- LGR_import(inputfile = file.path)

Note that raw data can also be imported from a multitude of other instruments, and example data files are provided for all of them:

  • LI-COR: LI-6400, LI-7810, LI-7820, LI-8100, LI-8200 (smart chamber)
  • Los Gatos Research instruments: (e.g. UGGA and m-GGA)
  • GAIA2TECH (DMR) automated chamber ECOFlux
  • Picarro: G2508

To import multiple files from a folder, use the wrapper function import2RData().

# Get help for import functions from the GoFluxYourself package
?G2508_import
?GAIA_import
?LI6400_import
?LI7810_import
?LI7820_import
?LI8100_import
?LI8200_import
?import2RData

Manual identification of measurements

In this example, the start.time for each measurement (UniqueID) was noted manually in the field, and are provided in an auxiliary file (auxfile).

# Other usefull packages
require(dplyr)
require(purrr)

# The auxfile requires start.time and UniqueID
# start.time must be in the format "%Y-%m-%d %H:%M:%S"
aux.path <- system.file("extdata", "LGR/example_LGR_aux.txt", 
                        package = "GoFluxYourself")
auxfile <- read.delim(aux.path) %>% 
  mutate(start.time = as.POSIXct(start.time, tz = "UTC"))

When running the function click.peak(), for each measurement, a window will open, in which you must click on the start point and the end point. The observation window is based on the start.time given in the auxfile, the length of the measurement (obs.length), and a shoulder before start.time and after start.time + obs.length. In this example, the observation time is 3 minutes (180 seconds) and the shoulder is 30 seconds. Therefore, the observation window shows 30 seconds before the start.time and 210 seconds after.

?obs.win

# Define the measurements' window of observation
example_LGR_ow <- obs.win(inputfile = example_LGR_imp, auxfile = auxfile,
                          obs.length = 180, shoulder = 30)

Pay attention to the warning message given by obs.win() when there are more than 20 measurements (which is not the case in this example).

Note that for more than one gas measurement, it is better to use the function click.peak.loop() with lapply() rather than using click.peak() for each measurement individually.

?click.peak
?click.peak.loop

# Manually identify measurements by clicking on the start and end points
example_LGR_manID <- lapply(seq_along(example_LGR_ow), click.peak.loop, 
                            flux.unique = example_LGR_ow) %>%
  map_df(., ~as.data.frame(.x))

If the number of observation is under a certain threshold (warn.length = 60), a warning will be given after clicking on the start and end points as such:

Warning message: Observation length for UniqueID: 733_C_C is 59 observations

Otherwise, if the number of observation satisfies this threshold, then the following message is given instead:

Good window of observation for UniqueID: 733a_C_C

Between each measurement, the result of the click.peak() function is displayed for 3 seconds. To increase this delay, change the parameter sleep in the function click.peak.loop().

To convert the flux estimate’s units into nmol CO2/H2O m-2s-1 or µmol CH4/N2O m-2s-1, the temperature inside the chamber (Tcham; °C) and the atmospheric pressure inside the chamber (Pcham; kPa) are also required. If Pcham and Tcham are missing, normal atmospheric pressure (101.325 kPa) and normal air temperature (15 °C) are used.

Additionally, one must provide the surface area inside the chamber (Area; cm2) and the total volume in the system, including tubing, instruments and chamber (Vtot; L). If Vtot is missing, one must provide an offset (distance between the chamber and the soil surface; cm) and the volume of the chamber (Vcham; L). In that case, the volume inside the tubing and the instruments is considered negligible, or it should be added to Vcham.

The final output, before flux calculation requires: UniqueID, Etime, flag, Vtot (or Vcham and offset), Area, Pcham, Tcham, H2O_ppm and other gases.

# Additional auxiliary data required for flux calculation.
example_LGR_manID <- example_LGR_manID %>%
  left_join(auxfile %>% select(UniqueID, Area, Vtot, Tcham, Pcham))

Flux calculation

The hardest part is now behind you. All that’s left is to run the flux calculation with the goFlux() function. Then use the best.flux() function to select the best flux estimates (LM or HM) with our list of non-arbitrary thresholds, and plot the results for visual inspection.

# Flux calculation -------------------------------------------------------------
?goFlux
?best.flux

# Calculate fluxes for all gas types
CO2_results <- goFlux(example_LGR_manID, "CO2dry_ppm")
CH4_results <- goFlux(example_LGR_manID, "CH4dry_ppb")
H2O_results <- goFlux(example_LGR_manID, "H2O_ppm")

# Use best.flux to select the best flux estimates (LM or HM)
# based on a list of criteria
criteria <- c("g.factor", "kappa", "MDF", "R2", "SE.rel")

CO2_flux_res <- best.flux(CO2_results, criteria)
CH4_flux_res <- best.flux(CH4_results, criteria)
H2O_flux_res <- best.flux(H2O_results, criteria)

# Plots results ----------------------------------------------------------------
?flux.plot
?flux2pdf

# Make a list of plots of all measurements, for each gastype
CO2_flux_plots <- flux.plot(CO2_flux_res, example_LGR_manID, "CO2dry_ppm")
CH4_flux_plots <- flux.plot(CH4_flux_res, example_LGR_manID, "CH4dry_ppb")
H2O_flux_plots <- flux.plot(H2O_flux_res, example_LGR_manID, "H2O_ppm")

# Combine plot lists into one list
flux_plot.ls <- c(CO2_flux_plots, CH4_flux_plots, H2O_flux_plots)

# Save plots to pdf
flux2pdf(flux_plot.ls, outfile = "demo.results.pdf")

Here is an example of how the plots are saved as pdf:

You can then save the results as RData or in an Excel sheet to further process the results.

require(openxlsx)

# Save RData
save(CO2_flux_res, file = "CO2_flux_res.RData")
save(CH4_flux_res, file = "CH4_flux_res.RData")
save(H2O_flux_res, file = "H2O_flux_res.RData")

# Save to Excel
write.xlsx(CO2_flux_res, file = "CO2_flux_res.xlsx")
write.xlsx(CH4_flux_res, file = "CH4_flux_res.xlsx")
write.xlsx(H2O_flux_res, file = "H2O_flux_res.xlsx")

Community Guidelines

Authors: Karelle Rheault and Klaus Steenberg Larsen

To report problems, seek support or contribute to the package, please contact the maintainer, Karelle Rheault ([email protected]). Suggestions for new features or improvements are always welcome.

gofluxyourself's People

Contributors

qepanna avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.