[7d6351e] | 1 | """ |
---|
| 2 | HFIR 1D 4-column data reader |
---|
| 3 | """ |
---|
[0997158f] | 4 | ##################################################################### |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[7d6351e] | 7 | #project funded by the US National Science Foundation. |
---|
[0997158f] | 8 | #See the license text in license.txt |
---|
| 9 | #copyright 2008, University of Tennessee |
---|
| 10 | ###################################################################### |
---|
[bee885e] | 11 | import numpy |
---|
| 12 | import os |
---|
[b699768] | 13 | from sas.sascalc.dataloader.data_info import Data1D |
---|
[bee885e] | 14 | |
---|
| 15 | # Check whether we have a converter available |
---|
| 16 | has_converter = True |
---|
| 17 | try: |
---|
[b699768] | 18 | from sas.sascalc.data_util.nxsunit import Converter |
---|
[bee885e] | 19 | except: |
---|
| 20 | has_converter = False |
---|
| 21 | |
---|
| 22 | class Reader(object): |
---|
| 23 | """ |
---|
[0997158f] | 24 | Class to load HFIR 1D 4-column files |
---|
[bee885e] | 25 | """ |
---|
| 26 | ## File type |
---|
[7d6351e] | 27 | type_name = "HFIR 1D" |
---|
[28caa03] | 28 | ## Wildcards |
---|
[bee885e] | 29 | type = ["HFIR 1D files (*.d1d)|*.d1d"] |
---|
| 30 | ## List of allowed extensions |
---|
[7d6351e] | 31 | ext = ['.d1d'] |
---|
[bee885e] | 32 | |
---|
| 33 | def read(self, path): |
---|
| 34 | """ |
---|
[0997158f] | 35 | Load data file |
---|
| 36 | |
---|
| 37 | :param path: file path |
---|
| 38 | |
---|
| 39 | :return: Data1D object, or None |
---|
| 40 | |
---|
| 41 | :raise RuntimeError: when the file can't be opened |
---|
| 42 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
[bee885e] | 43 | """ |
---|
| 44 | if os.path.isfile(path): |
---|
[7d6351e] | 45 | basename = os.path.basename(path) |
---|
[bee885e] | 46 | root, extension = os.path.splitext(basename) |
---|
| 47 | if extension.lower() in self.ext: |
---|
| 48 | try: |
---|
[7d6351e] | 49 | input_f = open(path,'r') |
---|
| 50 | except: |
---|
[bee885e] | 51 | raise RuntimeError, "hfir1d_reader: cannot open %s" % path |
---|
| 52 | buff = input_f.read() |
---|
| 53 | lines = buff.split('\n') |
---|
[7d6351e] | 54 | x = numpy.zeros(0) |
---|
| 55 | y = numpy.zeros(0) |
---|
[bee885e] | 56 | dx = numpy.zeros(0) |
---|
| 57 | dy = numpy.zeros(0) |
---|
| 58 | output = Data1D(x, y, dx=dx, dy=dy) |
---|
| 59 | self.filename = output.filename = basename |
---|
| 60 | |
---|
| 61 | data_conv_q = None |
---|
| 62 | data_conv_i = None |
---|
| 63 | |
---|
[ca10d8e] | 64 | if has_converter == True and output.x_unit != '1/A': |
---|
| 65 | data_conv_q = Converter('1/A') |
---|
[bee885e] | 66 | # Test it |
---|
| 67 | data_conv_q(1.0, output.x_unit) |
---|
| 68 | |
---|
[ca10d8e] | 69 | if has_converter == True and output.y_unit != '1/cm': |
---|
| 70 | data_conv_i = Converter('1/cm') |
---|
[bee885e] | 71 | # Test it |
---|
| 72 | data_conv_i(1.0, output.y_unit) |
---|
| 73 | |
---|
| 74 | for line in lines: |
---|
| 75 | toks = line.split() |
---|
| 76 | try: |
---|
| 77 | _x = float(toks[0]) |
---|
| 78 | _y = float(toks[1]) |
---|
| 79 | _dx = float(toks[3]) |
---|
| 80 | _dy = float(toks[2]) |
---|
| 81 | |
---|
| 82 | if data_conv_q is not None: |
---|
| 83 | _x = data_conv_q(_x, units=output.x_unit) |
---|
| 84 | _dx = data_conv_q(_dx, units=output.x_unit) |
---|
| 85 | |
---|
| 86 | if data_conv_i is not None: |
---|
[7d6351e] | 87 | _y = data_conv_i(_y, units=output.y_unit) |
---|
| 88 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
[bee885e] | 89 | |
---|
[7d6351e] | 90 | x = numpy.append(x, _x) |
---|
| 91 | y = numpy.append(y, _y) |
---|
| 92 | dx = numpy.append(dx, _dx) |
---|
| 93 | dy = numpy.append(dy, _dy) |
---|
[bee885e] | 94 | except: |
---|
| 95 | # Couldn't parse this line, skip it |
---|
| 96 | pass |
---|
| 97 | |
---|
| 98 | # Sanity check |
---|
| 99 | if not len(y) == len(dy): |
---|
[a7a5886] | 100 | msg = "hfir1d_reader: y and dy have different length" |
---|
| 101 | raise RuntimeError, msg |
---|
[bee885e] | 102 | if not len(x) == len(dx): |
---|
[a7a5886] | 103 | msg = "hfir1d_reader: x and dx have different length" |
---|
| 104 | raise RuntimeError, msg |
---|
[bee885e] | 105 | |
---|
| 106 | # If the data length is zero, consider this as |
---|
| 107 | # though we were not able to read the file. |
---|
[a7a5886] | 108 | if len(x) == 0: |
---|
[bee885e] | 109 | raise RuntimeError, "hfir1d_reader: could not load file" |
---|
| 110 | |
---|
| 111 | output.x = x |
---|
| 112 | output.y = y |
---|
| 113 | output.dy = dy |
---|
| 114 | output.dx = dx |
---|
| 115 | if data_conv_q is not None: |
---|
| 116 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
| 117 | else: |
---|
| 118 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 119 | if data_conv_i is not None: |
---|
[0e2aa40] | 120 | output.yaxis("\\rm{Intensity}", output.y_unit) |
---|
[bee885e] | 121 | else: |
---|
[7d6351e] | 122 | output.yaxis("\\rm{Intensity}", "cm^{-1}") |
---|
[bee885e] | 123 | |
---|
[fe78c7b] | 124 | # Store loading process information |
---|
[7d6351e] | 125 | output.meta_data['loader'] = self.type_name |
---|
[bee885e] | 126 | return output |
---|
| 127 | else: |
---|
| 128 | raise RuntimeError, "%s is not a file" % path |
---|
| 129 | return None |
---|