def get_file_contents(self):
     """
     Reader specific class to access the contents of the file
     All reader classes that inherit from FileReader must implement
     """
     x = np.zeros(0)
     y = np.zeros(0)
     self.current_dataset = plottable_1D(x, y)
     self.current_datainfo = DataInfo()
     self.current_datainfo.meta_data["blah"] = self.nextline()
     self.send_to_output()
Esempio n. 2
0
    def add_data_set(self, key=""):
        """
        Adds the current_dataset to the list of outputs after preforming final processing on the data and then calls a
        private method to generate a new data set.

        :param key: NeXus group name for current tree level
        """

        if self.current_datainfo and self.current_dataset:
            self.final_data_cleanup()
        self.data1d = []
        self.data2d = []
        self.current_datainfo = DataInfo()
Esempio n. 3
0
 def reset_state(self):
     self.current_dataset = plottable_1D(np.empty(0), np.empty(0), np.empty(0), np.empty(0))
     self.current_datainfo = DataInfo()
     self.datasets = []
     self.raw_data = None
     self.errors = set()
     self.logging = []
     self.output = []
     self.detector = Detector()
     self.collimation = Collimation()
     self.aperture = Aperture()
     self.process = Process()
     self.source = Source()
     self.sample = Sample()
     self.trans_spectrum = TransmissionSpectrum()
     self.upper = 5
     self.lower = 5
Esempio n. 4
0
    def get_file_contents(self):
        """
        Get the contents of the file
        """

        buff = self.readall()
        filepath = self.f_open.name
        lines = buff.splitlines()
        self.output = []
        self.current_datainfo = DataInfo()
        self.current_datainfo.filename = filepath
        self.reset_data_list(len(lines))

        # The first good line of data will define whether
        # we have 2-column or 3-column ascii
        has_error_dx = None
        has_error_dy = None

        # Initialize counters for data lines and header lines.
        is_data = False
        # More than "5" lines of data is considered as actual
        # To count # of current data candidate lines
        candidate_lines = 0
        # To count total # of previous data candidate lines
        candidate_lines_previous = 0
        # Current line number
        line_no = 0
        # minimum required number of columns of data
        lentoks = 2
        for line in lines:
            toks = self.splitline(line.strip())
            # To remember the number of columns in the current line of data
            new_lentoks = len(toks)
            try:
                if new_lentoks == 0:
                    # If the line is blank, skip and continue on
                    # In case of breaks within data sets.
                    continue
                elif new_lentoks != lentoks and is_data:
                    # If a footer is found, break the loop and save the data
                    break
                elif new_lentoks != lentoks and not is_data:
                    # If header lines are numerical
                    candidate_lines = 0
                    self.reset_data_list(len(lines) - line_no)

                self.current_dataset.x[candidate_lines] = float(toks[0])

                if new_lentoks > 1:
                    self.current_dataset.y[candidate_lines] = float(toks[1])

                # If a 3rd row is present, consider it dy
                if new_lentoks > 2:
                    self.current_dataset.dy[candidate_lines] = \
                        float(toks[2])
                    has_error_dy = True

                # If a 4th row is present, consider it dx
                if new_lentoks > 3:
                    self.current_dataset.dx[candidate_lines] = \
                        float(toks[3])
                    has_error_dx = True

                candidate_lines += 1
                # If 5 or more lines, this is considering the set data
                if candidate_lines >= self.min_data_pts:
                    is_data = True

                if is_data and new_lentoks >= 8:
                    msg = "This data looks like 2D ASCII data. Use the file "
                    msg += "converter tool to convert it to NXcanSAS."
                    raise FileContentsException(msg)

                # To remember the # of columns on the current line
                # for the next line of data
                lentoks = new_lentoks
                line_no += 1
            except ValueError:
                # ValueError is raised when non numeric strings conv. to float
                # It is data and meet non - number, then stop reading
                if is_data:
                    break
                # Delete the previously stored lines of data candidates if
                # the list is not data
                self.reset_data_list(len(lines) - line_no)
                lentoks = 2
                has_error_dx = None
                has_error_dy = None
                # Reset # of lines of data candidates
                candidate_lines = 0

        if not is_data:
            self.set_all_to_none()
            if self.extension in self.ext:
                msg = "ASCII Reader error: Fewer than five Q data points found "
                msg += "in {}.".format(filepath)
                raise FileContentsException(msg)
            else:
                msg = "ASCII Reader could not load the file {}".format(
                    filepath)
                raise DefaultReaderException(msg)
        # Sanity check
        if has_error_dy and not len(self.current_dataset.y) == \
                len(self.current_dataset.dy):
            msg = "ASCII Reader error: Number of I and dI data points are"
            msg += " different in {}.".format(filepath)
            # TODO: Add error to self.current_datainfo.errors instead?
            self.set_all_to_none()
            raise FileContentsException(msg)
        if has_error_dx and not len(self.current_dataset.x) == \
                len(self.current_dataset.dx):
            msg = "ASCII Reader error: Number of Q and dQ data points are"
            msg += " different in {}.".format(filepath)
            # TODO: Add error to self.current_datainfo.errors instead?
            self.set_all_to_none()
            raise FileContentsException(msg)

        self.remove_empty_q_values()
        self.current_dataset = self.set_default_1d_units(self.current_dataset)

        # Store loading process information
        self.current_datainfo.meta_data['loader'] = self.type_name
        self.send_to_output()