1 | ################################################################################ |
---|
2 | # |
---|
3 | # Import PDFgetN data |
---|
4 | # |
---|
5 | ################################################################################ |
---|
6 | |
---|
7 | def parseIDLData(filename): |
---|
8 | """ Parse PDFgetN data file |
---|
9 | |
---|
10 | Return: |
---|
11 | - List of tuple |
---|
12 | - x_array |
---|
13 | - y_array |
---|
14 | - e_array |
---|
15 | """ |
---|
16 | import math |
---|
17 | print "Parsing IDL File %s" % (filename) |
---|
18 | |
---|
19 | # 1. Open file |
---|
20 | manfile = open(filename, "r") |
---|
21 | lines = manfile.readlines() |
---|
22 | manfile.close() |
---|
23 | |
---|
24 | # 2. Parse |
---|
25 | xl = [] |
---|
26 | yl = [] |
---|
27 | el = [] |
---|
28 | |
---|
29 | xlargestnan = -1.0E10 |
---|
30 | |
---|
31 | for lindex in xrange(0, len(lines)): |
---|
32 | line = lines[lindex].strip() |
---|
33 | |
---|
34 | if line == "": |
---|
35 | # a) Empty line |
---|
36 | pass |
---|
37 | |
---|
38 | elif line.startswith("#"): |
---|
39 | # b) #-line: pass |
---|
40 | pass |
---|
41 | |
---|
42 | else: |
---|
43 | # c) Data line |
---|
44 | line = line.lower() |
---|
45 | terms = line.split() |
---|
46 | if line.count("n") > 0 or line.count("i"): |
---|
47 | # NAN or Infty: Special treat |
---|
48 | x = float(terms[0]) |
---|
49 | y = 0.0 |
---|
50 | e = 1.0E5 |
---|
51 | |
---|
52 | if x > xlargestnan: |
---|
53 | xlargestnan = x |
---|
54 | |
---|
55 | else: |
---|
56 | # regular data-line |
---|
57 | x = float(terms[0]) |
---|
58 | y = float(terms[1]) |
---|
59 | e = math.sqrt(abs(y)) |
---|
60 | # END-IF |
---|
61 | xl.append(x) |
---|
62 | yl.append(y) |
---|
63 | el.append(e) |
---|
64 | |
---|
65 | # END-IF |
---|
66 | # END-FOR |
---|
67 | |
---|
68 | dataset = (xl, yl, el) |
---|
69 | |
---|
70 | print "Data File %s Range = (%f, ...)" % (filename, xlargestnan) |
---|
71 | |
---|
72 | return dataset |
---|
73 | |
---|
74 | |
---|
75 | def revertArray(a): |
---|
76 | """ Revert order of arrays |
---|
77 | """ |
---|
78 | aa = [] |
---|
79 | for ai in xrange(len(a)): |
---|
80 | item = a[ai] |
---|
81 | aa.insert(0, item) |
---|
82 | |
---|
83 | return aa |
---|
84 | |
---|
85 | |
---|
86 | def main(argv, unit): |
---|
87 | """ Main method: Import a series of file |
---|
88 | """ |
---|
89 | # 1. Parse args |
---|
90 | filenames = [] |
---|
91 | for iarg in xrange(1, len(argv)): |
---|
92 | filename = argv[iarg] |
---|
93 | filenames.append(filename) |
---|
94 | |
---|
95 | # 2. Generate Workspace |
---|
96 | for filename in filenames: |
---|
97 | # 2.1 Parse data file |
---|
98 | dataset = parseIDLData(filename) |
---|
99 | x = dataset[0] |
---|
100 | y = dataset[1] |
---|
101 | e = dataset[2] |
---|
102 | |
---|
103 | # 2.2 in right order |
---|
104 | if x[1] < x[0]: |
---|
105 | x = revertArray(x) |
---|
106 | y = revertArray(y) |
---|
107 | e = revertArray(e) |
---|
108 | # ENDIF |
---|
109 | |
---|
110 | # 2.3 Create Workspace |
---|
111 | wsname = filename.split("/")[-1].split('.')[0] |
---|
112 | CreateWorkspace(wsname, x, y, e, 1, unit) |
---|
113 | # END-FOR |
---|
114 | |
---|
115 | return |
---|
116 | |
---|
117 | |
---|
118 | if __name__=="__main__": |
---|
119 | argv = ["x", |
---|
120 | "./IDLData/nom1818bank0.dat", |
---|
121 | "./IDLData/nom1818bank1.dat", |
---|
122 | "./IDLData/nom1818bank2.dat", |
---|
123 | "./IDLData/nom1818bank3.dat", |
---|
124 | "./IDLData/nom1818bank4.dat", |
---|
125 | "./IDLData/nom1818bank5.dat", |
---|
126 | ] |
---|
127 | unit = "MomentumTransfer" |
---|
128 | argv = ["x", |
---|
129 | "./NOM_1818ftf.gr" |
---|
130 | ] |
---|
131 | unit = "" |
---|
132 | main(argv, unit) |
---|