-
Notifications
You must be signed in to change notification settings - Fork 130
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
+ script checking if fixing to ref run worked, allows to fix that in post-processing #1410
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ace7fe1
+ script checking if fixing to ref run worked, allows to fix that in …
orichters 42a863b
rename column to have well-defined order and less ugly code
orichters 0e8db57
improve csv file of fixOnRef
orichters 8a85589
adapt changelog, allow for minimal differences, add falsepositives
orichters 23bf912
use piamInterfaces::fixOnRef
orichters 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# | (C) 2006-2023 Potsdam Institute for Climate Impact Research (PIK) | ||
# | authors, and contributors see CITATION.cff file. This file is part | ||
# | of REMIND and licensed under AGPL-3.0-or-later. Under Section 7 of | ||
# | AGPL-3.0, you are granted additional permissions described in the | ||
# | REMIND License Exception, version 1.0 (see LICENSE file). | ||
# | Contact: remind@pik-potsdam.de | ||
|
||
# if you want to change the reference run for yourrun, you can run: | ||
# Rscript scripts/output/single/fixRefOn.R -i outputdir=yourrun,newreferencerun | ||
|
||
suppressPackageStartupMessages(library(tidyverse)) | ||
|
||
if(! exists("source_include")) { | ||
# Define arguments that can be read from command line | ||
outputdir <- "." | ||
flags <- readArgs("outputdir", .flags = c(i = "--interactive")) | ||
} | ||
|
||
findRefMif <- function(outputdir, envi) { | ||
stopifnot(length(outputdir) == 1) | ||
inputref <- try(envi$cfg$files2export$start[["input_ref.gdx"]], silent = TRUE) | ||
if (inherits(inputref, "try-error") || is.na(inputref) || isTRUE(inputref == "NA") || length(inputref) == 0) { | ||
message("No input_ref.gdx found in config.") | ||
return(NULL) | ||
} | ||
refdir <- dirname(inputref) | ||
if (! file.exists(file.path(refdir, "config.Rdata"))) { | ||
message("Config in reference directory '", refdir, "' not found.") | ||
return(NULL) | ||
} | ||
refscen <- lucode2::getScenNames(refdir) | ||
refmif <- file.path(refdir, paste0("REMIND_generic_", refscen, ".mif")) | ||
if (! file.exists(refmif)) { | ||
message("Reference mif '", refmif, "' not found, run reporting!") | ||
return(NULL) | ||
} | ||
return(refmif) | ||
} | ||
|
||
fixOnMif <- function(outputdir) { | ||
|
||
gdxs <- file.path(outputdir, "fulldata.gdx") | ||
configs <- file.path(outputdir, "config.Rdata") | ||
message("### Checking if mif is correctly fixed on reference run for ", outputdir) | ||
if (! all(file.exists(gdxs, configs))) stop("gdx or config.Rdata not found!") | ||
scens <- lucode2::getScenNames(outputdir) | ||
mifs <- file.path(outputdir, paste0("REMIND_generic_", scens, ".mif")) | ||
if (! all(file.exists(mifs))) stop("mif file not found, run reporting!") | ||
|
||
envi <- new.env() | ||
load(configs[[1]], env = envi) | ||
title <- envi$cfg$title | ||
stopifnot(title == scens[[1]]) | ||
startyear <- envi$cfg$gms$cm_startyear | ||
|
||
if (length(outputdir) == 1) { | ||
refmif <- findRefMif(outputdir, envi) | ||
if (is.null(refmif)) return(NULL) | ||
} else if (length(outputdir) == 2) { | ||
refmif <- mifs[[2]] | ||
} else { | ||
stop("length(outputdir)=", length(outputdir), ", is bigger than 2.") | ||
} | ||
refname <- basename(dirname(refmif)) | ||
d <- quitte::as.quitte(mifs) | ||
dref <- quitte::as.quitte(refmif) | ||
failfile <- file.path(outputdir, "log_fixOnRef.csv") | ||
fixeddata <- piamInterfaces::fixOnRef(d, dref, ret = "fixed", startyear = startyear, failfile = failfile) | ||
|
||
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]]) | ||
tmpfile <- paste0(mifs[[1]], "fixOnMif") | ||
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(NULL) | ||
} | ||
|
||
invisible(fixOnMif(outputdir)) |
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.
yes, let's run it always to besure that everything is working later and for gettign teh info now for the fixing