Skip to content

feat(data.land): Add fraction_urea_n column to fertilizer_composition_data (#3963) - #4047

Open
ANAMASGARD wants to merge 3 commits into
PecanProject:developfrom
ANAMASGARD:feature/add-fraction-urea-n
Open

ANAMASGARD wants to merge 3 commits into
PecanProject:developfrom
ANAMASGARD:feature/add-fraction-urea-n

Conversation

@ANAMASGARD

Copy link
Copy Markdown
Contributor

Fixes #3963

Context

The fertilizer_composition_data table lacked a dedicated column for urea nitrogen (urea-N), forcing developers to inconsistently shoehorn urea data into other fields:

  • urea: encoded in fraction_nh3_n
  • 46_00_00: encoded in fraction_no3_n
  • uan_32: encoded in fraction_organic_n

This PR establishes consistency by creating a clean fraction_urea_n column and updating all affected entries without altering the total nitrogen budget per row.


🛠️ Changes

1. Data Processing (data-raw/create_fertilizer_data.R)

  • Added fraction_urea_n = 0 by default inside the SWAT parsing pipeline.
  • Expanded the custom fertilizer tribble matrix with the new column definition.
  • Re-encoded urea, 46_00_00, and uan_32 rows so their values live inside fraction_urea_n.
  • Used bind_rows(custom, SWAT) |> distinct(name, .keep_all = TRUE) to guarantee that custom overrides cleanly replace legacy SWAT rows.
  • Patched an out-of-date remote SWAT URL pointing to a 404 resource.

2. Query Routine (R/look_up_fertilizer_components.R)

  • Added UREA_N = round(amount * .data$fraction_urea_n) inside the main lookup path.
  • Set UREA_N = 0 inside fallback return tracks (NN-PP-KK format parser and user-specified organic fraction overrides) to protect backward-compatibility.
  • Updated internal dplyr::select() strings to map the extra list item.

3. Documentation & Tests (R/data.R, tests/)

  • Updated roxygen2 blocks and regenerated package documentation files (.Rd) and the binary data asset (.rda).
  • Updated legacy test blocks to expect the new 6-element list layout containing UREA_N.
  • Appended explicit unit test contexts verifying component data splits for uan_32 and 46_00_00.

✅ Verification Proof

All 9 local unit tests pass cleanly on Fedora with zero errors or regressions:
image

Post-Migration Table State:
image

- Add fraction_urea_n column (default 0) to the data table
- Re-encode urea: move 0.46 from fraction_nh3_n to fraction_urea_n
- Re-encode 46_00_00: move 0.46 from fraction_no3_n to fraction_urea_n
- Re-encode uan_32: move 0.165 from fraction_organic_n to fraction_urea_n, zero out fraction_c and cn_ratio (urea-C no longer tracked)
- Update look_up_fertilizer_components() to return UREA_N
- Update documentation and tests
- Use distinct(name) to let custom overrides cleanly replace SWAT rows

Fixes PecanProject#3963

@dlebauer dlebauer left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DO NOT MERGE BEFORE SEPT 30, 2026 to avoid breaking anything before an important deadline. Or, make sure downstream consumers handle urea correctly.

First priority - decide how PEcAn should handle UREA (I like the idea but want to confirm logic + downstream functionality). Specifically, before merging, write.events.SIPNET needs to be updated to handle this (i.e. by converting it directly to minN). I think that is the only current downstream issue, but it is worth a search.


convert_swat_fert_table_to_pkg_df <- function() {
fertilizer.frt <- "https://raw.githubusercontent.com/swat-model/swatplus/refs/heads/main/data/Osu_1hru/fertilizer.frt"
fertilizer.frt <- "https://raw.githubusercontent.com/swat-model/swatplus/main/refdata/Ames_sub1/fertilizer.frt"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the rationale for this change? it removes org_compost

Comment on lines -90 to +91
RoxygenNote: 7.3.3
X-schema.org-keywords: soil-data, vegetation-data, land-cover
Config/roxygen2/version: 8.0.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is due to roxygen2 v 8.x changing how it stores Roxygen version metadata.

This is a minor annoyance that causes CI build error.

Easiest, but fragile, fix is to revert this change.

A more robust fix is to install and use roxygen2 v 7.3.3, and pin to this version.

remotes::install_version("roxygen2", version = "7.3.3", upgrade = "never")

Currently,

  • make document will fail if the wrong version is installed.
  • devtools::document() outside of make can create a diff like this.

I've proposed adding the installation code to a make document error message in #4108

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.

2 participants