Skip to content

fix(sipnet): use max timesteps per day to detect out_day, fixing partial first-day edge case#3989

Open
anshul23102 wants to merge 1 commit into
PecanProject:developfrom
anshul23102:fix/sipnet-out_day-partial-first-day
Open

fix(sipnet): use max timesteps per day to detect out_day, fixing partial first-day edge case#3989
anshul23102 wants to merge 1 commit into
PecanProject:developfrom
anshul23102:fix/sipnet-out_day-partial-first-day

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

Summary

Fixes #3624.

model2netcdf.SIPNET determines how many output timesteps SIPNET writes per day (out_day) by counting rows for a single day. The old code always used the first day of the simulation year:

out_day <- sum(
  sipnet_output$year == simulation_years[1] &
    sipnet_output$day == unique(sipnet_output$day)[1],
  na.rm = TRUE
) # switched to day 2 in case first day is partial

The comment acknowledged the problem (partial first day) but the code still read from day 1 ([1]), not day 2. If SIPNET started mid-day, day 1 would have fewer rows than a complete day, making out_day too small and applying an incorrect timestep to all flux unit conversions for the entire year.

The fix counts rows per day across all days in the first simulation year and takes the maximum, which is the correct count for a complete day regardless of whether the first or last day is partial:

steps_per_day <- table(
  sipnet_output$day[sipnet_output$year == simulation_years[1]]
)
out_day <- max(steps_per_day)

Changes

  • models/sipnet/R/model2netcdf.SIPNET.R: replace single-day row count with max(table(...)) over all days
  • models/sipnet/tests/testthat/test-model2netcdf.SIPNET.R: add regression test with a partial first day (1 timestep on day 1, 2 on days 2 and 3) that would have produced wrong GPP values under the old code
  • models/sipnet/NEWS.md: changelog entry

Test plan

  • New test "out_day is correct when the first day of output is partial" passes
  • Existing SIPNET tests still pass (normal complete-day runs are unaffected)

@anshul23102 anshul23102 force-pushed the fix/sipnet-out_day-partial-first-day branch from 1c4a00d to 3d21280 Compare May 16, 2026 08:05
The previous code counted timesteps only from the first day of the
simulation year to determine out_day (timesteps per day). If the
simulation started mid-day, day 1 would have fewer rows than a
complete day, causing out_day to be undercounted and all flux unit
conversions to be off by a corresponding factor.

Fix by counting rows per day across the entire first year and taking
the maximum, which correctly represents a complete day regardless of
whether the first or last day is partial.

The old comment "switched to day 2 in case first day is partial" also
suggested this intent, but the code still used [1] (day 1). This
removes the ambiguity.

Adds a regression test that verifies GPP conversion is correct when
day 1 has only one timestep and subsequent days have two.

Fixes PecanProject#3624.
@anshul23102 anshul23102 force-pushed the fix/sipnet-out_day-partial-first-day branch from c84f908 to 109f16b Compare May 17, 2026 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model2netcf.SIPNET: Check logic for out_day

2 participants