getReportData <- function(path_to_report, inputpath_mag = "magpie", inputpath_acc = "costs") {
.bioenergy_price <- function(mag) {
notGLO <- magclass::getRegions(mag)[!(magclass::getRegions(mag) == "GLO")]
if ("Demand|Bioenergy|++|2nd generation (EJ/yr)" %in% magclass::getNames(mag)) {
# MAgPIE 4
out <- mag[, , "Prices|Bioenergy (US$05/GJ)"] * 0.0315576 # with transformation factor from US$2005/GJ to US$2005/Wa
} else {
# MAgPIE 3
out <- mag[, , "Price|Primary Energy|Biomass (US$2005/GJ)"] * 0.0315576 # with transformation factor from US$2005/GJ to US$2005/Wa
}
out["JPN", is.na(out["JPN", , ]), ] <- 0
dimnames(out)[[3]] <- NULL # Delete variable name to prevent it from being written into output file
magclass::write.magpie(out[notGLO, , ], paste0("./modules/30_biomass/", inputpath_mag, "/input/p30_pebiolc_pricemag_coupling.csv"), file_type = "csvr")
}
.bioenergy_costs <- function(mag) {
notGLO <- magclass::getRegions(mag)[!(magclass::getRegions(mag) == "GLO")]
if ("Production Cost|Agriculture|Biomass|Energy Crops (million US$2005/yr)" %in% magclass::getNames(mag)) {
out <- mag[, , "Production Cost|Agriculture|Biomass|Energy Crops (million US$2005/yr)"] / 1000 / 1000 # with transformation factor from 10E6 US$2005 to 10E12 US$2005
} else {
# in old MAgPIE reports the unit is reported to be "billion", however the values are in million
out <- mag[, , "Production Cost|Agriculture|Biomass|Energy Crops (billion US$2005/yr)"] / 1000 / 1000 # with transformation factor from 10E6 US$2005 to 10E12 US$2005
}
out["JPN", is.na(out["JPN", , ]), ] <- 0
dimnames(out)[[3]] <- NULL
magclass::write.magpie(out[notGLO, , ], paste0("./modules/30_biomass/", inputpath_mag, "/input/p30_pebiolc_costsmag.csv"), file_type = "csvr")
}
.bioenergy_production <- function(mag) {
notGLO <- magclass::getRegions(mag)[!(magclass::getRegions(mag) == "GLO")]
if ("Demand|Bioenergy|2nd generation|++|Bioenergy crops (EJ/yr)" %in% magclass::getNames(mag)) {
# MAgPIE 4
out <- mag[, , "Demand|Bioenergy|2nd generation|++|Bioenergy crops (EJ/yr)"] / 31.536 # EJ to TWa
} else {
# MAgPIE 3
out <- mag[, , "Primary Energy Production|Biomass|Energy Crops (EJ/yr)"] / 31.536 # EJ to TWa
}
out[which(out < 0)] <- 0 # set negative values to zero since they cause errors in GMAS power function
out["JPN", is.na(out["JPN", , ]), ] <- 0
dimnames(out)[[3]] <- NULL
magclass::write.magpie(out[notGLO, , ], paste0("./modules/30_biomass/", inputpath_mag, "/input/p30_pebiolc_demandmag_coupling.csv"), file_type = "csvr")
}
.emissions_mac <- function(mag) {
# define three columns of dataframe:
# emirem (remind emission names)
# emimag (magpie emission names)
# factor_mag2rem (factor for converting magpie to remind emissions)
# 1/1000*28/44, # kt N2O/yr -> Mt N2O/yr -> Mt N/yr
# 28/44, # Tg N2O/yr = Mt N2O/yr -> Mt N/yr
# 1/1000*12/44, # Mt CO2/yr -> Gt CO2/yr -> Gt C/yr
map <- data.frame(emirem = NULL, emimag = NULL, factor_mag2rem = NULL, stringsAsFactors = FALSE)
if ("Emissions|N2O|Land|Agriculture|+|Animal Waste Management (Mt N2O/yr)" %in% magclass::getNames(mag)) {
# MAgPIE 4 (up to date)
map <- rbind(map, data.frame(emimag = "Emissions|CO2|Land|+|Land-use Change (Mt CO2/yr)", emirem = "co2luc", factor_mag2rem = 1 / 1000 * 12 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land|Agriculture|+|Animal Waste Management (Mt N2O/yr)", emirem = "n2oanwstm", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land|Agriculture|Agricultural Soils|+|Inorganic Fertilizers (Mt N2O/yr)", emirem = "n2ofertin", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land|Agriculture|Agricultural Soils|+|Manure applied to Croplands (Mt N2O/yr)", emirem = "n2oanwstc", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land|Agriculture|Agricultural Soils|+|Decay of Crop Residues (Mt N2O/yr)", emirem = "n2ofertcr", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land|Agriculture|Agricultural Soils|+|Soil Organic Matter Loss (Mt N2O/yr)", emirem = "n2ofertsom", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land|Agriculture|Agricultural Soils|+|Pasture (Mt N2O/yr)", emirem = "n2oanwstp", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land|Agriculture|+|Rice (Mt CH4/yr)", emirem = "ch4rice", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land|Agriculture|+|Animal waste management (Mt CH4/yr)", emirem = "ch4anmlwst", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land|Agriculture|+|Enteric fermentation (Mt CH4/yr)", emirem = "ch4animals", factor_mag2rem = 1, stringsAsFactors = FALSE))
} else if ("Emissions|N2O-N|Land|Agriculture|+|Animal Waste Management (Mt N2O-N/yr)" %in% magclass::getNames(mag)) {
# MAgPIE 4 (intermediate - wrong units)
map <- rbind(map, data.frame(emimag = "Emissions|CO2|Land|+|Land-use Change (Mt CO2/yr)", emirem = "co2luc", factor_mag2rem = 1 / 1000 * 12 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O-N|Land|Agriculture|+|Animal Waste Management (Mt N2O-N/yr)", emirem = "n2oanwstm", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O-N|Land|Agriculture|Agricultural Soils|+|Inorganic Fertilizers (Mt N2O-N/yr)", emirem = "n2ofertin", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O-N|Land|Agriculture|Agricultural Soils|+|Manure applied to Croplands (Mt N2O-N/yr)", emirem = "n2oanwstc", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O-N|Land|Agriculture|Agricultural Soils|+|Decay of Crop Residues (Mt N2O-N/yr)", emirem = "n2ofertcr", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O-N|Land|Agriculture|Agricultural Soils|+|Soil Organic Matter Loss (Mt N2O-N/yr)", emirem = "n2ofertsom", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O-N|Land|Agriculture|Agricultural Soils|+|Pasture (Mt N2O-N/yr)", emirem = "n2oanwstp", factor_mag2rem = 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land|Agriculture|+|Rice (Mt CH4/yr)", emirem = "ch4rice", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land|Agriculture|+|Animal waste management (Mt CH4/yr)", emirem = "ch4anmlwst", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land|Agriculture|+|Enteric fermentation (Mt CH4/yr)", emirem = "ch4animals", factor_mag2rem = 1, stringsAsFactors = FALSE))
} else {
# MAgPIE 3
map <- rbind(map, data.frame(emimag = "Emissions|CO2|Land Use (Mt CO2/yr)", emirem = "co2luc", factor_mag2rem = 1 / 1000 * 12 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Agriculture|AWM (kt N2O/yr)", emirem = "n2oanwstm", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Agriculture|Cropland Soils|Inorganic Fertilizers (kt N2O/yr)", emirem = "n2ofertin", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Agriculture|Cropland Soils|Manure applied to Croplands (kt N2O/yr)", emirem = "n2oanwstc", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Agriculture|Cropland Soils|Decay of crop residues (kt N2O/yr)", emirem = "n2ofertcr", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Agriculture|Cropland Soils|Soil organic matter loss (kt N2O/yr)", emirem = "n2ofertsom", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Agriculture|Cropland Soils|Lower N2O emissions of rice (kt N2O/yr)", emirem = "n2ofertrb", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Agriculture|Pasture (kt N2O/yr)", emirem = "n2oanwstp", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Biomass Burning|Forest Burning (kt N2O/yr)", emirem = "n2oforest", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Biomass Burning|Savannah Burning (kt N2O/yr)", emirem = "n2osavan", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|N2O|Land Use|Biomass Burning|Agricultural Waste Burning (kt N2O/yr)", emirem = "n2oagwaste", factor_mag2rem = 1 / 1000 * 28 / 44, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land Use|Agriculture|Rice (Mt CH4/yr)", emirem = "ch4rice", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land Use|Agriculture|AWM (Mt CH4/yr)", emirem = "ch4anmlwst", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land Use|Agriculture|Enteric Fermentation (Mt CH4/yr)", emirem = "ch4animals", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land Use|Biomass Burning|Forest Burning (Mt CH4/yr)", emirem = "ch4forest", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land Use|Biomass Burning|Savannah Burning (Mt CH4/yr)", emirem = "ch4savan", factor_mag2rem = 1, stringsAsFactors = FALSE))
map <- rbind(map, data.frame(emimag = "Emissions|CH4|Land Use|Biomass Burning|Agricultural Waste Burning (Mt CH4/yr)", emirem = "ch4agwaste", factor_mag2rem = 1, stringsAsFactors = FALSE))
}
# Read data from MAgPIE report and convert to REMIND data, collect in 'out' object
out <- NULL
for (i in 1:nrow(map)) {
tmp <- magclass::setNames(mag[, , map[i, ]$emimag], map[i, ]$emirem)
tmp <- tmp * map[i, ]$factor_mag2rem
# tmp["JPN",is.na(tmp["JPN",,]),] <- 0
# preliminary fix 20160111
# cat("Preliminary quick fix: filtering out NAs for all and negative values for almost all landuse emissions except for co2luc and n2ofertrb\n")
# tmp[is.na(tmp)] <- 0
# preliminary 20160114: filter out negative values except for co2luc and n2ofertrb
# if (map[i,]$emirem!="co2luc" && map[i,]$emirem!="n2ofertrb") {
# tmp[tmp<0] <- 0
# }
# Check for negative values, since only "co2luc" is allowed to be
# negative. All other emission variables are positive by definition.
if (map[i, ]$emirem != "co2luc") {
if (!(all(tmp >= 0))) {
# Hotfix 2021-09-28: Raise warning and set negative values to zero.
# XXX Todo XXX: Make sure that MAgPIE is not reporting negative N2O
# or CH4 emissions and convert this warning into an error that
# breaks the model instead of setting the values to zero.
print(paste0(
"Warning: Negative values detected for '",
map[i, ]$emirem, "' / '", map[i, ]$emimag, "'. ",
"Hot fix: Set respective values to zero."
))
tmp[tmp < 0] <- 0
}
}
# Add emission variable to full dataframe
out <- magclass::mbind(out, tmp)
}
# Write REMIND input file
notGLO <- magclass::getRegions(mag)[!(magclass::getRegions(mag) == "GLO")]
filename <- paste0("./core/input/f_macBaseMagpie_coupling.cs4r")
magclass::write.magpie(out[notGLO, , ], filename)
write(paste0("*** EOF ", filename, " ***"), file = filename, append = TRUE)
}
.agriculture_costs <- function(mag) {
notGLO <- magclass::getRegions(mag)[!(magclass::getRegions(mag) == "GLO")]
out <- mag[, , "Costs|MainSolve w/o GHG Emissions (million US$05/yr)"] / 1000 / 1000 # with transformation factor from 10E6 US$2005 to 10E12 US$2005
out["JPN", is.na(out["JPN", , ]), ] <- 0
dimnames(out)[[3]] <- NULL # Delete variable name to prevent it from being written into output file
magclass::write.magpie(out[notGLO, , ], paste0("./modules/26_agCosts/", inputpath_acc, "/input/p26_totLUcost_coupling.csv"), file_type = "csvr")
}
.agriculture_tradebal <- function(mag) {
notGLO <- magclass::getRegions(mag)[!(magclass::getRegions(mag) == "GLO")]
out <- mag[, , "Trade|Agriculture|Trade Balance (billion US$2005/yr)"] / 1000 # with transformation factor from 10E9 US$2005 to 10E12 US$2005
out["JPN", is.na(out["JPN", , ]), ] <- 0
dimnames(out)[[3]] <- NULL
magclass::write.magpie(out[notGLO, , ], paste0("./modules/26_agCosts/", inputpath_acc, "/input/trade_bal_reg.rem.csv"), file_type = "csvr")
}
rep <- magclass::read.report(path_to_report, as.list = FALSE)
if (length(magclass::getNames(rep, dim = "scenario")) != 1) stop("getReportData: MAgPIE data contains more or less than 1 scenario.")
rep <- magclass::collapseNames(rep) # get rid of scenrio and model dimension if they exist
years <- 2000 + 5 * (1:30)
mag <- magclass::time_interpolate(rep, years)
.bioenergy_price(mag)
# .bioenergy_costs(mag) # Obsolete since bioenergy costs are not calculated by MAgPIE anymore but by integrating the supplycurve
.bioenergy_production(mag)
.emissions_mac(mag)
.agriculture_costs(mag)
# need to be updated to MAgPIE 4 interface
# .agriculture_tradebal(mag)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.