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 |
---|
9 | import xml.dom, xml.dom.minidom |
---|
10 | |
---|
11 | class cf(object): |
---|
12 | def __init__(self,fn='cf-standard-name-table_v44.xml', regions='standardized_region_names.xml'): |
---|
13 | self.regionsFile = regions |
---|
14 | self.parseRegions() |
---|
15 | vocabs = xml.dom.minidom.parse( fn ) |
---|
16 | |
---|
17 | el = vocabs.getElementsByTagName( 'entry' ) |
---|
18 | self.names = {} |
---|
19 | self.alias = {} |
---|
20 | |
---|
21 | for e in el: |
---|
22 | sn = e.getAttribute('id') |
---|
23 | unitsE = e.getElementsByTagName( 'canonical_units' ) |
---|
24 | u = '' |
---|
25 | if len(unitsE) > 0 and type( unitsE[0] ) != type(None): |
---|
26 | c1 = unitsE[0].firstChild |
---|
27 | if type( c1 ) != type(None): |
---|
28 | u = c1.data |
---|
29 | descE = e.getElementsByTagName( 'description' ) |
---|
30 | d = '' |
---|
31 | if len(descE) > 0 and type( descE[0] ) != type(None): |
---|
32 | c1 = descE[0].firstChild |
---|
33 | if type( c1 ) != type(None): |
---|
34 | d = c1.data |
---|
35 | |
---|
36 | self.names[sn] = (d,u ) |
---|
37 | el = vocabs.getElementsByTagName( 'alias' ) |
---|
38 | for e in el: |
---|
39 | sn = e.getAttribute('id') |
---|
40 | x = e.getElementsByTagName( 'entry_id' ) |
---|
41 | self.alias[sn] = x[0].firstChild.data |
---|
42 | |
---|
43 | def parseRegions(self): |
---|
44 | s = set() |
---|
45 | vocabs = xml.dom.minidom.parse( self.regionsFile ) |
---|
46 | |
---|
47 | el = vocabs.getElementsByTagName( 'entry' ) |
---|
48 | |
---|
49 | for e in el: |
---|
50 | r = str( e.getAttribute('id') ) |
---|
51 | s.add(r) |
---|
52 | self.regions = sorted( list( s ) ) |
---|
53 | |
---|
54 | if __name__ == "__main__": |
---|
55 | c = cf() |
---|
56 | ls = sorted( c.names.keys() ) |
---|
57 | for s in ls: |
---|
58 | if s.find( '_flux' ) != -1: |
---|
59 | print s |
---|