1 | from utils_wb import workbook |
---|
2 | import collections, uuid |
---|
3 | import xlrd |
---|
4 | |
---|
5 | nt__varGroup = collections.namedtuple( 'varGroup', ['uid','label','title','mip','ref','refNote'] ) |
---|
6 | |
---|
7 | def cellMethodCheck(cm): |
---|
8 | rv = 0 |
---|
9 | if cm.find( ' ' ) != -1: |
---|
10 | cm = cm.replace( ' ', ' ' ) |
---|
11 | rv = 1 |
---|
12 | |
---|
13 | if cm.find( "area: where" ) != -1: |
---|
14 | cm = cm.replace( "area: where", "area: mean where" ) |
---|
15 | rv = 1 |
---|
16 | |
---|
17 | if cm.find( "time:mean" ) != -1: |
---|
18 | cm = cm.replace( "time:mean", "time: mean" ) |
---|
19 | rv = 1 |
---|
20 | elif cm.find( "weighted b " ) != -1: |
---|
21 | return ( cm.replace( "weighted b ", "weighted by " ), 1) |
---|
22 | |
---|
23 | if cm.find( "xxxxxtime: mean" ) == 0 and len(cm) > 10: |
---|
24 | if cm[10:].strip()[0] == '(': |
---|
25 | ix = cm.index(')') + 1 |
---|
26 | else: |
---|
27 | ix = 10 |
---|
28 | cm0 = cm |
---|
29 | cm = cm0[ix:].strip( ) + ' ' + cm0[:ix] |
---|
30 | rv = 1 |
---|
31 | |
---|
32 | if cm.find( "time: point" ) == 0 and len(cm) > 11: |
---|
33 | if cm[11:].strip()[0] == '(': |
---|
34 | ix = cm.index(')') + 1 |
---|
35 | else: |
---|
36 | ix = 11 |
---|
37 | cm0 = cm |
---|
38 | cm = cm0[ix:].strip( ) + ' ' + cm0[:ix] |
---|
39 | rv = 1 |
---|
40 | return (cm,rv) |
---|
41 | |
---|
42 | class loadCellm(object): |
---|
43 | def __init__(self): |
---|
44 | """Information on cell methods. cmmap [dict] defines how to map old cell methods string to new. |
---|
45 | cminfo .. provides a tuple (title,label,uid) for each cell methods string""" |
---|
46 | |
---|
47 | wb = xlrd.open_workbook( 'inputs/cm.xls' ) |
---|
48 | |
---|
49 | sht = wb.sheet_by_name( 'Sheet1' ) |
---|
50 | ll = [] |
---|
51 | for i in range( sht.nrows ): |
---|
52 | ll.append( [x.value for x in sht.row(i) ] ) |
---|
53 | |
---|
54 | self.cmmap = {} |
---|
55 | self.cminfo = {} |
---|
56 | ee = {} |
---|
57 | kk = 0 |
---|
58 | for r in ll: |
---|
59 | v0 = r[0].strip() |
---|
60 | if len(v0) != 0 and v0[0] != '#': |
---|
61 | v1 = r[3].strip() |
---|
62 | vv,rv = cellMethodCheck(v0) |
---|
63 | if r[1] in ['redirect','change']: |
---|
64 | self.cmmap[v0] = v1 |
---|
65 | if r[1] == 'redirect' and vv != v0: |
---|
66 | self.cmmap[vv] = v1 |
---|
67 | v0 = v1 |
---|
68 | vv,rv = cellMethodCheck(v0) |
---|
69 | if r[1] != 'redirect': |
---|
70 | if len(r) >= 6: |
---|
71 | desc = r[6] |
---|
72 | else: |
---|
73 | desc = None |
---|
74 | t = r[5].strip() |
---|
75 | l = r[4].strip() |
---|
76 | if vv != v0: |
---|
77 | self.cminfo[vv] = (t,l,'CellMethods::%s' % l, desc) |
---|
78 | self.cmmap[v0] = vv |
---|
79 | else: |
---|
80 | self.cminfo[v0] = (t,l,'CellMethods::%s' % l, desc) |
---|
81 | |
---|
82 | if r[1] in ['ok','ok?']: |
---|
83 | v = r[0].strip() |
---|
84 | elif r[1] != 'redirect': |
---|
85 | v = r[3].strip() |
---|
86 | else: |
---|
87 | v = r[0].strip() |
---|
88 | assert v not in ee, 'duplicate: %s: %s' % (v,str(r)) |
---|
89 | ee[v] = kk |
---|
90 | kk += 1 |
---|
91 | |
---|
92 | class loadCmorDims(object): |
---|
93 | def __init__(self): |
---|
94 | wb = workbook( 'inputs/CMORdims.xls' ) |
---|
95 | s1 = wb.book.sheet_by_name(u'Sheet1') |
---|
96 | self.ss = {} |
---|
97 | for i in range(3,s1.nrows): |
---|
98 | rr = [ str(x.value).strip() for x in s1.row(i)] |
---|
99 | if rr[1] != '': |
---|
100 | assert rr[1] not in self.ss, 'Duplicate dimension: %s' % rr[1] |
---|
101 | self.ss[ rr[1] ] = rr |
---|
102 | |
---|
103 | class loadSpatial(object): |
---|
104 | def __init__(self): |
---|
105 | wb = workbook( 'inputs/spatialShape_ref.xls' ) |
---|
106 | s1 = wb.book.sheet_by_name(u'ss_ref') |
---|
107 | self.ss = {} |
---|
108 | self.ssu = {} |
---|
109 | self.labByUid = {} |
---|
110 | self.uidByLab = {} |
---|
111 | uu = set() |
---|
112 | for i in range(s1.nrows): |
---|
113 | rr = [x.value for x in s1.row(i)] |
---|
114 | if rr[0] != 'label' and rr[0][0] != '#': |
---|
115 | assert rr[2] not in self.ss, 'Duplicate dimension set in spatial shape sheet: %s' % rr[2] |
---|
116 | lab,ttl,dims,lf = tuple( [x.strip() for x in rr[:4] ] ) |
---|
117 | li = str( int( rr[4] ) ) |
---|
118 | u = str( rr[6] ) |
---|
119 | assert u not in uu, 'Duplicate uid in spatialShape_ref.xls: %s' % u |
---|
120 | self.labByUid[u] = rr[0] |
---|
121 | self.uidByLab[rr[0]] = u |
---|
122 | self.ss[ dims ] = (lab,ttl,lf,li,u) |
---|
123 | self.ssu[ u ] = (lab,ttl,dims,lf,li,u) |
---|
124 | |
---|
125 | def addTime(self): |
---|
126 | tshp = { 'time':['time-mean','Temporal mean'], 'time1':['time-point','Instantaneous value (i.e. synoptic or time-step value)'], \ |
---|
127 | 'time2':['climatology','Climatological mean'], \ |
---|
128 | 'time3':['diurnal-cycle','Mean Diurnal Cycle'], \ |
---|
129 | '':['None','No temporal dimensions ... fixed field'] } |
---|
130 | tuid = ['7a96eb30-8042-11e6-97ee-ac72891c3257', '7a972f78-8042-11e6-97ee-ac72891c3257', '7a976ce0-8042-11e6-97ee-ac72891c3257', '7a97ae58-8042-11e6-97ee-ac72891c3257','cf34c974-80be-11e6-97ee-ac72891c3257'] |
---|
131 | self.etd = {} |
---|
132 | self.tsu = {} |
---|
133 | self.tByLab = {} |
---|
134 | |
---|
135 | k = 0 |
---|
136 | for s in tshp: |
---|
137 | label, description = tshp[s] |
---|
138 | u = tuid[k] |
---|
139 | self.etd[s] = (u,label,description,s) |
---|
140 | self.tsu[u] = (label,description,s) |
---|
141 | self.tByLab[label] = (u,description,s) |
---|
142 | k+=1 |
---|
143 | |
---|
144 | class run(object): |
---|
145 | def __init__(self): |
---|
146 | self.ls = loadSpatial() |
---|
147 | self.cm = loadCmorDims() |
---|
148 | self.cellm = loadCellm() |
---|
149 | |
---|
150 | if __name__ == '__main__': |
---|
151 | r = run() |
---|