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

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

# We build a BGF from
myBGF<-from_standard_record(ReactorLayout = "Substrat A",
                     ProcessTemp = 80,
                     InocToSubRatio = 0.1,
                     path = system.file("extdata","Fermentation_A.tsv",package = "bgfanalyzer"),
                     time_col = 1,
                     product_col = 3,
                     BlankLabel = "Blank",
                     name = "myBGF",
                     units = "days")
# inspect object
myBGF

# have a closer look at its 'metaData'-layer
myBGF$metaData

## ----headTail-----------------------------------------------------------------
# look at the first six rows of the 'BioGasData'-layer
head(myBGF$BioGasData)

# look at a snippet of the last six rows of the 'BioGasData'-layer
tail(myBGF$BioGasData[c(1,2,3,(length(myBGF$BioGasData)-c(2,1,0)))])

## ----gasq_A-------------------------------------------------------------------
# import the gas quality measurements stored in a separate file
gasq_A <- import_standard_record(ipath = system.file("extdata","gasq_A.tsv",package = "bgfanalyzer"),
                                 dec = ".",
                                 sep = "\t",
                                 header = TRUE,
                                 mkFRTime = "2025-05-11 14:08:35",
                                 FRTime_col = 1,
                                 units = "days")

# add gas quality measurements to BGF
myBGF <- add_BG_parameter(myBGF,
                          parameter = gasq_A,
                          reactor = "R1",
                          time = 3,
                          value = 2,
                          name = "H2",
                          cut_zero = TRUE,
                          interpolate_missing = FALSE)

# look at a snippet of the first six rows of the 'BioGsaData'-layer to see the results
head(myBGF$BioGasData[c(1,2,3,(length(myBGF$BioGasData)-c(3,2,1,0)))])


## ----insertion-gaps-----------------------------------------------------------
# data insertion created gaps
myBGF$BioGasData[c(97:99),c(1,2,3,(length(myBGF$BioGasData)-c(3,2,1,0)))]

## ----na_correction------------------------------------------------------------
# close the gaps
myBGF <- na_correction(myBGF)

# look at a snippet of the first six rows
head(myBGF$BioGasData[c(1,2,3,(length(myBGF$BioGasData)-c(3,2,1,0)))])

# look at the gap that arrose from data insertion
myBGF$BioGasData[c(97:99),c(1,2,3,(length(myBGF$BioGasData)-c(3,2,1,0)))]

# look at a snippet of the last six rows
tail(myBGF$BioGasData[c(1,2,3,(length(myBGF$BioGasData)-c(3,2,1,0)))])

## ----sepcific_correction------------------------------------------------------
# correct remaining NA's in H2
myBGF <- na_correction(myBGF,
                       which = "H2",
                       end=F,
                       sub_zero=0)

# insepct results
tail(myBGF$BioGasData[c(1,2,3,(length(myBGF$BioGasData)-c(3,2,1,0)))])

## ----flow_from_volume---------------------------------------------------------
# cumulative exhaust gas volume mesurements in 'product' can be used to calculate 
# the 'production', a.k.a the biogas flow
myBGF <- calculate_flow_from_volume(myBGF)

# inspect standard columns of 'BioGasData'-layer
head(myBGF$BioGasData[c(1:7)])


## ----rel_prod-----------------------------------------------------------------
# calculate relative production
myBGF <- relative_production(myBGF)

# inspect standard columns of 'BioGasData'-layer
head(myBGF$BioGasData[c(1:7)])


## ----netGasGC-----------------------------------------------------------------
# calculate net_product
myBGF <- netGasGC(myBGF,
                  purity = "H2",
                  substract_blank = FALSE)

# inspect standard columns of 'BioGasData'-layer
tail(myBGF$BioGasData[c(1:7)])


## ----add-oTS------------------------------------------------------------------
# add the oTS to 'metaData'-layer
myBGF <- add_metaData(myBGF,98.89,lab = "oTS")

# inspect change in 'metaData'-layer
myBGF$metaData

## ----yield_cal----------------------------------------------------------------
# calculate 'yield'
myBGF <- calc_yield(myBGF,pos = 4)

# inspect standard columns of 'BioGasData'-layer
tail(myBGF$BioGasData[c(1:7)])

