Skip to content

Commit

Permalink
use piamInterfaces, not yet tested
Browse files Browse the repository at this point in the history
  • Loading branch information
orichters committed Sep 29, 2023
1 parent 8a85589 commit 4075ff5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 46 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ Imports:
nleqslv,
optparse,
piamenv (>= 0.4.0),
piamInterfaces (>= 0.0.88),
piamInterfaces (>= 0.4.0),
plotly,
purrr,
quitte (>= 0.3106.0),
quitte (>= 0.3123.0),
R.utils,
raster,
readr,
Expand Down
48 changes: 4 additions & 44 deletions scripts/output/single/fixOnRef.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,61 +64,21 @@ fixOnMif <- function(outputdir) {
refname <- basename(dirname(refmif))
d <- quitte::as.quitte(mifs)
dref <- quitte::as.quitte(refmif)
if (identical(levels(d$scenario), levels(dref$scenario))) {
levels(dref$scenario) <- paste0(levels(dref$scenario), "_ref")
}

falsepositives <- grep("Moving Avg$", levels(d$variable), value = TRUE)
failfile <- file.path(outputdir, "log_fixOnRef.csv")
fixeddata <- piamInterfaces::fixOnRef(d, dref, ret = "fixed", startyear = startyear, failfile = failfile)

message("Comparing ", title, " with reference run ", refname, " for t < ", startyear)
mismatches <- rbind(d, dref) %>%
filter(period < startyear, ! variable %in% falsepositives) %>%
group_by(model, region, variable, unit, period) %>%
filter(1e-16 < var(value)) %>%
ungroup() %>%
distinct(variable, period) %>%
group_by(variable) %>%
summarise(period = paste(sort(period), collapse = ', '))
if (nrow(mismatches) == 0) {
message("# Run is perfectly fixed on reference run!")
return(TRUE)
}
showrows <- 50
theserows <- match(unique(gsub("\\|.*$", "", mismatches$variable)), gsub("\\|.*$", "", mismatches$variable))
# extract some representative variables that differ in the first two parts of A|B|C…
first2elements <- gsub("(\\|.*?)\\|.*$", "\\1", mismatches$variable)
theserows <- match(unique(first2elements), first2elements)
theserows <- theserows[seq(min(length(theserows), showrows))]
rlang::with_options(width = 160, print(mismatches[theserows, ], n = showrows))
if (length(theserows) < nrow(mismatches)) {
message("The variables above are representative, avoiding repetition after A|B. ",
"Additional ", (nrow(mismatches) - length(theserows)), " variables differ.")
}
filename <- file.path(outputdir, "log_fixOnRef.csv")
message("Find failing variables in '", filename, "'.")
csvdata <- rbind(mutate(d, scenario = "value"), mutate(dref, scenario = "ref")) %>%
filter(! variable %in% falsepositives, period < startyear) %>%
pivot_wider(names_from = scenario) %>%
filter(abs(value - ref) > 0) %>%
add_column(scenario = title) %>%
droplevels()
write.csv(csvdata, filename, quote = FALSE, row.names = FALSE)
if (exists("flags") && isTRUE("--interactive" %in% flags)) {
message("\nDo you want to fix that by overwriting ", title, " mif with reference run ", refname, " for t < ", startyear, "?\nType: y/N")
if (tolower(gms::getLine()) %in% c("y", "yes")) {
message("Updating ", mifs[[1]])
di <- rbind(
filter(d, period >= startyear | ! variable %in% levels(dref$variable) | variable %in% falsepositives),
mutate(filter(dref, period < startyear & variable %in% levels(d$variable)) & ! variable %in% falsepositives, scenario = title)
)
tmpfile <- paste0(mifs[[1]], "fixOnMif")
quitte::write.mif(di, tmpfile)
quitte::write.mif(fixeddata, tmpfile)
file.rename(tmpfile, mifs[[1]])
remind2::deletePlus(mifs[[1]], writemif = TRUE)
message("Keep in mind to update the runs that use this as `path_gdx_ref` as well.")
}
}
return(mismatches)
return(NULL)
}

invisible(fixOnMif(outputdir))

0 comments on commit 4075ff5

Please sign in to comment.