source: sasview/DataLoader/plugins/test_reader.py @ 0a0624f

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 0a0624f was daa56d0, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Mostly completed DataLoader?. Reimplemented using REFL registry as base class. Added writing capability and dynamic loading of readers (need zip file support for future py2exe use). All tests pass.

  • Property mode set to 100644
File size: 2.0 KB
Line 
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
11from DataLoader.data_info import Data1D
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   
63                       
Note: See TracBrowser for help on using the repository browser.