-
Notifications
You must be signed in to change notification settings - Fork 3
/
LoadGrace.py
79 lines (66 loc) · 2.14 KB
/
LoadGrace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
import numpy as np
import re
class LoadGrace:
"""
Simple module to load a Grace file (.agr) plot and extract
legends, comments, and -- importantly -- data into numpy arrays.
"""
def __init__(self, filename):
self._sets = []
self._legends = []
self._comments = []
f = open(filename, 'r')
for line in f:
if re.compile('@ s(.*) title').search(line):
self._legends.append( line.split('"')[1] )
if re.compile('@ s(.*) legend').search(line):
self._legends.append( line.split('"')[1] )
if re.compile('@\s*yaxis\s*label').search(line):
self._legends.append( line.split('"')[1] )
if re.compile('@ s(.*) comment').search(line):
self._comments.append( line.split('"')[1] )
if '@target' in line:
tmp = []
next(f)
for row in f:
if row!='&\n':
tmp.append( np.fromstring( row, sep=' ' ) )
else:
self._sets.append( np.array(tmp) )
break
if '@ zeroxaxis bar linestyle 3' in line:
tmp = []
next(f)
for row in f:
if row!='&\n':
tmp.append( np.fromstring( row, sep=' ' ) )
else:
self._sets.append( np.array(tmp) )
break
if '@TYPE xy' in line:
tmp = []
next(f)
for row in f:
if row!='&\n':
tmp.append( np.fromstring( row, sep=' ' ) )
else:
self._sets.append( np.array(tmp) )
break
f.close()
def sets(self): return self._sets
def legends(self): return self._legends
def comments(self): return self._comments
def __len__(self): return len(self._sets)
# If run as main program
if __name__ == "__main__":
import argparse
ps = argparse.ArgumentParser( description = 'Extract data from xmgrace (.agr) files')
ps.add_argument( 'filename', type = str, help = 'xmgrace file' )
args = ps.parse_args()
d = LoadGrace( args.filename )
for i in range(0, len(d)):
#print ('# legend=' + d.legends()[i] + ' comment=' + d.comments()[i])
print ('# legend=' + d.legends()[i])
print (d.sets()[i])
print