forked from leifdenby/namelist_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamelist.py
459 lines (382 loc) · 14.2 KB
/
namelist.py
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import unittest
try:
from collections import OrderedDict
except ImportError:
from utils import OrderedDict
import re
class NoSingleValueFoundException(Exception):
pass
def read_namelist_file(filename):
return Namelist(open(filename, 'r').read())
class AttributeMapper():
"""
Simple mapper to access dictionary items as attributes
"""
def __init__(self, obj):
self.__dict__['data'] = obj
def __getattr__(self, attr):
if attr in self.data:
found_attr = self.data[attr]
if isinstance(found_attr, dict):
return AttributeMapper(found_attr)
else:
return found_attr
else:
raise AttributeError
def __setattr__(self, attr, value):
if attr in self.data:
self.data[attr] = value
else:
raise NotImplementedError
def __dir__(self):
return self.data.keys()
class Namelist():
"""
Parses namelist files in Fortran 90 format, recognised groups are
available through 'groups' attribute.
"""
def __init__(self, input_str):
self.groups = OrderedDict()
group_re = re.compile(r'&([^&/]+)/', re.DOTALL) # allow blocks to span multiple lines
array_re = re.compile(r'(\w+)\((\d+)\)')
string_re = re.compile(r"\'\s*\w[^']*\'")
self._complex_re = re.compile(r'^\((\d+.?\d*),(\d+.?\d*)\)$')
# remove all comments, since they may have forward-slashes
# TODO: store position of comments so that they can be re-inserted when
# we eventually save
filtered_lines = []
for line in input_str.split('\n'):
if line.strip().startswith('!'):
continue
else:
filtered_lines.append(line)
group_blocks = re.findall(group_re, "\n".join(filtered_lines))
for group_block in group_blocks:
block_lines = group_block.split('\n')
group_name = block_lines.pop(0).strip()
group = {}
for line in block_lines:
line = line.strip()
if line == "":
continue
if line.startswith('!'):
continue
# commas at the end of lines seem to be optional
if line.endswith(','):
line = line[:-1]
k, v = line.split('=')
variable_name = k.strip()
variable_value = v.strip()
variable_name_groups = re.findall(array_re, k)
variable_index = None
if len(variable_name_groups) == 1:
variable_name, variable_index = variable_name_groups[0]
variable_index = int(variable_index)-1 # python indexing starts at 0
try:
parsed_value = self._parse_value(variable_value)
if variable_index is None:
group[variable_name] = parsed_value
else:
if not variable_name in group:
group[variable_name] = {'_is_list': True}
group[variable_name][variable_index] = parsed_value
except NoSingleValueFoundException as e:
# see we have several values inlined
if variable_value.count("'") in [0, 2]:
variable_arr_entries = variable_value.split()
else:
# we need to be more careful with lines with escaped
# strings, since they might contained spaces
matches = re.findall(string_re, variable_value)
variable_arr_entries = [s.strip() for s in matches]
for variable_index, inline_value in enumerate(variable_arr_entries):
parsed_value = self._parse_value(inline_value)
if variable_index is None:
group[variable_name] = parsed_value
else:
if not variable_name in group:
group[variable_name] = {'_is_list': True}
group[variable_name][variable_index] = parsed_value
self.groups[group_name] = group
self._check_lists()
def _parse_value(self, variable_value):
"""
Tries to parse a single value, raises an exception if no single value is matched
"""
try:
parsed_value = int(variable_value)
except ValueError:
try:
parsed_value = float(variable_value)
except ValueError:
# check for complex number
complex_values = re.findall(self._complex_re, variable_value)
if len(complex_values) == 1:
a, b = complex_values[0]
parsed_value = complex(float(a),float(b))
elif variable_value in ['.true.', 'T']:
# check for a boolean
parsed_value = True
elif variable_value in ['.false.', 'F']:
parsed_value = False
else:
# see if we have an escaped string
if variable_value.startswith("'") and variable_value.endswith("'") and variable_value.count("'") == 2:
parsed_value = variable_value[1:-1]
else:
raise NoSingleValueFoundException(variable_value)
return parsed_value
def _check_lists(self):
for group in self.groups.values():
for variable_name, variable_values in group.items():
if isinstance(variable_values, dict):
if '_is_list' in variable_values and variable_values['_is_list']:
variable_data = variable_values
del(variable_data['_is_list'])
num_entries = len(variable_data.keys())
variable_list = [None]*num_entries
for i, value in variable_data.items():
if i >= num_entries:
raise Exception("The variable '%s' has an array index assignment that is inconsistent with the number of list values" % variable)
else:
variable_list[i] = value
group[variable_name] = variable_list
def dump(self, array_inline=True):
lines = []
for group_name, group_variables in self.groups.items():
lines.append("&%s" % group_name)
for variable_name, variable_value in group_variables.items():
if isinstance(variable_value, list):
if array_inline:
lines.append("%s= %s" % (variable_name, " ".join([self._format_value(v) for v in variable_value])))
else:
for n, v in enumerate(variable_value):
lines.append("%s(%d)=%s" % (variable_name, n+1, self._format_value(v)))
else:
lines.append("%s=%s" % (variable_name, self._format_value(variable_value)))
lines.append("/")
return "\n".join(lines)
def _format_value(self, value):
if isinstance(value, bool):
return value and '.true.' or '.false.'
elif isinstance(value, int):
return "%d" % value
elif isinstance(value, float):
return "%f" % value
elif isinstance(value, str):
return "'%s'" % value
elif isinstance(value, complex):
return "(%s,%s)" % (self._format_value(value.real), self._format_value(value.imag))
else:
raise Exception("Variable type not understood: %s" % type(value))
@property
def data(self):
return AttributeMapper(self.groups)
class ParsingTests(unittest.TestCase):
def test_single_value(self):
input_str = """
&CCFMSIM_SETUP
CCFMrad=800.0
/
"""
namelist = Namelist(input_str)
expected_output = {'CCFMSIM_SETUP': { 'CCFMrad': 800. }}
self.assertEqual(namelist.groups, expected_output)
def test_multigroup(self):
input_str = """
&CCFMSIM_SETUP
CCFMrad=800.0
/
&GROUP2
R=500.0
/
"""
namelist = Namelist(input_str)
expected_output = {'CCFMSIM_SETUP': { 'CCFMrad': 800. },
'GROUP2': { 'R': 500. }}
self.assertEqual(namelist.groups, expected_output)
def test_comment(self):
input_str = """
! Interesting comment at the start
&CCFMSIM_SETUP
CCFMrad=800.0
! And a comment some where in the middle
/
&GROUP2
R=500.0
/
"""
namelist = Namelist(input_str)
expected_output = {'CCFMSIM_SETUP': { 'CCFMrad': 800. },
'GROUP2': { 'R': 500. }}
self.assertEqual(namelist.groups, expected_output)
def test_array(self):
input_str = """
&CCFMSIM_SETUP
ntrac_picture=4
var_trac_picture(1)='watcnew'
des_trac_picture(1)='cloud_water'
var_trac_picture(2)='watpnew'
des_trac_picture(2)='rain'
var_trac_picture(3)='icecnew'
des_trac_picture(3)='cloud_ice'
var_trac_picture(4)='granew'
des_trac_picture(4)='graupel'
/
"""
namelist = Namelist(input_str)
expected_output = {
'CCFMSIM_SETUP': {
'ntrac_picture': 4,
'var_trac_picture': [
'watcnew',
'watpnew',
'icecnew',
'granew',
],
'des_trac_picture': [
'cloud_water',
'rain',
'cloud_ice',
'graupel',
],
},
}
self.assertEqual(dict(namelist.groups), expected_output)
def test_boolean_sciformat(self):
input_str = """
&ATHAM_SETUP
nz =300
zstart =0.
ztotal =15000.
dzzoom =50.
kcenter =20
nztrans =0
nztrans_boundary =6
cpumax =9.e6
no_uwind=.false.
no_vwind=.true.
/
"""
namelist = Namelist(input_str)
expected_output = {
'ATHAM_SETUP': {
'nz': 300,
'zstart': 0.,
'ztotal': 15000.,
'dzzoom': 50.,
'kcenter': 20,
'nztrans': 0,
'nztrans_boundary': 6,
'cpumax': 9.e6,
'no_uwind': False,
'no_vwind': True,
}
}
self.assertEqual(dict(namelist.groups), expected_output)
def test_comment_with_forwardslash(self):
input_str = """
! Interesting comment at the start
&CCFMSIM_SETUP
CCFMrad=800.0
! And a comment some where in the middle/halfway !
var2=40
/
&GROUP2
R=500.0
/
"""
namelist = Namelist(input_str)
expected_output = {'CCFMSIM_SETUP': { 'CCFMrad': 800., 'var2': 40 },
'GROUP2': { 'R': 500. }}
self.assertEqual(namelist.groups, expected_output)
def test_inline_array(self):
input_str = """
! can have blank lines and comments in the namelist input file
! place these comments between NAMELISTs
!
! not every compiler supports comments within the namelist
! in particular vastf90/g77 does not
!
! some will skip NAMELISTs not directly referenced in read
!&BOGUS rko=1 /
!
&TTDATA
TTREAL = 1.,
TTINTEGER = 2,
TTCOMPLEX = (3.,4.),
TTCHAR = 'namelist',
TTBOOL = T/
&AADATA
AAREAL = 1. 1. 2. 3.,
AAINTEGER = 2 2 3 4,
AACOMPLEX = (3.,4.) (3.,4.) (5.,6.) (7.,7.),
AACHAR = 'namelist' 'namelist' 'array' ' the lot',
AABOOL = T T F F/
&XXDATA
XXREAL = 1.,
XXINTEGER = 2,
XXCOMPLEX = (3.,4.)/! can have blank lines and comments in the namelist input file
"""
expected_output = {
'TTDATA': {
'TTREAL': 1.,
'TTINTEGER': 2,
'TTCOMPLEX': 3. + 4.j,
'TTCHAR': 'namelist',
'TTBOOL': True,
},
'AADATA': {
'AAREAL': [1., 1., 2., 3.,],
'AAINTEGER': [2, 2, 3, 4],
'AACOMPLEX': [3.+4.j, 3.+4.j, 5.+6.j, 7.+7.j],
'AACHAR': ['namelist', 'namelist', 'array', ' the lot'],
'AABOOL': [True, True, False, False],
},
'XXDATA': {
'XXREAL': 1.,
'XXINTEGER': 2.,
'XXCOMPLEX': 3.+4.j,
},
}
namelist = Namelist(input_str)
self.assertEqual(dict(namelist.groups), expected_output)
class ParsingTests(unittest.TestCase):
def test_single_value(self):
input_str = """&CCFMSIM_SETUP
CCFMrad=800.
/"""
namelist = Namelist(input_str)
self.assertEqual(namelist.dump(), input_str)
def test_multigroup(self):
input_str = """&CCFMSIM_SETUP
CCFMrad=800.
/
&GROUP2
R=500.
/"""
namelist = Namelist(input_str)
self.assertEqual(namelist.dump(), input_str)
def test_array(self):
input_str = """&CCFMSIM_SETUP
var_trac_picture(1)='watcnew'
var_trac_picture(2)='watpnew'
var_trac_picture(3)='icecnew'
var_trac_picture(4)='granew'
des_trac_picture(1)='cloud_water'
des_trac_picture(2)='rain'
des_trac_picture(3)='cloud_ice'
des_trac_picture(4)='graupel'
/"""
namelist = Namelist(input_str)
self.assertEqual(namelist.dump(array_inline=False), input_str)
def test_inline_array(self):
input_str = """&AADATA
AACOMPLEX= (3.,4.) (3.,4.) (5.,6.) (7.,7.)
/"""
namelist = Namelist(input_str)
print input_str
print namelist.dump()
self.assertEqual(namelist.dump(), input_str)
if __name__=='__main__':
unittest.main()