/[volute]/trunk/projects/std-vounits/known-units-to-tex.py
ViewVC logotype

Contents of /trunk/projects/std-vounits/known-units-to-tex.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2310 - (show annotations)
Fri Oct 18 14:14:36 2013 UTC (7 years, 11 months ago) by norman.x.gray
File MIME type: text/x-python
File size: 1818 byte(s)
Integrate fixes from Mireille Louys, following suggestions from Pat Dowler
1 #! /usr/bin/python
2
3 import sys
4 import string
5 import re
6
7 lines = []
8
9 for line in sys.stdin:
10 if not re.search('#',line):
11 lines.append(line.strip().split(','))
12
13 nrows = (len(lines)+1)/2
14
15 # The name-generating hack below doesn't work well in all cases,
16 # so supply some explicit replacements.
17 replacements = { 'unity:DetectorChannel': 'channel',
18 'qudt:DegreeAngle': 'degree (angle)',
19 'unity:DistributionBin': 'bin',
20 'qudt:Gram': 'gramme',
21 'unity:MilliArcSecond': 'milliarcsecond',
22 'qudt:MinuteTime': 'minute (time)',
23 'qudt:SecondTime': 'second (time)',
24 'unity:SolarLuminosity': 'luminosity',
25 'unity:StellarMagnitude': 'magnitudes',
26 'qudt:UnifiedAtomicMassUnit': 'AMU',
27 }
28
29 all_uppercase = re.compile('^[A-Z]+$')
30 initial_capital = re.compile('[A-Z][^A-Z]*')
31
32 def fmtname(uname):
33 if uname in replacements:
34 rval = replacements[uname]
35 else:
36 nameparts = uname.split(':')
37 if len(nameparts) != 2:
38 print >>sys.stderr, "name %s malformed" % uname
39 rval ='XXX'
40 else:
41 tname = nameparts[1]
42 if all_uppercase.match(tname):
43 rval = tname
44 elif initial_capital.match(tname):
45 rval = string.join(map(string.lower, initial_capital.findall(tname)), ' ')
46 else:
47 rval = tname
48 return [rval]
49
50 def fmtrow(r):
51 return string.join([r[0]] + fmtname(r[1]) + (map(lambda s: "$\\cdot$" if s=="1" else s[1:], r[2:])),'&')
52
53 for r in range(nrows):
54 r2 = r+nrows
55 if r2 >= len(lines):
56 print "%s\\\\" % (fmtrow(lines[r]))
57 else:
58 print "%s & %s\\\\" % (fmtrow(lines[r]), fmtrow(lines[r2]))

msdemlei@ari.uni-heidelberg.de
ViewVC Help
Powered by ViewVC 1.1.26