[18e7309] | 1 | from sas.sascalc.file_converter.core.bsl_loader import CLoader |
---|
[6eaf89ea] | 2 | from copy import deepcopy |
---|
| 3 | import os |
---|
[2760662] | 4 | import numpy as np |
---|
[18e7309] | 5 | |
---|
[9c500ab] | 6 | class BSLParsingError(Exception): |
---|
| 7 | pass |
---|
| 8 | |
---|
[18e7309] | 9 | class BSLLoader(CLoader): |
---|
[9c500ab] | 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 | """ |
---|
[18e7309] | 17 | |
---|
[535e181] | 18 | def __init__(self, filename): |
---|
[9c500ab] | 19 | """ |
---|
| 20 | Parses the BSL header file and sets instance variables apropriately |
---|
| 21 | |
---|
| 22 | :param filename: Path to the BSL header file |
---|
| 23 | """ |
---|
[6eaf89ea] | 24 | header_file = open(filename, 'r') |
---|
| 25 | data_info = {} |
---|
| 26 | is_valid = True |
---|
| 27 | err_msg = "" |
---|
[18e7309] | 28 | |
---|
[6eaf89ea] | 29 | [folder, filename] = os.path.split(filename) |
---|
[9c500ab] | 30 | # SAS data will be in file Xnn001.mdd |
---|
| 31 | sasdata_filename = filename.replace('000.', '001.') |
---|
| 32 | if sasdata_filename == filename: |
---|
[dc8a553] | 33 | err_msg = ("Invalid header filename {}.\nShould be of the format " |
---|
[9c500ab] | 34 | "Xnn000.XXX where X is any alphanumeric character and n is any" |
---|
| 35 | " digit.").format(filename) |
---|
| 36 | raise BSLParsingError(err_msg) |
---|
[6eaf89ea] | 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 |
---|
[9c500ab] | 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 |
---|
[6eaf89ea] | 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 |
---|
[c3f0114] | 70 | err_msg = "Invalid metadata in header file for {}" |
---|
[9c500ab] | 71 | err_msg = err_msg.format(sasdata_filename) |
---|
[6eaf89ea] | 72 | break |
---|
| 73 | |
---|
[d889d80] | 74 | header_file.close() |
---|
[6eaf89ea] | 75 | if not is_valid: |
---|
[9c500ab] | 76 | raise BSLParsingError(err_msg) |
---|
[6eaf89ea] | 77 | |
---|
[535e181] | 78 | CLoader.__init__(self, data_info['filename'], data_info['frames'], |
---|
[6eaf89ea] | 79 | data_info['pixels'], data_info['rasters'], data_info['swap_bytes']) |
---|
[18e7309] | 80 | |
---|
[cc03f59] | 81 | def __setattr__(self, name, value): |
---|
| 82 | if name == 'filename': |
---|
| 83 | return self.set_filename(value) |
---|
[535e181] | 84 | elif name == 'n_frames': |
---|
| 85 | return self.set_n_frames(value) |
---|
[cc03f59] | 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) |
---|
[6eaf89ea] | 92 | elif name == 'swap_bytes': |
---|
| 93 | return self.set_swap_bytes(value) |
---|
| 94 | return CLoader.__setattr__(self, name, value) |
---|
[2760662] | 95 | |
---|
| 96 | def __getattr__(self, name): |
---|
| 97 | if name == 'filename': |
---|
| 98 | return self.get_filename() |
---|
[535e181] | 99 | elif name == 'n_frames': |
---|
| 100 | return self.get_n_frames() |
---|
[2760662] | 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() |
---|
[6eaf89ea] | 107 | elif name == 'swap_bytes': |
---|
| 108 | return self.get_swap_bytes() |
---|
| 109 | return CLoader.__getattr__(self, name) |
---|