-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathReadInput.py
More file actions
257 lines (223 loc) · 9.04 KB
/
ReadInput.py
File metadata and controls
257 lines (223 loc) · 9.04 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
"""
Created on Thurs Dec 2 15:18:25 2021
@author: haranga
Creation of a md table containing the input parameters and their description.
This table is based only on BG_Flood files used to read, allocate and initiate
the code variables.
First, the ReadInput.cu file is used to get the list of available input keys
and code variable associated to the "param" and "forcing" objects. We concidere
that the fist input key correspond to the variable name.
Then, having the list of keys and their associated variable, the "Param.h"/"Forcing.h"
files are used to get the description of the variable, default value, and possibly
examples.
Finally, these keys are listed in tables: one for the parameter, one for the forcings
(and a last one for the non-identified keys).
# Updated to fit in mkdocs setup - Sept 2025
"""
#%% Configuration
import re;
BG_ReadInput="src/ReadInput.cu"
BG_Params_h="src/Param.h"
BG_Forcing_h="src/Forcing.h"
ParamListFile="docs/ParametersList-py.md"
#%% Getting the list of the available entry keys
# Get the parameter keys, as well as the list of variables associated to the
# Param and Forcing objects, and the reference names
keys=[]
ref_name=[]
Params=[]
Forcings=[]
R=open(BG_ReadInput,'r')
lines = R.readlines()
R.close()
for line in lines:
# if not(re.findall('^\s*(//).*',line)):
if (('param.' in line) or ('XParam.' in line)):
Params.append(re.findall(r'(?:param\.|XParam\.)(\w+)', line, re.IGNORECASE)[0])
#Params.append(re.findall("\.*(param.|XParam.)(\w+)\.*", line, re.IGNORECASE)[0][1])
if (('forcing.' in line) or ('XForcing.' in line)):
Forcings.append(re.findall(r'(?:forcing\.|XForcing\.)(\w+)', line, re.IGNORECASE)[0])
#Forcings.append(re.findall("\.*(forcing.|XForcing.)(\w+)\.*", line, re.IGNORECASE)[0][1])
line=re.sub(r"[\t\s]*","",line)
if ('parameterstr=' in line):
key_loc=re.findall(r'parameterstr="([\w_]+)"', line, re.IGNORECASE)[0]
if not (key_loc in keys):
keys.append(key_loc)
ref_name.append(key_loc)
elif ('paramvec=' in line):
key_loc=re.findall(r'.*paramvec={(.*)}.*', line, re.IGNORECASE)[0]
key_loc=re.sub(r"\""," ",key_loc)
if not (key_loc in keys):
keys.append(key_loc)
ref_name.append(re.findall(r'([\w_]+) ,.*',key_loc,re.IGNORECASE)[0])
myParams=list(set(Params))
myForcings=list(set(Forcings))
#%% Getting the information from the others files
#Getting the information from the others files (Param.h / Forcing.h) and link
#keys with comment/Example and Default value
# Get the parameters variables
Default=['']*len(keys)
Example=['']*len(keys)
Comment=['']*len(keys)
Domain=['NN']*len(keys)
P=open(BG_Params_h,'r')
P_lines = P.readlines()
F=open(BG_Forcing_h,'r')
F_lines = F.readlines()
F.close()
P.close()
class InfoTable:
def __init__(self):
self.Reference=[]
self.Keys=[]
self.Default=[]
self.Example=[]
self.Comment=[]
self.Line=[]
ParamTable=InfoTable()
ForcingTable=InfoTable()
NonIdTable=InfoTable()
##Creation of the variables for the tables
for ind in range(len(ref_name)):
refword=ref_name[ind]
key=keys[ind]
#print(refword)
if (refword in myParams):
#Domain='Param'
com=[]
for i in range(len(P_lines)):
line=P_lines[i]
if 'std' in line:
found=re.findall(rf"\s*std.*\s* {re.escape(refword)}\s*=*(.*);\s*(\/\/)*\s*(.*)" , line)
else:
found=re.findall(rf"\s*\w\s* {re.escape(refword)} \s*=*(.*);\s*(\/\/)*\s*(.*)" , line)
#found=re.findall(rf"\.*{re.escape(refword)}\s*=*\s*([^\s]+)*;\s*(//)*\s*(.*)" , line)
if len(found) > 0:
com=found[0]
Line=i
Default=''
Example=''
Comment=''
if (re.search(r'\s*\/\*', P_lines[i+1])):
j=1
while j > 0:
#print(j)
line=P_lines[i+j]
j=j+1
#EXX=re.search(r'\.*Ex:\s*(.*)',line);
DEF=re.search(r'\.*Default:\s*(.*)',line);
ENDCOM=re.search(r'\.*\*\/\s*',line);
line=re.sub(r"[\t\n]*","",line)
line=line.replace("*/","")
line=line.replace("/*","")
#if (EXX):
# Example[ind] = Example[ind] + '<br>' + EXX[1]
if (DEF):
Default=Default + '<br>' + DEF[1]
else:
if (re.search(r'\w',line)):
Comment = Comment + '<br>' + line
if (ENDCOM):
j=-1
if com:
Comment=Comment + '<br>' + com[2]
Default=Default + '<br>' + com[0]
ParamTable.Reference.append(refword)
ParamTable.Keys.append(key)
ParamTable.Default.append(Default)
ParamTable.Comment.append(Comment)
ParamTable.Line.append(Line)
###
if (refword in myForcings):
Domain='Forcing'
com=[]
for i in range(len(F_lines)):
found=re.findall(rf"\.*{re.escape(refword)}\s*;", F_lines[i])
#print(i)
if (len(found) > 0) and (re.search(r'\s*\/\*', F_lines[i+1])):
j=1
Default=''
Example=''
Comment=''
while j > 0:
#print(j)
line=F_lines[i+j]
j=j+1
EXX=re.search(r'\.*Ex:\s*(.*)',line);
DEF=re.search(r'\.*Default:\s*(.*)',line);
ENDCOM=re.search(r'\.*\*\/\s*',line);
line=re.sub(r"[\t\n]*","",line)
line=line.replace("*/","")
line=line.replace("/*","")
if (EXX):
Example = Example + '<br>' + EXX[1]
elif (DEF):
Default=Default + '<br>' + DEF[1]
else:
if (re.search(r'\w',line)):
Comment = Comment + '<br>' + line
if (ENDCOM):
j=-1
ForcingTable.Reference.append(refword)
ForcingTable.Keys.append(key)
ForcingTable.Default.append(Default)
ForcingTable.Example.append(Example)
ForcingTable.Comment.append(Comment)
if not ((refword in myForcings) or (refword in myParams)):
NonIdTable.Reference.append(refword)
NonIdTable.Keys.append(key)
#%% Reading the subgroups in the Param.h file
SubTitle={}
for i in range(len(P_lines)):
line=P_lines[i]
if '//*' in line:
found=re.findall(r"\s*(\/\/\*)(.*)", line, re.IGNORECASE)
SubTitle[i]=found[0][1]
#%% Creation of the output file
#Creating the mark-down file/table for the list of the user input parameters
Out=open(ParamListFile,'w')
Out.write('# Paramter and Forcing list for BG_Flood\n\n')
Out.write('BG_flood user interface consists in a text file (`BG_param.txt` by default), associating key words to user chosen input parameters and forcing information.\n')
#Creation of the Parameter table in MD
#####Paramters
Out.write('## List of the input Parameters\n\n')
#Out.write('|_Reference_|_Keys_|_default_|_Explanation_|\n')
#Out.write('|---|---|---|---|\n')
First=0
for ii in range(len(P_lines)):
if ii in SubTitle:
#if not First == 0:
#Out.write('---\n\n')
#else:
# First=1
Out.write('\n')
Out.write("### " + str(SubTitle[ii]) + "\n")
Out.write('|_Reference_|_Keys_|_default_|_Explanation_|\n')
Out.write('|---|---|---|---|\n')
else:
for ind in range(len(ParamTable.Reference)):
if ParamTable.Line[ind] == ii:
mystr= "|" + str(ParamTable.Reference[ind]) + "|" + str(ParamTable.Keys[ind]) + "|" + str(ParamTable.Default[ind][4:]) + "|" + str(ParamTable.Comment[ind][4:]) + "|\n"
Out.write(mystr)
Out.write('\n\n')
#Creation of the Forcing table in MD
#####Forcings
Out.write('## List of the Forcings\' inputs\n\n')
Out.write('|_Reference_|_Keys_|_default_|_Example_|_Explanation_|\n')
Out.write('|---|---|---|---|---|\n')
for ind in range(len(ForcingTable.Reference)):
mystr= "|" + str(ForcingTable.Reference[ind]) + "|" + str(ForcingTable.Keys[ind]) + "|" + str(ForcingTable.Default[ind][4:]) + "|" + str(ForcingTable.Example[ind][4:]) + "|"+ str(ForcingTable.Comment[ind][4:]) + "|\n"
Out.write(mystr)
Out.write('\n\n')
#Creation of the non-identified entries table in MD
#####Non-identified
Out.write('## List of the non-identified inputs\n\n')
Out.write('|_Reference_|_Keys_|\n')
Out.write('|---|---|\n')
for ind in range(len(NonIdTable.Reference)):
mystr= "|" + str(NonIdTable.Reference[ind]) + "|" + str(NonIdTable.Keys[ind]) + "|\n"
Out.write(mystr)
Out.write('\n\n')
Out.write('!!! note \n The keys are not case sensitive.\n')
Out.close()