[8bcf866] | 1 | """ |
---|
| 2 | ASCII 2D Loader |
---|
| 3 | """ |
---|
| 4 | from sas.sascalc.dataloader.data_info import Data2D |
---|
| 5 | from sas.sascalc.file_converter.nxcansas_writer import NXcanSASWriter |
---|
| 6 | import numpy as np |
---|
| 7 | |
---|
| 8 | # ISIS 2D ASCII File Format |
---|
| 9 | # line: property |
---|
| 10 | # 0: File header |
---|
| 11 | # 1: q_x axis label and units |
---|
| 12 | # 2: q_y axis label and units |
---|
| 13 | # 3: Intensity axis label and units |
---|
| 14 | # 4: nUseRec - number of lines of user content following this line |
---|
| 15 | # 5 - 5+nUseRec: user content |
---|
| 16 | # Number of qx points |
---|
| 17 | # List of qx points |
---|
| 18 | # Number of qy points |
---|
| 19 | # List of qy points |
---|
| 20 | # numqx numqy scale |
---|
| 21 | |
---|
| 22 | class ASCII2DLoader(object): |
---|
| 23 | |
---|
| 24 | def __init__(self, data_path): |
---|
| 25 | self.data_path = data_path |
---|
| 26 | |
---|
| 27 | def load(self): |
---|
| 28 | file_handle = open(self.data_path, 'r') |
---|
| 29 | file_buffer = file_handle.read() |
---|
| 30 | all_lines = file_buffer.splitlines() |
---|
| 31 | |
---|
| 32 | def _load_qs(lines, start_line, num_points): |
---|
| 33 | qs = np.zeros(num_points) |
---|
| 34 | n = start_line |
---|
| 35 | filled = 0 |
---|
| 36 | while filled < num_points: |
---|
| 37 | row = np.fromstring(lines[n], dtype=np.float32, sep=' ') |
---|
| 38 | qs[filled:filled+len(row)] = row |
---|
| 39 | filled += len(row) |
---|
| 40 | n += 1 |
---|
| 41 | return n, qs |
---|
| 42 | |
---|
| 43 | # Skip nUseRec lines |
---|
| 44 | current_line = 4 |
---|
| 45 | nUseRec = int(all_lines[current_line].strip()[0]) |
---|
| 46 | current_line += nUseRec + 1 |
---|
| 47 | |
---|
| 48 | # Read qx data |
---|
| 49 | num_qs = int(all_lines[current_line].strip()) |
---|
| 50 | current_line += 1 |
---|
| 51 | current_line, qx = _load_qs(all_lines, current_line, num_qs) |
---|
| 52 | |
---|
| 53 | # Read qy data |
---|
| 54 | num_qs = int(all_lines[current_line].strip()) |
---|
| 55 | current_line += 1 |
---|
| 56 | current_line, qy = _load_qs(all_lines, current_line, num_qs) |
---|
| 57 | |
---|
| 58 | # dimensions: [width, height, scale] |
---|
| 59 | dimensions = np.fromstring(all_lines[current_line], dtype=np.float32, sep=' ') |
---|
| 60 | width = int(dimensions[0]) |
---|
| 61 | height = int(dimensions[1]) |
---|
| 62 | |
---|
| 63 | # More qx and/or qy points can be provided than are actually used |
---|
| 64 | qx = qx[:width] |
---|
| 65 | qy = qy[:height] |
---|
| 66 | |
---|
| 67 | current_line += 1 |
---|
| 68 | # iflag = 1 => Only intensity data (not dealt with here) |
---|
| 69 | # iflag = 2 => q axis and intensity data |
---|
| 70 | # iflag = 3 => q axis, intensity and error data |
---|
| 71 | iflag = int(all_lines[current_line].strip()[0]) |
---|
| 72 | current_line += 1 |
---|
| 73 | |
---|
| 74 | current_line, I = _load_qs(all_lines, current_line, width*height) |
---|
| 75 | dI = np.zeros(width*height) |
---|
| 76 | |
---|
| 77 | if iflag == 3: |
---|
| 78 | _, dI = _load_qs(all_lines, current_line, width*height) |
---|
| 79 | |
---|
| 80 | # Format data for use with Data2D |
---|
| 81 | qx = list(qx) * height |
---|
| 82 | qy = np.array([[y] * width for y in qy]).flatten() |
---|
| 83 | |
---|
| 84 | data = Data2D(qx_data=qx, qy_data=qy, data=I, err_data=dI) |
---|
| 85 | |
---|
| 86 | return data |
---|