[beba407] | 1 | """ |
---|
[b09095a] | 2 | This is the base file reader class most file readers should inherit from. |
---|
[beba407] | 3 | All generic functionality required for a file loader/reader is built into this |
---|
| 4 | class |
---|
| 5 | """ |
---|
| 6 | |
---|
| 7 | import os |
---|
| 8 | import logging |
---|
[b09095a] | 9 | import numpy as np |
---|
[beba407] | 10 | from abc import abstractmethod |
---|
| 11 | from loader_exceptions import NoKnownLoaderException, FileContentsException,\ |
---|
| 12 | DataReaderException |
---|
| 13 | from data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D,\ |
---|
| 14 | combine_data_info_with_plottable |
---|
| 15 | |
---|
| 16 | logger = logging.getLogger(__name__) |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | class FileReader(object): |
---|
| 20 | # List of Data1D and Data2D objects to be sent back to data_loader |
---|
| 21 | output = [] |
---|
[b09095a] | 22 | # Current plottable_(1D/2D) object being loaded in |
---|
[beba407] | 23 | current_dataset = None |
---|
[b09095a] | 24 | # Current DataInfo object being loaded in |
---|
[beba407] | 25 | current_datainfo = None |
---|
[b09095a] | 26 | # String to describe the type of data this reader can load |
---|
| 27 | type_name = "ASCII" |
---|
| 28 | # Wildcards to display |
---|
| 29 | type = ["Text files (*.txt|*.TXT)"] |
---|
[beba407] | 30 | # List of allowed extensions |
---|
| 31 | ext = ['.txt'] |
---|
| 32 | # Bypass extension check and try to load anyway |
---|
| 33 | allow_all = False |
---|
[b09095a] | 34 | # Able to import the unit converter |
---|
| 35 | has_converter = True |
---|
| 36 | # Open file handle |
---|
| 37 | f_open = None |
---|
| 38 | # Default value of zero |
---|
| 39 | _ZERO = 1e-16 |
---|
[beba407] | 40 | |
---|
| 41 | def read(self, filepath): |
---|
| 42 | """ |
---|
| 43 | Basic file reader |
---|
| 44 | |
---|
| 45 | :param filepath: The full or relative path to a file to be loaded |
---|
| 46 | """ |
---|
| 47 | if os.path.isfile(filepath): |
---|
| 48 | basename, extension = os.path.splitext(os.path.basename(filepath)) |
---|
| 49 | # If the file type is not allowed, return nothing |
---|
| 50 | if extension in self.ext or self.allow_all: |
---|
| 51 | # Try to load the file, but raise an error if unable to. |
---|
| 52 | try: |
---|
[b09095a] | 53 | self.unit_converter() |
---|
| 54 | self.f_open = open(filepath, 'rb') |
---|
| 55 | self.get_file_contents() |
---|
| 56 | self.sort_one_d_data() |
---|
[beba407] | 57 | except RuntimeError: |
---|
[b09095a] | 58 | # Reader specific errors |
---|
| 59 | # TODO: Give a specific error. |
---|
[beba407] | 60 | pass |
---|
| 61 | except OSError as e: |
---|
[b09095a] | 62 | # If the file cannot be opened |
---|
[beba407] | 63 | msg = "Unable to open file: {}\n".format(filepath) |
---|
| 64 | msg += e.message |
---|
| 65 | self.handle_error_message(msg) |
---|
| 66 | except Exception as e: |
---|
[b09095a] | 67 | # Handle any other generic error |
---|
| 68 | # TODO: raise or log? |
---|
| 69 | raise |
---|
| 70 | finally: |
---|
| 71 | if not self.f_open.closed: |
---|
| 72 | self.f_open.close() |
---|
[beba407] | 73 | else: |
---|
| 74 | msg = "Unable to find file at: {}\n".format(filepath) |
---|
| 75 | msg += "Please check your file path and try again." |
---|
| 76 | self.handle_error_message(msg) |
---|
[b09095a] | 77 | # Return a list of parsed entries that data_loader can manage |
---|
[beba407] | 78 | return self.output |
---|
| 79 | |
---|
| 80 | def handle_error_message(self, msg): |
---|
| 81 | """ |
---|
| 82 | Generic error handler to add an error to the current datainfo to |
---|
| 83 | propogate the error up the error chain. |
---|
| 84 | :param msg: Error message |
---|
| 85 | """ |
---|
| 86 | if isinstance(self.current_datainfo, DataInfo): |
---|
| 87 | self.current_datainfo.errors.append(msg) |
---|
| 88 | else: |
---|
| 89 | logger.warning(msg) |
---|
| 90 | |
---|
| 91 | def send_to_output(self): |
---|
| 92 | """ |
---|
| 93 | Helper that automatically combines the info and set and then appends it |
---|
| 94 | to output |
---|
| 95 | """ |
---|
| 96 | data_obj = combine_data_info_with_plottable(self.current_dataset, |
---|
| 97 | self.current_datainfo) |
---|
| 98 | self.output.append(data_obj) |
---|
| 99 | |
---|
[b09095a] | 100 | def unit_converter(self): |
---|
| 101 | """ |
---|
| 102 | Generic unit conversion import |
---|
| 103 | """ |
---|
| 104 | # Check whether we have a converter available |
---|
| 105 | self.has_converter = True |
---|
| 106 | try: |
---|
| 107 | from sas.sascalc.data_util.nxsunit import Converter |
---|
| 108 | except: |
---|
| 109 | self.has_converter = False |
---|
| 110 | |
---|
| 111 | def sort_one_d_data(self): |
---|
| 112 | """ |
---|
| 113 | Sort 1D data along the X axis for consistency |
---|
| 114 | """ |
---|
| 115 | final_list = [] |
---|
| 116 | for data in self.output: |
---|
| 117 | if isinstance(data, Data1D): |
---|
| 118 | ind = np.lexsort((data.y, data.x)) |
---|
| 119 | data.x = np.asarray([data.x[i] for i in ind]) |
---|
| 120 | data.y = np.asarray([data.y[i] for i in ind]) |
---|
| 121 | if data.dx is not None: |
---|
| 122 | data.dx = np.asarray([data.dx[i] for i in ind]) |
---|
| 123 | if data.dxl is not None: |
---|
| 124 | data.dxl = np.asarray([data.dxl[i] for i in ind]) |
---|
| 125 | if data.dxw is not None: |
---|
| 126 | data.dxw = np.asarray([data.dxw[i] for i in ind]) |
---|
| 127 | if data.dy is not None: |
---|
| 128 | data.dy = np.asarray([data.dy[i] for i in ind]) |
---|
| 129 | if data.lam is not None: |
---|
| 130 | data.lam = np.asarray([data.lam[i] for i in ind]) |
---|
| 131 | if data.dlam is not None: |
---|
| 132 | data.dlam = np.asarray([data.dlam[i] for i in ind]) |
---|
| 133 | final_list.append(data) |
---|
| 134 | self.output = final_list |
---|
| 135 | |
---|
| 136 | @staticmethod |
---|
| 137 | def splitline(line): |
---|
| 138 | """ |
---|
| 139 | Splits a line into pieces based on common delimeters |
---|
| 140 | :param line: A single line of text |
---|
| 141 | :return: list of values |
---|
| 142 | """ |
---|
| 143 | # Initial try for CSV (split on ,) |
---|
| 144 | toks = line.split(',') |
---|
| 145 | # Now try SCSV (split on ;) |
---|
| 146 | if len(toks) < 2: |
---|
| 147 | toks = line.split(';') |
---|
| 148 | # Now go for whitespace |
---|
| 149 | if len(toks) < 2: |
---|
| 150 | toks = line.split() |
---|
| 151 | return toks |
---|
| 152 | |
---|
[beba407] | 153 | @abstractmethod |
---|
[b09095a] | 154 | def get_file_contents(self): |
---|
[beba407] | 155 | """ |
---|
[b09095a] | 156 | All reader classes that inherit from FileReader must implement |
---|
[beba407] | 157 | """ |
---|
| 158 | pass |
---|