-
Notifications
You must be signed in to change notification settings - Fork 6
/
cleanUpOutput.R
54 lines (46 loc) · 2.09 KB
/
cleanUpOutput.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
#' @title Clean up output based on geographic trimming
#'
#' @param areas Character. vector of areas (folders). Links will also be cleaned based on getLinks() results
#' @param opts List. simulation options
#'
#' @export
cleanUpOutput <- function(areas = NULL, opts = simOptions()){
if (!is.null(areas) && length(areas) == 1 && areas == "all") areas <- opts$areaList
filteringData <- getGeographicTrimming(areas)
areasFiltering <- rbindlist(filteringData$areas, idcol = T)
linksFiltering <- rbindlist(filteringData$links, idcol = T)
params <- readIni(file.path("settings", "generaldata"))
mc_all <- params$output$synthesis
mc_ind <- params$general$`year-by-year`
if (!mc_all) unlink(file.path(opts$simDataPath, "mc-all"))
if (!mc_ind) unlink(file.path(opts$simDataPath, "mc-ind"))
if (mc_all | mc_ind){
lapply(areasFiltering$.id, .cleanUpOutputSingle, data = areasFiltering,
type = "areas", mc_all = mc_all, mc_ind = mc_ind, opts = opts)
lapply(linksFiltering$.id, .cleanUpOutputSingle, data = linksFiltering,
type = "links", mc_all = mc_all, mc_ind = mc_ind, opts = opts)
}
print("Output clean up success.")
}
.cleanUpOutputSingle <- function(x, data, type, mc_all = T, mc_ind = T, opts){
synthesis <- unlist(strsplit(data[data[[1]] == x]$`filter-synthesis`, ", "))
year_by_year <- unlist(strsplit(data[data[[1]] == x]$`filter-year-by-year`, ", "))
#mc-all
if (mc_all){
curr_path <- file.path(opts$simDataPath, "mc-all", type, x)
filesCurrent <- list.files(curr_path, full.names = T)
filesToKeep <- unlist(lapply(as.list(synthesis), function(y){
filesCurrent[grep(y, gsub(opts$simDataPath, "", filesCurrent))]
}))
unlink(setdiff(filesCurrent, filesToKeep))
}
#mc-ind
if (mc_ind){
curr_path <- file.path(opts$simDataPath, "mc-ind", sprintf("%05.f", opts$mcYears), type, x)
filesCurrent <- list.files(curr_path, full.names = T)
filesToKeep <- unlist(lapply(as.list(year_by_year), function(y){
filesCurrent[grep(y, gsub(opts$simDataPath, "", filesCurrent))]
}))
unlink(setdiff(filesCurrent, filesToKeep))
}
}