source: sasview/test/sasdataloader/plugins/test_reader.py @ c721b1a

Last change on this file since c721b1a was b699768, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Initial commit of the refactored SasCalc? module.

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[daa56d0]1"""
2This software was developed by the University of Tennessee as part of the
3Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4project funded by the US National Science Foundation.
5
6See the license text in license.txt
7
8copyright 2008, University of Tennessee
9"""
10import numpy, os
[b699768]11from sas.sascalc.dataloader.data_info import Data1D
[daa56d0]12
13
14class Reader:
15    """
16        Test reader to check plugin functionality
17    """
18    ## File type
19    type = ["ASCII files (*.test)|*.test"]
20    ## List of allowed extensions
21    ext=['.test'] 
22   
23    def read(self, path):
24        """
25            Load data file
26           
27            @param path: file path
28            @return: Data1D object, or None
29            @raise RuntimeError: when the file can't be opened
30            @raise ValueError: when the length of the data vectors are inconsistent
31        """
32        if os.path.isfile(path):
33            basename  = os.path.basename(path)
34            root, extension = os.path.splitext(basename)
35            if extension.lower() in self.ext:
36                try:
37                    input_f =  open(path,'r')
38                except :
39                    raise  RuntimeError, "ascii_reader: cannot open %s" % path
40                buff = input_f.read()
41                lines = buff.split('\n')
42                x  = numpy.zeros(0)
43                y  = numpy.zeros(0)
44                dy = numpy.zeros(0)
45                output = Data1D(x, y, dy=dy)
46                self.filename = output.filename = basename
47           
48                for line in lines:
49                    x  = numpy.append(x,  float(line)) 
50                   
51                output.x = x
52                return output
53        else:
54            raise RuntimeError, "%s is not a file" % path
55        return None
56   
57if __name__ == "__main__": 
58    reader = Reader()
59    output = reader.read("../test/test_data.test")
60    print output.x
61   
62   
[35ec279]63                       
Note: See TracBrowser for help on using the repository browser.