Skip to content

Commit

Permalink
Updated Mehta data for Area Equipped for Irrigation. Affects non-defa…
Browse files Browse the repository at this point in the history
…ult option in MAgPIE and validation data.
  • Loading branch information
FelicitasBeier committed Apr 25, 2024
1 parent e395927 commit 902a123
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '28841544'
ValidationKey: '28963480'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mrmagpie: madrat based MAgPIE Input Data Library'
version: 1.45.4
date-released: '2024-04-23'
version: 1.46.0
date-released: '2024-04-25'
abstract: Provides functions for MAgPIE country and cellular input data generation.
authors:
- family-names: Karstens
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrmagpie
Title: madrat based MAgPIE Input Data Library
Version: 1.45.4
Date: 2024-04-23
Version: 1.46.0
Date: 2024-04-25
Authors@R: c(
person("Kristine", "Karstens", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand Down
19 changes: 13 additions & 6 deletions R/calcAreaEquippedForIrrigation.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ calcAreaEquippedForIrrigation <- function(cellular = FALSE,
cells = "lpjcell",
selectyears = "past") {

selectyears <- sort(findset(selectyears, noset = "original"))
selectyears <- sort(magpiesets::findset(selectyears, noset = "original"))

##########################################
### Read in LUH2v2 irrigated area data ###
Expand Down Expand Up @@ -70,12 +70,19 @@ calcAreaEquippedForIrrigation <- function(cellular = FALSE,
getItems(luh, dim = 3) <- "LUH2v2"

########################################
### Read in Mehta et al. (2022) data ###
### Read in Mehta et al. (2024) data ###
########################################
mehta <- readSource("Mehta2022", convert = "onlycorrect")
years <- intersect(getItems(mehta, dim = 2), selectyears)
mehta <- mehta[, years, ]
getItems(mehta, dim = 3) <- "Mehta2022"
mehta1 <- readSource("Mehta2024", subtype = "GMIA", convert = "onlycorrect")
years <- intersect(getItems(mehta1, dim = 2), selectyears)
mehta1 <- mehta1[, years, ]
getItems(mehta1, dim = 3) <- "Mehta2024_Siebert2013"

mehta2 <- readSource("Mehta2024", subtype = "Meier2018", convert = "onlycorrect")
years <- intersect(getItems(mehta2, dim = 2), selectyears)
mehta2 <- mehta2[, years, ]
getItems(mehta2, dim = 3) <- "Mehta2024_Meier2018"

mehta <- mbind(mehta1, mehta2)

#########################
### Combine data sets ###
Expand Down
10 changes: 5 additions & 5 deletions R/correctMehta2022.R → R/correctMehta2024.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' @title correctMehta2022
#' @description correct lobal Area Equipped for Irrigation Dataset 1900-2015
#' from Mehta et al., 2022
#' @title correctMehta2024
#' @description correct Global Area Equipped for Irrigation Dataset 1900-2015
#' from Mehta et al., 2024
#'
#' @param x magpie object provided by the read function
#'
Expand All @@ -10,10 +10,10 @@
#' @examples
#'
#' \dontrun{
#' readSource("Mehta2022", convert="onlycorrect")
#' readSource("Mehta2024", convert="onlycorrect")
#' }

correctMehta2022 <- function(x) {
correctMehta2024 <- function(x) {

x <- toolConditionalReplace(x,
conditions = c("is.na()", "<0"),
Expand Down
51 changes: 0 additions & 51 deletions R/downloadMehta2022.R

This file was deleted.

73 changes: 73 additions & 0 deletions R/downloadMehta2024.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' @title downloadMehta2024
#' @description download Global Area Equipped for Irrigation Dataset 1900-2015 from Mehta et al. (2024).
#' Gridded dataset is created based on (sub-)national statistics from FAOSTAT, AQUASTAT, EUROSTAT
#' and country's census data downscaled using two alternative gridded irrigation maps
#' (GMIA from Siebert et al. 2013 and Meier et al. 2018)
#'
#' @param subtype data subtype to be downloaded.
#' Subtypes available:
#' 'GMIA': gridded base map for downscaling from Stefan et al. (2013).
#' Global Map of Irrigation Areas version 5.
#' 'Meier2018': gridded base map for downscaling from Meier, et al. (2018).
#' Global Irrigated Areas.
#'
#' @author Felicitas Beier
#' @seealso [downloadSource()] [readMehta2024()]
#' @examples
#' \dontrun{
#' a <- downloadSource()
#' }
#'
#' @importFrom utils download.file
#' @importFrom withr with_options

downloadMehta2024 <- function(subtype = "GMIA") {

aeiURL <- "https://zenodo.org/records/7809342"

if (subtype == "GMIA") {
dataname <- "G_AEI_"
} else if (subtype == "Meier2018") {
dataname <- "MEIER_G_AEI_"
} else {
stop("The selected subtype is not available for downloadMehta2024. Please select 'GMIA' or 'Meier2018'.")
}

years <- c(seq(1900, 1970, by = 10),
seq(1980, 2015, by = 5))

files <- c(paste0(dataname, years, ".ASC"))

# repeat with count and maxcount is necessary for case of 'Couldn't connect to server' error
maxcount <- 10
count <- 0
for (file in files) {
repeat {

withr::with_options(list(timeout = NULL),
code = try(download.file(paste0(aeiURL, "/files/", file),
destfile = file, mode = "wb")))

count <- count + 1
if (file.exists(file) || count >= maxcount) {
break
}
}
}

return(list(url = aeiURL,
doi = "10.5281/zenodo.6740334",
title = "Global Area Equipped for Irrigation Dataset 1900-2015",
revision = "2024",
release_date = "2023-04-07",
author = "Mehta, Piyush; Siebert, Stefan;
Kummu, Matti; Deng, Qinyu; Ali, Tariq;
Marston, Landon; Xie, Wei; Davis, Kyle",
description = paste0("Gridded dataset of Areas Equipped for Irrigation. ",
"Created based on (sub-)national statistics from FAOSTAT, ",
"AQUASTAT, EUROSTAT and country's census data ",
"downscaled using two alternative gridded irrigation maps ",
"(GMIA from Siebert et al. 2013 and Meier et al. 2018)"),
unit = "ha",
license = "Creative Commons Attribution 4.0 International"))
}
51 changes: 31 additions & 20 deletions R/readMehta2022.R → R/readMehta2024.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
#' @title readMehta2022
#' @title readMehta2024
#' @description reads in Global Area Equipped for Irrigation for years 1900-2015 from Mehta et al. (2022)
#'
#' @param subtype data subtype to be downloaded.
#' Subtypes available:
#' 'GMIA': gridded base map for downscaling from Stefan et al. (2013).
#' Global Map of Irrigation Areas version 5.
#' 'Meier2018': gridded base map for downscaling from Meier, et al. (2018).
#' Global Irrigated Areas.
#'
#' @author Felicitas Beier
#' @seealso [correctMehta2022()]
#' @seealso [correctMehta2024()]
#' @examples
#'
#' \dontrun{ a <- readSource("Mehta2022")
#' \dontrun{
#' a <- readSource("Mehta2024")
#' }
#' @importFrom terra aggregate project rast global
#' @importFrom magclass as.magpie
#' @importFrom mrcommons toolGetMappingCoord2Country

readMehta2022 <- function() {
readMehta2024 <- function(subtype = "GMIA") {

if (subtype == "GMIA") {
dataname <- "G_AEI_"
itemname <- "AEI_Mehta2024_Siebert2013"
} else if (subtype == "Meier2018") {
dataname <- "MEIER_G_AEI_"
itemname <- "AEI_Mehta2024_Meier2018"
} else {
stop("The selected subtype is not available for downloadMehta2024. Please select 'GMIA' or 'Meier2018'.")
}

years <- c(seq(1900, 1970, by = 10),
seq(1980, 2015, by = 5))
years1 <- years[years < 2000]
years2 <- years[years >= 2000]

files <- c(paste0("G_AEI_", years1, ".ASC"),
paste0("G_AEI_", years2, ".asc"))
files <- c(paste0(dataname, years, ".ASC"))

.transformObject <- function(x) {

resolution <- terra::rast(res = 0.5)

# global sum of AEI (in ha)
checkSum <- terra::global(x, sum, na.rm = TRUE)
# aggregate to 0.5 degree
x <- suppressWarnings(terra::aggregate(x, fact = 6, fun = "sum", na.rm = TRUE))
# Check whether sum before and after aggregation is the same.
if (any(round(checkSum - terra::global(x, sum, na.rm = TRUE), digits = 4) != 0)) {
warning(paste0("The sum before and after aggregation differ: ",
"Min. deviation is: ",
min(round(checkSum - terra::global(x, sum, na.rm = TRUE), digits = 4)),
". Max. deviation is: ",
max(round(checkSum - terra::global(x, sum, na.rm = TRUE), digits = 4))))
saveRDS(x, file = "/p/projects/magpie/readMehta2022_x.rds") # ToDo: remove once this issue is solved! #nolint
saveRDS(checkSum, file = "/p/projects/magpie/readMehta2022_checkSum.rds") # ToDo: remove once this issue is solved! #nolint
stop("There is an issue with the aggregation. Please check mrmagpie::readMehta")
if (any(round(checkSum - terra::global(x, sum, na.rm = TRUE), digits = 0) != 0)) {
warning(paste0("The global sum of AEI before and after aggregation differ: ",
"Deviation is: ",
round(checkSum - terra::global(x, sum, na.rm = TRUE), digits = 4)))
stop("There is an issue with the aggregation. Please check mrmagpie::readMehta2024")
}
x <- suppressWarnings(terra::project(x, resolution))
x <- as.magpie(x)
Expand All @@ -53,8 +64,8 @@ readMehta2022 <- function() {
print(paste0("Read in ", file))
aei <- .transformObject(x = aei)

getItems(aei, dim = 2) <- gsub("G_AEI_", "y", getItems(aei, dim = 3))
getItems(aei, dim = 3) <- "AEI"
getItems(aei, dim = 2) <- gsub(dataname, "y", getItems(aei, dim = 3))
getItems(aei, dim = 3) <- itemname

out <- mbind(out, aei)
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat based MAgPIE Input Data Library

R package **mrmagpie**, version **1.45.4**
R package **mrmagpie**, version **1.46.0**

[![CRAN status](https://www.r-pkg.org/badges/version/mrmagpie)](https://cran.r-project.org/package=mrmagpie) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4319612.svg)](https://doi.org/10.5281/zenodo.4319612) [![R build status](https://github.com/pik-piam/mrmagpie/workflows/check/badge.svg)](https://github.com/pik-piam/mrmagpie/actions) [![codecov](https://codecov.io/gh/pik-piam/mrmagpie/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrmagpie) [![r-universe](https://pik-piam.r-universe.dev/badges/mrmagpie)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Kristine Karstens <karstens@pik-p

To cite package **mrmagpie** in publications use:

Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, Köberle A, v. Jeetze P, Mishra A, Humpenoeder F, Sauer P (2024). _mrmagpie: madrat based MAgPIE Input Data Library_. doi: 10.5281/zenodo.4319612 (URL: https://doi.org/10.5281/zenodo.4319612), R package version 1.45.4, <URL: https://github.com/pik-piam/mrmagpie>.
Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, Köberle A, v. Jeetze P, Mishra A, Humpenoeder F, Sauer P (2024). _mrmagpie: madrat based MAgPIE Input Data Library_. doi:10.5281/zenodo.4319612 <https://doi.org/10.5281/zenodo.4319612>, R package version 1.46.0, <https://github.com/pik-piam/mrmagpie>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrmagpie: madrat based MAgPIE Input Data Library},
author = {Kristine Karstens and Jan Philipp Dietrich and David Chen and Michael Windisch and Marcos Alves and Felicitas Beier and Alexandre Köberle and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Sauer},
year = {2024},
note = {R package version 1.45.4},
note = {R package version 1.46.0},
doi = {10.5281/zenodo.4319612},
url = {https://github.com/pik-piam/mrmagpie},
}
Expand Down
27 changes: 0 additions & 27 deletions man/correctMehta2022.Rd

This file was deleted.

27 changes: 27 additions & 0 deletions man/correctMehta2024.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 902a123

Please sign in to comment.