-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparameters.py
More file actions
309 lines (235 loc) · 10.1 KB
/
parameters.py
File metadata and controls
309 lines (235 loc) · 10.1 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
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
'''
PRMS-Python: Powerful, sane tools for manipulating PRMS input data to create
new scenarios or parameterizations for sensitivity analysis, scenario
modeling, or whatever other uses this might have.
The fundamental process in scenario development is to modify some "base"
starting data to create some "scenario" data. No matter what data we're using,
once it's ready, we run a PRMS "simulation" on that data.
This module presents a Simulation and Scenario class, where each tracks
relevant provenance information and input files to facilitate better
data management techniques to streamline later analyses.
'''
import datetime
import io
import itertools
import numpy as np
from collections import OrderedDict
def modify_params(params_in, params_out, param_mods=None):
'''
Given a parameter file in and a dictionary of param_mods, write modified
parameters to params_out.
Example:
Below we modify the monthly jh_coef by increasing it 10% for every month.
>>> params_in = 'models/lbdc/params'
>>> params_out = 'scenarios/jh_coef_1.1/params'
>>> scale_10pct = lambda x: x * 1.1
>>> modify_params(params_in, params_out, {'jh_coef': scale_10pct})
So param_mods is a dictionary of with keys being parameter names and
values a function that operates on a single value. Currently we only
accept functions that operate on single values without reference to any
other parameters. The function will be applied to every cell, month, or
cascade routing rule for which the parameter is defined.
Arguments:
params_in (str): location on disk of the base parameter file
params_out (str): location on disk where the modified parameters will be written
param_mods (dict): param name-keyed, param modification function-valued
Returns:
None
'''
p_in = Parameters(params_in)
for k in param_mods:
p_in[k] = param_mods[k](p_in[k])
p_in.write(params_out)
class Parameters(object):
'''
Disk-based representation of a PRMS parameter file. For the sake of
memory efficiency, we only load parameters from ``base_file`` that get
modifified through item assignment, for example
>>> p = Parameters('example_params')
>>> p['jh_coef'] = p['jh_coef']*1.1
>>> p.write('example_modified_params')
will read parameter information from the params file to check that
``jh_coef`` is present in the parameter file, read the lines corresponding
to ``jh_coef`` data and assign the new value as requested. Internally,
a reference is kept to only modified parameter data,
so when ``p.write(modified_params_file)`` is called, mostly this will copy
from ``base_file`` to ``modified_params_file``.
'''
def __init__(self, base_file):
self.base_file = base_file
self.base_file_reader = open(base_file)
self.dimensions, self.base_params = self.__read_base(base_file)
self.param_arrays = dict()
def write(self, out_name):
with open(self.base_file, 'r') as base_file:
with open(out_name, 'w') as out_file:
# write metadata
out_file.write('File Auto-generated by PRMS-Python\n')
out_file.write(datetime.datetime.now().isoformat() + '\n')
# # write dimensions
out_file.write('** Dimensions **\n')
# write parameters; pre-sorted by data start line on read
name_is_next = False
params_start = False
write_params_lines = False
for l in base_file:
if not params_start and l.strip() == '** Parameters **':
out_file.write('** Parameters **\n')
params_start = True
elif l.strip() == '####':
name_is_next = True
elif name_is_next:
name = l.strip().split()[0]
if name not in self.param_arrays:
out_file.write('####\n')
out_file.write(name + '\n')
name_is_next = False
write_params_lines = True
else:
write_params_lines = False
name_is_next = False
elif write_params_lines:
out_file.write(l.strip() + '\n')
# write all parameters that had been accessed and/or modified
for param, new_arr in self.param_arrays.items():
out_file.write('####\n')
param_info = [el for el in self.base_params
if el['name'] == param].pop()
out_file.write(str(param_info['name']) + '\n')
out_file.write(str(param_info['ndims']) + '\n')
for dimname in param_info['dimnames']:
out_file.write(dimname + '\n')
out_file.write(str(param_info['length']) + '\n')
out_file.write(str(param_info['vartype']) + '\n')
out_file.writelines([str(a) + '\n'
for a in new_arr.flatten()])
def __read_base(self, base_file):
"Read base file returning 2-tuple of dimension and params dict"
params_startline, dimensions = self.__make_dimensions_dict(base_file)
base_params = self.__make_parameter_dict(base_file, params_startline)
return (dimensions, base_params)
def __make_dimensions_dict(self, base_file):
"""
Extract dimensions and each dimension length. Run before
__make_parameter_dict.
"""
ret = OrderedDict()
dim_name = ''
dim_len = 0
# finished = False
found_dim_start = False
# while not finished:
for idx, l in enumerate(self.base_file_reader):
if l.strip() == '** Dimensions **': # start of dimensions
found_dim_start = True
elif '#' in l: # comments
pass
elif l.strip() == '** Parameters **': # start of parameters
dimlines = idx
# finished = True
break
elif found_dim_start:
if dim_name == '':
dim_name = l.strip()
else:
dim_len = int(l)
ret.update({dim_name: dim_len})
dim_name = ''
return (dimlines, ret)
def __make_parameter_dict(self, base_file, params_startline=0):
ret = []
name = ''
ndims = 0
dimnames = []
length = 0
vartype = ''
dimnames_read = 0
data_startline = 0
for idx, l in enumerate(self.base_file_reader):
if '#' in l:
# we have a comment; the next lines will be new
# parameter metadata. No data for the first time through, so
# we don't want to append an metadata blob with empty values
if name:
ret.append(
dict(
name=name,
ndims=ndims,
dimnames=dimnames,
length=length,
vartype=vartype,
data_startline=data_startline
)
)
name = ''
ndims = 0
dimnames = []
length = 0
vartype = ''
dimnames_read = 0
elif not name:
name = l.strip().split()[0] # in case old format with integer after name
elif not ndims:
ndims = int(l.strip())
elif not (dimnames_read == ndims):
dimnames.append(l.strip())
dimnames_read += 1
elif not length:
length = int(l.strip())
elif not vartype:
vartype = l.strip()
# advance one from current position and account for starting
# to count from zero
data_startline = params_startline + idx + 2
# need to append one more time since iteration will have stopped after
# last line
ret.append(
dict(
name=name,
ndims=ndims,
dimnames=dimnames,
length=length,
vartype=vartype,
data_startline=data_startline
)
)
return ret
def __getitem__(self, key):
"""
Look up a parameter by its name.
Raises:
KeyError if parameter name is not valid
"""
def load_parameter_array(param_metadata):
startline = param_metadata['data_startline']
endline = startline + param_metadata['length'] + 1
param_slice = itertools.islice(
io.open(self.base_file, 'rb'), startline, endline
)
arr = np.genfromtxt(param_slice)
if param_metadata['ndims'] > 1:
dimsizes = [
self.dimensions[d] for d in param_metadata['dimnames']
]
dimsizes.reverse()
arr = arr.reshape(dimsizes)
return arr
if key in self.param_arrays:
return self.param_arrays[key]
else:
try:
param_metadata = [
el for el in self.base_params if el['name'] == key
].pop()
except IndexError:
raise KeyError(key)
arr = load_parameter_array(param_metadata)
# cache the value for future access (but maybe shouldn't?)
self.param_arrays.update({key: arr})
return arr
def __setitem__(self, key, value):
if key in self.param_arrays:
cur_arr = self.param_arrays[key]
if not value.shape == cur_arr.shape:
raise ValueError('New array does not match existing')
self.param_arrays[key] = value