6. Feeds silently get 0 ha

When a feed's yield or removal fraction is 0, the area calculation divides by zero. The package then replaces the resulting infinite or missing value with 0 ha (R/land_requirement.R:120-121), without any message. iCLEANED pre-fills removal as 0 for new feeds, so this happens often. The demo below sets the pasture's yield to 0.

Version Pasture DM demand (kg) Pasture area (ha) Message to the user
Current 14,231 0.00 (no message)
Fixed 15,631 0.00 land_requirement(): Naturally occuring pasture - green fodder has 2253 kg DM demand but zero yield or removal; its area is set to 0 ha.

The proposed fix doesn't change the result. It warns the user, so the app can show the problem. DM demand differs between the two rows because fix 1 corrects intake. How much land purchased feeds should count for is a separate question, left to the authors.

--- a/R/land_requirement.R	2026-09-29 10:31:44
+++ b/R/land_requirement.R	2026-09-29 10:31:44
@@ -28,6 +28,15 @@
 
 land_requirement <- function(feed_basket_quality, energy_required, para){
 
+  warn_zero_land <- function(df) {
+    bad <- df$feed_item_dm > 0 & !is.finite(df$area_total)
+    if (any(bad, na.rm = TRUE)) {
+      warning(sprintf("land_requirement(): %s has %.0f kg DM demand but zero yield or removal; its area is set to 0 ha.",
+                      paste(unique(df$feed[bad]), collapse = ", "), sum(df$feed_item_dm[bad])), call. = FALSE)
+    }
+    df
+  }
+
   livestock_category_code <- unique(feed_basket_quality$livestock_category_code)
 
   livestock_requirements <- list()
@@ -117,6 +126,7 @@
                  farm_dm = (feed_item_dm - rough_of_dm - conc_of_dm - conc_ip_dm),
                  grasses_dm = ifelse(feed_item_selected$category == "grass", feed_item_dm, 0),
                  tree_legume_dm = ifelse(feed_item_selected$category == "tree crop" | feed_item_selected$category == "tree legume", feed_item_dm, 0)) %>%
+          warn_zero_land() %>%
           dplyr::mutate_if(is.numeric, list(~na_if(.,Inf))) %>%
           replace(is.na(.), 0)
 
Reproduce this Open in Colab (R runtime; section 6)
Rscript reproduce_critical_issues.R 6 using reproduce_critical_issues.R (installs what it needs into its own library and cleaned @ 800d53a). Add CLEANED_FIXES=proposed_fixes.R with proposed_fixes.R for the fixed column.
Test: test-critical-06-land-silent-zero.R · Proposed change: F06.diff · Open an issue in CIAT/cleaned

Comments

Comments are GitHub Discussions on peetmate/cleaned-review; sign in with GitHub to post.