diff --git a/.buildlibrary b/.buildlibrary index 898daac2..6fecfb6b 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '28841544' +ValidationKey: '28963480' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 5944f2da..0d14d0f7 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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 diff --git a/DESCRIPTION b/DESCRIPTION index 9b583bcf..128973e3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "karstens@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), diff --git a/R/calcAreaEquippedForIrrigation.R b/R/calcAreaEquippedForIrrigation.R index 9d45bc2e..eb21676d 100644 --- a/R/calcAreaEquippedForIrrigation.R +++ b/R/calcAreaEquippedForIrrigation.R @@ -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 ### @@ -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 ### diff --git a/R/correctMehta2022.R b/R/correctMehta2024.R similarity index 58% rename from R/correctMehta2022.R rename to R/correctMehta2024.R index 35e39d3b..deb844f3 100644 --- a/R/correctMehta2022.R +++ b/R/correctMehta2024.R @@ -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 #' @@ -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"), diff --git a/R/downloadMehta2022.R b/R/downloadMehta2022.R deleted file mode 100644 index 2784e2d4..00000000 --- a/R/downloadMehta2022.R +++ /dev/null @@ -1,51 +0,0 @@ -#' @title downloadMehta2022 -#' @description download Global Area Equipped for Irrigation Dataset 1900-2015 from Mehta et al. (2022) -#' -#' @author Felicitas Beier -#' @seealso [downloadSource()] [readMehta2022()] -#' @examples -#' \dontrun{ -#' a <- downloadSource() -#' } -#' -#' @importFrom utils download.file -#' @importFrom withr with_options - -downloadMehta2022 <- function() { - - aeiURL <- "https://zenodo.org/record/6886564" - - 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")) - - 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 = "https://doi.org/10.5281/zenodo.6886564", - title = "Global Area Equipped for Irrigation Dataset 1900-2015", - revision = "2022", - release_date = "2015-07-01", - author = "Mehta, Piyush; Siebert, Stefan; - Kummu, Matti; Deng, Qinyu; Ali, Tariq; - Marston, Landon; Xie, Wei; Davis, Kyle", - license = "Creative Commons Attribution 4.0 International")) -} diff --git a/R/downloadMehta2024.R b/R/downloadMehta2024.R new file mode 100644 index 00000000..066c586a --- /dev/null +++ b/R/downloadMehta2024.R @@ -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")) +} diff --git a/R/readMehta2022.R b/R/readMehta2024.R similarity index 55% rename from R/readMehta2022.R rename to R/readMehta2024.R index b825ba2a..e070919f 100644 --- a/R/readMehta2022.R +++ b/R/readMehta2024.R @@ -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) @@ -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) } diff --git a/README.md b/README.md index 43aa9404..72dc2b9f 100644 --- a/README.md +++ b/README.md @@ -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) @@ -39,7 +39,7 @@ In case of questions / problems please contact Kristine Karstens . +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 , R package version 1.46.0, . A BibTeX entry for LaTeX users is @@ -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}, } diff --git a/man/correctMehta2022.Rd b/man/correctMehta2022.Rd deleted file mode 100644 index 01407363..00000000 --- a/man/correctMehta2022.Rd +++ /dev/null @@ -1,27 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/correctMehta2022.R -\name{correctMehta2022} -\alias{correctMehta2022} -\title{correctMehta2022} -\usage{ -correctMehta2022(x) -} -\arguments{ -\item{x}{magpie object provided by the read function} -} -\value{ -magpie object in cellular resolution -} -\description{ -correct lobal Area Equipped for Irrigation Dataset 1900-2015 - from Mehta et al., 2022 -} -\examples{ - -\dontrun{ - readSource("Mehta2022", convert="onlycorrect") -} -} -\author{ -Felicitas Beier -} diff --git a/man/correctMehta2024.Rd b/man/correctMehta2024.Rd new file mode 100644 index 00000000..3a62821e --- /dev/null +++ b/man/correctMehta2024.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/correctMehta2024.R +\name{correctMehta2024} +\alias{correctMehta2024} +\title{correctMehta2024} +\usage{ +correctMehta2024(x) +} +\arguments{ +\item{x}{magpie object provided by the read function} +} +\value{ +magpie object in cellular resolution +} +\description{ +correct Global Area Equipped for Irrigation Dataset 1900-2015 + from Mehta et al., 2024 +} +\examples{ + +\dontrun{ + readSource("Mehta2024", convert="onlycorrect") +} +} +\author{ +Felicitas Beier +} diff --git a/man/downloadMehta2022.Rd b/man/downloadMehta2022.Rd deleted file mode 100644 index 727ce991..00000000 --- a/man/downloadMehta2022.Rd +++ /dev/null @@ -1,23 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/downloadMehta2022.R -\name{downloadMehta2022} -\alias{downloadMehta2022} -\title{downloadMehta2022} -\usage{ -downloadMehta2022() -} -\description{ -download Global Area Equipped for Irrigation Dataset 1900-2015 from Mehta et al. (2022) -} -\examples{ -\dontrun{ -a <- downloadSource() -} - -} -\seealso{ -[downloadSource()] [readMehta2022()] -} -\author{ -Felicitas Beier -} diff --git a/man/downloadMehta2024.Rd b/man/downloadMehta2024.Rd new file mode 100644 index 00000000..47a6e190 --- /dev/null +++ b/man/downloadMehta2024.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/downloadMehta2024.R +\name{downloadMehta2024} +\alias{downloadMehta2024} +\title{downloadMehta2024} +\usage{ +downloadMehta2024(subtype = "GMIA") +} +\arguments{ +\item{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.} +} +\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) +} +\examples{ +\dontrun{ +a <- downloadSource() +} + +} +\seealso{ +[downloadSource()] [readMehta2024()] +} +\author{ +Felicitas Beier +} diff --git a/man/readMehta2022.Rd b/man/readMehta2022.Rd deleted file mode 100644 index b33027e8..00000000 --- a/man/readMehta2022.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/readMehta2022.R -\name{readMehta2022} -\alias{readMehta2022} -\title{readMehta2022} -\usage{ -readMehta2022() -} -\description{ -reads in Global Area Equipped for Irrigation for years 1900-2015 from Mehta et al. (2022) -} -\examples{ - -\dontrun{ a <- readSource("Mehta2022") -} -} -\seealso{ -[correctMehta2022()] -} -\author{ -Felicitas Beier -} diff --git a/man/readMehta2024.Rd b/man/readMehta2024.Rd new file mode 100644 index 00000000..c05e3521 --- /dev/null +++ b/man/readMehta2024.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readMehta2024.R +\name{readMehta2024} +\alias{readMehta2024} +\title{readMehta2024} +\usage{ +readMehta2024(subtype = "GMIA") +} +\arguments{ +\item{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.} +} +\description{ +reads in Global Area Equipped for Irrigation for years 1900-2015 from Mehta et al. (2022) +} +\examples{ +\dontrun{ +a <- readSource("Mehta2024") +} +} +\seealso{ +[correctMehta2024()] +} +\author{ +Felicitas Beier +}