-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest-convGDX2mif.R
90 lines (76 loc) · 3.76 KB
/
test-convGDX2mif.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# uncomment to skip test
# skip("Skip GDX test")
# Check REMIND output. dt is a data.table in *wide* format,
# i.e., variables are columns. `eqs` is a list of equations of the form
# list(LHS = "RHS", ...). The scope determines if the equations
# should be checked for regions ("regional"), only globally ("world") or
# both ("all"). Sensitivity determines the allowed offset when comparing
# LHS to RHS
library(dplyr)
library(gdx)
test_that("Test if REMIND reporting is produced as it should and check data integrity", {
skip_if_not(as.logical(gdxrrw::igdx(silent = TRUE)), "gdxrrw is not initialized properly")
# GDXs for comparison.
gdxList <- c("fulldata-SSP2-EU21-PkBudg650-release.gdx" = "https://rse.pik-potsdam.de/data/example/remind2_test-convGDX2MIF_SSP2-EU21-PkBudg650_2024-06-18_22.43.19.gdx",
"fulldata-SSP2-NPi-AMT.gdx" = "https://rse.pik-potsdam.de/data/example/remind2_test-convGDX2MIF_SSP2-NPi-AMT.gdx",
# this gdx is from REMIND develop and tests the latest Emission reporting after breaking changes
# this gdx can be removed from remind2 with Release 3.5.0, as the changes will be then reflected in the former gdxes
"fulldata-SSP2-NPi-latest.gdx" = "https://rse.pik-potsdam.de/data/example/remind2_test-convGDX2MIF_SSP2-NPi-latest.gdx")
gdxPaths <- NULL
for (i in seq_along(gdxList)) {
from <- gdxList[i]
to <- file.path(tempdir(), names(gdxList[i]))
if (!file.exists(to)) {
utils::download.file(from, to, mode = "wb", quiet = TRUE)
}
gdxPaths <- c(gdxPaths, to)
}
checkPiamTemplates <- function(computedVariables) {
# if you add a new template here, make sure to adjust the piamInterfaces version in the DESCRIPTION
templates <- c("AR6", "AR6_NGFS", "ELEVATE", "NAVIGATE", "SHAPE", "ARIADNE", "ECEMF")
for (template in templates) {
templateVariables <- template %>%
piamInterfaces::getREMINDTemplateVariables() %>%
unique() %>%
deletePlus()
expect_true(any(computedVariables %in% templateVariables))
missingVariables <- setdiff(templateVariables, computedVariables)
if (length(missingVariables) > 0) {
warning("The following variables are expected in the piamInterfaces package ",
"for template ", template,
", but cannot be found in the reporting generated by ", gdxPath, ":\n ",
paste(missingVariables, collapse = ",\n "))
}
}
}
# uncomment to add current calibration gdxes
# gdxPaths <- c(gdxPaths, Sys.glob("/p/projects/remind/inputdata/CESparametersAndGDX/*.gdx"))
numberOfMifs <- 0
for (gdxPath in gdxPaths) {
numberOfMifs <- numberOfMifs + 1
message("Running convGDX2MIF(", gdxPath, ")...")
refpolicycost <- if (gdxPath == gdxPaths[[1]]) gdxPath else NULL
mifContent <- convGDX2MIF(gdxPath, gdx_refpolicycost = refpolicycost, testthat = TRUE)
expect_no_warning(piamInterfaces::checkVarNames(getNames(mifContent, dim = 3)))
computedVariables <- deletePlus(getItems(mifContent, dim = 3.3))
computedVariables <- gsub("\\(\\)", "(unitless)", computedVariables)
checkPiamTemplates(computedVariables)
expect_no_error(
test_ranges(
data = mifContent,
tests = list(
list(
"^Emi\\|CO2\\|Energy\\|Demand\\|Industry\\|.*Fossil \\(Mt CO2/yr\\)$",
low = 0),
list("Share.*\\((%|Percent)\\)$", low = 0, up = 100)),
reaction = 'stop'))
magclass::write.report(
x = magclass::collapseNames(mifContent),
file = file.path(tempdir(), paste0(numberOfMifs, ".mif")),
scenario = paste0(magclass::getItems(mifContent, dim = "scenario"), numberOfMifs),
model = "REMIND"
)
}
unlink(tempdir(), recursive = TRUE)
tempdir(TRUE)
})