Skip to content

Commit 3961d44

Browse files
committed
Remove error-handling code (no point); clarify comments
1 parent cb39ca0 commit 3961d44

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

synoptic/L1.qmd

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ HTML outfile is "`r params$html_outfile`".
5858
## Processing
5959

6060
```{r processing}
61-
errors <- 0
62-
6361
f <- function(dir_name, dirs_to_process, out_dir) {
6462
message(Sys.time(), " Processing ", basename(dir_name))
6563
d <- dirs_to_process[[dir_name]]
@@ -71,8 +69,7 @@ f <- function(dir_name, dirs_to_process, out_dir) {
7169
dat_raw <- read_csv_group(d,
7270
remove_input_files = params$remove_input_files,
7371
col_types = "cccccTdcccdi")
74-
errors <<- errors + attr(dat_raw, "errors")
75-
72+
7673
# File-based summary
7774
message("\tTotal data: ", nrow(dat_raw), " rows, ", ncol(dat_raw), " columns")
7875
smry <- data.frame(Dir = dir_name,
@@ -124,18 +121,6 @@ error = function(e) {
124121
})
125122
```
126123

127-
## File summary
128-
129-
```{r summary}
130-
#| echo: false
131-
#| output: asis
132-
if(errors) {
133-
cat("### WARNING: ", errors, " file read/write error(s)\n")
134-
log_warning(paste("File read/write error(s)", params$html_outfile),
135-
logfile = params$logfile)
136-
}
137-
```
138-
139124
## Metadata
140125

141126
L1 metadata template directory is `r params$L1_METADATA`.
@@ -214,7 +199,7 @@ for(dd in data_dirs) {
214199
md <- append(md, col_md_for_insert, after = col_info_pos)
215200
md <- md[-col_info_pos]
216201
# The NA code is an in-line replacement
217-
md <- gsub("[NA_CODE_L1]", NA_CODE_L1, md, fixed = TRUE)
202+
md <- gsub("[NA_STRING_L1]", NA_STRING_L1, md, fixed = TRUE)
218203
219204
# Insert variable metadata
220205
var_info_pos <- grep("[VARIABLE_INFO]", md, fixed = TRUE)

synoptic/data_TEST/L1_metadata/L1_metadata_columns.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ Column,Description
22
TIMESTAMP,Original datalogger timestamp (EST) (POSIXct)
33
design_link,Design name that links to experimental unit (character)
44
research_name,Measurement name (character)
5-
value,Observed value (numeric). The no-data value is '[NA_CODE_L1]'
5+
value,Observed value (numeric). The no-data value is '[NA_STRING_L1]'
66
ID,Observation ID (character)
77
OOB,"Out of instrumental bounds flag (1=TRUE, 0=FALSE) (logical)"

synoptic/helpers.R

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
library(lubridate)
44
library(readr)
55

6+
# Constants used in this file and elsewhere in the system
67
GIT_COMMIT <- substr(system("git rev-parse HEAD", intern = TRUE), 1, 7)
78

8-
NA_CODE_L1 <- "NA"
9-
NA_CODE_L2 <- "-9999"
9+
NA_STRING_L1 <- "NA"
10+
NA_STRING_L2 <- "-9999"
1011

1112
# Small helper functions to make the various steps obvious in the log
1213
if(!exists("LOGFILE")) LOGFILE <- ""
@@ -44,22 +45,21 @@ copy_output <- function(from, to, overwrite = TRUE) {
4445
# is returned as an attribute of the output
4546
read_csv_group <- function(files, col_types = NULL,
4647
remove_input_files = FALSE, quiet = FALSE, ...) {
47-
errors <- 0
48+
# Warnings are not allowed here, as this usually means a column format
49+
# problem that we want to fix immediately
50+
oldwarn <- options()$warn
51+
options(warn = 2)
4852

49-
# Read in all files and bind data frames
50-
readf <- function(fn) {
53+
# File-reading function
54+
readf <- function(fn, quiet, ...) {
5155
if(!quiet) message("\tReading ", basename(fn))
52-
x <- try(read_csv(fn, col_types = col_types, ...))
53-
if(!is.data.frame(x)) {
54-
errors <- errors + 1
55-
return(NULL)
56-
}
56+
x <- read_csv(fn, col_types = col_types, ...)
5757
if(remove_input_files) file.remove(fn)
5858
x
5959
}
60-
# Store the number of errors as an attribute of the data and return
61-
dat <- do.call("rbind", lapply(files, readf))
62-
attr(dat, "errors") <- errors
60+
# Read all files, bind data frames, and return
61+
dat <- do.call("rbind", lapply(files, readf, quiet, ...))
62+
options(warn = oldwarn)
6363
dat
6464
}
6565

@@ -74,7 +74,7 @@ read_csv_group <- function(files, col_types = NULL,
7474
# Folders are site_year
7575
# Filenames are site_timeperiod_table_L2
7676

77-
# The data (x) should be a data frame with a posixct 'TIMESTAMP' column
77+
# The data (x) should be a data frame with a POSIXct 'TIMESTAMP' column
7878
# This is used to split the data for sorting into <yyyy>_<mm> folders
7979
# Returns a list of filenames written (names) and number of data lines (values)
8080
write_to_folders <- function(x, root_dir, data_level, site,
@@ -84,7 +84,14 @@ write_to_folders <- function(x, root_dir, data_level, site,
8484

8585
lines_written <- list()
8686
for(y in unique(years)) {
87+
if(is.na(y)) {
88+
stop(data_level, " invalid year ", y)
89+
}
90+
8791
for(m in unique(months)) {
92+
if(is.na(m)) {
93+
stop(data_level, " invalid month ", m)
94+
}
8895

8996
# Isolate the data to write
9097
dat <- x[y == years & m == months,]
@@ -106,15 +113,15 @@ write_to_folders <- function(x, root_dir, data_level, site,
106113
# overwrite anything that's already there
107114
short_hash <- substr(digest::digest(dat, algo = "md5"), 1, 4)
108115
filename <- paste0(paste(logger, table, y, m, short_hash, sep = "_"), ".csv")
109-
na <- NA_CODE_L1
116+
na_string <- NA_STRING_L1
110117
} else if(data_level == "L1") {
111118
folder <- file.path(root_dir, paste(site, y, sep = "_"))
112119
filename <- paste0(paste(site, time_period, data_level, sep = "_"), ".csv")
113-
na <- NA_CODE_L1
120+
na_string <- NA_STRING_L1
114121
} else if(data_level == "L2") {
115122
folder <- file.path(root_dir, paste(site, y, sep = "_"))
116123
filename <- paste0(paste(site, time_period, table, data_level, sep = "_"), ".csv")
117-
na <- NA_CODE_L2
124+
na_string <- NA_STRING_L2
118125
} else {
119126
stop("Unkown data_level ", data_level)
120127
}
@@ -141,7 +148,7 @@ write_to_folders <- function(x, root_dir, data_level, site,
141148
if(file.exists(fn)) message("NOTE: overwriting existing file")
142149
# We were using readr::write_csv for this but it was
143150
# randomly crashing on GA (Error in `vroom write()`: ! bad value)
144-
write.csv(dat, fn, row.names = FALSE, na = na)
151+
write.csv(dat, fn, row.names = FALSE, na = na_string)
145152
if(!file.exists(fn)) {
146153
stop("File ", fn, "was not written")
147154
}

0 commit comments

Comments
 (0)