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

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c0ca2e3d was 959eb01, checked in by ajj, 8 years ago

normalising line endings

  • Property mode set to 100644
File size: 1.9 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.