# get the yield summary
myBGF <- summarize_yield(myBGF)

# inspect change in 'metaData'-layer
myBGF$metaData

## ----bgf_plot-type-all,fig.width=7,fig.height=5-------------------------------
# build all standard plots
bgf_plot(myBGF)


## ----interactive_product,fig.width=7,fig.height=5-----------------------------
# print 'product_curve' interactively
bgf_plot(myBGF,type = "product",interaction=TRUE)


## ----trim_FRA,fig.width=7,fig.height=5----------------------------------------
# trim the fermentation
myBGF<-trim_FR_time(myBGF,0.11)

# inspect results by ploting
bgf_plot(myBGF,type = "product")


## ----rerun-data-proc,fig.width=7,fig.height=5---------------------------------
# ensure internal logic of the 'BGF'
myBGF<-update_BGF(myBGF)

# re-calculate 'production'
myBGF <- calculate_flow_from_volume(myBGF)

# re-calculate 'rel_production'
myBGF <- relative_production(myBGF)

# re-calculate 'net_product'
myBGF <- netGasGC(myBGF,"H2",substract_blank = FALSE)

# re-calculate 'yield'
myBGF <- calc_yield(myBGF,pos = 4)

# plot 'BGF' again
bgf_plot(myBGF)

## ----full_hombrew_BGF---------------------------------------------------------
# print the BGF
myBGF

## ----input-second-fr----------------------------------------------------------
# Import the new data directly from the input file with cumulative exhaust gas volume measurements
myBGF <- add_standard_record(myBGF,
                             path = system.file("extdata","Fermentation_B.tsv",package = "bgfanalyzer"),
                             RName = "R2",
                             time_col = "UTC",
                             units = "days",
                             product_col = "GCounter..ml.")

# updating  internal logic is highly recommended
myBGF <- update_BGF(myBGF)

# correct 'metaData$Layout' for new fermentation
myBGF <- alter_whatever(myBGF,layer = "metaData",what = "Layout",value = "Substrate A")

# already add 'metaData$oTS' at this point
myBGF <- alter_whatever(myBGF,layer = "metaData",what = "oTS",value = 98.89,ID = "R2")

# import respective gas quality measurements
gasq_B <- import_standard_record(ipath = system.file("extdata","gasq_B.tsv",package = "bgfanalyzer"),
                                 dec = ".",
                                 sep = "\t",
                                 header = TRUE,
                                 mkFRTime = "2025-05-19 22:00:00",
                                 FRTime_col = 1,
                                 units = "days")

# add gas quality measurements to BGF
myBGF <- add_BG_parameter(myBGF,
                          parameter = gasq_B,
                          reactor = "R2",
                          time = 3,
                          value = 2,
                          name = "H2",
                          makeCol = FALSE,
                          cut_zero = TRUE,
                          interpolate_missing = TRUE)

# look at a snippet of the first six rows of the 'BioGsaData'-layer to see the results
tail(myBGF$BioGasData[c(1,2,3,(length(myBGF$BioGasData)-c(3,2,1,0)))])

## ----interactive_product_2,fig.width=7,fig.height=5---------------------------
# plot interactive product curve of BGF
plot_product_curve(myBGF,interaction=TRUE)


## ----trim_FRB,fig.width=7,fig.height=5----------------------------------------
# remove data later than 2.49d
myBGF <- trim_FR_time(myBGF,
                      value = 2.49,
                      mode = "R2",
                      left_end = FALSE)

# remove data before 0.61d
myBGF <- trim_FR_time(myBGF,
                      value = 0.61,
                      mode = "R2")

# plot product curve again to inspect results
plot_product_curve(myBGF)

## ----rerun-data-proc_2,fig.width=7,fig.height=5-------------------------------

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

# close gaps
myBGF <- na_correction(myBGF)

# re-calculate 'production'
myBGF <- calculate_flow_from_volume(myBGF)

# re-calculate 'rel_production'
myBGF <- relative_production(myBGF)

# re-calculate 'net_product'
myBGF <- netGasGC(myBGF,"H2",substract_blank = FALSE)

# re-calculate 'yield'
myBGF <- calc_yield(myBGF,pos = 4)

# summarize yield
myBGF <- summarize_yield(myBGF)

# print the new BGF
myBGF

