Skip to content

Commit 9c0b316

Browse files
committed
fix issue reps smart chamber (import.LI8200)
1 parent a1900d7 commit 9c0b316

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

R/LI8200_import.R

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -115,65 +115,67 @@ LI8200_import <- function(inputfile, date.format = "ymd",
115115

116116
# Create empty lists to extract objects from lists of data.raw.ls
117117
df.ls <- list()
118-
plotID.ls <- list()
119-
cham.close.ls <- list()
120-
deadband.ls <- list()
121-
Vcham.ls <- list()
122-
offset.ls <- list()
123-
Area.ls <- list()
124118

125119
# Loop through data.raw.ls
126120
for (i in 1:length(data.raw.ls$datasets)) {
121+
127122
# loop through all measurements reps
128123
all.reps <- data.raw.ls$datasets[[i]][[1]]$reps
129-
# Extract plotID from list "datasets"
130-
plotID.ls[[i]] <- names(data.raw.ls$datasets[[i]])
124+
131125
if(length(all.reps) > 0) {
126+
# Create empty lists to store results
132127
rep.ls <- list()
128+
cham.close.ls <- list()
129+
deadband.ls <- list()
130+
Vcham.ls <- list()
131+
offset.ls <- list()
132+
Area.ls <- list()
133+
133134
for (j in 1:length(all.reps)) {
134135
# Extract gas measurements from "data"
135136
rep.ls[[j]] <- all.reps[[j]]$data
136137
# Extract chamber closure time from "Date"
137-
cham.close.ls[[i]] <- all.reps[[j]]$header$Date
138+
cham.close.ls[[j]] <- all.reps[[j]]$header$Date
138139
# Extract deadband before measurement start from "DeadBand"
139-
deadband.ls[[i]] <- all.reps[[j]]$header$DeadBand
140+
deadband.ls[[j]] <- all.reps[[j]]$header$DeadBand
140141
# Extract chamber volume from "Vcham"
141-
Vcham.ls[[i]] <- all.reps[[j]]$header$TotalVolume
142+
Vcham.ls[[j]] <- all.reps[[j]]$header$TotalVolume
142143
# Extract chamber offset (collar height) from "Offset"
143-
offset.ls[[i]] <- all.reps[[j]]$header$Offset
144+
offset.ls[[j]] <- all.reps[[j]]$header$Offset
144145
# Extract chamber Area (collar inner Area) from "Area"
145-
Area.ls[[i]] <- all.reps[[j]]$header$Area
146+
Area.ls[[j]] <- all.reps[[j]]$header$Area
146147
}
147148
# Convert list of data into a dataframe
148-
df.ls[[i]] <- map_df(rep.ls, ~as.data.frame(.x), .id="rep")
149+
rep.res <- map_df(rep.ls, ~as.data.frame(.x), .id="rep")
150+
151+
# Convert lists of metadata into matrix
152+
metadata <- cbind.data.frame(
153+
rep = as.character(c(1:length(all.reps))),
154+
cham.close = unlist(cham.close.ls),
155+
deadband = unlist(deadband.ls),
156+
Vcham = unlist(Vcham.ls),
157+
offset = unlist(offset.ls),
158+
Area = unlist(Area.ls)) %>%
159+
mutate(Vcham = Vcham/100)
160+
161+
# Merge data with metadata
162+
rep.res <- rep.res %>%
163+
left_join(as.data.frame(metadata), by = "rep") %>%
164+
mutate(plotID = names(data.raw.ls$datasets[[i]]))
149165

150166
} else {
151-
cham.close.ls[[i]] <- NA
152-
deadband.ls[[i]] <- NA
153-
Vcham.ls[[i]] <- NA
154-
offset.ls[[i]] <- NA
155-
Area.ls[[i]] <- NA
167+
rep.res <- cbind.data.frame(plotID = names(data.raw.ls$datasets[[i]]))
156168
}
157-
}
158169

159-
# Convert lists of metadata into matrix
160-
metadata <- cbind.data.frame(
161-
Obs = as.character(c(1:length(data.raw.ls$datasets))),
162-
plotID = unlist(plotID.ls),
163-
cham.close = unlist(cham.close.ls),
164-
deadband = unlist(deadband.ls),
165-
Vcham = unlist(Vcham.ls),
166-
offset = unlist(offset.ls),
167-
Area = unlist(Area.ls)) %>%
168-
mutate(Vcham = Vcham/100)
170+
df.ls[[i]] <- rep.res
171+
}
169172

170173
# Create extra columns for CO2, CH4 or N2O, if missing
171174
cols <- c(co2 = NA_real_, ch4 = NA_real_, n2o = NA_real_)
172175

173176
# Convert list of dataframe into a dataframe
174177
data.raw <- map_df(df.ls, ~as.data.frame(.x), .id="Obs") %>%
175-
# Add metadata and extra columns for additional gases
176-
left_join(as.data.frame(metadata), by = "Obs")%>%
178+
# Add extra columns for additional gases
177179
add_column(!!!cols[!names(cols) %in% names(.)]) %>%
178180
# Convert H2O_mmol/mol into H2O_ppm
179181
mutate(H2O_ppm = h2o*1000)
@@ -252,3 +254,4 @@ LI8200_import <- function(inputfile, date.format = "ymd",
252254
#' @export
253255
#' @rdname LI8200_import
254256
import.LI8200 <- LI8200_import
257+

0 commit comments

Comments
 (0)