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

## ----setup--------------------------------------------------------------------
# load package
library(bgfanalyzer)

# create a BGF
myBGF <- BGF(ReactorLayout = c("A","B"),MeasurementType = "manuel")

# print it to the console
myBGF

## ----inspect-ExpParam---------------------------------------------------------
# print ExpParam-layer
print(myBGF$ExpParam)

## ----modify_ExpParam----------------------------------------------------------
# set a value for ProcessTemp
myBGF$ExpParam$ProcessTemp=52 # 52°C is a good temperature for thermophilic anaerobic digestion

# alternatively use alter_whatever() to modify existing entries in ExpParam
myBGF <- alter_whatever(myBGF,layer = "ExpParam",what = "name",value = "manualBGF")

# new parameters can be added using add_ExpParam()
myBGF <- add_ExpParam(myBGF,what = c("timeScale"="Day"))

# inspect the changes
print(myBGF$ExpParam)

## ----list-metaData------------------------------------------------------------
# inspect the current metaData
myBGF$metaData

## ----add-VS-------------------------------------------------------------------
# add VS concentration
myBGF <- add_metaData(myBGF,what = c(2.8,1.9),lab = "cVS")

# inspect changes
myBGF$metaData

## ----blank-A------------------------------------------------------------------
# change the status of Fermentation A to Blank=TRUE
myBGF <- alter_whatever(myBGF,layer = "metaData",
                        what = "Blank",value = TRUE,ID = "R1")

# inspect change
myBGF$metaData

## ----inoc_Matrix--------------------------------------------------------------
# calculate a VS-based inoculation matrix
InocMatrix <- calc_inoc_matrix_from_metaData(myBGF,col = 4,reactor = 5000) 
# reactor = 5000 for a 5L reactor
InocMatrix

## ----add-VS-mass--------------------------------------------------------------
# add inoculum mass for each fermentation
myBGF <- add_metaData(myBGF,
                      what = as.numeric(InocMatrix["Inoculum",])*(myBGF$metaData["R1","cVS"]/100),
                      lab = "mInoc")

# add substrate mass for each fermentation
myBGF <- add_metaData(myBGF,
                      what = as.numeric(InocMatrix["Substrate",])*(myBGF$metaData["R2","cVS"]/100),
                      lab = "mSub")


## ----initial-value-FRA--------------------------------------------------------
# add a first value
myBGF <- add_BG_measurement(myBGF,
                            reactor = "R1",
                            time = 0,
                            col = "product",
                            measurement = 0)

# have a look at BioGasData upon value addition
myBGF$BioGasData


## ----add-further-values-to-FRA------------------------------------------------
# set seed
set.seed(656)

# generate a vector with 30 random numbers between 0.85 and 1.15
gas <- runif(30,0.85,1.15)

# convert these random numbers to hypothetical gas counter values
## assume 120 ml gas per value
gas <- gas * 120
## accumulate 'daily production'
for(i in c(2:length(gas))) gas[i]=c(gas[i]+gas[i-1])

# add hypothetical gas counter values to BGF
for(i in c(1:length(gas))) myBGF <- add_BG_measurement(myBGF,
                            reactor = "R1",
                            time = i,
                            col = "product",
                            measurement = gas[i])

# look at the end of BioGasData-layer to see the effect
tail(myBGF$BioGasData)

## ----initial-value-FRB--------------------------------------------------------
# add a first value
myBGF <- add_BG_measurement(myBGF,
                            reactor = "R2",
                            time = 0,
                            col = "product",
                            measurement = 0)

# look at the end of BioGasData-layer to see the effect
tail(myBGF$BioGasData)

## ----add-further-values-to-FRB------------------------------------------------
# final product around
final_p = 25000

# set max slope
max_slope = 0.31

products <- final_p * (1 - exp(-max_slope * (0:(length(gas)))))
# convert the random numbers to hypothetical gas counter values
gas = products[c(2:31)]

for(i in c(1:length(gas))) myBGF <- add_BG_measurement(myBGF,
                            reactor = "R2",
                            time = i,
                            col = "product",
                            measurement = gas[i])

# look at the end of BioGasData-layer to see the effect
tail(myBGF$BioGasData)

## ----data-processing----------------------------------------------------------
# convert standard parameters to numeric
myBGF <- cols_to_numeric(myBGF,c(2:7))

# ensure internal logic of the BGF
myBGF <- update_BGF(myBGF)

# calculate production
myBGF <- calculate_flow_from_volume(myBGF)

# and relative production
myBGF <- relative_production(myBGF)

# calculate net gas (subtract Blank)
myBGF <- netGas(myBGF,purity = 0.6,pos = 5)

# calculate yield
myBGF <- calc_yield(myBGF,pos = 6)

# summarize yield
myBGF <- summarize_yield(myBGF)

# print the BGF to the console
myBGF