## ----create_myBGF2------------------------------------------------------------
# create a new BGF directly from the record of a third fermentation
myBGF2<-from_standard_record(ReactorLayout = c("2*Substrate B"),
                             ProcessTemp = 80,
                             InocToSubRatio = 0.1,
                             path = system.file("extdata","Fermentation_C.tsv",package = "bgfanalyzer"),
                             time_col = 1,
                             product_col = 3,
                             BlankLabel = "Blank",
                             name = "myBGF2",
                             units = "days")

# import respective gas quality data
gasq_C <- import_standard_record(ipath = system.file("extdata","gasq_C.tsv",package = "bgfanalyzer"),
                                 dec = ".",
                                 sep = "\t",
                                 header = TRUE,
                                 mkFRTime = "2024-10-27 05:30:00",
                                 FRTime_col = 1,
                                 units = "days")

# add gas quality measurements to BGF
myBGF2 <- add_BG_parameter(myBGF2,
                          parameter = gasq_C,
                          reactor = "R1",
                          time = 3,
                          value = 2,
                          name = "H2",
                          cut_zero = TRUE,
                          interpolate_missing = TRUE)


# print the new BGF
myBGF2

## ----adding_FRD---------------------------------------------------------------

myBGF2 <- add_standard_record(myBGF2,
                             path = system.file("extdata","Fermentation_D.tsv",package = "bgfanalyzer"),
                             RName = "R2",
                             time_col = "UTC",
                             units = "days",
                             product_col = "GCounter..ml.")

# updating  internal logic is highly recommended
myBGF2 <- update_BGF(myBGF2)

# import respective gas quality data
gasq_D <- import_standard_record(ipath = system.file("extdata","gasq_D.tsv",package = "bgfanalyzer"),
                                 dec = ".",
                                 sep = "\t",
                                 header = TRUE,
                                 mkFRTime = "2024-10-30 23:00:00",
                                 FRTime_col = 1,
                                 units = "days")

# add gas quality measurements to BGF
myBGF2 <- add_BG_parameter(myBGF2,
                          parameter = gasq_D,
                          reactor = "R2",
                          time = 3,
                          value = 2,
                          name = "H2",
                          makeCol = FALSE,
                          cut_zero = TRUE,
                          interpolate_missing = TRUE)

# print the BGF
myBGF2


## ----interactive_product_myBGF2,fig.width=7,fig.height=5----------------------
# interactively plot product curve
plot_product_curve(myBGF2,interaction=TRUE)

## ----trimming_myBGF2,fig.width=7,fig.height=5---------------------------------
# trim 'R1' fermentation
myBGF2 <- trim_FR_time(myBGF2,4.08,"R1",left_end = FALSE)

# updating  internal logic is highly recommended
myBGF2 <- update_BGF(myBGF2)

# trim 'R1' fermentation
myBGF2 <- trim_FR_time(myBGF2,0.42,"R1")

# updating  internal logic is highly recommended
myBGF2 <- update_BGF(myBGF2)

# trim 'R2' fermentation
myBGF2 <- trim_FR_time(myBGF2,0.56,"R2")

# updating  internal logic is highly recommended
myBGF2 <- update_BGF(myBGF2)

# plot the product curve again to see results
plot_product_curve(myBGF2)

## ----processing_myBGF2--------------------------------------------------------
# updating  internal logic is highly recommended
myBGF2 <- update_BGF(myBGF2)

# close gaps in data
myBGF2 <- na_correction(myBGF2,end=F)

# calculate production
myBGF2 <- calculate_flow_from_volume(myBGF2)

# calculate relative production
myBGF2 <- relative_production(myBGF2)

# calculate net gas
myBGF2 <- netGasGC(myBGF2,"H2",substract_blank = FALSE)

# add a oTS column at the metaData-layer
myBGF2<-add_metaData(myBGF2,c(85.5,85.5),lab="oTS")

# calculate yield
myBGF2<-calc_yield(myBGF2,4)

# summarize yield
myBGF2<-summarize_yield(myBGF2)

# print BGF
myBGF2


## ----merge_BGF----------------------------------------------------------------
# merge the two BGFs
mergedBGF<-merge_BGF(myBGF,myBGF2,name = "merged BGF")

# print the merged BGF
mergedBGF

