-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_minmax.r
59 lines (57 loc) · 2.18 KB
/
make_minmax.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
# convert daily weather into min/max weather format
library(data.table)
source("util.r")
# This function is for the method that takes old traditional 1pm weather (labelled here as noon)
# and estimates a diurnal temperature range to fit into the scheme that flows into the max/min method
# Temp input in Celsius RH input in Percent. These should be that traditional 1pm values
# Written as a function to enable upgrading later if needs be
temp_min_max <- function(temp_noon, rh_noon) {
temp_range <- 17 - 0.16 * rh_noon + 0.22 * temp_noon
temp_max <- ifelse(temp_range <= 2,
temp_noon + 1,
temp_noon + 2
)
temp_min <- ifelse(temp_range <= 2,
temp_noon - 1,
temp_max - temp_range
)
return(list(temp_min, temp_max))
}
#' Convert daily noon values stream to daily min/max values stream.
#' Uses values from statistics to do the conversion.
#'
#' @param df daily noon values weather stream [lat, long, yr, mon, day, temp, rh, ws, prec]
#' @return daily min/max values weather stream [lat, long, yr, mon, day, temp_min, temp_max, rh_min, rh_max, ws_min, ws_max, prec]
#' @export daily_to_minmax
daily_to_minmax <- function(df) {
df <- data.table(df)
hadId <- FALSE
if ("id" %in% tolower(colnames(df))) {
hadId <- TRUE
}
df[, c("temp_min", "temp_max") := temp_min_max(temp, rh)]
df[, q := findQ(temp, rh)]
df[, rh_min := pmin(100, pmax(0, findrh(q, temp_max)))]
df[, rh_max := pmin(100, pmax(0, findrh(q, temp_min)))]
df[, ws_min := 0.15 * ws]
df[, ws_max := 1.25 * ws]
if (hadId) {
df <- df[, c("id", "lat", "long", "yr", "mon", "day", "temp_min", "temp_max", "rh_min", "rh_max", "ws_min", "ws_max", "prec")]
} else {
df <- df[, c("lat", "long", "yr", "mon", "day", "temp_min", "temp_max", "rh_min", "rh_max", "ws_min", "ws_max", "prec")]
}
return(df)
}
# so this can be run via Rscript
if ("--args" %in% commandArgs()) {
args <- commandArgs(trailingOnly = TRUE)
if (2 == length(args)) {
inp <- args[1]
out <- args[2]
df <- as.data.table(read.csv(inp))
df_minmax <- daily_to_minmax(df)
save_csv(df_minmax, out)
} else {
message("Wrong number of arguments")
}
}