-
Notifications
You must be signed in to change notification settings - Fork 0
/
OldPrimaryExports.py
executable file
·414 lines (347 loc) · 12.2 KB
/
OldPrimaryExports.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
#!/usr/bin/env python3
import shlex
class OldPrimaryExports:
__zero_freq = "PT0S"
@staticmethod
def get_primary(exports,input_rc):
i = 0
found_start = False
found_block = False
for line in input_rc:
if "PrimaryExports%%" in line:
start = i+1
found_start = True
elif found_start and ("%%" in line):
end = i
found_block = True
break
i=i+1
if found_block:
for i in range(start,end):
exports.append(input_rc[i].strip())
exports = OldPrimaryExports.get_primary(exports,input_rc[end+1:])
return(exports)
@staticmethod
def get_derived(derived,input_rc):
i = 0
found_start = False
found_block = False
for line in input_rc:
if "DerivedExports%%" in line:
start = i+1
found_start = True
elif found_start and ("%%" in line):
end = i
found_block = True
break
i=i+1
if found_block:
for i in range(start,end):
derived.append(input_rc[i].strip())
derived = OldPrimaryExports.get_derived(derived,input_rc[end+1:])
return(derived)
@classmethod
def create_primary_entry(cls,entry):
options = shlex.split(entry)
vals = {}
vals.update({"clim":options[2].lower()})
vals.update({"regrid":options[3].lower()})
vals.update({"refresh":options[4].lower()})
vals.update({"offset":options[5].lower()})
vals.update({"scale":options[6].lower()})
vals.update({"var":options[7]})
vals.update({"templ":options[8]})
return {options[0]:vals}
@classmethod
def create_derived_entry(cls,entry):
options = shlex.split(entry)
vals = {}
vals.update({"function":options[1]})
vals.update({"refresh":options[2].lower()})
return {options[0]:vals}
@classmethod
def create_default_entry(cls):
vals = {}
vals.update({"clim":"n"})
vals.update({"regrid":"n"})
vals.update({"refresh":"0"})
vals.update({"scale":"none"})
vals.update({"offset":"none"})
vals.update({"var":"na"})
vals.update({"templ":"na"})
return {"default_export":vals}
@classmethod
def generate_pathkey_name(cls,filepath):
if "/" in filepath:
tstring = filepath.rsplit("/",1)
return(tstring[1])
else:
return filepath
@classmethod
def convert_inttime_to_iso(cls,str_time):
int_time = int(str_time)
hour = int_time//10000
minute = (int_time%10000)//100
second = int_time%100
duration = "PT"
if hour != 0:
duration=duration+str(hour)+"H"
if minute != 0:
duration=duration+str(minute)+"M"
if second != 0:
duration=duration+str(second)+"S"
if duration == "PT":
duration = cls.__zero_freq
return duration
@classmethod
def generate_duration(cls,hour,minute,second):
if "%" in hour:
hour = "0"
if "%" in minute:
minute = "0"
if "%" in second:
second = "0"
duration = "PT"
if hour != "0" and hour != "00":
duration = duration + hour +"H"
if minute != "0" and minute != "00":
duration = duration + minute +"M"
#if second != "0" and second != "00":
#duration = duration + second +"S"
if duration == "PT":
duration = cls.__zero_freq
return duration
@classmethod
def generate_freq_offset_reff(cls,old_sample):
reff = "0"
(before_token,after_token) = old_sample.rsplit("%",1)
token = after_token[0]
if token == "y":
freq = "P1Y"
elif token == "m":
freq = "P1M"
elif token == "d":
freq = "PT24H"
elif token == "h":
freq = "PT1H"
elif token == "n":
freq = "PT1M"
(date,time) = old_sample.split("t")
count_colon = time.count(":")
if count_colon == 1:
(hours,minutes)=time.split(":")
offset = cls.generate_duration(hours,minutes,"0")
elif count_colon ==2:
(hours,minutes,seconds)=time.split(":")
offset = cls.generate_duration(hours,minutes,seconds)
return (freq,offset,reff)
@classmethod
def generate_sampling_map(cls,old_sample, old_clim):
frequency = cls.__zero_freq
offset = cls.__zero_freq
has_offset = False
has_frequency = False
has_ref_time = False
is_fix = False
is_clim = False
has_climyear = False
persist_closest = False
if "f" == old_sample[0]:
is_fix = True
old_sample = old_sample[1:]
if "%" in old_sample:
has_ref_time = True
(frequency,offset,ref_time) = cls.generate_freq_offset_reff(old_sample)
else:
if old_sample == "-":
persist_closest = True
else:
if ";" in old_sample:
templ = old_sample.split(";")
offset = cls.convert_inttime_to_iso(templ[1])
old_sample = templ[0]
if old_sample == "0":
default_freq = True
else:
# To do!
#frequency = cls.convert_inttime_to_iso(old_sample)
default_freq = True
if frequency != cls.__zero_freq:
has_frequency = True
if offset != cls.__zero_freq:
has_offset = True
if old_clim != "n":
is_clim = True
if old_clim != "y":
has_climyear= True
climyear = old_clim
sample_map={}
if persist_closest:
sample_map.update({"extrapolation":"persist_closest"})
if is_clim:
sample_map.update({"extrapolation":"clim"})
if is_fix:
sample_map.update({"time_interpolation":False})
if has_frequency:
sample_map.update({"update_frequency":frequency})
if has_offset:
sample_map.update({"update_offset":offset})
if has_ref_time:
sample_map.update({"update_reference_time":ref_time})
return sample_map
@classmethod
def generate_regrid(cls,regrid_line):
if regrid_line == "n":
return "default"
elif regrid_line == "y":
return "CONSERVE"
elif regrid_line == "v":
return "VOTE"
elif regrid_line[0] == "f":
tstr = regrid_line.split(";")
fraction = "FRACTION;"+tstr[1]
return fraction
@classmethod
def generate_scale(cls,offset,scale):
if scale.strip() == "none":
int_scale = 1.0
else:
int_scale = float(scale)
if offset.strip() == "none":
int_offset = 0.0
else:
int_offset = float(offset)
if int_offset == 0.0 and int_scale == 1.0:
return []
else:
return[int_offset,int_scale]
def __init__(self,input_rc):
primary_exports = []
derived_exports = []
primary_exports = self.get_primary(primary_exports,input_rc)
derived_exports = self.get_derived(derived_exports,input_rc)
self.exports = {}
self.derived = {}
for line in primary_exports:
if ("#" not in line) and (line != ""):
self.exports.update(self.create_primary_entry(line))
for line in derived_exports:
if ("#" not in line) and (line != ""):
self.derived.update(self.create_derived_entry(line))
def get_exports(self):
return self.exports
def generate_new_collections(self,collection_map,unique_paths,gridcomp):
if gridcomp != "":
prefix = gridcomp+"_"
else:
prefix = ""
self.collections = {}
if collection_map:
tmap = collection_map["Collections"]
else:
collection_map = {"Collections":{}}
tmap = {}
for key in self.exports:
export = self.exports[key]
filepath = export["templ"]
if "/dev/null" not in filepath:
if filepath not in unique_paths:
unique_paths.append(filepath)
pathkey = self.generate_pathkey_name(filepath)
pathkey = prefix + pathkey
self.collections.update({key:pathkey})
pathmap = {"template":filepath}
tmap.update({pathkey:pathmap})
collection_map.update({"Collections":tmap})
else:
pathkey = self.generate_pathkey_name(filepath)
pathkey = prefix + pathkey
self.collections.update({key:pathkey})
else:
self.collections.update({key:"/dev/null"})
return collection_map,unique_paths
def generate_new_samplings(self,sampling_map,samplings_list,gridcomp):
if gridcomp != "":
prefix = gridcomp+"_"
else:
prefix = ""
self.samplings = {}
if sampling_map:
tmap = sampling_map["Samplings"]
else:
sampling_map = {"Samplings":{}}
tmap = {}
for key in self.exports:
export = self.exports[key]
refresh = export["refresh"]
clim = export["clim"]
refresh = refresh.strip()
clim = clim.strip()
smap = self.generate_sampling_map(refresh,clim)
if smap:
if smap not in samplings_list:
samplings_list.append(smap)
ith = len(samplings_list)-1
tmap.update({prefix+"sample_"+str(ith):smap})
sampling_map.update({"Samplings":tmap})
else:
for i in range(len(samplings_list)):
if smap == samplings_list[i]:
ith = i
sample_key = prefix+"sample_"+str(ith)
self.samplings.update({key:sample_key})
for key in self.derived:
derived = self.derived[key]
refresh = derived["refresh"]
refresh = refresh.strip()
clim = "n"
smap = self.generate_sampling_map(refresh,clim)
if smap:
if smap not in samplings_list:
samplings_list.append(smap)
ith = len(samplings_list)-1
tmap.update({prefix+"sample_"+str(ith):smap})
sampling_map.update({"Samplings":tmap})
else:
for i in range(len(samplings_list)):
if smap == samplings_list[i]:
ith = i
sample_key = prefix+"sample_"+str(ith)
self.samplings.update({key:sample_key})
return sampling_map,samplings_list
def generate_new_exports(self):
export_map = {"Exports":{}}
emap = {}
for key in self.exports:
export = self.exports[key]
tmap = {}
tmap.update({"collection":self.collections[key]})
if self.collections[key] != "/dev/null":
tmap.update({"variable":export["var"]})
if key in self.samplings:
tmap.update({"sample":self.samplings[key]})
regrid = self.generate_regrid(export["regrid"])
if regrid != "default":
tmap.update({"regrid":regrid})
linear_trans = self.generate_scale(export["offset"],export["scale"])
if linear_trans != []:
tmap.update({"linear_transformation":linear_trans})
emap.update({key:tmap})
export_map.update({"Exports":emap})
return export_map
def generate_new_derived(self):
if self.derived:
export_map = {"Derived":{}}
emap = {}
for key in self.derived:
export = self.derived[key]
tmap = {}
func = export["function"]
tmap.update({"function":func})
if key in self.samplings:
tmap.update({"sample":self.samplings[key]})
emap.update({key:tmap})
export_map.update({"Derived":emap})
else:
export_map = {}
return export_map