From 038c158b2584ecceb4bdcd8270666af0c0c4b30e Mon Sep 17 00:00:00 2001 From: Felicitas Date: Thu, 25 Apr 2024 12:05:52 +0200 Subject: [PATCH] Updated AEI validation data and lintering some functions --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/calcGMIA.R | 99 ++++++++++++++++++++----------------------- R/calcValidAEI.R | 2 +- R/fullVALIDATION.R | 2 +- R/readWaterUsage.R | 64 +++++++++++++--------------- README.md | 6 +-- man/calcGMIA.Rd | 5 +-- man/readWaterUsage.Rd | 5 +-- 10 files changed, 88 insertions(+), 105 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 9f2b958..fe8a030 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '507885960' +ValidationKey: '508110694' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 486abac..e118691 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: 'mrvalidation: madrat data preparation for validation purposes' -version: 2.56.12 -date-released: '2024-04-17' +version: 2.56.13 +date-released: '2024-04-25' abstract: Package contains routines to prepare data for validation exercises. authors: - family-names: Bodirsky diff --git a/DESCRIPTION b/DESCRIPTION index 7170f94..2d42d4c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrvalidation Title: madrat data preparation for validation purposes -Version: 2.56.12 -Date: 2024-04-17 +Version: 2.56.13 +Date: 2024-04-25 Authors@R: c( person("Benjamin Leon", "Bodirsky", , "bodirsky@pik-potsdam.de", role = c("aut", "cre")), person("Stephen", "Wirth", role = "aut"), diff --git a/R/calcGMIA.R b/R/calcGMIA.R index ffbe02c..40eb280 100644 --- a/R/calcGMIA.R +++ b/R/calcGMIA.R @@ -1,66 +1,57 @@ -#'@title calcGMIA -#' +#' @title calcGMIA +#' #' @description Filling gaps in the Historical area equipped for irrigation from GMIA. -#' +#' #' @return list of magpie object with data and weight #' @author Stephen Wirth, Anne Biewald #' @examples -#' -#' \dontrun{ -#' a <- calcOutput("GMIA",aggregate="regglo") +#' \dontrun{ +#' a <- calcOutput("GMIA", aggregate = "regglo") #' } -calcGMIA <- function(){ +calcGMIA <- function() { x <- readSource("GMIA", "all_data_national", convert = TRUE) - - small_island_states <- c("ATG","BRB", "COK","NRU", "TUV","NIU", "MDV", "MLT", "GRD", - "VCT","PLW", "LCA", "SGP", "FSM", "BHR", "DMA", "TON", - "KIR", "STP", "SYC") - - antarctica_or_arctic <- c("ATA", "ATF", "GRL", "SJM") - desserts_no_agg <- c("ESH", "GIB") - - x[antarctica_or_arctic,,] <- 0 - x[desserts_no_agg,,] <- 0 - + + smallIslandStates <- c("ATG", "BRB", "COK", "NRU", "TUV", "NIU", "MDV", "MLT", "GRD", + "VCT", "PLW", "LCA", "SGP", "FSM", "BHR", "DMA", "TON", + "KIR", "STP", "SYC") + + antarcticORarctic <- c("ATA", "ATF", "GRL", "SJM") + desertNoAgg <- c("ESH", "GIB") + + x[antarcticORarctic, , ] <- 0 + x[desertNoAgg, , ] <- 0 + area <- readSource("FAO_online", "Land") - sudan <- area[c("SDN","SSD"),1,"6600|Country area.Area_(1000_ha)"] - proportion_sudan <- sudan/as.numeric(as.numeric(sudan["SDN",,]) + as.numeric(sudan["SSD",,])) - x["SSD",,] <- as.numeric(x["SDN",,])*as.numeric(proportion_sudan["SSD",,]) - x["SDN",,] <- as.numeric(x["SDN",,])*as.numeric(proportion_sudan["SDN",,]) + sudan <- area[c("SDN", "SSD"), 1, "6600|Country area.Area_(1000_ha)"] + sudanRatio <- sudan / as.numeric(as.numeric(sudan["SDN", , ]) + as.numeric(sudan["SSD", , ])) + x["SSD", , ] <- as.numeric(x["SDN", , ]) * as.numeric(sudanRatio["SSD", , ]) + x["SDN", , ] <- as.numeric(x["SDN", , ]) * as.numeric(sudanRatio["SDN", , ]) + # Disaggregate values for China: Mainland, Hong Kong, Taiwan, and Macao + # Based on AEI shares the information from (http://www.fao.org/nr/water/aquastat/irrigationmap/CHN/index.stm) + x["HKG", , ] <- as.numeric(x["CHN", , ]) * 0.00003 + x["TWN", , ] <- as.numeric(x["CHN", , ]) * 0.0078 + x["CHN", , ] <- as.numeric(x["CHN", , ]) * 0.992 + x["MAC", , ] <- 0 - #Dissaggregate values for China: Mainland, Hong Kong, Tiwan, and Macao - #Based on AEI shares the information from (http://www.fao.org/nr/water/aquastat/irrigationmap/CHN/index.stm) - x["HKG",,] <- as.numeric(x["CHN",,]) * 0.00003 - x["TWN",,] <- as.numeric(x["CHN",,]) * 0.0078 - x["CHN",,] <- as.numeric(x["CHN",,]) * 0.992 - x["MAC",,] <- 0 - - - value_small_islands <- as.magpie(apply(x[small_island_states,,],MARGIN = c(2,3),FUN = "mean")) - - for(i in getRegions(x)) - { - tmp <- dimSums(x[i,,], dim=c(2,3)) - if(is.na(tmp)) - { - area_tmp <- area[i,length(getYears(area)),"6600|Country area.Area_(1000_ha)"] - if(area_tmp!=0) - { - x[i,,] <- value_small_islands - } - else - { - x[i,,] <- 0 + + valSmallIslands <- as.magpie(apply(x[smallIslandStates, , ], MARGIN = c(2, 3), FUN = "mean")) + + for (i in getItems(x, dim = 1.1)) { + tmp <- dimSums(x[i, , ], dim = c(2, 3)) + if (is.na(tmp)) { + areaTMP <- area[i, length(getYears(area)), "6600|Country area.Area_(1000_ha)"] + if (areaTMP != 0) { + x[i, , ] <- valSmallIslands + } else { + x[i, , ] <- 0 } } } - - return(list(x=x, - weight=NULL, - unit="ha", - min=0, - description="Area equipped for Irrigation in ha") - ) - - } + + return(list(x = x, + weight = NULL, + unit = "ha", + min = 0, + description = "Area equipped for irrigation in ha")) +} diff --git a/R/calcValidAEI.R b/R/calcValidAEI.R index a98a3ac..96b61ac 100644 --- a/R/calcValidAEI.R +++ b/R/calcValidAEI.R @@ -11,7 +11,7 @@ calcValidAEI <- function(datasource = "LUH2v2") { - if (datasource == "LUH2v2" || datasource == "Mehta2022") { + if (datasource == "LUH2v2" || datasource == "Mehta2024_Siebert2013" || datasource == "Mehta2024_Meier2018") { out <- collapseNames(calcOutput("AreaEquippedForIrrigation", cells = "lpjcell", cellular = FALSE, aggregate = FALSE)[, , datasource]) } else if (datasource == "HID") { diff --git a/R/fullVALIDATION.R b/R/fullVALIDATION.R index b510410..735df12 100644 --- a/R/fullVALIDATION.R +++ b/R/fullVALIDATION.R @@ -172,7 +172,7 @@ fullVALIDATION <- function(rev = 0.1) { # Area equipped for Irrigation calcOutput(type = "ValidAEI", datasource = "LUH2v2", aggregate = "REG+GLO", file = "validation.mif", append = TRUE, try = TRUE) - calcOutput(type = "ValidAEI", datasource = "Mehta2022", aggregate = "REG+GLO", + calcOutput(type = "ValidAEI", datasource = "Mehta2024_Siebert2013", aggregate = "REG+GLO", file = "validation.mif", append = TRUE, try = TRUE) calcOutput(type = "ValidAEI", datasource = "HID", aggregate = "REG+GLO", file = "validation.mif", append = TRUE, try = TRUE) diff --git a/R/readWaterUsage.R b/R/readWaterUsage.R index b4e0f05..3d672de 100644 --- a/R/readWaterUsage.R +++ b/R/readWaterUsage.R @@ -1,7 +1,7 @@ #' @title readWaterUsage #' @description Read Historic and projcted Agricultural water consumption -#' -#' +#' +#' #' @param subtype Available subtypes are: #' \itemize{ #' \item historical: @@ -25,45 +25,39 @@ #' @author Stephen Wirth #' @seealso \code{\link{readSource}} #' @examples -#' -#' \dontrun{ +#' \dontrun{ #' a <- readSource("foley_2011") #' a <- readSource("aquastat_2008_12") #' } -#' #'@importFrom reshape2 dcast +#' #' @importFrom reshape2 dcast #' @importFrom magclass read.magpie getSets -readWaterUsage <- function(subtype=NULL){ - - - files <- c(foley_2011= "Foley_2011.csv", - shiklomanov_2000="shiklomanov_2000.csv", - wada_2011="Wada_2011.csv", - wisser_2008="Wisser_2008.csv", - fischer_IIASA="Fischer_IIASA.csv", - hejazi_2013="Hejazi_2013.csv", - molden_IWMI="Molden_IWMI.csv", - seckler_IWMI="Seckler_IWMI.csv", - shiklomanov="Shiklomanov.csv", - aquastat_2008_12="aquastat_use_2008_12.csv" - ) - - +readWaterUsage <- function(subtype = NULL) { + + files <- c(foley_2011 = "Foley_2011.csv", + shiklomanov_2000 = "shiklomanov_2000.csv", + wada_2011 = "Wada_2011.csv", + wisser_2008 = "Wisser_2008.csv", + fischer_IIASA = "Fischer_IIASA.csv", + hejazi_2013 = "Hejazi_2013.csv", + molden_IWMI = "Molden_IWMI.csv", + seckler_IWMI = "Seckler_IWMI.csv", + shiklomanov = "Shiklomanov.csv", + aquastat_2008_12 = "aquastat_use_2008_12.csv") + file <- toolSubtypeSelect(subtype, files) - - if(subtype== "aquastat_2008_12") - { - a <- read.csv(file, header = F, skip=2, stringsAsFactors = FALSE) - colnames(a) <- as.character(a[1,]) - a <- a[c(2:372),c(1,3,5,6)] + + if (subtype == "aquastat_2008_12") { + a <- read.csv(file, header = FALSE, skip = 2, stringsAsFactors = FALSE) + colnames(a) <- as.character(a[1, ]) + a <- a[c(2:372), c(1, 3, 5, 6)] a$Value <- as.numeric(a$Value) - colnames(a)[2] <- "VariableName" - b <- reshape2::dcast(a,Area+Year~a$VariableName) - return(as.magpie(b, spatial=1, temporal=2)) - } - else - x <- read.magpie(file_name = file) - getSets(x) <- c("region", "year", "data") + colnames(a)[2] <- "VariableName" + b <- reshape2::dcast(a, Area + Year ~ a$VariableName) + return(as.magpie(b, spatial = 1, temporal = 2)) + } else { + x <- read.magpie(file_name = file) + getSets(x) <- c("region", "year", "data") return(x) - + } } diff --git a/README.md b/README.md index 85a6dfb..381a220 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat data preparation for validation purposes -R package **mrvalidation**, version **2.56.12** +R package **mrvalidation**, version **2.56.13** [![CRAN status](https://www.r-pkg.org/badges/version/mrvalidation)](https://cran.r-project.org/package=mrvalidation) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4317826.svg)](https://doi.org/10.5281/zenodo.4317826) [![R build status](https://github.com/pik-piam/mrvalidation/workflows/check/badge.svg)](https://github.com/pik-piam/mrvalidation/actions) [![codecov](https://codecov.io/gh/pik-piam/mrvalidation/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrvalidation) [![r-universe](https://pik-piam.r-universe.dev/badges/mrvalidation)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky , R package version 2.56.12, . +Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 , R package version 2.56.13, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrvalidation: madrat data preparation for validation purposes}, author = {Benjamin Leon Bodirsky and Stephen Wirth and Kristine Karstens and Florian Humpenoeder and Mishko Stevanovic and Abhijeet Mishra and Anne Biewald and Isabelle Weindl and Felicitas Beier and David Chen and Michael Crawford and Debbora Leip and Edna {Molina Bacca} and Ulrich Kreidenweis and Amsalu {W. Yalew} and Patrick {von Jeetze} and Xiaoxi Wang and Jan Philipp Dietrich and Marcos Alves}, year = {2024}, - note = {R package version 2.56.12}, + note = {R package version 2.56.13}, doi = {10.5281/zenodo.4317826}, url = {https://github.com/pik-piam/mrvalidation}, } diff --git a/man/calcGMIA.Rd b/man/calcGMIA.Rd index c8d7149..5a8507d 100644 --- a/man/calcGMIA.Rd +++ b/man/calcGMIA.Rd @@ -13,9 +13,8 @@ list of magpie object with data and weight Filling gaps in the Historical area equipped for irrigation from GMIA. } \examples{ - -\dontrun{ -a <- calcOutput("GMIA",aggregate="regglo") +\dontrun{ +a <- calcOutput("GMIA", aggregate = "regglo") } } \author{ diff --git a/man/readWaterUsage.Rd b/man/readWaterUsage.Rd index ae997eb..3b2f63c 100644 --- a/man/readWaterUsage.Rd +++ b/man/readWaterUsage.Rd @@ -34,12 +34,11 @@ magpie object containing data on water usage Read Historic and projcted Agricultural water consumption } \examples{ - -\dontrun{ +\dontrun{ a <- readSource("foley_2011") a <- readSource("aquastat_2008_12") } -#'@importFrom reshape2 dcast +#' @importFrom reshape2 dcast } \seealso{ \code{\link{readSource}}