snapshot of 4.0a development. initial git repo commit
[erange.git] / binstocdf.py
1 import sys
2
3 print 'version 1.0'
4
5 def main(argv=None):
6     if not argv:
7         argv = sys.argv
8
9     if len(argv) < 2:
10         print 'usage: python %s infile outfile' % sys.argv[0]
11         sys.exit(1)
12
13     infilename = argv[0]
14     outfilename = argv[1]
15
16     binToCDF(infilename, outfilename)
17
18
19 def binToCDF(infilename, outfilename):
20     infile = open(infilename)
21     outfile = open(outfilename, 'w')
22
23     for line in infile:
24         fields = line.strip().split()
25         if len(fields) < 4:
26             continue
27
28         total = int(fields[2])
29         if total == 0:
30             outfile.write(line)
31             continue
32
33         outfile.write('%s\t%s\t%s\t%s' % (fields[0], fields[1], fields[2], fields[3]))
34         cum = 0
35         for bin in fields[4:]:
36             cum += int(bin)
37             percent = 100 * cum / total
38             outfile.write('\t%d' % percent)
39
40         outfile.write('\n')
41
42     infile.close()
43     outfile.close()
44
45 if __name__ == "__main__":
46     main(sys.argv)