## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----from_AMPTSV2,warning=FALSE-----------------------------------------------
# load library
library(bgfanalyzer)

# load the example reactor layout
RL <- LabscaleBiogasLayout

# inspect reactor layout
RL

# import the CFS data to a BGF
myBGF <- from_AMPTSV2_report(ReactorLayout = RL, 
                             BlankLabel = "Blank",
                             name = "CFS data",
                             ProcessTemp = 42,
                             InocToSubRatio = 2,
                             path = system.file("extdata","AMPTSV2.csv",package="bgfanalyzer"))

# inspect BGF
myBGF

## ----subset-------------------------------------------------------------------
# inspect reactor layout of BGF
get_ReactorLayout(myBGF) # Layout 4, 5, 6, 7, 8, 9 have 'S1' in their layout, 10, 11, 12, 13, 14, 15 have 'S2'

# create a subset BGF by selecting individual reactors
S1_subsetBGF <- subset_BGF(myBGF,
                           reactor = c("R1","R2","R4","R5","R6","R7","R8","R9"), # <<-- # specify reactors by their ID in 'BioGasData$reactor' (= rownames of 'metaData'-layer)
                           name = "S1 subset") # <<-- # this line creates a new 'ExpParam$name'

# create a subset BGF by selecting individual reactor layout
S2_subsetBGF <- subset_BGF(myBGF,
                           layout = c("Blank","S2 ctrl","S2 4d","S2 6d"), # <<-- # specify reactors by their 'metaData$Layout' value
                           name = "S2 subset") # <<-- # this line creates a new 'ExpParam$name'



## ----sub-setting-result,fig.width=5-------------------------------------------
# original yield boxplot
bgf_plot(myBGF,type = "yield_box")

# S1 yield boxplot
bgf_plot(S1_subsetBGF,type = "yield_box")

# S2 yield boxplot
bgf_plot(S2_subsetBGF,type = "yield_box")


## ----MeasurementType----------------------------------------------------------
# print the measurement type of the BGF
myBGF$ExpParam$MeasurementType

## ----Alternative-BGF----------------------------------------------------------
# we take the same specifications as for the cal to from_AMPTSV2_report()
# the first that happens when from_AMPTSV2_report() is called is a call of BGF()
altBGF <- BGF(ReactorLayout = RL,
              BlankLabel = "Blank",
              name = "Step-by-step CFS",
              ProcessTemp = 42,
              InocToSubRatio = 2,
              MeasurementType = "AMPTSV2") # <<-- # The 'MeasurementType' is set during object creation

## ----add_bmp_measurement------------------------------------------------------
# adding data to alternative BGF
altBGF2 <- add_bmp_measurement(x = altBGF,
                              path = system.file("extdata","AMPTSV2.csv",package="bgfanalyzer"),
                              mode = altBGF$ExpParam$MeasurementType)

# inspect result
altBGF2

## ----add_bmp_measurement-details----------------------------------------------
# import the external file to an R list
ExFile <- read_raw_AMPTSV2_report(system.file("extdata","AMPTSV2.csv",package="bgfanalyzer"))

# add data from 'ExFile' to 'ExpParam'-layer of altBGF  
altBGF <- add_ExpPara(x = altBGF,rawReport = ExFile)
altBGF <- add_ExpSetup(x = altBGF,rawReport = ExFile)
altBGF <- sort_AMPTSV2_reactors(x = altBGF,rawReport = ExFile)

# inspect result
altBGF

## ----plot-works,fig.width=7,fig.height=5--------------------------------------
# try to plot altBGF
plot(altBGF)


## ----bgf_plot-Error,fig.width=7,fig.height=5----------------------------------
# try to plot altBGF
bgf_plot(altBGF,type = "product")


## ----cols_to_num--------------------------------------------------------------
# convert columns in 'BioGasData'-layer to numeric data type
altBGF <- cols_to_numeric(altBGF)


## ----bgf_plot_now_working,fig.width=7,fig.height=5----------------------------
# try to plot altBGF again
bgf_plot(altBGF,type = "product")


## ----close_gaps,fig.width=7,fig.height=5--------------------------------------
# Close gaps in 'BioGasData$product'
altBGF <- close_gaps(altBGF)

# See the plot now
bgf_plot(altBGF,type = "product")

## ----correct-production-------------------------------------------------------
# correct NA's in BioGasData$production
altBGF$BioGasData$production[grep(T,is.na(altBGF$BioGasData$production))] <- 0

## ----netGas-------------------------------------------------------------------
# calculate the netGas
altBGF <- netGas(altBGF)


## ----rel_production-----------------------------------------------------------
# calculate the relative production
altBGF <- relative_production(altBGF)


## ----calc_yield---------------------------------------------------------------
# calculate yield
altBGF <- calc_yield(altBGF)


## ----summarize_yield,warning=FALSE--------------------------------------------
# create a yield summary
altBGF <- summarize_yield(altBGF)

# inspect the final BGF
altBGF

