diff --git a/.buildlibrary b/.buildlibrary index 756b3ecf..3c39893a 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '227136960' +ValidationKey: '227213987' AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' - 'Warning: namespace ''.*'' is not available and has been replaced' diff --git a/CITATION.cff b/CITATION.cff index 53338c5e..b2952083 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: 'remind2: The REMIND R package (2nd generation)' -version: 1.143.0 -date-released: '2024-05-29' +version: 1.143.1 +date-released: '2024-06-03' abstract: Contains the REMIND-specific routines for data and model output manipulation. authors: - family-names: Rodrigues diff --git a/DESCRIPTION b/DESCRIPTION index fd2ecc21..f21bb7e2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: remind2 Title: The REMIND R package (2nd generation) -Version: 1.143.0 -Date: 2024-05-29 +Version: 1.143.1 +Date: 2024-06-03 Authors@R: c( person("Renato", "Rodrigues", , "renato.rodrigues@pik-potsdam.de", role = c("aut", "cre")), person("Lavinia", "Baumstark", role = "aut"), diff --git a/R/reportEmiAirPol.R b/R/reportEmiAirPol.R index b26d27df..68332f01 100644 --- a/R/reportEmiAirPol.R +++ b/R/reportEmiAirPol.R @@ -26,480 +26,13 @@ reportEmiAirPol <- function(gdx,regionSubsetList=NULL,t=c(seq(2005,2060,5),seq(2 # Get realisation name realisation <- readGDX(gdx, "module2realisation") realisation <- realisation[which(realisation[,1] == "aerosols"),2] - if(is.null(realisation)) realisation <- "postIIASA" # default value ######### initialisation ########### tmp <- NULL out <- NULL - #=================================================================== - # If realisation is postIIASA... - #=================================================================== - if (realisation == "postIIASA") { + if (! realisation == "exoGAINS") stop("not allowed air pollution realization.") - ######### initialisation ########### - airpollutants <- c("SO2","BC","OC","CO","VOC","NOx","NH3") - airpollutants_low <- c("so2","bc","oc","CO","VOC","NOx","NH3") - - generateReportingEmiAP <- function(pollutant){ - - pollutant_low <- tolower(pollutant) - - # Remove unecessary technological dimensions - dataEndo <- collapseNames(dimSums(p11_emi_postrun[,,pollutant_low], dim=c("all_enty","all_enty1","all_te"),na.rm=TRUE)) - dataExog <- collapseNames(pm_emiAP[,,pollutant_low]) - - # Replace REMIND sector names by reporting ones - - mapping = data.frame( - remind = c("power", "indst", "res", "trans", "indprocess", "solvents", "extraction"), - reporting = c(paste0("Emi|", pollutant, "|Energy Supply|Electricity (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Buildings (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Transport|Ground Trans (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Solvents (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Supply|Extraction (Mt ", pollutant, "/yr)"))) - - magclass::getNames(dataEndo) <- paste(sapply(magclass::getNames(dataEndo), function(x) mapping$reporting[mapping$remind == x])) - magclass::getNames(dataExog) <- paste(sapply(magclass::getNames(dataExog), function(x) mapping$reporting[mapping$remind == x])) - - dataAll <- mbind(dataEndo[,,paste(mapping$reporting[which(!mapping$remind %in% c("indst","indprocess", "solvents","extraction"))])], - dataExog[,,paste(mapping$reporting[which(!mapping$remind %in% c("power","indst","res","trans","indprocess"))])], - dataEndo[,,paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)")] + - dataExog[,,paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)")]) - - if (is.null(pm_limits_wp4_rcp_world)) { - tmp1 <- - mbind(dataAll, - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"international shipping",sep= ".")], - paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"aviation",sep= ".")], - paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"ag waste burning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Agricultural Waste Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"agriculture",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Agriculture (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"forest burning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Forest Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"grassland burning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Savannah Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"waste",sep= ".")], - paste0("Emi|",pollutant,"|Waste (Mt ",pollutant,"/yr)"))) - } else { - tmp1 <- - mbind(dataAll, - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"internationalshipping",sep= ".")], - paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"aviation",sep= ".")], - paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"agwasteburning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Agricultural Waste Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"agriculture",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Agriculture (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"forestburning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Forest Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"grasslandburning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Savannah Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"waste",sep= ".")], - paste0("Emi|",pollutant,"|Waste (Mt ",pollutant,"/yr)"))) - } - - # Aggregation: Transport and Energy Supply - tmp2 <- mbind(tmp1, - setNames(dimSums(tmp1[,, - c(paste0("Emi|",pollutant,"|Energy Demand|Transport|Ground Trans (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)")), - setNames(dimSums(tmp1[,, - c(paste0("Emi|",pollutant,"|Energy Supply|Electricity (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Supply|Extraction (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Energy Supply (Mt ",pollutant,"/yr)"))) - - # Aggregation: Energy Demand + Energy Supply, Land Use - tmp3 <- mbind(tmp2, - setNames(dimSums(tmp2[,,c(paste0("Emi|",pollutant,"|Energy Demand|Industry (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Buildings (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Supply (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)")), - setNames(dimSums(tmp2[,,c(paste0("Emi|",pollutant,"|Land Use|Savannah Burning (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use|Forest Burning (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use|Agriculture (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use|Agricultural Waste Burning (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Land Use (Mt ",pollutant,"/yr)"))) - - # Compute total - tmp4 <- mbind(tmp3, - setNames(dimSums(tmp3[,,c(paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Solvents (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Waste (Mt ",pollutant,"/yr)"))],dim=3), - paste0("Emi|",pollutant," (Mt ",pollutant,"/yr)"))) - - - # Set NAs to 0 - tmp4[is.na(tmp4)] <- 0 - - return(tmp4) - } - - ####### conversion factors ########## - pm_conv_TWa_EJ <- 31.536 - conv_MtSO2_to_MtS <- 1/2 # 32/(32+2*16) - - ####### read in needed data ######### - - ## parameter - p11_emi_postrun <- readGDX(gdx, name=c("pm_emi_postrun","p11_emi_postrun"), field="l", format="first_found",restore_zeros = FALSE) - pm_limits_wp4_rcp <- readGDX(gdx, name=c("pm_emiAPexo","pm_limits_wp4_rcp"), field="l", format="first_found")[,,airpollutants_low] - pm_limits_wp4_rcp_world <- readGDX(gdx, name=c("pm_emiAPexoGlob","pm_limits_wp4_rcp_world"), field="l", format="first_found")[,,airpollutants_low] - pm_emiAP <- readGDX(gdx, name="pm_emiAP", field="l", format="first_found") - p11_emi_nh3_agwaste <- readGDX(gdx, name="p11_emi_nh3_agwaste", field="l") - p11_emi_nh3_ag <- readGDX(gdx, name="p11_emi_nh3_ag", field="l") - p11_emi_nh3_forest <- readGDX(gdx, name="p11_emi_nh3_forest", field="l") - p11_emi_nh3_savannah <- readGDX(gdx, name="p11_emi_nh3_savannah", field="l") - - - if (!is.null(pm_emiAP)) { - - ####### process data ######### - magclass::getNames(p11_emi_postrun) <- tolower(magclass::getNames(p11_emi_postrun)) - magclass::getNames(pm_emiAP) <- tolower(magclass::getNames(pm_emiAP)) - - if (is.null(pm_limits_wp4_rcp_world)) { - pm_limits_wp4_rcp <- do.call("mbind", - lapply(setdiff(magclass::getNames(pm_limits_wp4_rcp,dim = 1),"WORLD"), - function(reg){ - mreg <- collapseNames(pm_limits_wp4_rcp[,,reg]) - getItems(mreg, dim = 1) <- reg - return(mreg) - })) - - magclass::getNames(pm_limits_wp4_rcp) <- tolower(magclass::getNames(pm_limits_wp4_rcp)) - } else { - magclass::getNames(pm_limits_wp4_rcp) <- tolower(magclass::getNames(pm_limits_wp4_rcp)) - magclass::getNames(pm_limits_wp4_rcp_world) <- tolower(magclass::getNames(pm_limits_wp4_rcp_world)) - } - - - p11_emi_postrun[,,"so2"] <- 1/conv_MtSO2_to_MtS * p11_emi_postrun[,,"so2"] - pm_limits_wp4_rcp[,,"so2"] <- 1/conv_MtSO2_to_MtS * pm_limits_wp4_rcp[,,"so2"] - pm_emiAP[,,"so2"] <- 1/conv_MtSO2_to_MtS * pm_emiAP[,,"so2"] - - - if (is.null(pm_limits_wp4_rcp_world)) { - pm_limits_wp4_rcp_world[,,"so2"] <- 1/conv_MtSO2_to_MtS * pm_limits_wp4_rcp_world[,,"so2"] - } - - if (is.null(pm_limits_wp4_rcp_world)) { - # Merge NH3 data with other air pollutants data - magclass::getNames(p11_emi_nh3_agwaste) <- "nh3.ag waste burning" - magclass::getNames(p11_emi_nh3_ag) <- "nh3.agriculture" - magclass::getNames(p11_emi_nh3_forest) <- "nh3.forest burning" - magclass::getNames(p11_emi_nh3_savannah) <- "nh3.grassland burning" - p_emi_nh3_others = new.magpie(cells_and_regions = getRegions(p11_emi_nh3_savannah), years = getYears(p11_emi_nh3_savannah), names = c("nh3.waste", "nh3.international shipping", "nh3.aviation"), fill=0.0) - p_emi_nh3 = mbind(p11_emi_nh3_agwaste, p11_emi_nh3_ag, p11_emi_nh3_forest, p11_emi_nh3_savannah, p_emi_nh3_others) - } else { - magclass::getNames(p11_emi_nh3_agwaste) <- "nh3.agwasteburning" - magclass::getNames(p11_emi_nh3_ag) <- "nh3.agriculture" - magclass::getNames(p11_emi_nh3_forest) <- "nh3.forestburning" - magclass::getNames(p11_emi_nh3_savannah) <- "nh3.grasslandburning" - p_emi_nh3_others = new.magpie(cells_and_regions = getRegions(p11_emi_nh3_savannah), years = getYears(p11_emi_nh3_savannah), names = c("nh3.waste", "nh3.internationalshipping", "nh3.aviation"), fill=0.0) - p_emi_nh3 = mbind(p11_emi_nh3_agwaste, p11_emi_nh3_ag, p11_emi_nh3_forest, p11_emi_nh3_savannah, p_emi_nh3_others) - } - ####### calculate minimal temporal and spatial resolutions ##### - getItems(pm_limits_wp4_rcp, dim = 1) <- toupper(getRegions(pm_limits_wp4_rcp)) - y <- Reduce(intersect,list(getYears(p11_emi_postrun), getYears(pm_limits_wp4_rcp), getYears(pm_emiAP))) - r <- Reduce(intersect,list(getRegions(p11_emi_postrun),getRegions(pm_limits_wp4_rcp), getRegions(pm_emiAP))) - p11_emi_postrun <- p11_emi_postrun[r,y,] - pm_limits_wp4_rcp <- pm_limits_wp4_rcp[r,y,] - if (!is.null(pm_limits_wp4_rcp_world)) { - pm_limits_wp4_rcp_world <- pm_limits_wp4_rcp_world[,y,] - } - pm_emiAP <- pm_emiAP[r,y,] - p_emi_nh3 <- p_emi_nh3[r,y,] - pm_limits_wp4_rcp <- pm_limits_wp4_rcp[,,magclass::getNames(p_emi_nh3),invert=TRUE] - pm_limits_wp4_rcp = mbind(pm_limits_wp4_rcp, p_emi_nh3) - - - ####### calculate reporting parameters ############ - # Loop over air pollutants and call reporting generating function - out <- do.call("mbind", lapply(airpollutants, generateReportingEmiAP)) - - # Add global values - out <- mbind(out, dimSums(out,dim=1)) - - # Add Aviation and Int. Shipping emissions - if (!is.null(pm_limits_wp4_rcp_world)) { - for (pollutant in airpollutants) { - pollutant_low <- tolower(pollutant) - - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] <- pm_limits_wp4_rcp_world["GLO",,paste(pollutant_low,"internationalshipping",sep= ".")] - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] <- pm_limits_wp4_rcp_world["GLO",,paste(pollutant_low,"aviation",sep= ".")] - - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)")] <- out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] - - out["GLO",,paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)")] <- out["GLO",,paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] - - out["GLO",,paste0("Emi|",pollutant," (Mt ",pollutant,"/yr)")] <- out["GLO",,paste0("Emi|",pollutant," (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] - } - } - - - } - - #=================================================================== - # if realisation is postECLIPSE - #=================================================================== - } else if (realisation == "postECLIPSE") { - - # DEBUG - #gdx = "path_to_gdx_directory/fulldata.gdx" - #mainfolder = "path_to_rd3_inputdata/" - - #require(moinput,quietly = TRUE) - #setConfig(mainfolder = mainfolder) - - ######### initialisation ########### - airpollutants <- c("SO2","BC","OC","CO","VOC","NOx","NH3") - - generateReportingEmiAP <- function(pollutant){ - - pollutant_low <- tolower(pollutant) - - # Remove unecessary technological dimensions - dataEndo <- collapseNames(dimSums(p11_emi_postrun[,,pollutant_low], dim=c("all_enty","all_enty1","all_te"),na.rm=TRUE)) - - if (!is.null(p11_emiAP_endu) & pollutant_low != "nh3") { - dataEndo <- dataEndo[,,"indst", pmatch=TRUE, invert=TRUE] - dataEndo <- dataEndo[,,"res", pmatch=TRUE, invert=TRUE] - - dataEndo <- mbind(dataEndo, collapseNames(p11_emiAP_endu[,,pollutant_low])[,,"indst"]) - dataEndo <- mbind(dataEndo, collapseNames(p11_emiAP_endu[,,pollutant_low])[,,"res"]) - } - - dataExog <- collapseNames(pm_emiAP[,,pollutant_low]) - - # Apply emission base year calibration (if required) - if (!is.null(pm_emi_calib)) { - if (pollutant_low %in% c("nox", "co", "voc")) { - # Industry - dataEndo[,,"indst"] = dataEndo[,,"indst"] * setYears(collapseNames(pm_emi_calib[,2005,paste0(pollutant_low, ".indst")],1) / (dataEndo[,2005,"indst"] + setNames(dataExog[,2005,"indprocess"], NULL)), NULL) - dataExog[,,"indprocess"] = dataExog[,,"indprocess"] * setYears(collapseNames(pm_emi_calib[,2005,paste0(pollutant_low, ".indst")],1) / (dataEndo[,2005,"indst"] + setNames(dataExog[,2005,"indprocess"], NULL)), NULL) - # Residential and commercial - dataEndo[,,"res"] = dataEndo[,,"res"] * setYears(collapseNames(pm_emi_calib[,2005,paste0(pollutant_low, ".res")],1) / (dataEndo[,2005,"res"]), NULL) - # Power - dataEndo[,,"power"] = dataEndo[,,"power"] * setYears(collapseNames(pm_emi_calib[,2005,paste0(pollutant_low, ".power")],1) / (dataEndo[,2005,"power"] + setNames(dataExog[,2005,"extraction"], NULL)), NULL) - dataExog[,,"extraction"] = dataExog[,,"extraction"] * setYears(collapseNames(pm_emi_calib[,2005,paste0(pollutant_low, ".power")],1) / (dataEndo[,2005,"power"] + setNames(dataExog[,2005,"extraction"], NULL)), NULL) - # Transport - dataEndo[,,"trans"] = dataEndo[,,"trans"] * setYears(collapseNames(pm_emi_calib[,2005,paste0(pollutant_low, ".trans")],1) / (dataEndo[,2005,"trans"]), NULL) - } - } - if (!is.null(pm_emi_calib)) { - if (pollutant_low %in% c("so2", "bc", "oc")) { - # Cross-sectoral scaling factor - ship = pm_limits_wp4_rcp[,2005,paste0(pollutant_low,".internationalshipping")] - air = pm_limits_wp4_rcp[,2005,paste0(pollutant_low,".aviation")] - scalfac <- setNames(setYears((dimSums(pm_emi_calib[,2005,pollutant_low]) + ship + air )/(dimSums(dataEndo[,2005,]) + dimSums(dataExog[,2005,]) + ship + air), NULL), NULL) - # Multiply factor - dataEndo[,,] = dataEndo[,,] * scalfac - dataExog[,,] = dataExog[,,] * scalfac - pm_limits_wp4_rcp[,,paste0(pollutant_low,".internationalshipping")] = pm_limits_wp4_rcp[,,paste0(pollutant_low,".internationalshipping")] * scalfac - pm_limits_wp4_rcp[,,paste0(pollutant_low,".aviation")] = pm_limits_wp4_rcp[,,paste0(pollutant_low,".aviation")] * scalfac - } - } - - # Replace REMIND sector names by reporting ones - mapping = data.frame( - remind = c("power", "indst", "res", "trans", "indprocess", "solvents", "extraction"), - reporting = c(paste0("Emi|", pollutant, "|Energy Supply|Electricity (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Buildings (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Transport|Ground Trans (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Solvents (Mt ", pollutant, "/yr)"), - paste0("Emi|", pollutant, "|Energy Supply|Extraction (Mt ", pollutant, "/yr)"))) - - magclass::getNames(dataEndo) <- paste(sapply(magclass::getNames(dataEndo), function(x) mapping$reporting[mapping$remind == x])) - magclass::getNames(dataExog) <- paste(sapply(magclass::getNames(dataExog), function(x) mapping$reporting[mapping$remind == x])) - - dataAll <- mbind(dataEndo[,,paste(mapping$reporting[which(!mapping$remind %in% c("indst","indprocess", "solvents","extraction"))])], - dataExog[,,paste(mapping$reporting[which(!mapping$remind %in% c("power","indst","res","trans","indprocess"))])], - dataEndo[,,paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)")] + - dataExog[,,paste0("Emi|", pollutant, "|Energy Demand|Industry (Mt ", pollutant, "/yr)")]) - - tmp1 <- - mbind(dataAll, - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"internationalshipping",sep= ".")], - paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"aviation",sep= ".")], - paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"agwasteburning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Agricultural Waste Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"agriculture",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Agriculture (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"forestburning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Forest Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"grasslandburning",sep= ".")], - paste0("Emi|",pollutant,"|Land Use|Savannah Burning (Mt ",pollutant,"/yr)")), - setNames(pm_limits_wp4_rcp[,,paste(pollutant_low,"waste",sep= ".")], - paste0("Emi|",pollutant,"|Waste (Mt ",pollutant,"/yr)"))) - - # Aggregation: Transport and Energy Supply - tmp2 <- mbind(tmp1, - setNames(dimSums(tmp1[,, - c(paste0("Emi|",pollutant,"|Energy Demand|Transport|Ground Trans (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)")), - setNames(dimSums(tmp1[,, - c(paste0("Emi|",pollutant,"|Energy Supply|Electricity (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Supply|Extraction (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Energy Supply (Mt ",pollutant,"/yr)")) - ) - - # Aggregation: Energy Demand + Energy Supply, Land Use - tmp3 <- mbind(tmp2, - setNames(dimSums(tmp2[,,c(paste0("Emi|",pollutant,"|Energy Demand|Industry (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Buildings (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Energy Supply (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)")), - setNames(dimSums(tmp2[,,c(paste0("Emi|",pollutant,"|Land Use|Savannah Burning (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use|Forest Burning (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use|Agriculture (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use|Agricultural Waste Burning (Mt ",pollutant,"/yr)"))],dim = 3), - paste0("Emi|",pollutant,"|Land Use (Mt ",pollutant,"/yr)"))) - - # Compute total - tmp4 <- mbind(tmp3, - setNames(dimSums(tmp3[,,c(paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Solvents (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Land Use (Mt ",pollutant,"/yr)"), - paste0("Emi|",pollutant,"|Waste (Mt ",pollutant,"/yr)"))],dim=3), - paste0("Emi|",pollutant," (Mt ",pollutant,"/yr)"))) - - # Set NAs to 0 - tmp4[is.na(tmp4)] <- 0 - - return(tmp4) - } - - ####### conversion factors ########## - pm_conv_TWa_EJ <- 31.536 - conv_MtSO2_to_MtS <- 1/2 # 32/(32+2*16) - - - ####### read in needed data ######### - ## parameter - p11_emi_postrun <- readGDX(gdx, name=c("pm_emi_postrun","p11_emi_postrun"), field="l", format="first_found",restore_zeros = FALSE) - pm_limits_wp4_rcp <- readGDX(gdx, name=c("pm_emiAPexo","pm_limits_wp4_rcp"), field="l", format="first_found") - pm_limits_wp4_rcp_world <- readGDX(gdx, name=c("pm_emiAPexoGlob","pm_limits_wp4_rcp_world"), field="l", format="first_found") - pm_emiAP <- readGDX(gdx, name="pm_emiAP", field="l", format="first_found") - p11_emiAP_endu <- readGDX(gdx, name=c("pm_emiAP_endu","p11_emiAP_endu"), format="first_found") - p11_emi_nh3_agwaste <- readGDX(gdx, name="p11_emi_nh3_agwaste", field="l") - p11_emi_nh3_ag <- readGDX(gdx, name="p11_emi_nh3_ag", field="l") - p11_emi_nh3_forest <- readGDX(gdx, name="p11_emi_nh3_forest", field="l") - p11_emi_nh3_savannah <- readGDX(gdx, name="p11_emi_nh3_savannah", field="l") - pm_emi_calib <- readGDX(gdx, name=c("pm_APemi_calib","pm_emi_calib_ceds"), format="first_found") - - if (!is.null(pm_emiAP)) { - ####### process data ######### - magclass::getNames(p11_emi_postrun) <- tolower(magclass::getNames(p11_emi_postrun)) - magclass::getNames(pm_limits_wp4_rcp) <- tolower(magclass::getNames(pm_limits_wp4_rcp)) - magclass::getNames(pm_limits_wp4_rcp_world) <- tolower(magclass::getNames(pm_limits_wp4_rcp_world)) - magclass::getNames(pm_emiAP) <- tolower(magclass::getNames(pm_emiAP)) - magclass::getNames(p11_emiAP_endu) <- tolower(magclass::getNames(p11_emiAP_endu)) - magclass::getNames(pm_emi_calib) <- tolower(magclass::getNames(pm_emi_calib)) - - p11_emi_postrun[,,"so2"] <- 1/conv_MtSO2_to_MtS * p11_emi_postrun[,,"so2"] - pm_limits_wp4_rcp[,,"so2"] <- 1/conv_MtSO2_to_MtS * pm_limits_wp4_rcp[,,"so2"] - pm_limits_wp4_rcp_world[,,"so2"] <- 1/conv_MtSO2_to_MtS * pm_limits_wp4_rcp_world[,,"so2"] - pm_emiAP[,,"so2"] <- 1/conv_MtSO2_to_MtS * pm_emiAP[,,"so2"] - p11_emiAP_endu[,,"so2"] <- 1/conv_MtSO2_to_MtS * p11_emiAP_endu[,,"so2"] - pm_emi_calib[,,"so2"] <- 1/conv_MtSO2_to_MtS * pm_emi_calib[,,"so2"] - - if (is.null(pm_limits_wp4_rcp_world)) { - pm_limits_wp4_rcp <- do.call("mbind", - lapply(setdiff(magclass::getNames(pm_limits_wp4_rcp,dim = 1),"WORLD"), - function(reg){ - mreg <- collapseNames(pm_limits_wp4_rcp[,,reg]) - getItems(mreg, dim = 1) <- reg - return(mreg) - })) - - magclass::getNames(pm_limits_wp4_rcp) <- tolower(magclass::getNames(pm_limits_wp4_rcp)) - } else { - select_species.sector <- paste(sapply(tolower(airpollutants), function(x) paste(x,c("agriculture","agwasteburning","forestburning","grasslandburning","aviation","internationalshipping","waste"), sep="."))) - pm_limits_wp4_rcp <- pm_limits_wp4_rcp[,,select_species.sector][,,"nh3", invert=TRUE] - } - - # Merge NH3 data with other air pollutants data - magclass::getNames(p11_emi_nh3_agwaste) <- "nh3.agwasteburning" - magclass::getNames(p11_emi_nh3_ag) <- "nh3.agriculture" - magclass::getNames(p11_emi_nh3_forest) <- "nh3.forestburning" - magclass::getNames(p11_emi_nh3_savannah) <- "nh3.grasslandburning" - p_emi_nh3_others = new.magpie(cells_and_regions = getRegions(p11_emi_nh3_savannah), years = getYears(p11_emi_nh3_savannah), names = c("nh3.waste", "nh3.internationalshipping", "nh3.aviation"), fill=0.0) - p_emi_nh3 = mbind(p11_emi_nh3_agwaste, p11_emi_nh3_ag, p11_emi_nh3_forest, p11_emi_nh3_savannah, p_emi_nh3_others) - - ####### calculate minimal temporal and spatial resolutions ##### - getItems(pm_limits_wp4_rcp, dim = 1) <- toupper(getRegions(pm_limits_wp4_rcp)) - y <- Reduce(intersect,list(getYears(p11_emi_postrun), getYears(pm_limits_wp4_rcp), getYears(pm_emiAP), getYears(p11_emiAP_endu))) - r <- Reduce(intersect,list(getRegions(p11_emi_postrun),getRegions(pm_limits_wp4_rcp), getRegions(pm_emiAP), getRegions(p11_emiAP_endu))) - p11_emi_postrun <- p11_emi_postrun[r,y,] - pm_limits_wp4_rcp <- pm_limits_wp4_rcp[r,y,] - pm_limits_wp4_rcp_world <- pm_limits_wp4_rcp_world[,y,] - pm_emiAP <- pm_emiAP[r,y,] - p11_emiAP_endu <- p11_emiAP_endu[r,y,] - p_emi_nh3 <- p_emi_nh3[r,y,] - - pm_limits_wp4_rcp = mbind(pm_limits_wp4_rcp, p_emi_nh3) - - - ####### calculate reporting parameters ############ - # Loop over air pollutants and call reporting generating function - out <- do.call("mbind", lapply(airpollutants, generateReportingEmiAP)) - - - # Add global values - out <- mbind(out, dimSums(out,dim=1)) - - # Add Aviation and Int. Shipping emissions - if (!is.null(pm_limits_wp4_rcp_world)) { - for (pollutant in airpollutants) { - pollutant_low <- tolower(pollutant) - - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] <- pm_limits_wp4_rcp_world["GLO",,paste(pollutant_low,"internationalshipping",sep= ".")] - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] <- pm_limits_wp4_rcp_world["GLO",,paste(pollutant_low,"aviation",sep= ".")] - - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)")] <- out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] - - out["GLO",,paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)")] <- out["GLO",,paste0("Emi|",pollutant,"|Energy Supply and Demand (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] - - out["GLO",,paste0("Emi|",pollutant," (Mt ",pollutant,"/yr)")] <- out["GLO",,paste0("Emi|",pollutant," (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|International Shipping (Mt ",pollutant,"/yr)")] + - out["GLO",,paste0("Emi|",pollutant,"|Energy Demand|Transport|Aviation (Mt ",pollutant,"/yr)")] - } - } - - } - - #=================================================================== - # if realisation is exoGAINS - #=================================================================== - } else if (realisation == "exoGAINS") { ######### initialisation ########### airpollutants <- c("so2","bc","oc","CO","VOC","NOx","NH3") @@ -615,9 +148,6 @@ reportEmiAirPol <- function(gdx,regionSubsetList=NULL,t=c(seq(2005,2060,5),seq(2 ) } - - } else {stop("not allowed AP-realization")} - getSets(out)[3] <- "variable" return(out) } diff --git a/README.md b/README.md index b2aa771d..7eedf9f9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # The REMIND R package (2nd generation) -R package **remind2**, version **1.143.0** +R package **remind2**, version **1.143.1** [![CRAN status](https://www.r-pkg.org/badges/version/remind2)](https://cran.r-project.org/package=remind2) [![R build status](https://github.com/pik-piam/remind2/workflows/check/badge.svg)](https://github.com/pik-piam/remind2/actions) [![codecov](https://codecov.io/gh/pik-piam/remind2/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/remind2) [![r-universe](https://pik-piam.r-universe.dev/badges/remind2)](https://pik-piam.r-universe.dev/builds) @@ -49,7 +49,7 @@ In case of questions / problems please contact Renato Rodrigues . +Rodrigues R, Baumstark L, Benke F, Dietrich J, Dirnaichner A, Duerrwaechter J, Führlich P, Giannousakis A, Hasse R, Hilaire J, Klein D, Koch J, Kowalczyk K, Levesque A, Malik A, Merfort A, Merfort L, Morena-Leiva S, Pehl M, Pietzcker R, Rauner S, Richters O, Rottoli M, Schötz C, Schreyer F, Siala K, Sörgel B, Spahr M, Strefler J, Verpoort P, Weigmann P, Rüter T (2024). _remind2: The REMIND R package (2nd generation)_. R package version 1.143.1, . A BibTeX entry for LaTeX users is @@ -58,7 +58,7 @@ A BibTeX entry for LaTeX users is title = {remind2: The REMIND R package (2nd generation)}, author = {Renato Rodrigues and Lavinia Baumstark and Falk Benke and Jan Philipp Dietrich and Alois Dirnaichner and Jakob Duerrwaechter and Pascal Führlich and Anastasis Giannousakis and Robin Hasse and Jérome Hilaire and David Klein and Johannes Koch and Katarzyna Kowalczyk and Antoine Levesque and Aman Malik and Anne Merfort and Leon Merfort and Simón Morena-Leiva and Michaja Pehl and Robert Pietzcker and Sebastian Rauner and Oliver Richters and Marianna Rottoli and Christof Schötz and Felix Schreyer and Kais Siala and Björn Sörgel and Mike Spahr and Jessica Strefler and Philipp Verpoort and Pascal Weigmann and Tonn Rüter}, year = {2024}, - note = {R package version 1.143.0}, + note = {R package version 1.143.1}, url = {https://github.com/pik-piam/remind2}, } ``` diff --git a/man/test_ranges.Rd b/man/test_ranges.Rd new file mode 100644 index 00000000..ba3d15f1 --- /dev/null +++ b/man/test_ranges.Rd @@ -0,0 +1,58 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/test_ranges.R +\name{test_ranges} +\alias{test_ranges} +\title{Test Ranges on Variables in magpie Objects} +\usage{ +test_ranges( + data, + tests, + reaction = c("warning", "stop"), + report.missing = FALSE +) +} +\arguments{ +\item{data}{A \code{\link[magclass:magclass-package]{magpie}} object to test.} + +\item{tests}{A list of tests to perform, where each tests consists of: +- A regular expression to match variables names in \code{data} (mandatory +first item). +- A named entry \code{low} to test the lower bound. Can be set to \code{NULL} or +omitted. +- A named entry \code{up} to test the upper bound. Can be set to \code{NULL} or +omitted. +- An optional entry \code{ignore.case} which can be set to \code{FALSE} if the +regular expression should be matched case-sensitive.} + +\item{reaction}{A character string, either \code{'warning'} or \code{'stop'}, to either +warn or throw an error if variables exceed the ranges.} + +\item{report.missing}{If set to \code{TRUE}, will message about regular +expressions from \code{tests} not matching any variables in \code{data}.} +} +\description{ +Test Ranges on Variables in magpie Objects +} +\examples{ +require(dplyr) +require(tidyr) + +(data <- bind_rows( + expand_grid(variable = 'Foo Share (\%)', + value = c(-1, 0, 42, 100, 101)), + expand_grid(variable = 'bar share (percent)', + value = c(-1, 0, 42, 100, 101))) \%>\% + group_by(variable) \%>\% + mutate(year = 2000 + (1:n())) \%>\% + ungroup() \%>\% + select(year, variable, value) \%>\% + as.magpie(spatial = 0, temporal = 1, data = 3)) + +tests <- list(list("Share.*\\\\((\%|Percent)\\\\)$", low = 0, up = 100)) + +test_ranges(data, tests) + +} +\author{ +Michaja Pehl +}