forked from TillF/MMEMO
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_comp_table.R
210 lines (167 loc) · 7.47 KB
/
process_comp_table.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#process the comparison table and determine the respective metrics
base_dir="./runs3/"
configs_dirs=read.table(file=paste0(base_dir,"configuration.txt"), sep="\t", header=FALSE, skip=2, nrows=1) #read configurations directories
configs =read.table(file=paste0(base_dir,"configuration.txt"), sep="\t", header=TRUE, skip=1, stringsAsFactors=FALSE) #read configurations
parameterizations_header = read.table(file=paste0(base_dir,"parameterizations.txt"), sep="\t", header=FALSE, nrows=3) #read parameterizations.txt
parameterizations=read.table(file=paste0(base_dir,"parameterizations.txt"), sep="\t", header=TRUE, skip=3, stringsAsFactors=FALSE) #read parameterizations.txt
assemble_config= function(parameterization_id)
#assemble all necessary files according to info in "configs" into a directory residing in base_dir
{
row_ix =which(parameterizations$parameterization_ID==parameterization_id)
config_id=parameterizations$config_ID[row_ix]
t_res =parameterizations$resolution[row_ix]
config_row = which(configs$configuration.ID == config_id) #corresponding row in "configs"
target_dir=paste0(base_dir,parameterization_id)
target_dir=paste0(target_dir,"/")
if (!dir.create(target_dir)) stop(paste("Couldn't create ",target_dir, sep=""))
#copy common files for calibration (scripts, executable, etc.); default input
src_files=dir(path=paste0(base_dir,"templates/all/all/",""), full.names=TRUE, all.files = TRUE, no.. = TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
jobscr=scan(file=paste0(target_dir,"job_parallel.pbs"), what = character(), sep = "\n")
jobscr=sub(pattern="config_id", repl=paste0(config_id,"_u_",t_res), x=jobscr)
write(x = jobscr, file = paste0(target_dir,"job_parallel.pbs")) #modify jobscript to contain parameterization_id
#target_dir=paste0(target_dir,"/all/",t_res, "/")
#copy time-dependent common files to directory
#if (!dir.create(target_dir)) stop(paste("Couldn't create ",target_dir, sep=""))
#copy common files
#src_files=dir(paste0(base_dir,"templates/all/all/",""), full.names=TRUE)
#file.copy(from=src_files, to=target_dir, recursive=TRUE)
#specific to temporal resolution
src_files=dir(paste0(base_dir,"templates/all/",t_res,""), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
#copy model enhancements (overwrites default files, if activated)
for (i in 3:ncol(configs_dirs))
{
config_dir=as.character(configs_dirs[1,i])
if (is.na(config_dir)) next
if (configs[config_row,i] != 0) #if an enhancement is selected for this configuration, copy respective files
{
#common files
src_files=dir(paste0(base_dir,"templates/",config_dir,"/all/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
#specific to temporal resolution
src_files=dir(paste0(base_dir,"templates/",config_dir,"/",t_res), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
}
}
if(grepl(pattern="^A", x=configs$configuration.ID[config_row])) #for all A-parameterisations, we are done
return()
#special cases that don't fit into the scheme above because of interdependencies of model improvements
#B* (basic files for B with interactions)
if(grepl(pattern="^B", x=configs$configuration.ID[config_row]))
{
src_files=dir(paste0(base_dir,"templates/B/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
}
#B-3
if (configs[config_row,"landuse"] == 0 &&
configs[config_row,"LAI.C.factor"] == 1 &&
configs[config_row,"LAI.C.seasonality"] == 1
)
{
#c-seasons, rainyseason.dat
src_files=dir(paste0(base_dir,"templates/ts_seasons/all/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
#svc.dat
src_files=dir(paste0(base_dir,"templates/B-3/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
#sdr_lu.dat
src_files=dir(paste0(base_dir,"templates/ms_conn_ix/all/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
#terrain.dat
src_files=dir(paste0(base_dir,"templates/ms_flow_conc/all/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
}
#B-4
if (configs[config_row,"LAI.C.factor"] == 0 )
{
#svc.dat, ...
src_files=dir(paste0(base_dir,"templates/B-4/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
}
#B-5
if (configs[config_row,"LAI.C.seasonality"] == 0 )
{
#svc.dat
src_files=dir(paste0(base_dir,"templates/B-5/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
unlink(paste0(target_dir,"init_config/input/isabena_2010-2013/Hillslope/c_seasons.dat"))
unlink(paste0(target_dir,"init_config/input/isabena_2010-2013/Hillslope/rainy_season.dat"))
}
#B-6
if (configs[config_row,"connectivity.index"] == 0 )
#sdr_lu.dat
unlink(paste0(target_dir,"init_config/input/isabena_2010-2013/Hillslope/sdr_lu.dat"))
#B-7
if (configs[config_row,"flow.concentration"] == 0 )
{
#terrain.dat
src_files=dir(paste0(base_dir,"templates/B-7/"), full.names=TRUE)
file.copy(from=src_files, to=target_dir, recursive=TRUE)
}
}
treat_parameterization = function(parameterization_id){
#assemble, run and evaluate a parameterisation
#lookup parameterization table
#assemble/prepare requested configuration
assemble_config(parameterization_id)
#(calibrate or) simply run parameterization
#compute metrics
#store in file
print("Don't forget to convert linebreaks of job_parallel.pbs manually!")
}
#select a single param
# parameterization_id="A_u_24"
parameterization_id="A+1_u_24"
#
parameterization_id="A+2_u_24"
parameterization_id="A+3_u_24"
parameterization_id="A+4_u_24"
parameterization_id="A+5_u_24"
#
parameterization_id="A+6_u_24"
parameterization_id="A+7_u_24"
parameterization_id="B_u_24"
parameterization_id="B-1_u_24"
parameterization_id="B-2_u_24"
parameterization_id="B-3_u_24"
parameterization_id="B-4_u_24"
parameterization_id="B-5_u_24"
parameterization_id="B-7_u_24"
#
#
# parameterization_id="A_u_1"
# parameterization_id="A+1_u_1"
# #
# #
# parameterization_id="A+2_u_1"
# parameterization_id="A+3_u_1"
# parameterization_id="A+4_u_1"
# parameterization_id="A+5_u_1"
# parameterization_id="A+7_u_1"
#
#
# #prepared
#
# # #water
#
# #to be prepared
#
#
# parameterization_id="B_u_1"
# parameterization_id="B-1_u_1"
# parameterization_id="B-2_u_1"
# parameterization_id="B-3_u_1"
# parameterization_id="B-4_u_1"
# parameterization_id="B-5_u_1"
# parameterization_id="B-7_u_1"
#
#treat_parameterization("B-3_u_1")
#treat_parameterization(parameterization_id)
params2gen = parameterizations$parameterization_ID #do all
#params2gen = params2gen[!grepl(pattern = "_c", params2gen)] #only use "_u_"
#params2gen = params2gen[grepl(pattern = "B", params2gen)] #all B
#params2gen = params2gen[grepl(pattern = "_1", params2gen)] # all 1
#params2gen = params2gen[grepl(pattern = "B", params2gen)] #all B
#params2gen = params2gen[!grepl(pattern = "7", params2gen)] #not 7
sapply(X=params2gen, treat_parameterization) #do all runs