source: CMIP6dreqbuild/trunk/src/framework/docChecks.py @ 1255

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/CMIP6dreqbuild/trunk/src/framework/docChecks.py@1291
Revision 1255, 2.5 KB checked in by mjuckes, 16 months ago (diff)

add pressure level plots

Line 
1import lxml, string,  re, sys
2import lxml.etree as et
3from hashlib import md5
4
5python2 = True
6if sys.version_info[0] == 3:
7  assert False, 'HAVE NOT YET MADE THE CANONICAL WRITE SECTION BELOW WORK WITH PYTHON 3 ... ISSUE WITH STRINGIO API'
8  python2 = False
9  import dreqPy
10  import dreqPy.utilP3
11  ##from dreqPy.utilP3 import mlog3
12  from io import StringIO
13  prnt = dreqPy.utilP3.mlog3().prnt
14else:
15  from dreqPy.utilP2 import util
16  from StringIO import StringIO
17  prnt = util.mlog().prnt
18
19if len(sys.argv) > 1:
20  ifile = sys.argv[1]
21  ofile = 'dreq.xml'
22else:
23  ##ifile = 'out/annotated_20150731_mt.xml'
24  ifile = 'ing02/test.xml'
25  ofile = 'out/annotated_20150731_chk.xml'
26
27if len(sys.argv) > 2:
28  ofile = sys.argv[2]
29
30##parser = et.XMLParser( remove_blank_text=True )
31##doc = et.parse( ifile, parser )
32doc = et.parse( ifile )
33root = doc.getroot()
34chapters = root.getchildren()
35snames = []
36snamesx = []
37for c in chapters[1:]:
38  for s in c.getchildren():
39    tn = s.tag
40    ix = tn.find( '}' )
41    if ix != -1:
42      tn = tn[ix+1:]
43    snames.append(tn)
44    snamesx.append(s.tag)
45
46###ss = chapters[1].getchildren()[0]
47###i = root.makeelement( '{urn:w3id.org:cmip6.dreq.dreq:a}item', {'uid':'xxxxxx','label':'test', 'title':'A test element'} )
48###i.tail = '\n'
49###ss.append( i )
50
51def lower(x):
52  if x == None:
53    return ''
54  else:
55    return x.lower()
56def getkey(elem):
57    return (lower(elem.get('label')),lower(elem.get('uid')))
58
59for s in snames:
60  try:
61    section = doc.findall('.//{urn:w3id.org:cmip6.dreq.dreq:a}%s' % s)
62    ##lines = section[0].findall('.//{urn:w3id.org:cmip6.dreq.dreq:a}item' )
63    lines = section[0]
64    lines[:] = sorted(lines, key=getkey)
65    ##for l in lines[:3]:
66      ##print s, l.get( 'label' ),l.get( 'uid' )
67   
68  except:
69    print 'Failed to find: %s' % s
70    raise
71
72output = StringIO()
73if python2:
74  doc.write_c14n(output)
75else:
76  doc.write_c14n(output,encoding='unicode')
77sdoc = output.getvalue()
78ll = string.split( sdoc, '\n' )
79s2 = string.join( ll, '\n' )
80md5f = md5( sdoc ).hexdigest()
81md5f2 = md5( s2 ).hexdigest()
82md5p = md5( string.join( ll[1:], '\n' ) ).hexdigest()
83prnt ('%s, %s' % (md5f, md5p ) )
84prnt (md5f2 )
85oo = open( ofile, 'w' )
86oo.write( sdoc )
87oo.close()
88
89## use re to extract sections of canonical document
90oo = open('sectionChecksums.csv', 'w' )
91oo.write( 'Section\tMD5 Checksum\n' )
92for s in snames:
93  s1 = re.compile( '<%s(.*)</%s>' % (s,s), re.DOTALL )
94  x = s1.search( sdoc ).group(0)
95  md5x = md5( x ).hexdigest()
96  prnt ( 'Checksum of section %s: %s' % (s,md5x) )
97  oo.write( '%s\t%s\n' % (s,md5x) )
98oo.close()
Note: See TracBrowser for help on using the repository browser.