-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathms_overwrites.R
306 lines (237 loc) · 11.2 KB
/
ms_overwrites.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
ms_conversions <- function(d,
convert_units_from = 'mg/l',
convert_units_to,
convert_molecules,
macrosheds_root){
if(missing(macrosheds_root)){
stop('Please provide macrosheds_root, information needed to convert variables is stored here')
}
ms_vars_path <- paste0(macrosheds_root, '/ms_vars.feather')
if(! file.exists(ms_vars_path)){
ms_vars <- readr::read_csv('https://figshare.com/articles/dataset/variable_metadata/19358585/files/35134504',
col_types = readr::cols())
feather::write_feather(ms_vars, ms_vars_path)
} else{
ms_vars <- feather::read_feather(ms_vars_path)
}
#checks
cm <- ! missing(convert_molecules)
cuF <- ! missing(convert_units_from) && ! is.null(convert_units_from)
cuT <- ! missing(convert_units_to) && ! is.null(convert_units_to)
if(sum(cuF, cuT) == 1){
stop('convert_units_from and convert_units_to must be supplied together')
}
if(length(convert_units_from) != length(convert_units_to)){
stop('convert_units_from and convert_units_to must have the same length')
}
vars <- ms_drop_var_prefix(d$var)
if(any(!vars %in% ms_vars$variable_code)){
not_a_ms_var <- unique(vars[!vars %in% ms_vars$variable_code])
stop(paste0(paste(not_a_ms_var, collapse = ', '),
' is not a MacroSheds variable. only MacroSheds variables can be converted'))
}
if(any(duplicated(names(convert_units_from)))){
stop('duplicated names in convert_units_from')
}
if(any(duplicated(names(convert_units_to)))){
stop('duplicated names in convert_units_to')
}
vars_convertable <- ms_vars %>%
filter(variable_code %in% !!vars) %>%
pull(unit) %>%
tolower()
if(length(convert_units_from) == 1 && length(convert_units_to) == 1){
if(! all(vars_convertable == 'mg/l')){
print(all(vars_convertable))
warning('unable to convert non-concentration variables')
}
} else{
if(! all(vars %in% names(convert_units_from)) || ! all(vars %in% names(convert_units_to))){
stop('when specifying individual variable conversions, all variables in d must be accounted for')
}
cu_shared_names <- base::intersect(names(convert_units_from),
names(convert_units_to))
if(length(cu_shared_names) != length(convert_units_to)){
stop('names of convert_units_from and convert_units_to must match')
}
}
convert_units_from <- tolower(convert_units_from)
convert_units_to <- tolower(convert_units_to)
whole_molecule <- c('NO3', 'SO4', 'PO4', 'SiO2', 'SiO3', 'NH4', 'NH3',
'NO3_NO2')
element_molecule <- c('NO3_N', 'SO4_S', 'PO4_P', 'SiO2_S', 'SiO3_S', 'NH4_N',
'NH3_N', 'NO3_NO2_N')
if(cm){
whole_to_element <- grep(paste0(paste0('^', convert_molecules, '$'), collapse = '|'),
whole_molecule)
element_to_whole <- grep(paste0(paste0('^', convert_molecules, '$'), collapse = '|'),
element_molecule)
if(length(element_to_whole) == 0 && length(whole_to_element) == 0){
stop(paste0('convert_molecules must be one of: ', paste(whole_molecule, collapse = ' '),
' or: ', paste(element_molecule, collapse = ' ')))
}
} else{
convert_molecules <- NULL
}
molecular_conversion_map <- list(
NH4 = 'N',
NO3 = 'N',
NH3 = 'N',
SiO2 = 'Si',
SiO3 = 'Si',
SO4 = 'S',
PO4 = 'P',
NO3_NO2 = 'N')
# handle molecular conversions, like NO3 -> NO3_N
if(cm && length(whole_to_element) > 0){
convert_molecules_element <- whole_molecule[whole_to_element]
for(v in 1:length(convert_molecules_element)){
molecule_real <- ms_vars %>%
filter(variable_code == !!convert_molecules_element[v]) %>%
pull(molecule)
if(is.na(molecule_real)) {
molecule_real <- convert_molecules_element[v]
}
d$val[vars == convert_molecules_element[v]] <-
convert_molecule(x = d$val[vars == convert_molecules_element[v]],
from = molecule_real,
to = unname(molecular_conversion_map[v]))
check_double <- stringr::str_split_fixed(unname(molecular_conversion_map[v]), '', n = Inf)[1,]
if(length(check_double) > 1 && length(unique(check_double)) == 1) {
molecular_conversion_map[v] <- unique(check_double)
}
new_name <- paste0(d$var[vars == convert_molecules_element[v]], '_', unname(molecular_conversion_map[v]))
d$var[vars == convert_molecules_element[v]] <- new_name
}
}
# handle molecular conversions, like NO3_N -> NO3
if(cm && length(element_to_whole) > 0){
convert_molecules_element <- element_molecule[element_to_whole]
for(v in 1:length(convert_molecules_element)){
molecule_real <- ms_vars %>%
filter(variable_code == !!convert_molecules_element[v]) %>%
pull(molecule)
if(is.na(molecule_real)) {
molecule_real <- convert_molecules_element[v]
}
d$val[vars == convert_molecules_element[v]] <-
convert_molecule(x = d$val[vars == convert_molecules_element[v]],
from = molecule_real,
to = whole_molecule[element_to_whole[v]])
# check_double <- stringr::str_split_fixed(unname(molecular_conversion_map[v]), '', n = Inf)[1,]
#
# if(length(check_double) > 1 && length(unique(check_double)) == 1) {
# molecular_conversion_map[v] <- unique(check_double)
# }
old_var <- unique(d$var[vars == convert_molecules_element[v]])
new_name <- substr(d$var[vars == convert_molecules_element[v]], 0, nchar(old_var)-2)
d$var[vars == convert_molecules_element[v]] <- new_name
}
}
# Turn a single input into a named vector with all variables in dataframe
if(length(convert_units_from) == 1){
all_vars <- unique(vars)
convert_units_from <- rep(convert_units_from, length(all_vars))
names(convert_units_from) <- all_vars
convert_units_to <- rep(convert_units_to, length(all_vars))
names(convert_units_to) <- all_vars
}
# Converts input to grams if the final unit contains grams
for(i in 1:length(convert_units_from)){
unitfrom <- convert_units_from[i]
unitto <- convert_units_to[i]
v <- names(unitfrom)
g_conver <- FALSE
if(grepl('mol|eq', unitfrom) && grepl('g', unitto) || v %in% convert_molecules){
molecule_real <- ms_vars %>%
filter(variable_code == !!v) %>%
pull(molecule)
if(! is.na(molecule_real)){
formula <- molecule_real
} else {
formula <- v
}
d$val[vars == v] <- convert_to_gl(x = d$val[vars == v],
input_unit = unitfrom,
formula = formula,
ms_vars = ms_vars)
g_conver <- TRUE
}
d$val[vars == v] <- convert_unit(x = d$val[vars == v],
input_unit = unitfrom,
output_unit = unitto)
#Convert to mol or eq if that is the output unit
if(grepl('mol|eq', unitto)) {
d$val[vars == v] <- convert_from_gl(x = d$val[vars == v],
input_unit = unitfrom,
output_unit = unitto,
molecule = v,
g_conver = g_conver,
ms_vars = ms_vars)
}
}
return(d)
}
convert_unit <- function(x, input_unit, output_unit){
units <- tibble(prefix = c('n', "u", "m", "c", "d", "h", "k", "M"),
convert_factor = c(0.000000001, 0.000001, 0.001, 0.01, 0.1, 100,
1000, 1000000))
old_fraction <- as.vector(stringr::str_split_fixed(input_unit, "/", n = Inf))
old_top <- as.vector(stringr::str_split_fixed(old_fraction[1], "", n = Inf))
if(length(old_fraction) == 2) {
old_bottom <- as.vector(stringr::str_split_fixed(old_fraction[2], "", n = Inf))
}
new_fraction <- as.vector(stringr::str_split_fixed(output_unit, "/", n = Inf))
new_top <- as.vector(stringr::str_split_fixed(new_fraction[1], "", n = Inf))
if(length(new_fraction == 2)) {
new_bottom <- as.vector(stringr::str_split_fixed(new_fraction[2], "", n = Inf))
}
old_top_unit <- tolower(stringr::str_split_fixed(old_top, "", 2)[1])
if(old_top_unit %in% c('g', 'e', 'q', 'l') || old_fraction[1] == 'mol') {
old_top_conver <- 1
} else {
old_top_conver <- as.numeric(filter(units, prefix == old_top_unit)[,2])
}
old_bottom_unit <- tolower(stringr::str_split_fixed(old_bottom, "", 2)[1])
if(old_bottom_unit %in% c('g', 'e', 'q', 'l') || old_fraction[2] == 'mol') {
old_bottom_conver <- 1
} else {
old_bottom_conver <- as.numeric(filter(units, prefix == old_bottom_unit)[,2])
}
new_top_unit <- tolower(stringr::str_split_fixed(new_top, "", 2)[1])
if(new_top_unit %in% c('g', 'e', 'q', 'l') || new_fraction[1] == 'mol') {
new_top_conver <- 1
} else {
new_top_conver <- as.numeric(filter(units, prefix == new_top_unit)[,2])
}
new_bottom_unit <- tolower(stringr::str_split_fixed(new_bottom, "", 2)[1])
if(new_bottom_unit %in% c('g', 'e', 'q', 'l') || new_fraction[2] == 'mol') {
new_bottom_conver <- 1
} else {
new_bottom_conver <- as.numeric(filter(units, prefix == new_bottom_unit)[,2])
}
new_val <- x*old_top_conver
new_val <- new_val/new_top_conver
new_val <- new_val/old_bottom_conver
new_val <- new_val*new_bottom_conver
return(new_val)
}
# End unit converstion
# 'errors' package handlers
sd_or_0 <- function(x, na.rm = FALSE) {
#Only used to bypass the tyranny of the errors package not letting
#me take the mean of an errors object of length 1 without setting the
#uncertainty to 0
x <- if(is.vector(x) || is.factor(x)) x else as.double(x)
if(length(x) == 1) return(0)
x <- sqrt(var(x, na.rm = na.rm))
}
mean_or_x <- function(x, na.rm = FALSE) {
# also used to bypass the tyranny of the errors package not letting
# someone take the mean of an errors object of length 1. this func returns the
# original value if the group is length one, and the mean otherwise
if(length(x) == 1) return(x)
x <- mean(var(x, na.rm = na.rm))
print('multiple values meaned')
return(x)
}