From 84905be1012496b55cbfe9de40471c443eb7a03f Mon Sep 17 00:00:00 2001 From: madhavzanwar Date: Tue, 17 Nov 2026 23:21:01 +0000 Subject: [PATCH] write.config.PEPRMT skip event sorting when no supported events remain Fixes #Sorting an empty list of events with as.Date(sapply(...)) errors. Only sort when there are events.:4255 --- CHANGELOG.md | 1 + models/peprmt/R/write.config.PEPRMT.R | 10 +++-- .../tests/testthat/test-write.config.PEPRMT.R | 39 +++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a565afcd3a3..2bb1931434a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ For more information about this file see also [Keep a Changelog](http://keepacha - Added statewide synthetic fertilization and compost amendment event workflows for CA ag parcels. Outputs share an ensemble naming so a downstream cleaner unions them into one fertilization event type for SIPNET. ### Fixed +- `PEcAn.PEPRMT::write.config.PEPRMT()` failed with a date conversion error when the event file had no supported events for the site (an empty `events` list, or only unsupported event types); such events are now simply skipped (#4255). - `PEcAn.utils::match_file()` anchors file prefixes to the beginning of the filename, so a request for `foo/site1` no longer matches `foo/old-site1` (but still finds `foo/site1-old` as previously) (#4196, @Shubham-Padkonde). - PEcAnAssimSequential: `simple.local()` and `piecew.poly.local()` now scale distances by their `scalef` argument instead of the undefined `rloc`; `GEF()`, `GEF.MultiSite()` and `EnKF.MultiSite()` read their extra arguments from `...` explicitly instead of assigning them into the global environment; `SDA_remote_launcher()` no longer references an undefined `stop.on.error`. Together these clear most of the package's "no visible binding for global variable" check notes (#2758). - `PEcAn.SIPNET::write.config.SIPNET()` wrote `stem_respiration_rate` and `root_respiration_rate` as per day rates into SIPNET's per year `baseVegResp` and `baseFineRootResp`, 365x too low. diff --git a/models/peprmt/R/write.config.PEPRMT.R b/models/peprmt/R/write.config.PEPRMT.R index 37690e5c081..2725fef810a 100644 --- a/models/peprmt/R/write.config.PEPRMT.R +++ b/models/peprmt/R/write.config.PEPRMT.R @@ -157,10 +157,12 @@ write.config.PEPRMT <- function(defaults, trait.values, settings, run.id) { ) evts <- evts[use_evt] } - evt_order <- sapply(evts, \(x) x$date) |> - as.Date() |> - order() - evts <- evts[evt_order] + if (length(evts) > 0) { + evt_order <- sapply(evts, \(x) x$date) |> + as.Date() |> + order() + evts <- evts[evt_order] + } # need to handle events sequentially so effects can stack # TODO think about efficiency, like, at all diff --git a/models/peprmt/tests/testthat/test-write.config.PEPRMT.R b/models/peprmt/tests/testthat/test-write.config.PEPRMT.R index bcbba069d92..90c7eb4d57f 100644 --- a/models/peprmt/tests/testthat/test-write.config.PEPRMT.R +++ b/models/peprmt/tests/testthat/test-write.config.PEPRMT.R @@ -60,3 +60,42 @@ test_that("impose_event", { c(33, 30, 30, 30) ) }) + +test_that("write.config.PEPRMT accepts event files with no supported events", { + d <- withr::local_tempdir() + rundir <- file.path(d, "run", "r1") + dir.create(rundir, recursive = TRUE) + met <- file.path(d, "met.dat") + data.frame( + Year = 2020, DOY_disc = 1:3, DOY = 1:3, TA_C = 10, PAR_umol_m2_day = 5 + ) |> + utils::write.table(met, row.names = FALSE, quote = FALSE) + peprmt_in <- file.path(d, "peprmt.csv") + data.frame( + Year = 2020, DOY_disc = 1:3, WTD_cm = 0, Salinity_daily_ave_ppt = 30 + ) |> + utils::write.csv(peprmt_in, row.names = FALSE) + evt_file <- file.path(d, "events.json") + writeLines( + '[{"site_id": "S1", "events": [{"event_type": "planting", "date": "2020-01-02"}]}]', + evt_file + ) + settings <- list( + host = list(rundir = file.path(d, "run"), outdir = file.path(d, "out")), + model = list(binary = "peprmt"), + run = list( + site = list(id = "S1", lat = 1, lon = 2), + start.date = "2020-01-01", + end.date = "2020-01-03", + inputs = list( + met = list(path = met), + PEPRMT = list(path = peprmt_in), + event_json = list(path = evt_file) + ) + ) + ) + suppressMessages( + write.config.PEPRMT(list(), list(pft1 = list(a0 = 1)), settings, "r1") + ) + expect_true(file.exists(file.path(rundir, "run_data.csv"))) +})