forked from PecanProject/pecan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlook_up_ca_n_rate.R
More file actions
208 lines (193 loc) · 7.04 KB
/
Copy pathlook_up_ca_n_rate.R
File metadata and controls
208 lines (193 loc) · 7.04 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
#' Look up California N application rates by crop
#'
#' Returns recommended nitrogen application rate ranges for California crops.
#' Rates are provided in both imperial (lbs N/acre) and SI (g N/m2) units.
#'
#' Matching is case-insensitive. Exact matches are returned directly.
#' If no exact match is found, partial matching is used to suggest
#' possible crops and an empty data frame is returned.
#'
#' @param crop Character string. Crop name to look up.
#' @param pft_group Optional character string. Filter results to a specific
#' plant functional type group (e.g. "row", "woody", "rice").
#' @param unit Character, one of "g_m2" (default) or "lbs_acre". Controls
#' which columns appear as `min_n` and `max_n` in the output.
#'
#' @return A tibble with columns: `pft_group`, `crop`, `min_n`, `max_n`,
#' `source`. The `min_n` and `max_n` columns are in the requested unit.
#' Returns an empty tibble (with warning) if no match is found.
#'
#' @source Rosenstock, T. S., Liptzin, D., Six, J., & Tomich, T. P. (2013).
#' Nitrogen fertilizer use in California: Assessing the data, trends and a
#' way forward. California Agriculture, 67(1).
#' \url{https://escholarship.org/uc/item/5mk2q1sm}
#' @source Meyer, R. D., Marcum, D. B., Orloff, S. B., & Schmierer, J. L.
#' (2007). Alfalfa fertilization strategies. UC ANR Publication 8292.
#'
#' @seealso [look_up_fertilizer_components()] for fertilizer nutrient
#' composition (N/C fractions) from the SWAT/DayCent database.
#' [ca_n_application_rate] for the underlying dataset.
#'
#' @examples
#' look_up_ca_n_rate("Tomatoes, Processing")
#' look_up_ca_n_rate("corn")
#' look_up_ca_n_rate("wheat", unit = "lbs_acre")
#' look_up_ca_n_rate("pistachios", pft_group = "woody")
#'
#' @export
look_up_ca_n_rate <- function(
crop,
pft_group = NULL,
unit = c("g_m2", "lbs_acre")
) {
unit <- match.arg(unit)
dat <- PEcAn.data.land::ca_n_application_rate
if (!is.null(pft_group)) {
dat <- dat |>
dplyr::filter(tolower(.data$pft_group) == tolower(.env$pft_group))
}
# try exact match first (case-insensitive)
result <- dat |>
dplyr::filter(tolower(.data$crop) == tolower(.env$crop))
# if no exact match, try partial and suggest
if (nrow(result) == 0) {
partial <- dat |>
dplyr::filter(grepl(tolower(.env$crop), tolower(.data$crop), fixed = TRUE))
if (nrow(partial) > 0) {
PEcAn.logger::logger.warn(
"No exact match for '", crop, "'. ",
"Did you mean one of: ",
paste(unique(partial$crop), collapse = ", "), "?"
)
} else {
PEcAn.logger::logger.warn(
"No N application rate found for crop '", crop, "'"
)
}
return(dplyr::tibble(
pft_group = character(),
crop = character(),
min_n = numeric(),
max_n = numeric(),
source = character()
))
}
if (unit == "g_m2") {
result |>
dplyr::transmute(
.data$pft_group,
.data$crop,
min_n = .data$min_n_g_m2,
max_n = .data$max_n_g_m2,
.data$source
)
} else {
result |>
dplyr::transmute(
.data$pft_group,
.data$crop,
min_n = .data$min_n_lbs_acre,
max_n = .data$max_n_lbs_acre,
.data$source
)
}
}
#' Look up California organic amendment properties
#'
#' Returns properties of organic amendment materials including carbon and
#' nitrogen content, C:N ratio, and plant-available nitrogen (PAN).
#' Application rates live in
#' \code{\link{ca_organic_amendment_app_rate}}; join on \code{material}.
#'
#' Matching is case-insensitive. Exact matches are returned directly.
#' If no exact match is found, partial matching suggests possible materials.
#'
#' Some materials have multiple rows from different sources (e.g. Cow manure,
#' Vegetable waste). Set `aggregate = "mean"` to collapse these into a
#' single row per material using the mean of numeric columns.
#'
#' @param material Character string. Amendment material to look up.
#' @param n_class Optional, one of "LOWER" or "HIGHER". Filter by N class.
#' @param aggregate Character, one of "none" (default) or "mean".
#' If "mean", rows for the same material are averaged into a single row.
#'
#' @return A tibble with columns: `material`, `material_class`,
#' `cn_min`, `cn_max`, `cn_avg`, `n_pct`, `pan_pct`, `n_class`, `source`.
#' Returns an empty tibble (with a warning) if no match is found.
#'
#' @source Eghball, B. Composting Manure and Other Organic Residues.
#' University of Nebraska-Lincoln Extension, Publication G2222.
#' \url{https://extensionpubs.unl.edu/publication/g2222/na/html/view}
#' @source Rynk, R. (ed.) Compost Production and Use in Sustainable
#' Farming Systems. NC State Extension.
#' \url{https://content.ces.ncsu.edu/compost-production-and-use-in-sustainable-farming-systems}
#'
#' @seealso [look_up_fertilizer_components()] for fertilizer nutrient
#' composition (N/C fractions) from the SWAT/DayCent database.
#' [ca_organic_amendment_properties] for the underlying dataset.
#' [ca_organic_amendment_app_rate] for the matching application rates.
#'
#' @examples
#' look_up_ca_organic_amendment("Cow manure")
#' look_up_ca_organic_amendment("Cow manure", aggregate = "mean")
#' look_up_ca_organic_amendment("Poultry litter", n_class = "LOWER")
#'
#' @export
look_up_ca_organic_amendment <- function(
material,
n_class = NULL,
aggregate = c("none", "mean")
) {
aggregate <- match.arg(aggregate)
dat <- PEcAn.data.land::ca_organic_amendment_properties
if (!is.null(n_class)) {
dat <- dat |>
dplyr::filter(toupper(.data$n_class) == toupper(.env$n_class))
}
# try exact match first (case-insensitive)
result <- dat |>
dplyr::filter(tolower(.data$material) == tolower(.env$material))
# if no exact match, try partial and suggest
if (nrow(result) == 0) {
partial <- dat |>
dplyr::filter(grepl(tolower(.env$material), tolower(.data$material), fixed = TRUE))
if (nrow(partial) > 0) {
PEcAn.logger::logger.warn(
"No exact match for '", material, "'. ",
"Did you mean one of: ",
paste(unique(partial$material), collapse = ", "), "?"
)
} else {
PEcAn.logger::logger.warn(
"No organic amendment found for material '", material, "'"
)
}
return(dplyr::tibble(
material = character(), material_class = character(),
cn_min = numeric(), cn_max = numeric(), cn_avg = numeric(),
n_pct = numeric(), pan_pct = numeric(), n_class = character(),
source = character()
))
}
out <- result |>
dplyr::select(
"material", "material_class",
"cn_min", "cn_max", "cn_avg",
"n_pct", "pan_pct", "n_class",
"source"
)
if (aggregate == "mean" && nrow(out) > 1) {
numeric_cols <- c(
"cn_min", "cn_max", "cn_avg", "n_pct", "pan_pct"
)
out <- out |>
dplyr::summarize(
dplyr::across(dplyr::all_of(numeric_cols), mean),
material_class = dplyr::first(.data$material_class),
n_class = dplyr::first(.data$n_class),
source = paste(unique(.data$source), collapse = "; "),
.by = "material"
)
}
out
}