source: sasview/src/sas/sascalc/file_converter/bsl_loader.py @ ac370c5

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since ac370c5 was d889d80, checked in by lewis, 8 years ago

Close file when done in BSLLoader

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