Skip to content

Commit e83c2f9

Browse files
authored
Merge pull request #54 from ccmmf/events-update
Event building: filtered and canonical
2 parents ae4d40e + afdac91 commit e83c2f9

5 files changed

Lines changed: 98 additions & 47 deletions

File tree

examples/3_rowcrop/example_user_config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ aws_profile: magic
3535
# Stage example-specific input files into run_dir before prepare runs.
3636
external_paths:
3737
template_file: "examples/3_rowcrop/template.xml"
38+
39+
paths:
40+
field_shape_path: "data_raw/management/crops/v4.1/parcels-consolidated.gpkg"
41+
phenology_info_path: "data_raw/management/phenology/v1.0/"
42+
planting_info_path: "data_raw/management/planting/v1.0/"
43+
harvest_info_path: "data_raw/management/harvest/v1.0/"
44+
tillage_info_path: "data_raw/management/tillage/v1.0/"
45+
irrigation_info_path: "data_raw/management/irrigation/v1.1/"
46+

tools/event_prep/01a-clean-irrigation.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ options <- list(
1717
"Format will be Parquet organized by ensemble member,",
1818
"with subdirectories named in 'hive partion' style."
1919
)
20+
),
21+
optparse::make_option("--site_info_path",
22+
default = "site_info.csv",
23+
help = "CSV giving ids to be extracted. Only column 'field_id' is used"
2024
)
2125
) |>
2226
# Show default values in help message
@@ -31,6 +35,10 @@ args <- optparse::OptionParser(option_list = options) |>
3135
## -------------------------- end option parsing ------------------------------
3236

3337
dir.create(args$outdir, showWarnings = FALSE, recursive = TRUE)
38+
siteids <- read.csv(args$site_info_path) |>
39+
_$field_id |>
40+
unique() |>
41+
glue::glue_collapse(",")
3442

