tlist_parameters.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
(HTM) git clone git://src.adamsgaard.dk/pism
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tlist_parameters.py (3740B)
---
1 #!/usr/bin/env python3
2
3 import netCDF4
4 import argparse
5
6
7 def is_special(name):
8 "Check if the name is 'special' and should not be included."
9
10 if name == "long_name":
11 return True
12
13 for n in ["_doc", "_units", "_type", "_option", "_choices"]:
14 if name.endswith(n):
15 return True
16
17 return False
18
19
20 def number_to_string(number):
21 "Format a number as a string. Use scientific notation for large and small numbers, remove '.0' from integers."
22 if abs(number) >= 1e7:
23 return '%e' % number # use scientific notation if a number is big
24 elif int(number) == number:
25 return '%d' % int(number) # remove zeros after the decimal point
26 elif abs(number) <= 1e-5:
27 return '%e' % number # use scientific notation if small (and not zero; previous case)
28 else:
29 return '%f' % number
30
31
32 def entry(name, T, value, option, description, choices=None):
33
34 if len(value) == 0:
35 value = "*no default*"
36
37 if value.find(",") != -1:
38 # A comma-separated list
39 value = "``{}``".format(value.replace(",", ", "))
40
41 if choices is not None:
42 # This is always a comma-separated list.
43 choices = "``{}``".format(choices.replace(",", ", "))
44 # value is a keyword, so we should format it as a literal
45 value = "``{}``".format(value)
46
47 template = """
48 #. :config:`{name}` (*{T}*)
49
50 :Value: {value}"""
51
52 if choices is not None:
53 template += """
54 :Choices: {choices}"""
55
56 if option is not None:
57 option = ":opt:`{}`".format(option)
58 template += """
59 :Option: {option}"""
60
61 template += """
62 :Description: {description}"""
63
64 return template.format(name=name,
65 T=T,
66 value=value,
67 choices=choices,
68 option=option,
69 description=description)
70
71
72 def print_string(var, name, T):
73 print(entry(name,
74 T,
75 value(var, name),
76 option(var, name),
77 doc(var, name)))
78
79
80 def print_number(var, name, T):
81 V = "{} ({})".format(number_to_string(value(var, name)), units(var, name))
82 print(entry(name, T, V, option(var, name), doc(var, name)))
83
84
85 def print_integer(var, name, T):
86 print(entry(name,
87 T,
88 str(value(var, name)),
89 option(var, name),
90 doc(var, name)))
91
92
93 def print_keyword(var, name, T):
94 print(entry(name,
95 T,
96 value(var, name),
97 option(var, name),
98 doc(var, name),
99 choices=value(var, name + "_choices")))
100
101
102 def value(var, name):
103 return var.getncattr(name)
104
105
106 def units(var, name):
107 return value(var, name + "_units")
108
109
110 def doc(var, name):
111 return value(var, name + "_doc")
112
113
114 def option(var, name):
115 try:
116 return "-" + value(var, name + "_option")
117 except AttributeError:
118 return None
119
120
121 printers = {"string": print_string,
122 "number": print_number,
123 "integer": print_integer,
124 "flag": print_string,
125 "keyword": print_keyword}
126
127 header = """.. -*- mode: rst -*-
128
129 .. DO NOT EDIT: This file was automatically generated. Edit src/pism_config.cdl instead.
130 """
131
132 if __name__ == "__main__":
133
134 parser = argparse.ArgumentParser()
135 parser.add_argument("FILE", nargs=1)
136 options = parser.parse_args()
137
138 f = netCDF4.Dataset(options.FILE[0], 'r')
139
140 var = f.variables['pism_config']
141
142 print(header)
143
144 for parameter in sorted(var.ncattrs()):
145 if is_special(parameter):
146 continue
147
148 T = var.getncattr(parameter + "_type")
149
150 printers[T](var, parameter, T)