1 | """loadcf |
---|
2 | ------ |
---|
3 | The loadcf module reads the cf standard name table into two dictionaries: |
---|
4 | names[<standard_name>] = (description, canonical units) |
---|
5 | alias[<alias>] = standard_name |
---|
6 | """ |
---|
7 | |
---|
8 | import xml, shelve |
---|
9 | import xml.dom, xml.dom.minidom |
---|
10 | import utils_wb |
---|
11 | |
---|
12 | class cf(object): |
---|
13 | def __init__(self): |
---|
14 | vocabs = xml.dom.minidom.parse( 'inputs/cf-standard-name-table_v43.xml' ) |
---|
15 | |
---|
16 | el = vocabs.getElementsByTagName( 'entry' ) |
---|
17 | self.names = {} |
---|
18 | self.alias = {} |
---|
19 | |
---|
20 | for e in el: |
---|
21 | sn = e.getAttribute('id') |
---|
22 | unitsE = e.getElementsByTagName( 'canonical_units' ) |
---|
23 | u = '' |
---|
24 | if len(unitsE) > 0 and type( unitsE[0] ) != type(None): |
---|
25 | c1 = unitsE[0].firstChild |
---|
26 | if type( c1 ) != type(None): |
---|
27 | u = c1.data |
---|
28 | descE = e.getElementsByTagName( 'description' ) |
---|
29 | d = '' |
---|
30 | if len(descE) > 0 and type( descE[0] ) != type(None): |
---|
31 | c1 = descE[0].firstChild |
---|
32 | if type( c1 ) != type(None): |
---|
33 | d = c1.data |
---|
34 | |
---|
35 | |
---|
36 | uid = str(sn) |
---|
37 | if uid.find( ' ' ) != -1: |
---|
38 | print 'SEVERE.sn.00001: BAD SN: %s' % uid |
---|
39 | uid = uid.replace( ' ', '' ) |
---|
40 | title = uid.replace( '_', ' ' ).title() |
---|
41 | label = title.replace( ' ', '' ) |
---|
42 | for w in ['Of','In','On','By','The','Due','To','From']: |
---|
43 | ww = ' %s ' % w |
---|
44 | title = title.replace( ww, ww.lower() ) |
---|
45 | self.names[uid] = (label,title, utils_wb.uniCleanFunc( d ),u ) |
---|
46 | |
---|
47 | el = vocabs.getElementsByTagName( 'alias' ) |
---|
48 | for e in el: |
---|
49 | sn = str( e.getAttribute('id') ) |
---|
50 | if sn.find( ' ' ) != -1: |
---|
51 | print 'SEVERE.sn.00001: BAD SN ALIAS: %s' % sn |
---|
52 | else: |
---|
53 | x = e.getElementsByTagName( 'entry_id' ) |
---|
54 | self.alias[sn] = str( x[0].firstChild.data ) |
---|
55 | |
---|
56 | def write(self,ofile='exports/l0sh/standardname'): |
---|
57 | sh = shelve.open( ofile, 'n' ) |
---|
58 | sh['__info__'] = {'label':'standardname', 'title':'Data Request Section, CF standard name list'} |
---|
59 | sh['__cols__'] = ['label','title','description','units','uid'] |
---|
60 | for k in sorted( self.names.keys() ): |
---|
61 | sh[k] = list( self.names[k] ) + [k,] |
---|
62 | for k in sorted( self.alias.keys() ): |
---|
63 | k1 = self.alias[k] |
---|
64 | lab,title,d,u = self.names[k1] |
---|
65 | title += ' [alias]' |
---|
66 | d = 'alias::%s' % k |
---|
67 | sh[k] = (lab,title,d,u,k) |
---|
68 | print 'INFO.standardnames.00001: names written to shelve: %s [%s]' % (len(self.names.keys()),len(self.alias.keys())) |
---|
69 | sh.close() |
---|
70 | |
---|
71 | |
---|
72 | if __name__ == '__main__': |
---|
73 | c = cf() |
---|
74 | c.write() |
---|