Skip to content

added download/correct functions for GLW4, download/read/correct functions for GLPS; updated readGLW3/4 and correctGLW3#69

Open
BinLin94 wants to merge 2 commits into
pik-piam:masterfrom
BinLin94:master
Open

added download/correct functions for GLW4, download/read/correct functions for GLPS; updated readGLW3/4 and correctGLW3#69
BinLin94 wants to merge 2 commits into
pik-piam:masterfrom
BinLin94:master

Conversation

@BinLin94
Copy link
Copy Markdown

@BinLin94 BinLin94 commented May 18, 2026

Summary

This PR adds and updates download, read, and correct functions for two gridded livestock data sources.

Gridded Livestock of the World (GLW3 / GLW4)

  • Added downloadGLW3 and downloadGLW4 with full metadata, including DOI, unit, and bibentry reference.
  • Updated readGLW3 to support all 16 subtypes: 8 species × 2 weighting methods. Previously, only one species was handled.
  • Updated readGLW4 to support both 2015 data, in heads/pixel, and 2020 data, in heads/km². Previously, only 2015 data was handled.
  • Both read functions now set the year, data dimension name, and unit attribute on the returned magpie object.
  • Added correctGLW4, which replaces NA and negative values with 0.
  • Updated correctGLW3 with minor improvements.

Global Livestock Production Systems (GLPS)

  • Added downloadGLPS, readGLPS, and correctGLPS for chicken, pig, and ruminant livestock production system rasters.
  • correctGLPS replaces NA and negative values with 0 for continuous subtypes, namely chicken and pig density.
  • For Ruminant_2000, which is categorical with class codes 1–15, NA values are left unchanged to avoid introducing an invalid class 0.

Other

  • Added archive to DESCRIPTION Imports, which is required by downloadGLPS for 7z extraction.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR expands mrland’s madrat source handlers for gridded livestock datasets by adding download/read/correct support for GLW3/GLW4 and GLPS subtypes, plus generated documentation and import updates.

Changes:

  • Adds GLW3/GLW4 download metadata and subtype-aware readers for additional species/years.
  • Adds GLPS download/read/correct handlers for chicken, pig, and ruminant production-system rasters.
  • Updates correction docs/imports and generated Rd files for the new and modified source functions.

Reviewed changes

Copilot reviewed 11 out of 20 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
R/readGLW4.R Extends GLW4 reading to 2015/2020 subtype sets and units.
R/readGLW3.R Extends GLW3 reading to all listed species/weighting subtypes.
R/readGLPS.R Adds GLPS raster reader for monogastric and ruminant subtypes.
R/downloadGLW4.R Adds GLW4 download handler and metadata.
R/downloadGLW3.R Adds GLW3 download handler and metadata.
R/downloadGLPS.R Adds GLPS download handler, including ruminant archive extraction.
R/correctGLW4.R Adds GLW4 correction for NA/negative values.
R/correctGLW3.R Updates GLW3 correction documentation.
R/correctGLPS.R Adds GLPS correction behavior by subtype.
NAMESPACE Updates imports for archive/terra and removed old raster/dplyr imports.
DESCRIPTION Adds archive to package imports.
man/readGLW4.Rd Generated documentation for updated GLW4 reader.
man/readGLW3.Rd Generated documentation for updated GLW3 reader.
man/readGLPS.Rd Generated documentation for new GLPS reader.
man/downloadGLW4.Rd Generated documentation for new GLW4 downloader.
man/downloadGLW3.Rd Generated documentation for new GLW3 downloader.
man/downloadGLPS.Rd Generated documentation for new GLPS downloader.
man/correctGLW4.Rd Generated documentation for new GLW4 correction.
man/correctGLW3.Rd Generated documentation for updated GLW3 correction.
man/correctGLPS.Rd Generated documentation for new GLPS correction.
Files not reviewed (9)
  • man/correctGLPS.Rd: Language not supported
  • man/correctGLW3.Rd: Language not supported
  • man/correctGLW4.Rd: Language not supported
  • man/downloadGLPS.Rd: Language not supported
  • man/downloadGLW3.Rd: Language not supported
  • man/downloadGLW4.Rd: Language not supported
  • man/readGLPS.Rd: Language not supported
  • man/readGLW3.Rd: Language not supported
  • man/readGLW4.Rd: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread R/readGLW3.R
Comment on lines +57 to +58
x <- as.magpie(x)
getYears(x) <- "y2010"
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.

I also second this analysis.

Comment thread R/readGLPS.R
Comment on lines +60 to +61
gisFile <- list.files(pattern = "\\.(tif|img|asc)$", ignore.case = TRUE)[1]
if (is.na(gisFile)) stop("No raster file found for Ruminant_2000. Run downloadSource first.")
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.

I also see the issue with those lines and the patch suggested by Copilot is fine.

Comment thread R/downloadGLW4.R Outdated
Comment on lines +6 to +7
#' Files are stored under species subdirectories matching the layout expected
#' by \code{readGLW4}.
Comment thread R/readGLPS.R Outdated
Comment on lines +2 to +3
#' @description Reads Global Livestock Production System (GLPS) data for
#' reference year 2010. Three animal groups are available:
- correctGLW3: fix formatting (spacing around <- and braces)
- downloadGLW4: remove redundant subdirectory sentence from description
- readGLPS: clarify that chicken/pig subtypes are 2010, Ruminant_2000 is ca. 2000;
  fix @param subtype to remove "for 2010" from description

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Member

@codeZeilen codeZeilen left a comment

Choose a reason for hiding this comment

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

Thanks for also adding the new and the missing download functions!

Comment thread R/readGLPS.R
Comment on lines +60 to +61
gisFile <- list.files(pattern = "\\.(tif|img|asc)$", ignore.case = TRUE)[1]
if (is.na(gisFile)) stop("No raster file found for Ruminant_2000. Run downloadSource first.")
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.

I also see the issue with those lines and the patch suggested by Copilot is fine.

Comment thread R/readGLW3.R
Comment on lines +57 to +58
x <- as.magpie(x)
getYears(x) <- "y2010"
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.

I also second this analysis.

Comment thread R/readGLW4.R
Aw_Ho = "Ho/6_Ho_2015_Aw.tif",
Aw_Dk = "Dk/6_Dk_2015_Aw.tif",
Aw_Bf = "Bf/6_Bf_2015_Aw.tif"))
subtypes2015 <- c(
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.

I was somewhat scared that this breaks the workflow on the HPC as files were previously organized in sub-folders, but apparently the GLW4 data was never downloaded, so no problem here.

Comment thread R/readGLW4.R
}

x <- as.magpie(x)
x[is.nan(x)] <- NA
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 this line for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants