-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertUnits.R
More file actions
396 lines (388 loc) · 12.9 KB
/
convertUnits.R
File metadata and controls
396 lines (388 loc) · 12.9 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
### ---------------------------------------------------------------- ###
### atmosch-R ###
### ---------------------------------------------------------------- ###
### Functions to convert between measurement units:
### - fConvTemp() : temperature
### - fConvPress() : pressure
### - fConvAngle() : angle
### - fConvTime() : time
### - fConcGas() : concentration (gas-phase)
### - fConcAq() : concentration (aqueous-phase)
### - fHumid() : humidity/water
###
### Conversion factors from WolframAlpha:
### https://www.wolframalpha.com/
###
### version 2.4, March 2024
### author: RS
### ---------------------------------------------------------------- ###
fConvTemp <- function(data.in, unit.in, unit.out) {
## Convert between units of temperature:
## * kelvin = "K"
## * celsius = "C"
## * fahreneit = "F"
##
## INPUT:
## data.in = data in original unit
## unit.in = original unit
## unit.out = final unit
## OUTPUT:
## data.out = data in final unit
## EXAMPLE:
## xx <- fConvTemp(298, "K", "C")
## ------------------------------------------------------------
## from original unit to reference unit (K)
convert_to_K <- function(data, unit) {
switch(unit,
"K" = data,
"C" = data + 273.15,
"F" = (data + 459.67) * 5/9,
stop("INPUT ERROR: unit not found")
)
}
## from reference unit (K) to final unit
convert_from_K <- function(data, unit) {
switch(unit,
"K" = data,
"C" = data - 273.15,
"F" = (data * 9/5) - 459.67,
stop("INPUT ERROR: unit not found")
)
}
## convert data from original unit to final unit
data.ref <- convert_to_K(data.in, unit.in)
data.out <- convert_from_K(data.ref, unit.out)
return(data.out)
}
fConvPress <- function(data.in, unit.in, unit.out) {
## Convert between units of pressure:
## * pascal = "Pa"
## * hectopascal = "hPa"
## * atmosphere = "atm"
## * torr = mmHg = "torr"
## * bar = "bar"
## * millibar = "mbar"
## * psi = "psi"
##
## INPUT:
## data.in = data in original unit
## unit.in = original unit
## unit.out = final unit
## OUTPUT:
## data.out = data in final unit
## EXAMPLE:
## xx <- fConvPress(1013, "mbar", "torr")
## ------------------------------------------------------------
## from original unit to reference unit (Pa)
convert_to_Pa <- function(data, unit) {
switch(unit,
"Pa" = data,
"hPa" = data * 1.0e+02,
"atm" = data * 1.01325e+05,
"torr" = data * (20265 / 152),
"bar" = data * 1.0e+05,
"mbar" = data * 1.0e+02,
"psi" = data * (8896443230521 / 1290320000),
stop("INPUT ERROR: unit not found")
)
}
## from reference unit (Pa) to final unit
convert_from_Pa <- function(data, unit) {
switch(unit,
"Pa" = data,
"hPa" = data / 1.0e+02,
"atm" = data / 1.01325e+05,
"torr" = data * (152 / 20265),
"bar" = data / 1.0e+05,
"mbar" = data / 1.0e+02,
"psi" = data * (1290320000 / 8896443230521),
stop("INPUT ERROR: unit not found")
)
}
## convert data from original unit to final unit
data.ref <- convert_to_Pa(data.in, unit.in)
data.out <- convert_from_Pa(data.ref, unit.out)
return(data.out)
}
fConvAngle <- function(data.in, unit.in, unit.out) {
## Convert between units of angle:
## * radian = "rad"
## * degree = "deg"
##
## INPUT:
## data.in = data in original unit
## unit.in = original unit
## unit.out = final unit
## OUTPUT:
## data.out = data in final unit
## EXAMPLE:
## xx <- fConvAngle(33, "deg", "rad")
## ------------------------------------------------------------
## from original unit to reference unit (rad)
convert_to_rad <- function(data, unit) {
switch(unit,
"rad" = data,
"deg" = data * (pi / 180),
stop("INPUT ERROR: unit not found")
)
}
## from reference unit (rad) to final unit
convert_from_rad <- function(data, unit) {
switch(unit,
"rad" = data,
"deg" = data * (180 / pi),
stop("INPUT ERROR: unit not found")
)
}
## convert data from original unit to final unit
data.ref <- convert_to_rad(data.in, unit.in)
data.out <- convert_from_rad(data.ref, unit.out)
return(data.out)
}
fConvTime <- function(data.in, unit.in, unit.out) {
## Convert between units of time:
## * second = "sec"
## * minute = "min"
## * hour = "hr"
## * day = "day"
## * week = "wk"
##
## INPUT:
## data.in = data in original unit
## unit.in = original unit
## unit.out = final unit
## OUTPUT:
## data.out = data in final unit
## EXAMPLE:
## xx <- fConvTime(2, "day", "sec")
## ------------------------------------------------------------
## from original unit to reference unit (sec)
convert_to_sec <- function(data, unit) {
switch(unit,
"sec" = data,
"min" = data * 60,
"hr" = data * 3600,
"day" = data * 86400,
"wk" = data * (86400 * 7),
stop("INPUT ERROR: unit not found")
)
}
## from reference unit (sec) to final unit
convert_from_sec <- function(data, unit) {
switch(unit,
"sec" = data,
"min" = data / 60,
"hr" = data / 3600,
"day" = data / 86400,
"wk" = data / (86400 * 7),
stop("INPUT ERROR: unit not found")
)
}
## convert data from original unit to final unit
data.ref <- convert_to_sec(data.in, unit.in)
data.out <- convert_from_sec(data.ref, unit.out)
return(data.out)
}
### ---------------------------------------------------------------- ###
fConcGas <- function(data.in, unit.in, unit.out, temp, press, m.mass=NULL) {
## Convert between units of concentration (gas-phase):
## * molecule cm-3 = "ND"
## * ppth = "ppth"
## * ppm = "ppm"
## * ppb = "ppb"
## * ppt = "ppt"
## * mole m-3 = "MD"
## * ug m-3 = "UG"
##
## NB: molar mass is required only for conversions to/from "UG".
##
## INPUT:
## data.in = data in original concentration unit
## unit.in = original concentration unit
## unit.out = final concentration unit
## temp = temperature (K)
## press = pressure (Pa)
## m.mass = molar mass (g/mole) [ OPTIONAL ]
## OUTPUT:
## data.out = data in final concentration unit
## EXAMPLE:
## xx <- fConcGas(data_df$O3, "ppb", "MD", data_df$Temp, data_df$Press)
## ------------------------------------------------------------
## check molar mass input
if (unit.in == "UG" || unit.out == "UG") {
if (is.null(m.mass)) {
stop("INPUT ERROR: molar mass needed")
}
}
## Avogadro number, number density of air
n.avog <- fConstant("Na")$Value
m.air <- fAirND(temp, press)$M
## from original unit to reference unit (ND)
convert_to_ND <- function(data, unit) {
switch(unit,
"ND" = data,
"ppth" = data * (m.air * 1.0e-03),
"ppm" = data * (m.air * 1.0e-06),
"ppb" = data * (m.air * 1.0e-09),
"ppt" = data * (m.air * 1.0e-12),
"MD" = data * (n.avog * 1.0e-06),
"UG" = {
data.tmp <- data * (n.avog * 1.0e-12)
if (length(m.mass) == 1) {
data.tmp / m.mass
} else {
t(apply(data.tmp, 1, function(x) x / m.mass))
}
},
stop("INPUT ERROR: unit not found")
)
}
## from reference unit (ND) to final unit
convert_from_ND <- function(data, unit) {
switch(unit,
"ND" = data,
"ppth" = data / (m.air * 1.0e-03),
"ppm" = data / (m.air * 1.0e-06),
"ppb" = data / (m.air * 1.0e-09),
"ppt" = data / (m.air * 1.0e-12),
"MD" = data / (n.avog * 1.0e-06),
"UG" = {
data.tmp <- data / (n.avog * 1.0e-12)
if (length(m.mass) == 1) {
data.tmp * m.mass
} else {
t(apply(data.tmp, 1, function(x) x * m.mass))
}
},
stop("INPUT ERROR: unit not found")
)
}
## convert concentration from original unit to final unit
data.ref <- convert_to_ND(data.in, unit.in)
data.out <- convert_from_ND(data.ref, unit.out)
return(data.out)
}
fConcAq <- function(data.in, unit.in, unit.out, m.mass=NULL) {
## Convert between units of concentration (aqueous-phase):
## * mole/L (molarity) = "M"
## * mole m-3 = "MD"
## * ug m-3 = "UG"
##
## NB: molar mass is required only for conversions to/from "UG".
##
## INPUT:
## data.in = data in original concentration unit
## unit.in = original concentration unit
## unit.out = final concentration unit
## m.mass = molar mass (g/mole) [ OPTIONAL ]
## OUTPUT:
## data.out = data in final concentration unit
## EXAMPLE:
## xx <- fConcAq(data_df$NO3, "M", "UG", 62)
## -------------------------------------------------------------
## check molar mass input
if (unit.in == "UG" || unit.out == "UG") {
if (is.null(m.mass)) {
stop("INPUT ERROR: molar mass needed")
}
}
## from original unit to reference unit (M)
convert_to_M <- function(data, unit) {
switch(unit,
"M" = data,
"MD" = data * 1.0e-03,
"UG" = {
data.tmp <- data * 1.0e-09
if (length(m.mass) == 1) {
data.tmp / m.mass
} else {
t(apply(data.tmp, 1, function(x) x / m.mass))
}
},
stop("INPUT ERROR: unit not found")
)
}
## from reference unit (M) to final unit
convert_from_M <- function(data, unit) {
switch(unit,
"M" = data,
"MD" = data / 1.0e-03,
"UG" = {
data.tmp <- data / 1.0e-09
if (length(m.mass) == 1) {
data.tmp * m.mass
} else {
t(apply(data.tmp, 1, function(x) x * m.mass))
}
},
stop("INPUT ERROR: unit not found")
)
}
## convert concentration from original unit to final unit
data.ref <- convert_to_M(data.in, unit.in)
data.out <- convert_from_M(data.ref, unit.out)
return(data.out)
}
fHumid <- function(data.in, unit.in, unit.out, temp, press=101325) {
## Convert between units of humidity at given temperature and
## pressure:
## * absolute humidity = "AH"
## mass of water vapour per volume air (g/m3)
## * specific humidity = "SH"
## mass of water vapour per mass air (g/kg)
## * mixing ratio = "MR"
## mass of water vapour per mass dry air (g/kg)
## * relative humidity = "RH"
## water vapour pressure to water vapour saturation pressure (%)
## * parts per million = "PPM"
## volume of water vapour per volume of dry air (ppm)
##
## [ from "Humidity Conversion Formulas", published by Vaisala:
## https://www.vaisala.com/ ]
##
## NB: pressure is required only for conversions to/from "PPM".
## If not specified, it is assumed to be 1 atm.
##
## INPUT:
## data.in = data in original humidity unit
## unit.in = original humidity unit
## unit.out = final humidity unit
## temp = temperature (K)
## press = pressure (Pa) [ OPTIONAL, default=101325 ]
## OUTPUT:
## data.out = data in final humidity unit
## EXAMPLE:
## xx <- fHumid(data_df, "AH", "RH", data_df$Temp)
## ------------------------------------------------------------
## convert temperature to C, pressure to hPa
temp.c <- fConvTemp(temp, "K", "C")
press.hpa <- fConvPress(press, "Pa", "hPa")
## water vapour saturation pressure (hPa)
pws <- 6.116441 * 10^( (7.591386 * temp.c) / (temp.c + 240.7263) )
## from original humidity to water vapour pressure (hPa)
convert_to_pw <- function(data, unit) {
switch(unit,
"AH" = 1.0e-02 * (data * temp.c) / 2.16679,
"SH" = 1.0e-03 * (data * press.hpa) / 0.622,
"MR" = (data * press.hpa) / (621.9907 + data),
"RH" = data * pws / 100,
"PPM" = (data * 1.0e-06 * press.hpa) / (1 + data * 1.0e-06),
stop("INPUT ERROR: unit not found")
)
}
## from water vapour pressure (hPa) to final humidity
convert_from_pw <- function(data, unit) {
switch(unit,
"AH" = 1.0e+02 * (data * 2.16679) / temp.c,
"SH" = 1.0e+03 * (data * 0.622) / press.hpa,
"MR" = (621.9907 * data) / (press.hpa - data),
"RH" = 100 * data / pws,
"PPM" = 1.0e+06 * data / (press.hpa - data),
stop("INPUT ERROR: unit not found")
)
}
## convert humidity from original unit to final unit
data.ref <- convert_to_pw(data.in, unit.in)
data.out <- convert_from_pw(data.ref, unit.out)
return(data.out)
}