|
1 | | -# getPotentialEvaporation ------------------------------------------------------ |
| 1 | +# getPotentialEvaporation ----------------------------------------------------- |
2 | 2 |
|
3 | 3 | #' Provide Data on Potential Evaporation |
4 | 4 | #' |
5 | | -#' @param input data frame with columns \code{usage}, \code{district} |
6 | | -#' @param config list with entry \code{potentialEvaporation} |
7 | | -#' @param default_etp default value for etp |
8 | | -#' @param default_etps default value for etps |
9 | | -#' @param default_etp_waterbody default value for etp for usage = 110 |
10 | | -#' (waterbody) |
| 5 | +#' @param isWaterbody (vector of) logical indicating whether a block area is |
| 6 | +#' of type (from the type/yield/irrigation tuple) "waterbody" |
| 7 | +#' @param district (vector of) integer indicating the district number of the |
| 8 | +#' plot area (from the original input column "BEZIRK") |
| 9 | +#' @param config list structure as returned by |
| 10 | +#' \code{\link{abimo_config_to_config}} |
11 | 11 | #' @export |
12 | 12 | #' @examples |
13 | | -#' getPotentialEvaporation(data.frame(usage = 10, district = 1), config = list( |
14 | | -#' potentialEvaporation = list(district_1 = list(etp = 100, etps = 200)) |
15 | | -#' )) |
| 13 | +#' \dontrun{ |
| 14 | +#' config <- abimo_config_to_config(kwb.abimo:::read_config()) |
| 15 | +#' getPotentialEvaporation( |
| 16 | +#' is_waterbody = TRUE, |
| 17 | +#' district = 1, |
| 18 | +#' config = config |
| 19 | +#' ) |
| 20 | +#' } |
16 | 21 | #' |
17 | | -getPotentialEvaporation <- function( |
18 | | - input, |
19 | | - config, |
20 | | - default_etp = 660L, |
21 | | - default_etps = 530L, |
22 | | - default_etp_waterbody = 775L |
23 | | -) |
| 22 | +getPotentialEvaporation <- function(isWaterbody, district, config) |
24 | 23 | { |
| 24 | + #`%>%` <- magrittr::`%>%` |
25 | 25 | #kwb.utils::assignPackageObjects("kwb.rabimo") |
26 | | - #input <- kwb.abimo::abimo_input_2019[1:10, ] |
27 | | - #config <- getDefaultConfiguration(1) |
| 26 | + #abimo_config <- kwb.abimo:::read_config() |
28 | 27 |
|
29 | | - # If more than one row is given, call this function for each row |
30 | | - if (nrow(input) > 1L) { |
| 28 | + #data <- data.frame(isWaterbody = FALSE, district = 10L) |
| 29 | + #data <- data.frame(isWaterbody = c(FALSE, TRUE, FALSE), district = 22:24) |
31 | 30 |
|
32 | | - results <- lapply(seq_len(nrow(input)), function(i) { |
33 | | - as.data.frame(getPotentialEvaporation(input[i, ], config)) |
34 | | - }) |
35 | | - |
36 | | - return(do.call(rbind, results)) |
37 | | - } |
| 31 | + # Prepare input data for multi_column_lookup |
| 32 | + data <- data.frame( |
| 33 | + isWaterbody = isWaterbody, |
| 34 | + district = district |
| 35 | + ) |
38 | 36 |
|
39 | | - # waterbody? |
40 | | - result <- if (select_elements(input, "usage") == 110) { |
| 37 | + # Create lookup table from abimo configuration object |
| 38 | + lookup <- select_elements(config, "potentialEvaporation") |
41 | 39 |
|
42 | | - list( |
43 | | - perYearInteger = default_etp_waterbody, |
44 | | - inSummerInteger = -1 # check that |
45 | | - ) |
| 40 | + result <- c(perYearInteger = "etp", inSummerInteger = "etps") %>% |
| 41 | + lapply(function(column) { |
| 42 | + multi_column_lookup(data, select_columns(lookup, c(names(data), column))) |
| 43 | + }) |
46 | 44 |
|
47 | | - } else { |
| 45 | + result[["perYearFloat"]] <- as.double(result[["perYearInteger"]]) |
48 | 46 |
|
49 | | - element <- paste0("district_", select_columns(input, "district")) |
50 | | - x <- select_elements(config, "potentialEvaporation")[[element]] |
51 | | - given <- !is.null(x) |
52 | 47 |
|
53 | | - list( |
54 | | - perYearInteger = if (given) select_elements(x, "etp") else default_etp, |
55 | | - inSummerInteger = if (given) select_elements(x, "etps") else default_etps |
56 | | - ) |
| 48 | + if (all(lengths(result) == 1L)) { |
| 49 | + return(result) |
57 | 50 | } |
58 | 51 |
|
59 | | - result[["perYearFloat"]] = as.double(result[["perYearInteger"]]) |
60 | | - |
61 | | - result |
| 52 | + as.data.frame(result) |
62 | 53 | } |
0 commit comments