1
+ #!/bin/env python
2
+
3
+ import numpy as np
4
+ import os
5
+ from PyQt5 .QtCore import pyqtSignal , QThread
6
+
7
+ from gisansexplorer .utils import Frozen , handle_exception
8
+
9
+
10
+ class Instrument (Frozen ):
11
+ def __init__ (self , name , pixel_size_mm = None , sample_detector_distance_mm = None , data_root_dir = "/" ):
12
+
13
+ if name .casefold () == "maria" :
14
+ self .name = "MARIA"
15
+ self .pixel_size_mm = 0.5755
16
+ self .sample_detector_distance_mm = 1990
17
+ elif name .casefold () == "other" :
18
+ self .name = "OTHER"
19
+ self .pixel_size_mm = 0.5755
20
+ self .sample_detector_distance_mm = 1990
21
+ else :
22
+ self .name = name
23
+ self .pixel_size_mm = pixel_size_mm
24
+ self .sample_detector_distance_mm = sample_detector_distance_mm
25
+
26
+ self ._freeze ()
27
+
28
+ return
29
+
30
+ __instrument__ = Instrument ("MARIA" )
31
+
32
+ class Experiment (Frozen ):
33
+ def __init__ (self , instrument = __instrument__ ):
34
+ if instrument is None :
35
+ self .instrument_name = "Default"
36
+ self .pixel_size_mm = 0.5755
37
+ self .sample_detector_distance_mm = 1990
38
+ else :
39
+ self .instrument_name = instrument .name
40
+ self .pixel_size_mm = instrument .pixel_size_mm
41
+ self .sample_detector_distance_mm = instrument .sample_detector_distance_mm
42
+
43
+ self .selector_lambda = None
44
+ self .angle_of_incidence = 0
45
+ self .sens = None
46
+ self .meansens = None
47
+ self .monitor_counts = None
48
+
49
+ # Define the position of the direct beam on the detector
50
+ # and the sensitivity map file
51
+ self .qyc = 0
52
+ self .qzc = 0
53
+ self .qzc_corr = 0
54
+ self .qzc_spec = 0
55
+ self .x0 = 128
56
+ self .y0 = 128
57
+ self .xf = 256
58
+ self .yf = 256
59
+ self .min_intensity = 1e-6
60
+ self .max_intensity = 1e-3
61
+ #self.qy = np.asarray([])
62
+ #self.qz = np.asarray([])
63
+ #self.I = np.asarray([])
64
+ #self.inputd = np.asarray([])
65
+
66
+ self .Imatrix = np .asarray ([])
67
+ self .qymatrix = np .asarray ([])
68
+ self .qzmatrix = np .asarray ([])
69
+
70
+ self .cut_Iz = np .asarray ([])
71
+ self .cut_Iy = np .asarray ([])
72
+
73
+ self ._freeze ()
74
+
75
+ return
76
+
77
+
78
+ #sin_alpha_f(j) = px_size*(j-zc)/np.sqrt(sdd*sdd+(px_size*(zc-j))*(px_size*(zc-j)))
79
+ #sin_2theta_f(i) = px_size*(i-yc)/np.sqrt(sdd*sdd+(px_size*(yc-i))*(px_size*(yc-i)))
80
+ #cos_alpha_f(j) = sdd/np.sqrt(sdd*sdd+(px_size*(zc-j))*(px_size*(zc-j)))
81
+
82
+
83
+ @property
84
+ def two_pi_over_lambda (self ):
85
+ return 2 * np .pi / float (self .selector_lambda )
86
+
87
+
88
+ @property
89
+ def sin_alpha_i (self ):
90
+ return np .sin (np .pi * float (self .angle_of_incidence )/ 180.0 )
91
+
92
+
93
+ @property
94
+ def cos_alpha_i (self ):
95
+ return np .cos (np .pi * float (self .angle_of_incidence )/ 180.0 )
96
+
97
+
98
+ def sin_2theta_f (self , pixel_i ):
99
+ px_size = self .pixel_size_mm
100
+ sdd = self .sample_detector_distance_mm
101
+ return px_size * (pixel_i - self .qyc )/ np .sqrt (sdd * sdd + (px_size * (self .qyc - pixel_i ))* (px_size * (self .qyc - pixel_i )))
102
+
103
+
104
+ def sin_alpha_f (self , pixel_j ):
105
+ px_size = self .pixel_size_mm
106
+ sdd = self .sample_detector_distance_mm
107
+ return px_size * (pixel_j - self .qzc_corr )/ np .sqrt (sdd * sdd + (px_size * (self .qzc_corr - pixel_j ))* (px_size * (self .qzc_corr - pixel_j )))
108
+
109
+
110
+ def cos_alpha_f (self , pixel_j ):
111
+ return np .sqrt (1 - self .sin_alpha_f (pixel_j )** 2 )
112
+
113
+
114
+
115
+ class Settings (Frozen ):
116
+ def __init__ (self ):
117
+ self .dataDirPath = None
118
+ self .datFileName = None
119
+ self .yamlFileNames = []
120
+ self .gzFileNames = []
121
+ self .sensFileName = "sensitivity_map"
122
+ self ._freeze ()
123
+ return
124
+
125
+
126
+ def datFilePath (self ):
127
+ if self .dataDirPath is None :
128
+ return None
129
+ return os .path .join (self .dataDirPath ,self .datFileName )
130
+
131
+
132
+ def yamlFilePaths (self ):
133
+ if self .dataDirPath is None :
134
+ return None
135
+ return [os .path .join (self .dataDirPath ,f ) for f in self .yamlFileNames ]
136
+
137
+
138
+ def gzFilePaths (self ):
139
+ if self .dataDirPath is None :
140
+ return None
141
+ return [os .path .join (self .dataDirPath ,f ) for f in self .gzFileNames ]
142
+
143
+
144
+ def sensFilePath (self ):
145
+ if self .dataDirPath is None :
146
+ return None
147
+ return os .path .join (self .dataDirPath , self .sensFileName )
148
+
149
+
150
+ def basename (self ):
151
+ if self .dataDirPath is None :
152
+ return None
153
+ return os .path .splitext (self .datFilePath ())[0 ]
154
+
155
+
156
+ def gisans_map_filepath (self ):
157
+ if self .dataDirPath is None :
158
+ return None
159
+ return self .basename ()+ "_GISANS.map"
160
+
161
+
162
+ def gisans_cut_filepath (self , y_or_z = "z" ):
163
+ if self .dataDirPath is None :
164
+ return None
165
+ return os .path .join (self .basename (),f"_line_cut_q{ y_or_z } .out" )
166
+
167
+
168
+ class FileReadingThread (QThread ):
169
+ progress_signal = pyqtSignal (int )
170
+ def __init__ (self , myframe ):
171
+ super ().__init__ ()
172
+ self .frame = myframe
173
+ self .retval = None
174
+ self .datFilePath = None
175
+
176
+ def run (self ):
177
+ myframe = self .frame
178
+ myframe .settings = Settings ()
179
+ myframe .experiment = Experiment ()
180
+
181
+ try :
182
+ self .progress_signal .emit (0 )
183
+ if not myframe .read_dat_file (self .datFilePath ):
184
+ self .retval = False , "dat file not read"
185
+ return
186
+
187
+ self .progress_signal .emit (10 )
188
+ if not myframe .read_yaml_file ():
189
+ self .retval = False , "yaml file not read"
190
+ return
191
+
192
+ self .progress_signal .emit (25 )
193
+ if not myframe .read_sensitivity_file ():
194
+ self .retval = False , "Sensitivity file not read"
195
+ return
196
+
197
+ self .progress_signal .emit (75 )
198
+ if not myframe .read_intensity_file ():
199
+ self .retval = False , "Intensity file not read"
200
+ return
201
+
202
+ self .progress_signal .emit (90 )
203
+ self .retval = True , ""
204
+ return
205
+ except Exception as e :
206
+ self .retval = False , "An exception occurred"
207
+ return
0 commit comments