From b0820b151942535c06466b134940c2d31048facf Mon Sep 17 00:00:00 2001 From: Claire Richert <claire.richert@agroparistech.fr> Date: Thu, 30 Jan 2025 15:08:05 +0100 Subject: [PATCH] fix bug in calc_isCycle: days numbered from first day of simu instead of day of year --- R/calc_isCycle.R | 2 +- tests/testthat/test-calc_isCycle.R | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/R/calc_isCycle.R b/R/calc_isCycle.R index 589e8d6..7f6f1ad 100644 --- a/R/calc_isCycle.R +++ b/R/calc_isCycle.R @@ -19,6 +19,6 @@ calc_isCycle <- function(cp, DatesR, sowing_date = cp$sowing_date) { doy_isCycle[which(doy_isCycle == 0):length(doy_isCycle)] <- doy_isCycle[which(doy_isCycle == 0):length(doy_isCycle)] + 1 } - isCycle <- lubridate::yday(DatesR) %in% doy_isCycle + isCycle <- l$xout %in% doy_isCycle return(isCycle) } diff --git a/tests/testthat/test-calc_isCycle.R b/tests/testthat/test-calc_isCycle.R index 0dfddc0..0c8e7fc 100644 --- a/tests/testthat/test-calc_isCycle.R +++ b/tests/testthat/test-calc_isCycle.R @@ -8,3 +8,26 @@ test_that("Cycle over 1st January works", { expect_true(isCycle[1]) expect_true(isCycle[length(isCycle)]) }) + + +test_that("Cycle with simulation not starting on the 1st of January works", { + + cp <- load_crop_params() + cp <- cp[cp$crop == "SB2023-colza", ] + DatesR <- seq(as.Date("2019-08-15"), as.Date("2020-08-15"), by = "1 day") + isCycle <- calc_isCycle(cp, DatesR) + + year <- lubridate::year(DatesR[1]) + sowing_date <- as.Date(paste0(year, "-", cp$sowing_date)) + + # The cycle starts 1 day before the sowing date + first_cycle_day <- which(DatesR == sowing_date - 1) + length_cycle <- cp$Lini + cp$Ldev + cp$Lmid + cp$Lend + last_cycle_day <- which(DatesR == sowing_date -1 + length_cycle) + + expect_equal(length(isCycle), length(DatesR)) + + expect_false(unique(isCycle[1:(first_cycle_day - 1)])) + expect_true(unique(isCycle[first_cycle_day:(last_cycle_day - 1)])) + expect_false(unique(isCycle[last_cycle_day:length(isCycle)])) +}) -- GitLab