source: sasview/test/sasdataloader/plugins/test_reader.py @ 0765c00

Last change on this file since 0765c00 was 9a5097c, checked in by andyfaff, 8 years ago

MAINT: import numpy as np

  • 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 os
11import numpy as np
12from sas.sascalc.dataloader.data_info import Data1D
13
14
15class Reader:
16    """
17        Test reader to check plugin functionality
18    """
19    ## File type
20    type = ["ASCII files (*.test)|*.test"]
21    ## List of allowed extensions
22    ext=['.test'] 
23   
24    def read(self, path):
25        """
26            Load data file
27           
28            @param path: file path
29            @return: Data1D object, or None
30            @raise RuntimeError: when the file can't be opened
31            @raise ValueError: when the length of the data vectors are inconsistent
32        """
33        if os.path.isfile(path):
34            basename  = os.path.basename(path)
35            root, extension = os.path.splitext(basename)
36            if extension.lower() in self.ext:
37                try:
38                    input_f =  open(path,'r')
39                except :
40                    raise  RuntimeError, "ascii_reader: cannot open %s" % path
41                buff = input_f.read()
42                lines = buff.split('\n')
43                x  = np.zeros(0)
44                y  = np.zeros(0)
45                dy = np.zeros(0)
46                output = Data1D(x, y, dy=dy)
47                self.filename = output.filename = basename
48           
49                for line in lines:
50                    x  = np.append(x,  float(line))
51                   
52                output.x = x
53                return output
54        else:
55            raise RuntimeError, "%s is not a file" % path
56        return None
57   
58if __name__ == "__main__": 
59    reader = Reader()
60    output = reader.read("../test/test_data.test")
61    print output.x
62   
63   
64                       
Note: See TracBrowser for help on using the repository browser.