1 | from sas.sascalc.file_converter.core.bsl_loader import CLoader |
---|
2 | from sas.sascalc.dataloader.data_info import Data2D |
---|
3 | from copy import deepcopy |
---|
4 | import os |
---|
5 | import numpy as np |
---|
6 | |
---|
7 | class BSLParsingError(Exception): |
---|
8 | pass |
---|
9 | |
---|
10 | class BSLLoader(CLoader): |
---|
11 | """ |
---|
12 | Loads 2D SAS data from a BSL file. |
---|
13 | CLoader is a C extension (found in c_ext/bsl_loader.c) |
---|
14 | |
---|
15 | See http://www.diamond.ac.uk/Beamlines/Soft-Condensed-Matter/small-angle/SAXS-Software/CCP13/BSL.html |
---|
16 | for more info on the BSL file format. |
---|
17 | """ |
---|
18 | |
---|
19 | def __init__(self, filename): |
---|
20 | """ |
---|
21 | Parses the BSL header file and sets instance variables apropriately |
---|
22 | |
---|
23 | :param filename: Path to the BSL header file |
---|
24 | """ |
---|
25 | header_file = open(filename, 'r') |
---|
26 | data_info = {} |
---|
27 | is_valid = True |
---|
28 | err_msg = "" |
---|
29 | |
---|
30 | [folder, filename] = os.path.split(filename) |
---|
31 | # SAS data will be in file Xnn001.mdd |
---|
32 | sasdata_filename = filename.replace('000.', '001.') |
---|
33 | if sasdata_filename == filename: |
---|
34 | err_msg = ("Invalid header filename {}.\nShould be of the format " |
---|
35 | "Xnn000.XXX where X is any alphanumeric character and n is any" |
---|
36 | " digit.").format(filename) |
---|
37 | raise BSLParsingError(err_msg) |
---|
38 | |
---|
39 | # First 2 lines are headers |
---|
40 | header_file.readline() |
---|
41 | header_file.readline() |
---|
42 | |
---|
43 | while True: |
---|
44 | metadata = header_file.readline().strip() |
---|
45 | metadata = metadata.split() |
---|
46 | data_filename = header_file.readline().strip() |
---|
47 | |
---|
48 | if len(metadata) != 10: |
---|
49 | is_valid = False |
---|
50 | err_msg = "Invalid header file: {}".format(filename) |
---|
51 | break |
---|
52 | |
---|
53 | if data_filename != sasdata_filename: |
---|
54 | last_file = (metadata[9] == '0') |
---|
55 | if last_file: # Reached last file we have metadata for |
---|
56 | is_valid = False |
---|
57 | err_msg = "No metadata for {} found in header file: {}" |
---|
58 | err_msg = err_msg.format(sasdata_filename, filename) |
---|
59 | break |
---|
60 | continue |
---|
61 | try: |
---|
62 | data_info = { |
---|
63 | 'filename': os.path.join(folder, data_filename), |
---|
64 | 'pixels': int(metadata[0]), |
---|
65 | 'rasters': int(metadata[1]), |
---|
66 | 'frames': int(metadata[2]), |
---|
67 | 'swap_bytes': int(metadata[3]) |
---|
68 | } |
---|
69 | except: |
---|
70 | is_valid = False |
---|
71 | err_msg = "Invalid metadata in header file for {}" |
---|
72 | err_msg = err_msg.format(sasdata_filename) |
---|
73 | break |
---|
74 | |
---|
75 | header_file.close() |
---|
76 | if not is_valid: |
---|
77 | raise BSLParsingError(err_msg) |
---|
78 | |
---|
79 | CLoader.__init__(self, data_info['filename'], data_info['frames'], |
---|
80 | data_info['pixels'], data_info['rasters'], data_info['swap_bytes']) |
---|
81 | |
---|
82 | def load_frames(self, frames): |
---|
83 | frame_data = [] |
---|
84 | # Prepare axis values (arbitrary scale) |
---|
85 | x = self.n_rasters * range(1, self.n_pixels+1) |
---|
86 | y = [self.n_pixels * [i] for i in range(1, self.n_rasters+1)] |
---|
87 | y = np.reshape(y, (1, self.n_pixels*self.n_rasters))[0] |
---|
88 | x_bins = x[:self.n_pixels] |
---|
89 | y_bins = y[0::self.n_pixels] |
---|
90 | |
---|
91 | for frame in frames: |
---|
92 | self.frame = frame |
---|
93 | raw_frame_data = self.load_data() |
---|
94 | data2d = Data2D(data=raw_frame_data, qx_data=x, qy_data=y) |
---|
95 | data2d.x_bins = x_bins |
---|
96 | data2d.y_bins = y_bins |
---|
97 | frame_data.append(data2d) |
---|
98 | |
---|
99 | return frame_data |
---|
100 | |
---|
101 | |
---|
102 | def __setattr__(self, name, value): |
---|
103 | if name == 'filename': |
---|
104 | return self.set_filename(value) |
---|
105 | elif name == 'n_frames': |
---|
106 | return self.set_n_frames(value) |
---|
107 | elif name == 'frame': |
---|
108 | return self.set_frame(value) |
---|
109 | elif name == 'n_pixels': |
---|
110 | return self.set_n_pixels(value) |
---|
111 | elif name == 'n_rasters': |
---|
112 | return self.set_n_rasters(value) |
---|
113 | elif name == 'swap_bytes': |
---|
114 | return self.set_swap_bytes(value) |
---|
115 | return CLoader.__setattr__(self, name, value) |
---|
116 | |
---|
117 | def __getattr__(self, name): |
---|
118 | if name == 'filename': |
---|
119 | return self.get_filename() |
---|
120 | elif name == 'n_frames': |
---|
121 | return self.get_n_frames() |
---|
122 | elif name == 'frame': |
---|
123 | return self.get_frame() |
---|
124 | elif name == 'n_pixels': |
---|
125 | return self.get_n_pixels() |
---|
126 | elif name == 'n_rasters': |
---|
127 | return self.get_n_rasters() |
---|
128 | elif name == 'swap_bytes': |
---|
129 | return self.get_swap_bytes() |
---|
130 | return CLoader.__getattr__(self, name) |
---|