-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adapt reportEmiAirPol.R
to use renamed air pollution variables after MAGICC6 removal
#682
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3fc3ed2
Added p11_emiAPexsolve.gdx as possible file name
5124f33
Adapted 'pm_X' vars to new prefix 'p11_X'
6b90ffc
Removed unused conversion factors
c1c6d11
Don't import getRegions, replace with getItems
e47ed22
Removed magclass:: prefix for getNames calls
b624b99
Re-formatted
874a9a5
Rm'd setYears, restructured imports
08c6f85
Merge branch 'dev/rm_magicc6'
8519c00
buildLibrary
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,136 +16,181 @@ | |
#' @author Antoine Levesque, Jerome Hilaire | ||
#' @seealso \code{\link{convGDX2MIF}} | ||
#' @examples | ||
#' \dontrun{reportEmiAirPol(gdx)} | ||
#' \dontrun{ | ||
#' reportEmiAirPol(gdx) | ||
#' } | ||
#' | ||
#' @export | ||
#' @importFrom gdx readGDX | ||
#' @importFrom magclass collapseNames getNames<- mbind setNames new.magpie getRegions getYears mbind setYears getItems<- | ||
reportEmiAirPol <- function(gdx,regionSubsetList=NULL,t=c(seq(2005,2060,5),seq(2070,2110,10),2130,2150)){ | ||
|
||
#' @importFrom magclass new.magpie mbind getNames<- setNames collapseNames getYears getItems | ||
reportEmiAirPol <- function(gdx, regionSubsetList = NULL, t = c(seq(2005, 2060, 5), seq(2070, 2110, 10), 2130, 2150)) { | ||
# Get realisation name | ||
realisation <- readGDX(gdx, "module2realisation") | ||
realisation <- realisation[which(realisation[,1] == "aerosols"),2] | ||
realisation <- realisation[which(realisation[, 1] == "aerosols"), 2] | ||
|
||
######### initialisation ########### | ||
tmp <- NULL | ||
out <- NULL | ||
|
||
if (! realisation == "exoGAINS") stop("not allowed air pollution realization.") | ||
if (!realisation == "exoGAINS") stop("not allowed air pollution realization.") | ||
|
||
######### initialisation ########### | ||
airpollutants <- c("so2","bc","oc","CO","VOC","NOx","NH3") | ||
airpollutants <- c("so2", "bc", "oc", "CO", "VOC", "NOx", "NH3") | ||
|
||
######### internal function ########### | ||
generateReportingEmiAirPol <- function(pollutant,i_emiAPexsolve=pm_emiAPexsolve,i_emiAPexo=pm_emiAPexo){ | ||
generateReportingEmiAirPol <- function(pollutant, i_emiAPexsolve = p11_emiAPexsolve, i_emiAPexo = p11_emiAPexo) { | ||
poll_rep <- toupper(pollutant) | ||
tmp <- NULL | ||
|
||
# reduce to the pollutant | ||
emiAPexsolve <- collapseNames(i_emiAPexsolve[,,pollutant]) | ||
emiAPexo <- collapseNames(i_emiAPexo[,,pollutant]) | ||
emiAPexsolve <- collapseNames(i_emiAPexsolve[, , pollutant]) | ||
emiAPexo <- collapseNames(i_emiAPexo[, , pollutant]) | ||
getSets(emiAPexo) <- getSets(emiAPexsolve) | ||
|
||
# add indprocess to indst | ||
emiAPexsolve[,,"indst"] <- emiAPexsolve[,,"indst"] + emiAPexsolve[,,"indprocess"] | ||
emiAPexsolve[, , "indst"] <- emiAPexsolve[, , "indst"] + emiAPexsolve[, , "indprocess"] | ||
|
||
# Replace REMIND sector names by reporting ones | ||
mapping = data.frame( | ||
mapping <- data.frame( | ||
remind = c("power", "indst", "res", "trans", "solvents", "extraction"), | ||
reporting = c(paste0("Emi|", poll_rep, "|Energy Supply|Electricity (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Industry (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Buildings (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport|Ground Trans (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Solvents (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Supply|Extraction (Mt ", poll_rep, "/yr)"))) | ||
|
||
emiAPexsolve <- setNames(emiAPexsolve[,,mapping$remind],as.character(mapping$reporting)) | ||
|
||
tmp <- | ||
mbind(emiAPexsolve, | ||
setNames(emiAPexo[,,"AgWasteBurning"], paste0("Emi|",poll_rep,"|Land Use|Agricultural Waste Burning (Mt ",poll_rep,"/yr)")), | ||
setNames(emiAPexo[,,"Agriculture"], paste0("Emi|",poll_rep,"|Land Use|Agriculture (Mt ",poll_rep,"/yr)")), | ||
setNames(emiAPexo[,,"ForestBurning"], paste0("Emi|",poll_rep,"|Land Use|Forest Burning (Mt ",poll_rep,"/yr)")), | ||
setNames(emiAPexo[,,"GrasslandBurning"],paste0("Emi|",poll_rep,"|Land Use|Savannah Burning (Mt ",poll_rep,"/yr)")), | ||
setNames(emiAPexo[,,"Waste"], paste0("Emi|",poll_rep,"|Waste (Mt ",poll_rep,"/yr)"))) | ||
|
||
reporting = c( | ||
paste0("Emi|", poll_rep, "|Energy Supply|Electricity (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Industry (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Buildings (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport|Ground Trans (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Solvents (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Supply|Extraction (Mt ", poll_rep, "/yr)") | ||
) | ||
) | ||
|
||
emiAPexsolve <- setNames(emiAPexsolve[, , mapping$remind], as.character(mapping$reporting)) | ||
|
||
tmp <- mbind( | ||
emiAPexsolve, | ||
setNames(emiAPexo[, , "AgWasteBurning"], paste0("Emi|", poll_rep, "|Land Use|Agricultural Waste Burning (Mt ", poll_rep, "/yr)")), | ||
setNames(emiAPexo[, , "Agriculture"], paste0("Emi|", poll_rep, "|Land Use|Agriculture (Mt ", poll_rep, "/yr)")), | ||
setNames(emiAPexo[, , "ForestBurning"], paste0("Emi|", poll_rep, "|Land Use|Forest Burning (Mt ", poll_rep, "/yr)")), | ||
setNames(emiAPexo[, , "GrasslandBurning"], paste0("Emi|", poll_rep, "|Land Use|Savannah Burning (Mt ", poll_rep, "/yr)")), | ||
setNames(emiAPexo[, , "Waste"], paste0("Emi|", poll_rep, "|Waste (Mt ", poll_rep, "/yr)")) | ||
) | ||
|
||
# Set NAs to 0 | ||
tmp[is.na(tmp)] <- 0 | ||
|
||
return(tmp) | ||
} | ||
|
||
####### conversion factors ########## | ||
pm_conv_TWa_EJ <- 31.536 | ||
conv_MtSO2_to_MtS <- 1/2 # 32/(32+2*16) | ||
|
||
####### read in needed data ######### | ||
## sets | ||
ttot <- as.numeric(readGDX(gdx, name=c("ttot"), format="first_found")) | ||
ttot <- as.numeric(readGDX(gdx, name = c("ttot"), format = "first_found")) | ||
## parameter | ||
pm_emiAPexsolve <- readGDX(gdx, name=c("pm_emiAPexsolve"), field="l", format="first_found")[,ttot,] | ||
pm_emiAPexo <- readGDX(gdx, name=c("pm_emiAPexo"), field="l", format="first_found")[,ttot,airpollutants] | ||
pm_emiAPexoGlob <- readGDX(gdx, name=c("pm_emiAPexoGlob"), field="l", format="first_found")[,ttot,airpollutants] | ||
p11_emiAPexsolve <- readGDX( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .. and added |
||
gdx, | ||
name = c("p11_emiAPexsolve", "pm_emiAPexsolve"), field = "l", format = "first_found" | ||
)[, ttot, ] | ||
p11_emiAPexo <- readGDX( | ||
gdx, | ||
name = c("p11_emiAPexo", "pm_emiAPexo"), field = "l", format = "first_found" | ||
)[, ttot, airpollutants] | ||
p11_emiAPexoGlob <- readGDX( | ||
gdx, | ||
name = c("p11_emiAPexoGlob", "pm_emiAPexoGlob"), field = "l", format = "first_found" | ||
)[, ttot, airpollutants] | ||
|
||
####### prepare parameter ######################## | ||
magclass::getNames(pm_emiAPexsolve) <- gsub("SOx","so2",magclass::getNames(pm_emiAPexsolve)) | ||
magclass::getNames(pm_emiAPexsolve) <- gsub("NMVOC","VOC",magclass::getNames(pm_emiAPexsolve)) | ||
getNames(p11_emiAPexsolve) <- gsub("SOx", "so2", getNames(p11_emiAPexsolve)) | ||
getNames(p11_emiAPexsolve) <- gsub("NMVOC", "VOC", getNames(p11_emiAPexsolve)) | ||
|
||
####### calculate reporting parameters ############ | ||
# Loop over air pollutants and call reporting generating function | ||
out <- do.call("mbind", lapply(airpollutants, generateReportingEmiAirPol)) | ||
|
||
# Add global values | ||
out <- mbind(out, dimSums(out,dim=1)) | ||
out <- mbind(out, dimSums(out, dim = 1)) | ||
# add other region aggregations | ||
if (!is.null(regionSubsetList)) | ||
if (!is.null(regionSubsetList)) { | ||
out <- mbind(out, calc_regionSubset_sums(out, regionSubsetList)) | ||
} | ||
|
||
# Loop over air pollutants and add some variables | ||
for (pollutant in airpollutants) { | ||
poll_rep <- toupper(pollutant) | ||
tmp <- NULL | ||
# Add Aviation and Int. Shipping emissions | ||
tmp <- mbind(tmp,setNames(pm_emiAPexoGlob["GLO",,"InternationalShipping"][,,pollutant],paste0("Emi|",poll_rep,"|Energy Demand|Transport|International Shipping (Mt ",poll_rep,"/yr)")), | ||
setNames(pm_emiAPexoGlob["GLO",,"Aviation"][,,pollutant], paste0("Emi|",poll_rep,"|Energy Demand|Transport|Aviation (Mt ",poll_rep,"/yr)")) | ||
) | ||
tmp1 <- new.magpie(getRegions(out),getYears(out),magclass::getNames(tmp),fill=0) | ||
tmp1["GLO",,] <- tmp["GLO",,] | ||
out <- mbind(out,tmp1) | ||
# Aggregation: Transport and Energy Supply | ||
out <- mbind(out, | ||
setNames(dimSums(out[,, | ||
c(paste0("Emi|",poll_rep,"|Energy Demand|Transport|Ground Trans (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Energy Demand|Transport|International Shipping (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Energy Demand|Transport|Aviation (Mt ",poll_rep,"/yr)"))],dim = 3), | ||
paste0("Emi|",poll_rep,"|Energy Demand|Transport (Mt ",poll_rep,"/yr)")), | ||
setNames(dimSums(out[,, | ||
c(paste0("Emi|",poll_rep,"|Energy Supply|Electricity (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Energy Supply|Extraction (Mt ",poll_rep,"/yr)"))],dim = 3), | ||
paste0("Emi|",poll_rep,"|Energy Supply (Mt ",poll_rep,"/yr)")) | ||
) | ||
# Aggregation: Energy Demand + Energy Supply, Land Use | ||
out <- mbind(out, | ||
setNames(dimSums(out[,,c(paste0("Emi|",poll_rep,"|Energy Demand|Industry (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Energy Demand|Buildings (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Energy Demand|Transport (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Energy Supply (Mt ",poll_rep,"/yr)"))],dim = 3), | ||
paste0("Emi|",poll_rep,"|Energy Supply and Demand (Mt ",poll_rep,"/yr)")), | ||
setNames(dimSums(out[,,c(paste0("Emi|",poll_rep,"|Land Use|Savannah Burning (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Land Use|Forest Burning (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Land Use|Agriculture (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Land Use|Agricultural Waste Burning (Mt ",poll_rep,"/yr)"))],dim = 3), | ||
paste0("Emi|",poll_rep,"|Land Use (Mt ",poll_rep,"/yr)")) | ||
) | ||
# Compute total | ||
out <- mbind(out, | ||
setNames(dimSums(out[,,c(paste0("Emi|",poll_rep,"|Energy Supply and Demand (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Solvents (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Land Use (Mt ",poll_rep,"/yr)"), | ||
paste0("Emi|",poll_rep,"|Waste (Mt ",poll_rep,"/yr)"))],dim=3), | ||
paste0("Emi|",poll_rep," (Mt ",poll_rep,"/yr)")) | ||
) | ||
poll_rep <- toupper(pollutant) | ||
tmp <- NULL | ||
# Add Aviation and Int. Shipping emissions | ||
tmp <- mbind( | ||
tmp, | ||
setNames( | ||
p11_emiAPexoGlob["GLO", , "InternationalShipping"][, , pollutant], | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport|International Shipping (Mt ", poll_rep, "/yr)") | ||
), | ||
setNames( | ||
p11_emiAPexoGlob["GLO", , "Aviation"][, , pollutant], | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport|Aviation (Mt ", poll_rep, "/yr)") | ||
) | ||
) | ||
tmp1 <- new.magpie(getItems(out, dim = 1), getYears(out), getNames(tmp), fill = 0) | ||
tmp1["GLO", , ] <- tmp["GLO", , ] | ||
out <- mbind(out, tmp1) | ||
# Aggregation: Transport and Energy Supply | ||
out <- mbind( | ||
out, | ||
setNames( | ||
dimSums(out[ | ||
, , | ||
c( | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport|Ground Trans (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport|International Shipping (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport|Aviation (Mt ", poll_rep, "/yr)") | ||
) | ||
], dim = 3), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport (Mt ", poll_rep, "/yr)") | ||
), | ||
setNames( | ||
dimSums(out[ | ||
, , | ||
c( | ||
paste0("Emi|", poll_rep, "|Energy Supply|Electricity (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Supply|Extraction (Mt ", poll_rep, "/yr)") | ||
) | ||
], dim = 3), | ||
paste0("Emi|", poll_rep, "|Energy Supply (Mt ", poll_rep, "/yr)") | ||
) | ||
) | ||
# Aggregation: Energy Demand + Energy Supply, Land Use | ||
out <- mbind( | ||
out, | ||
setNames( | ||
dimSums(out[, , c( | ||
paste0("Emi|", poll_rep, "|Energy Demand|Industry (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Buildings (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Demand|Transport (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Energy Supply (Mt ", poll_rep, "/yr)") | ||
)], dim = 3), | ||
paste0("Emi|", poll_rep, "|Energy Supply and Demand (Mt ", poll_rep, "/yr)") | ||
), | ||
setNames( | ||
dimSums(out[, , c( | ||
paste0("Emi|", poll_rep, "|Land Use|Savannah Burning (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Land Use|Forest Burning (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Land Use|Agriculture (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Land Use|Agricultural Waste Burning (Mt ", poll_rep, "/yr)") | ||
)], dim = 3), | ||
paste0("Emi|", poll_rep, "|Land Use (Mt ", poll_rep, "/yr)") | ||
) | ||
) | ||
# Compute total | ||
out <- mbind( | ||
out, | ||
setNames( | ||
dimSums(out[, , c( | ||
paste0("Emi|", poll_rep, "|Energy Supply and Demand (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Solvents (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Land Use (Mt ", poll_rep, "/yr)"), | ||
paste0("Emi|", poll_rep, "|Waste (Mt ", poll_rep, "/yr)") | ||
)], dim = 3), | ||
paste0("Emi|", poll_rep, " (Mt ", poll_rep, "/yr)") | ||
) | ||
) | ||
} | ||
|
||
getSets(out)[3] <- "variable" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced
pm_X
here..