1 | #!/usr/bin/env python |
---|
2 | """ |
---|
3 | Wrapper for the Disperser class extension |
---|
4 | |
---|
5 | @author: Mathieu Doucet / UTK |
---|
6 | @contact: mathieu.doucet@nist.gov |
---|
7 | """ |
---|
8 | |
---|
9 | import math |
---|
10 | import os, string, sys |
---|
11 | from sans.models.BaseComponent import BaseComponent |
---|
12 | from sans.models.CylinderModel import CylinderModel |
---|
13 | |
---|
14 | class WeightModel(CylinderModel): |
---|
15 | """ |
---|
16 | """ |
---|
17 | |
---|
18 | def __init__(self): |
---|
19 | """ Initialization |
---|
20 | @param model: Model to disperse [BaseComponent] |
---|
21 | @param param: Parameter to disperse [string] |
---|
22 | """ |
---|
23 | |
---|
24 | # Initialize BaseComponent first, then sphere |
---|
25 | CylinderModel.__init__(self) |
---|
26 | self.params['datafile'] = '' |
---|
27 | |
---|
28 | self.d = None |
---|
29 | ## Name of the model |
---|
30 | self.name = 'WeightModel' |
---|
31 | |
---|
32 | def run(self, x = 0.0): |
---|
33 | """ Evaluate the model |
---|
34 | @param x: input q, or [q,phi] |
---|
35 | @return: scattering function P(q) |
---|
36 | """ |
---|
37 | if len(self.d.x)==0: |
---|
38 | return 0.0 |
---|
39 | try: |
---|
40 | total = 0 |
---|
41 | norma = 0 |
---|
42 | for i in range(len(self.d.x)): |
---|
43 | self.params['cyl_phi'] = self.d.x[i]*math.pi/180.0 |
---|
44 | total += CylinderModel.run(self, x) * self.d.y[i] |
---|
45 | |
---|
46 | |
---|
47 | self.params['cyl_phi'] = -self.d.x[i]*math.pi/180.0 |
---|
48 | total += CylinderModel.run(self, x) * self.d.y[i] |
---|
49 | |
---|
50 | norma += 2.0*self.d.y[i] |
---|
51 | return total/norma |
---|
52 | except: |
---|
53 | print sys.exc_value |
---|
54 | |
---|
55 | return 0.0 |
---|
56 | |
---|
57 | def readData(self): |
---|
58 | self.d = DataReader(self.params['datafile']) |
---|
59 | self.d.read() |
---|
60 | |
---|
61 | |
---|
62 | class DataReader: |
---|
63 | """ Simple data reader for Igor data files """ |
---|
64 | |
---|
65 | def __init__(self, filename): |
---|
66 | """ Init |
---|
67 | @param filename: Name of Igor data file to read |
---|
68 | """ |
---|
69 | self.file = filename |
---|
70 | self.x = [] |
---|
71 | self.y = [] |
---|
72 | |
---|
73 | def read(self): |
---|
74 | """ Read file """ |
---|
75 | # Check if the file is there |
---|
76 | if not os.path.isfile(self.file): |
---|
77 | raise ValueError, \ |
---|
78 | "Specified file %s is not a regular file" % self.file |
---|
79 | |
---|
80 | # Read file |
---|
81 | f = open(self.file,'r') |
---|
82 | buf = f.read() |
---|
83 | |
---|
84 | # Get content |
---|
85 | dataStarted = False |
---|
86 | |
---|
87 | |
---|
88 | lines = string.split(buf,'\n') |
---|
89 | itot = 0 |
---|
90 | self.x = [] |
---|
91 | self.y = [] |
---|
92 | for line in lines: |
---|
93 | if line.count("Angle")>0: |
---|
94 | dataStarted = True |
---|
95 | continue |
---|
96 | |
---|
97 | if dataStarted == True: |
---|
98 | |
---|
99 | try: |
---|
100 | toks = line.split() |
---|
101 | except: |
---|
102 | print sys.exc_value |
---|
103 | |
---|
104 | self.x.append(float(toks[0])) |
---|
105 | |
---|
106 | self.y.append(float(toks[1])) |
---|
107 | |
---|
108 | |
---|
109 | itot += 1 |
---|
110 | |
---|
111 | print "Read %g points from file %s" % (len(self.image), self.file) |
---|
112 | |
---|
113 | |
---|
114 | if __name__ == '__main__': |
---|
115 | app = WeightModel() |
---|
116 | cyl = CylinderModel() |
---|
117 | print "%s: f(1) = %g" % (app.name, app.run(1)) |
---|
118 | |
---|
119 | # End of file |
---|