[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 | |
---|
| 6 | class BSLLoader(CLoader): |
---|
| 7 | |
---|
[2760662] | 8 | # TODO: Change to __init__(self, filename, frame) |
---|
| 9 | # and parse n_(pixels/rasters) from header file |
---|
[535e181] | 10 | def __init__(self, filename): |
---|
[6eaf89ea] | 11 | header_file = open(filename, 'r') |
---|
| 12 | data_info = {} |
---|
| 13 | is_valid = True |
---|
| 14 | err_msg = "" |
---|
[18e7309] | 15 | |
---|
[6eaf89ea] | 16 | [folder, filename] = os.path.split(filename) |
---|
| 17 | |
---|
| 18 | # First 2 lines are headers |
---|
| 19 | header_file.readline() |
---|
| 20 | header_file.readline() |
---|
| 21 | |
---|
| 22 | while True: |
---|
| 23 | metadata = header_file.readline().strip() |
---|
| 24 | metadata = metadata.split() |
---|
| 25 | data_filename = header_file.readline().strip() |
---|
| 26 | |
---|
| 27 | if len(metadata) != 10: |
---|
| 28 | is_valid = False |
---|
| 29 | err_msg = "Invalid header file: {}".format(filename) |
---|
| 30 | break |
---|
| 31 | # SAS data will be in file Xnn001.mdd |
---|
| 32 | if data_filename != filename.replace('0.', '1.'): |
---|
| 33 | continue |
---|
| 34 | try: |
---|
| 35 | data_info = { |
---|
| 36 | 'filename': os.path.join(folder, data_filename), |
---|
| 37 | 'pixels': int(metadata[0]), |
---|
| 38 | 'rasters': int(metadata[1]), |
---|
| 39 | 'frames': int(metadata[2]), |
---|
| 40 | 'swap_bytes': int(metadata[3]) |
---|
| 41 | } |
---|
| 42 | except: |
---|
| 43 | is_valid = False |
---|
[c3f0114] | 44 | err_msg = "Invalid metadata in header file for {}" |
---|
| 45 | err_msg = err_msg.format(filename.replace('0.', '1.')) |
---|
[6eaf89ea] | 46 | break |
---|
| 47 | |
---|
| 48 | if not is_valid: |
---|
| 49 | raise Exception(err_msg) |
---|
| 50 | |
---|
| 51 | if data_info['frames'] == 1: |
---|
| 52 | # File is actually in OTOKO (1D) format |
---|
| 53 | # Number of frames is 2nd indicator, |
---|
| 54 | data_info['frames'] = data_info['rasters'] |
---|
| 55 | data_info['rasters'] = data_info['pixels'] |
---|
| 56 | data_info['pixels'] = 1 |
---|
| 57 | |
---|
[535e181] | 58 | CLoader.__init__(self, data_info['filename'], data_info['frames'], |
---|
[6eaf89ea] | 59 | data_info['pixels'], data_info['rasters'], data_info['swap_bytes']) |
---|
[18e7309] | 60 | |
---|
[cc03f59] | 61 | def __setattr__(self, name, value): |
---|
| 62 | if name == 'filename': |
---|
| 63 | return self.set_filename(value) |
---|
[535e181] | 64 | elif name == 'n_frames': |
---|
| 65 | return self.set_n_frames(value) |
---|
[cc03f59] | 66 | elif name == 'frame': |
---|
| 67 | return self.set_frame(value) |
---|
| 68 | elif name == 'n_pixels': |
---|
| 69 | return self.set_n_pixels(value) |
---|
| 70 | elif name == 'n_rasters': |
---|
| 71 | return self.set_n_rasters(value) |
---|
[6eaf89ea] | 72 | elif name == 'swap_bytes': |
---|
| 73 | return self.set_swap_bytes(value) |
---|
| 74 | return CLoader.__setattr__(self, name, value) |
---|
[2760662] | 75 | |
---|
| 76 | def __getattr__(self, name): |
---|
| 77 | if name == 'filename': |
---|
| 78 | return self.get_filename() |
---|
[535e181] | 79 | elif name == 'n_frames': |
---|
| 80 | return self.get_n_frames() |
---|
[2760662] | 81 | elif name == 'frame': |
---|
| 82 | return self.get_frame() |
---|
| 83 | elif name == 'n_pixels': |
---|
| 84 | return self.get_n_pixels() |
---|
| 85 | elif name == 'n_rasters': |
---|
| 86 | return self.get_n_rasters() |
---|
[6eaf89ea] | 87 | elif name == 'swap_bytes': |
---|
| 88 | return self.get_swap_bytes() |
---|
| 89 | return CLoader.__getattr__(self, name) |
---|