11# Shared EVI -> LAI and LAI -> C/N tables for planting.
22# CLI: apply_planting.R
33# make_events_statewide planting reads assigned_year=Y_planting.parquet
4- # (does not recompute). Hay and woody are not planted here.
4+ # (does not recompute). Hay and mature woody are not planted here; YP is.
5+
6+ # Young perennial (CLASS=YP) has no crop subclass, so planting traits must come
7+ # from an orchard/vineyard code (D, C, or V).
8+ resolve_yp_planting_codes <- function (matched , year , paths ) {
9+ idx <- which(toupper(trimws(as.character(matched $ landiq_CLASS ))) == " YP" )
10+ if (! length(idx )) return (matched )
11+
12+ # Load existing transition CSVs (does not rebuild them).
13+ gf <- trimws(Sys.getenv(
14+ " LANDIQ_GAPFILL_ROOT" , file.path(Sys.getenv(" CCMMF_CODE" ), " landiq-gapfill" )
15+ ))
16+ source(file.path(gf , " scripts" , " R" , " county_transition.R" ), local = TRUE )
17+ state_csv <- file.path(dirname(paths $ cropcode_csv ), " state_transition_matrix.csv" )
18+ ag <- rownames(read.csv(state_csv , row.names = 1 , check.names = FALSE ))
19+ mats <- load_county_transition_matrices(path_county_transition_dir(), ag )
20+ fb <- load_transition_matrix_csv(state_csv , ag )
21+
22+ ov <- c(" D" , " C" , " V" )
23+ # Argmax of YP -> D/C/V from the county matrix (or statewide fallback).
24+ prior_one <- function (county ) {
25+ st <- county_matrix_stem(county )
26+ A <- if (! is.na(st ) && nzchar(st ) && st %in% names(mats )) mats [[st ]] else fb
27+ p <- as.numeric(A [" YP" , ov ])
28+ names(p ) <- ov
29+ p [! is.finite(p )] <- 0
30+ if (! any(p > 0 )) " D" else names(p )[which.max(p )]
31+ }
32+
33+ cls <- character (length(idx ))
34+ sub <- rep(" **" , length(idx ))
35+ county <- rep(NA_character_ , length(idx ))
36+
37+ # 1) Look-ahead: season-2 identity year -> year+1 (same helper as harvest clearing).
38+ tr <- load_landiq_season2_lookahead(year , paths )
39+ if (! is.null(tr )) {
40+ j <- match(as.character(matched $ parcel_id [idx ]), as.character(tr $ parcel_id ))
41+ hit <- ! is.na(j )
42+ county [hit ] <- as.character(tr $ prior_COUNTY [j [hit ]])
43+ cc <- toupper(trimws(as.character(tr $ curr_CLASS [j ])))
44+ # Only accept a mature orchard/vineyard destination.
45+ ok <- hit & cc %in% ov
46+ cls [ok ] <- cc [ok ]
47+ s <- as.character(tr $ curr_SUBCLASS [j [ok ]])
48+ s [is.na(s ) | trimws(s ) %in% c(" " , " **" )] <- " **"
49+ sub [ok ] <- s
50+ }
51+
52+ # 2) Still YP / no usable next class -> county (or state) transition prior.
53+ need <- ! nzchar(cls )
54+ cls [need ] <- vapply(county [need ], prior_one , character (1 ))
55+ message(
56+ " [planting] YP trait codes: " , length(idx ),
57+ " (look-ahead=" , sum(! need ), " , county prior=" , sum(need ), " )"
58+ )
59+ # Overwrite YP with the resolved D/C/V code used by the planting lookup.
60+ matched [idx , `:=`(landiq_CLASS = cls , landiq_SUBCLASS = sub )]
61+ matched
62+ }
563
664planting_keep_dated_crop_rows <- function (matched ) {
765 if (! " planting_date_str" %in% names(matched )) {
@@ -11,9 +69,19 @@ planting_keep_dated_crop_rows <- function(matched) {
1169 keep <- ! is.na(d ) & nzchar(d ) & d != " NA"
1270 matched <- matched [keep ]
1371 pft_l <- tolower(trimws(as.character(matched $ landiq_PFT )))
14- # Annual planting only (row, rice). Hay and woody are not planted each year;
15- # they get phenology + harvest instead. YP OGI stays on the overlay.
16- matched <- matched [! pft_l %in% c(" other" , " woody" , " hay" )]
72+ specond <- if (" landiq_SPECOND" %in% names(matched )) {
73+ as.character(matched $ landiq_SPECOND )
74+ } else {
75+ rep(NA_character_ , nrow(matched ))
76+ }
77+ # Plant annuals plus young woody. CLASS=YP needs trait resolve below;
78+ # SPECOND=Y already carries a D/C/V class and uses those traits as-is.
79+ # Mature woody and hay stay phenology + harvest only.
80+ young <- pft_l == " woody" & (
81+ toupper(trimws(as.character(matched $ landiq_CLASS ))) == " YP" |
82+ toupper(trimws(specond )) == " Y"
83+ )
84+ matched <- matched [pft_l %in% c(" row" , " rice" ) | young ]
1785 matched
1886}
1987
@@ -92,8 +160,12 @@ lookup_planting_lai_fallback <- function(class, pft, fb) {
92160}
93161
94162# Overlay rows -> LAI via compute_lai_from_mslsp (or CLASS/PFT fallback).
95- build_planting_lai_table <- function (matched , pool_env ) {
163+ # Keep filter first (while CLASS is still YP), then rewrite YP to D/C/V.
164+ build_planting_lai_table <- function (matched , pool_env , year = NULL , paths = NULL ) {
96165 matched <- planting_keep_dated_crop_rows(matched )
166+ if (! is.null(year ) && ! is.null(paths )) {
167+ matched <- resolve_yp_planting_codes(matched , year , paths )
168+ }
97169 message(" [lai] Crop rows with planting date: " , nrow(matched ))
98170 lai_fb <- planting_lai_fallbacks(matched , pool_env )
99171 message(
0 commit comments