-
Notifications
You must be signed in to change notification settings - Fork 4
/
mergeplot-pythia-outputs.R
172 lines (141 loc) · 6.04 KB
/
mergeplot-pythia-outputs.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#library(here)
library(argparser)
library(stringr)
library(png)
library(grid)
library(ggplot2)
library(gridExtra)
setwd(".")
defaultPlotOrders <- c("production", "monthly_production", "average_yield", "crop_per_drop", "crop_per_person", "crop_failure_area", "total_nitrogen_applied")
# defaultPNGLayout <- rbind(c(1,2,2),c(3,4,5),c(6,7,8))
defaultPNGLayout <- rbind(c(2,2,2),c(1,3,4),c(5,6,7))
supportFileTypes <- c("png", "pdf")
p <- argparser::arg_parser("Generate single file with merged plot graphs for World Modelers(fixed)")
p <- argparser::add_argument(p, "input", "the path to the directory with plot graph files")
p <- argparser::add_argument(p, "output", "Output directory or file for PDF file")
p <- argparser::add_argument(p, "--output_type", short = "-t", nargs = 1, default = "png", help = "The type of merged file, now support png and pdf")
p <- argparser::add_argument(p, "--file_filter", short = "-n", nargs = 1, default = "*.png", help = "The naming pattern of plot files which want to merged")
p <- argparser::add_argument(p, "--is_DSSAT_naming_rule", short="-d", flag = TRUE, help=paste0("Flag for if the file names of plots follow the current DSSAT Pythia naming rule <variable>-<admin level name>.png"))
p <- argparser::add_argument(p, "--output_prefix", short = "-p", nargs = 1, default = "", help = "The prefix text used for generating file name")
argv <- argparser::parse_args(p)
# for test only
# argv <- argparser::parse_args(p, c("test\\data\\case21\\analysis_out\\ETH_MZ_2022_N\\images", "test\\data\\case21\\analysis_out\\ETH_MZ_2022_N\\images2", "-d"))
# argv <- argparser::parse_args(p, c("test\\data\\case22\\analysis_out\\ETH_MZ_Mar22_Forecast_Ar\\images", "test\\data\\case22\\analysis_out\\ETH_MZ_Mar22_Forecast_Ar\\images2"))
suppressWarnings(in_dir <- normalizePath(argv$input))
suppressWarnings(out_file <- normalizePath(argv$output))
fileFilter <- argv$file_filter
isDSSATNamingRule <- argv$is_DSSAT_naming_rule
extension <- paste0(".", argv$output_type)
prefix <- argv$output_prefix
if (!dir.exists(in_dir) && !file.exists(in_dir)) {
stop(sprintf("%s does not exist.", in_dir))
}
if (!argv$output_type %in% supportFileTypes) {
stop(sprintf("Output %s is not supported.", argv$output_type))
}
if (file_test("-f", out_file) && !endsWith(tolower(out_file), extension)) {
stop(sprintf("Output file name %s does not match with the file type you requested", basename(out_file)))
}
if (endsWith(tolower(out_file), extension)) {
out_dir <- dirname(out_file)
file_name <- basename(out_file)
base_file_name <- tools::file_path_sans_ext(file_name)
} else {
out_dir <- out_file
file_name <- paste0(prefix, basename(out_file), extension)
base_file_name <- ""
}
if (!dir.exists(out_dir)) {
dir.create(out_dir, recursive = TRUE)
}
flist <- list()
print("Loading plot files for merging.")
if (!dir.exists(in_dir)) {
flist <- in_dir
} else {
flist <- list.files(path = in_dir, pattern = fileFilter, recursive = FALSE, full.names = TRUE)
}
if (isDSSATNamingRule) {
locations <- str_locate(tools::file_path_sans_ext(basename(flist)), "-")
starts <- locations[seq(1, length(locations)/2)]
# plotVars <- str_sub(tools::file_path_sans_ext(basename(flist)), 1, starts - 1)
factors <- unique(str_sub(tools::file_path_sans_ext(basename(flist)), starts + 1))
lay <- defaultPNGLayout
for (factor in factors) {
cat(paste0("Merging plots for ", factor, "..."))
flistFB <- list.files(path = in_dir, pattern = paste0("*-", factor, ".png"), recursive = FALSE, full.names = TRUE)
flistFBOrdered <- sort(factor(flistFB, levels = file.path(in_dir, paste0(defaultPlotOrders, "-", factor, ".png"))))
flistFBOrdered <- as.character(flistFBOrdered)
if (extension == ".png") {
plots <- lapply(flistFBOrdered, function(x){
img <- as.raster(readPNG(x))
rasterGrob(img)
})
ggsave(file.path(out_dir, paste0(prefix, factor, ".png")),width=20.4, height=26.4,
arrangeGrob(grobs = plots, layout_matrix = lay))
cat("done\r\n")
} else if (extension == ".pdf") {
pdf(file.path(out_dir, paste0(prefix, factor, extension)))
for (i in 1:length(flistFBOrdered)) {
f <- flistFBOrdered[i]
if (startsWith(basename(f), "monthly_")) {
grid.raster(readPNG(f), width=unit(0.9, "npc"), height= unit(0.45, "npc"))
} else {
grid.raster(readPNG(f), width=unit(0.7, "npc"), height= unit(0.7, "npc"))
}
if (i < length(flistFB)) {
plot.new()
}
}
dev.off()
cat("done\r\n")
} else {
cat("skipped\r\n")
}
} # end of factor loop
} else {
cat(paste0("Merging plots ..."))
if (extension == ".png") {
plots <- lapply(flist, function(x){
img <- as.raster(readPNG(x))
rasterGrob(img)
})
if (!F %in% startsWith(tools::file_path_sans_ext(basename(flist)), "forecast_") ) {
# merging forecast pngs
rowNum <- 3
lay <- rep(1, rowNum)
for (i in 2 : rowNum) {
lay <- rbind(lay, rep(1, rowNum))
}
for (i in 1:ceiling((length(flist) - 1) / rowNum)) {
lay <- rbind(lay, ((i-1)*rowNum+2):(i*rowNum+1))
}
file_name <- paste0(prefix, tools::file_path_sans_ext(basename(flist))[1], ".png")
ggsave(file.path(out_dir, file_name),width=20.4, height=26.4,
arrangeGrob(grobs = plots, layout_matrix = lay))
} else {
ggsave(file.path(out_dir, file_name),width=20.4, height=26.4,
arrangeGrob(grobs = plots))
}
cat("done\r\n")
} else if (extension == ".pdf") {
pdf(file.path(out_dir, file_name))
for (i in 1:length(flist)) {
f <- flist[i]
if (startsWith(basename(f), "monthly_")) {
grid.raster(readPNG(f), width=unit(0.9, "npc"), height= unit(0.45, "npc"))
} else {
grid.raster(readPNG(f), width=unit(0.7, "npc"), height= unit(0.7, "npc"))
}
f<-flist[72]
if (i < length(flist)) {
plot.new()
}
}
dev.off()
cat("done\r\n")
} else {
cat("skipped\r\n")
}
}
print("Complete.")