Skip to content

Commit a28bcee

Browse files
committed
Tweaks for clarity and speed
1 parent abebf93 commit a28bcee

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

R/parse-others.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,26 @@
1414
#' to change a stray "(" to "," line 365560. This is noted in the
1515
#' README in the raw data folder.
1616

17+
1718
parse_d20190830_LIANG <- function(path) {
1819
files <- list.files(path, pattern = ".dat$", full.names = TRUE, recursive = TRUE)
19-
co2files <- grep("Environ", files, invert = TRUE)[43:75]
20+
co2files <- grep("Environ", files, invert = TRUE)
2021
results <- list()
2122

2223
for(f in seq_along(co2files)) {
2324
fn <- files[co2files][f]
2425
message(f, "/", length(co2files), " ", fn)
2526
# cat(f, "/", length(co2files), " ", fn, "\n", file = "~/Desktop/log.txt", append = T)
2627

27-
# Find the TIMESTAMP header - location varies by file
28+
# Find and parse the header; its location varies by file
2829
top <- readLines(fn, n = 50)
2930
hl <- grep("TIMESTAMP", top)
31+
stopifnot(length(hl) == 1)
3032
hdr <- gsub('\\\"', "", top[hl])
3133
hdr <- strsplit(hdr, ",")[[1]]
3234

3335
dat <- read.table(fn,
34-
skip = hl + 1, # there's a units line after the header
36+
skip = hl + 1, # skip units line after the header
3537
sep = ",",
3638
header = FALSE,
3739
col.name = hdr,
@@ -65,8 +67,9 @@ parse_d20190830_LIANG <- function(path) {
6567
# Rs = 60.14 * Pair / (Tair + 273.15) * deltaC / deltaT
6668
d$secs <- d$TS - d$TS[1]
6769
m <- try(lm(CO2 ~ secs, data = d), silent = TRUE)
70+
mean_tair <- mean(d$Tair)
6871
if(class(m) == "lm") {
69-
resultsdf$Flux[i] <- 60.14 * 99.79 / (mean(d$Tair + 273.15)) * m$coefficients["secs"]
72+
resultsdf$Flux[i] <- 60.14 * 99.79 / (mean_tair + 273.15) * m$coefficients["secs"]
7073
resultsdf$R2[i] <- summary(m)$r.squared
7174
} else {
7275
resultsdf$Error[i] <- TRUE
@@ -75,13 +78,15 @@ parse_d20190830_LIANG <- function(path) {
7578
if("Humity" %in% names(d)) {
7679
resultsdf$Humity[i] <- mean(d$Humity)
7780
}
81+
if("RECORD" %in% names(d)) {
82+
resultsdf$RECORD[i] <- d$RECORD[1]
83+
}
7884

7985
resultsdf$TIMESTAMP[i] <- d$TIMESTAMP[1]
80-
resultsdf$RECORD[i] <- d$RECORD[1]
8186
resultsdf$Chamber[i] <- d$Chamber[1]
8287
resultsdf$CO2[i] <- mean(d$CO2)
8388
resultsdf$Tsoil[i] <- mean(d$Tsoil)
84-
resultsdf$Tair[i] <- mean(d$Tair)
89+
resultsdf$Tair[i] <- mean_tair
8590
resultsdf$N[i] <- nrow(d)
8691
}
8792
results[[f]] <- resultsdf

0 commit comments

Comments
 (0)