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
|
Rev | Line | |
---|
[daa56d0] | 1 | """ |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | See the license text in license.txt |
---|
| 7 | |
---|
| 8 | copyright 2008, University of Tennessee |
---|
| 9 | """ |
---|
[9a5097c] | 10 | import os |
---|
| 11 | import numpy as np |
---|
[b699768] | 12 | from sas.sascalc.dataloader.data_info import Data1D |
---|
[daa56d0] | 13 | |
---|
| 14 | |
---|
| 15 | class 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') |
---|
[9a5097c] | 43 | x = np.zeros(0) |
---|
| 44 | y = np.zeros(0) |
---|
| 45 | dy = np.zeros(0) |
---|
[daa56d0] | 46 | output = Data1D(x, y, dy=dy) |
---|
| 47 | self.filename = output.filename = basename |
---|
| 48 | |
---|
| 49 | for line in lines: |
---|
[9a5097c] | 50 | x = np.append(x, float(line)) |
---|
[daa56d0] | 51 | |
---|
| 52 | output.x = x |
---|
| 53 | return output |
---|
| 54 | else: |
---|
| 55 | raise RuntimeError, "%s is not a file" % path |
---|
| 56 | return None |
---|
| 57 | |
---|
| 58 | if __name__ == "__main__": |
---|
| 59 | reader = Reader() |
---|
| 60 | output = reader.read("../test/test_data.test") |
---|
| 61 | print output.x |
---|
| 62 | |
---|
| 63 | |
---|
[35ec279] | 64 | |
---|
Note: See
TracBrowser
for help on using the repository browser.