Skip to content

Commit

Permalink
Updated AEI validation data and lintering some functions
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicitasBeier committed Apr 25, 2024
1 parent 4d78416 commit 038c158
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '507885960'
ValidationKey: '508110694'
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: '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
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: 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", , "[email protected]", role = c("aut", "cre")),
person("Stephen", "Wirth", role = "aut"),
Expand Down
99 changes: 45 additions & 54 deletions R/calcGMIA.R
Original file line number Diff line number Diff line change
@@ -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"))
}
2 changes: 1 addition & 1 deletion R/calcValidAEI.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion R/fullVALIDATION.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 29 additions & 35 deletions R/readWaterUsage.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title readWaterUsage
#' @description Read Historic and projcted Agricultural water consumption
#'
#'
#'
#'
#' @param subtype Available subtypes are:
#' \itemize{
#' \item historical:
Expand All @@ -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)

}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky <bodirsky@

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

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 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.56.12, <https://github.com/pik-piam/mrvalidation>.
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 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.56.13, <https://github.com/pik-piam/mrvalidation>.

A BibTeX entry for LaTeX users is

Expand All @@ -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},
}
Expand Down
5 changes: 2 additions & 3 deletions man/calcGMIA.Rd

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

5 changes: 2 additions & 3 deletions man/readWaterUsage.Rd

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

0 comments on commit 038c158

Please sign in to comment.