Skip to content

fix(align.met): use length(stamps.hr) instead of stamps.hr as the rep() each argument#4012

Open
anshul23102 wants to merge 1 commit into
PecanProject:developfrom
anshul23102:fix/align-met-rep-each-length
Open

fix(align.met): use length(stamps.hr) instead of stamps.hr as the rep() each argument#4012
anshul23102 wants to merge 1 commit into
PecanProject:developfrom
anshul23102:fix/align-met-rep-each-length

Conversation

@anshul23102
Copy link
Copy Markdown
Contributor

@anshul23102 anshul23102 commented May 25, 2026

Summary

In the ensemble-source branch of align.met(), when the source dataset is at a coarser time step than the training data (align == "repeat"), source values must be repeated to match the finer training resolution. The original code was:

dat.tem <- rep(dat.tem, each = stamps.hr)

stamps.hr is a numeric vector of hour-of-day time stamps (e.g. c(1, 3, 5, ...) for 2-hourly data). Passing a vector to rep()'s each argument is silent in R -- the function uses only the first element and ignores the rest. The first element is hr.train / 2, a floating-point number that gets truncated to an integer, so the replication count is wrong.

The correct scalar to pass is length(stamps.hr) -- the number of output sub-steps that each coarser source value should fill. The equivalent code in the single-time-series branch of the same function (a few hundred lines earlier) already does this correctly:

dat.tem <- rep(dat.tem, each = length(stamps.hr))   # single-series path (correct)

The ensemble-source path was missing the length() call, producing silently corrupted met data with the wrong number of rows any time ensemble source data at a coarser temporal resolution was aligned with higher-resolution training data.

Change

  • modules/data.atmosphere/R/align_met.R line 431: each = stamps.hr -> each = length(stamps.hr).
  • modules/data.atmosphere/NEWS.md: added entry for the development version.

Test plan

  • Call align.met() with ensemble source data at daily resolution and training data at sub-daily resolution (e.g. 3-hourly); confirm the number of rows in met.out$dat.source matches the training data time dimension.
  • Confirm the aligned source values are repeated the correct number of times (e.g. a single daily value repeated 8 times for 3-hourly training data).
  • Confirm the single-time-series path is unaffected.
  • R CMD check on PEcAn.data.atmosphere passes without new warnings or errors.

…ach argument

In the ensemble-source code path of align.met(), when the source data is
at a coarser time step than the training data (align == "repeat"), the
call was:

    rep(dat.tem, each = stamps.hr)

`stamps.hr` is a numeric vector of hour-of-day time stamps, not a scalar
count. R's rep() silently uses only the first element of a vector passed
to `each`, so the replication count was stamps.hr[1] = hr.train / 2,
a floating-point number that gets truncated to an integer, yielding
completely wrong output dimensions and corrupted met data.

The equivalent code in the single-time-series branch (same function,
earlier) correctly writes:

    rep(dat.tem, each = length(stamps.hr))

This one-character fix aligns the ensemble path with the single-series
path and produces the correct number of output rows per source time step.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant