3
3
library(lubridate )
4
4
library(readr )
5
5
6
+ # Constants used in this file and elsewhere in the system
6
7
GIT_COMMIT <- substr(system(" git rev-parse HEAD" , intern = TRUE ), 1 , 7 )
7
8
8
- NA_CODE_L1 <- " NA"
9
- NA_CODE_L2 <- " -9999"
9
+ NA_STRING_L1 <- " NA"
10
+ NA_STRING_L2 <- " -9999"
10
11
11
12
# Small helper functions to make the various steps obvious in the log
12
13
if (! exists(" LOGFILE" )) LOGFILE <- " "
@@ -44,22 +45,21 @@ copy_output <- function(from, to, overwrite = TRUE) {
44
45
# is returned as an attribute of the output
45
46
read_csv_group <- function (files , col_types = NULL ,
46
47
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 )
48
52
49
- # Read in all files and bind data frames
50
- readf <- function (fn ) {
53
+ # File-reading function
54
+ readf <- function (fn , quiet , ... ) {
51
55
if (! quiet ) message(" \t Reading " , 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 , ... )
57
57
if (remove_input_files ) file.remove(fn )
58
58
x
59
59
}
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 )
63
63
dat
64
64
}
65
65
@@ -74,7 +74,7 @@ read_csv_group <- function(files, col_types = NULL,
74
74
# Folders are site_year
75
75
# Filenames are site_timeperiod_table_L2
76
76
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
78
78
# This is used to split the data for sorting into <yyyy>_<mm> folders
79
79
# Returns a list of filenames written (names) and number of data lines (values)
80
80
write_to_folders <- function (x , root_dir , data_level , site ,
@@ -84,7 +84,14 @@ write_to_folders <- function(x, root_dir, data_level, site,
84
84
85
85
lines_written <- list ()
86
86
for (y in unique(years )) {
87
+ if (is.na(y )) {
88
+ stop(data_level , " invalid year " , y )
89
+ }
90
+
87
91
for (m in unique(months )) {
92
+ if (is.na(m )) {
93
+ stop(data_level , " invalid month " , m )
94
+ }
88
95
89
96
# Isolate the data to write
90
97
dat <- x [y == years & m == months ,]
@@ -106,15 +113,15 @@ write_to_folders <- function(x, root_dir, data_level, site,
106
113
# overwrite anything that's already there
107
114
short_hash <- substr(digest :: digest(dat , algo = " md5" ), 1 , 4 )
108
115
filename <- paste0(paste(logger , table , y , m , short_hash , sep = " _" ), " .csv" )
109
- na <- NA_CODE_L1
116
+ na_string <- NA_STRING_L1
110
117
} else if (data_level == " L1" ) {
111
118
folder <- file.path(root_dir , paste(site , y , sep = " _" ))
112
119
filename <- paste0(paste(site , time_period , data_level , sep = " _" ), " .csv" )
113
- na <- NA_CODE_L1
120
+ na_string <- NA_STRING_L1
114
121
} else if (data_level == " L2" ) {
115
122
folder <- file.path(root_dir , paste(site , y , sep = " _" ))
116
123
filename <- paste0(paste(site , time_period , table , data_level , sep = " _" ), " .csv" )
117
- na <- NA_CODE_L2
124
+ na_string <- NA_STRING_L2
118
125
} else {
119
126
stop(" Unkown data_level " , data_level )
120
127
}
@@ -141,7 +148,7 @@ write_to_folders <- function(x, root_dir, data_level, site,
141
148
if (file.exists(fn )) message(" NOTE: overwriting existing file" )
142
149
# We were using readr::write_csv for this but it was
143
150
# 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 )
145
152
if (! file.exists(fn )) {
146
153
stop(" File " , fn , " was not written" )
147
154
}
0 commit comments