3543
dbdir <- tempfile("duckdb", fileext = ".duckdb")
3644
conn <- DBI::dbConnect(duckdb::duckdb(dbdir = dbdir))
@@ -58,6 +66,7 @@ DBI::dbExecute(conn, glue::glue("
5866
CAST (amount_mm AS DECIMAL(6, 2)) AS amount_mm,
5967
method
6068
FROM read_parquet('{args$irr_path}')
69+
WHERE site_id IN ({siteids})
6170
ORDER BY event_member_id, site_id, date
6271
) TO
6372
'{args$outdir}/irrigation.parquet'

tools/event_prep/01b-clean-other-events.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
## ---------------------- parse command-line options --------------------------
88
options <- list(
9+
optparse::make_option("--site_info_path",
10+
default = "site_info.csv",
11+
help = "CSV giving ids to be extracted. Only column 'field_id' is used"
12+
),
913
optparse::make_option("--pheno_dir",
1014
default = "data_raw/management/phenology/v1.0",
1115
help = "Directory containing Parquet files of phenology (leafon/leafoff) events"
@@ -53,6 +57,9 @@ args <- optparse::OptionParser(option_list = options) |>
5357

5458
## -------------------------- end option parsing ------------------------------
5559

60+
siteids <- read.csv(args$site_info_path) |>
61+
_$field_id |>
62+
unique()
5663

5764
harvest_files <- list.files(args$harvest_dir, "\\.parquet$", full.names = TRUE, recursive = TRUE)
5865
planting_files <- list.files(args$planting_dir, "\\.parquet$", full.names = TRUE, recursive = TRUE)
@@ -62,6 +69,7 @@ dir.create(args$outdir, showWarnings = FALSE, recursive = TRUE)
6269

6370
message("Writing harvest output")
6471
harvest <- arrow::open_dataset(harvest_files, format = "parquet") |>
72+
dplyr::filter(as.character(site_id) %in% siteids) |>
6573
dplyr::mutate(
6674
site_id = as.integer(site_id),
6775
date = as.Date(date)
@@ -74,6 +82,7 @@ harvest <- arrow::open_dataset(harvest_files, format = "parquet") |>
7482

7583
message("Writing planting output")
7684
planting <- arrow::open_dataset(planting_files, format = "parquet") |>
85+
dplyr::filter(as.character(site_id) %in% siteids) |>
7786
dplyr::mutate(
7887
site_id = as.integer(site_id),
7988
date = pmax(as.Date(date), as.Date(args$adjust_start)) # push earlier plantings forward to avoid beginning-of-run boundary error
@@ -96,6 +105,7 @@ planting <- arrow::open_dataset(planting_files, format = "parquet") |>
96105

97106
message("Writing tillage output")
98107
tillage <- arrow::open_dataset(tillage_files, format = "parquet") |>
108+
dplyr::filter(as.character(site_id) %in% siteids) |>
99109
dplyr::filter(
100110
is.finite(.data$ndti_pct_change),
101111
.data$ndti_pct_change >= 0
@@ -121,6 +131,7 @@ tillage <- arrow::open_dataset(tillage_files, format = "parquet") |>
121131
message("Writing phenology output")
122132
phenology <- arrow::open_dataset(phenology_files, format = "parquet")
123133
leafon <- phenology |>
134+
dplyr::filter(as.character(site_id) %in% siteids) |>
124135
dplyr::select("site_id", date = "leafonday") |>
125136
dplyr::mutate(
126137
site_id = as.integer(.data$site_id),
@@ -131,6 +142,7 @@ leafon <- phenology |>
131142
compression = "ZSTD"
132143
)
133144
leafoff <- phenology |>
145+
dplyr::filter(site_id %in% siteids) |>
134146
dplyr::select("site_id", date = "leafoffday") |>
135147
dplyr::mutate(
136148
site_id = as.integer(.data$site_id),
Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,27 @@ options <- list(
1111
default = "site_info.csv",
1212
help = "CSV giving ids, locations, and PFTs for sites of interest"
1313
),
14-
optparse::make_option("--raw_parquet_dir",
15-
default = "data_raw/management",
16-
help = paste(
17-
"Directory containing management inputs in Parquet format.",
18-
"Note: Code currently looks for subpaths that include hard-coded",
19-
"version numbers for each management type.",
20-
"If this path is empty, cleaning is skipped and clean Parquet files",
21-
" must already exist at the path specified by --clean_parquet_dir`."
22-
)
14+
optparse::make_option("--phenology_path",
15+
default = "data_raw/management/phenology/v1.0",
16+
help = "Parquet file or directory of Parquet files containing phenology events",
17+
),
18+
optparse::make_option("--planting_path",
19+
default = "data_raw/management/planting/v1.0",
20+
help = "Parquet file or directory of Parquet files containing planting events",
21+
),
22+
optparse::make_option("--harvest_path",
23+
default = "data_raw/management/harvest/v1.0",
24+
help = "Parquet file or directory of Parquet files containing harvest events",
25+
),
26+
optparse::make_option("--tillage_path",
27+
default = "data_raw/management/tillage/v1.0",
28+
help = "Parquet file or directory of Parquet files containing tillage events",
2329
),
30+
optparse::make_option("--irrigation_path",
31+
default = "data_raw/management/irrigation/v1.1",
32+
help = "Parquet file or directory of Parquet files containing tillage events",
33+
),
34+
# TODO add fertilization and NCC here
2435
optparse::make_option("--clean_parquet_dir",
2536
default = "data/management_ensembles",
2637
help = paste(
@@ -60,19 +71,7 @@ this_file <- function() {
6071
cmd_args <- commandArgs(trailingOnly = FALSE)
6172
normalizePath(sub("^--file=", "", grep("^--file=", cmd_args, value = TRUE)))
6273
}
63-
event_prep_dir <- normalizePath(file.path(dirname(this_file()), "..", "..", "tools", "event_prep"))
64-
65-
# TODO these probably deserve to be runtime args,
66-
# but first better fix other version-specific assumptions below
67-
mgmt_subdirs <- list(
68-
pheno = file.path(args$raw_parquet_dir, "phenology/v1.0"),
69-
plant = file.path(args$raw_parquet_dir, "planting/v1.0"),
70-
harv = file.path(args$raw_parquet_dir, "harvest/v1.0"),
71-
till = file.path(args$raw_parquet_dir, "tillage/v1.0"),
72-
irri = file.path(args$raw_parquet_dir, "irrigation/v1.1"),
73-
fert = NULL, # TODO
74-
occ = NULL # TODO
75-
)
74+
event_prep_dir <- normalizePath(file.path(dirname(this_file()), "..", "tools", "event_prep"))
7675

7776
if (!dir.exists(args$event_outdir)) {
7877
dir.create(args$event_outdir, recursive = TRUE)
@@ -82,28 +81,30 @@ cargs <- function(...) {
8281
paste0("--", ...names(), "=", list(...))
8382
}
8483

85-
if (args$raw_parquet_dir != "") {
86-
PEcAn.logger::logger.info("Cleaning irrigation files")
87-
callr::rscript(
88-
file.path(event_prep_dir, "01a-clean-irrigation.R"),
89-
cmdargs = cargs(
90-
irr_path = mgmt_subdirs$irri,
91-
outdir = args$clean_parquet_dir
92-
)
84+
85+
PEcAn.logger::logger.info("Cleaning irrigation files")
86+
callr::rscript(
87+
file.path(event_prep_dir, "01a-clean-irrigation.R"),
88+
cmdargs = cargs(
89+
irr_path = args$irrigation_path,
90+
outdir = args$clean_parquet_dir,
91+
site_info_path = args$site_info_path
9392
)
94-
PEcAn.logger::logger.info("Cleaning other management files")
95-
callr::rscript(
96-
file.path(event_prep_dir, "01b-clean-other-events.R"),
97-
cmdargs = cargs(
98-
pheno_dir = mgmt_subdirs$pheno,
99-
planting_dir = mgmt_subdirs$plant,
100-
harvest_dir = mgmt_subdirs$harv,
101-
tillage_dir = mgmt_subdirs$till,
102-
adjust_start = args$start_date,
103-
outdir = args$clean_parquet_dir
104-
)
93+
)
94+
PEcAn.logger::logger.info("Cleaning other management files")
95+
callr::rscript(
96+
file.path(event_prep_dir, "01b-clean-other-events.R"),
97+
cmdargs = cargs(
98+
site_info_path = args$site_info_path,
99+
pheno_dir = args$phenology_path,
100+
planting_dir = args$planting_path,
101+
harvest_dir = args$harvest_path,
102+
tillage_dir = args$tillage_path,
103+
adjust_start = args$start_date,
104+
outdir = args$clean_parquet_dir
105105
)
106-
}
106+
)
107+
107108

108109
PEcAn.logger::logger.info("converting management files to events")
109110
callr::rscript(

workflow/workflow_manifest.yaml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ steps:
6161
outputs:
6262
site_sipnet_met_path: site_sipnet_met_path
6363

64+
- name: build-events
65+
script: "workflow/02a_build_events.R"
66+
r_libraries: [tidyverse, callr, glue]
67+
inputs:
68+
site_info_path: site_info_file
69+
phenology_path: phenology_info_path
70+
planting_path: planting_info_path
71+
harvest_path: harvest_info_path
72+
tillage_path: tillage_info_path
73+
irrigation_path: irrigation_info_path
74+
params:
75+
start_date: start_date
76+
end_date: end_date
77+
outputs:
78+
clean_parquet_dir: clean_parquet_dir
79+
event_outdir: event_dir
80+
81+
6482
- name: build-ic
6583
script: "workflow/02_ic_build.R"
6684
r_libraries: [tidyverse]
@@ -253,11 +271,15 @@ steps:
253271
data_dir: data_dir
254272

255273
- name: build-events
256-
script: "examples/3_rowcrop/02a_build_events.R"
257-
r_libraries: [tidyverse, callr]
274+
script: "workflow/02a_build_events.R"
275+
r_libraries: [tidyverse, callr, glue]
258276
inputs:
259277
site_info_path: site_info_file
260-
raw_parquet_dir: raw_parquet_dir
278+
phenology_path: phenology_info_path
279+
planting_path: planting_info_path
280+
harvest_path: harvest_info_path
281+
tillage_path: tillage_info_path
282+
irrigation_path: irrigation_info_path
261283
params:
262284
start_date: start_date
263285
end_date: end_date
@@ -309,7 +331,6 @@ paths:
309331
site_info_file: "site_info.csv"
310332
site_sipnet_met_path: "data/ERA5_SIPNET"
311333
site_era5_path: "data_raw/ERA5_CA_nc"
312-
field_shape_path: "data_raw/management/crops/v4.1/parcels-consolidated.gpkg"
313334
data_dir: "data/IC_prep"
314335
ic_outdir: "IC_files"
315336
pft_dir: "data_raw/pfts"
@@ -322,7 +343,6 @@ paths:
322343
ic_dir: "IC_files"
323344
settings_xml: "settings.xml"
324345
config_xml: "output/pecan.CONFIGS.xml"
325-
raw_parquet_dir: "data_raw/management"
326346
clean_parquet_dir: "data/management_ensembles"
327347
event_dir: "data/events"
328348

0 commit comments

Comments
 (0)