[11a0319] | 1 | |
---|
[b99ac227] | 2 | |
---|
[0997158f] | 3 | ############################################################################ |
---|
| 4 | #This software was developed by the University of Tennessee as part of the |
---|
| 5 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 6 | #project funded by the US National Science Foundation. |
---|
| 7 | #If you use DANSE applications to do scientific research that leads to |
---|
| 8 | #publication, we ask that you acknowledge the use of the software with the |
---|
| 9 | #following sentence: |
---|
| 10 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
| 11 | #copyright 2008, University of Tennessee |
---|
| 12 | ############################################################################# |
---|
[b99ac227] | 13 | |
---|
[0997158f] | 14 | """ |
---|
| 15 | DANSE/SANS file reader |
---|
[b99ac227] | 16 | """ |
---|
| 17 | |
---|
[11a0319] | 18 | import math |
---|
[99d1af6] | 19 | import os |
---|
[a7a5886] | 20 | #import copy |
---|
[11a0319] | 21 | import numpy |
---|
[8d6440f] | 22 | import logging |
---|
[99d1af6] | 23 | from DataLoader.data_info import Data2D, Detector |
---|
[eadf1f9] | 24 | from DataLoader.manipulations import reader2D_converter |
---|
[99d1af6] | 25 | |
---|
| 26 | # Look for unit converter |
---|
| 27 | has_converter = True |
---|
| 28 | try: |
---|
| 29 | from data_util.nxsunit import Converter |
---|
| 30 | except: |
---|
| 31 | has_converter = False |
---|
[16d8e5f] | 32 | |
---|
[bb03739] | 33 | class Reader: |
---|
[11a0319] | 34 | """ |
---|
[0997158f] | 35 | Example data manipulation |
---|
[11a0319] | 36 | """ |
---|
| 37 | ## File type |
---|
[28caa03] | 38 | type_name = "DANSE" |
---|
| 39 | ## Wildcards |
---|
[11a0319] | 40 | type = ["DANSE files (*.sans)|*.sans"] |
---|
| 41 | ## Extension |
---|
[b99ac227] | 42 | ext = ['.sans', '.SANS'] |
---|
[11a0319] | 43 | |
---|
| 44 | def read(self, filename=None): |
---|
| 45 | """ |
---|
[0997158f] | 46 | Open and read the data in a file |
---|
| 47 | @param file: path of the file |
---|
[11a0319] | 48 | """ |
---|
| 49 | |
---|
| 50 | read_it = False |
---|
| 51 | for item in self.ext: |
---|
[a7a5886] | 52 | if filename.lower().find(item) >= 0: |
---|
[11a0319] | 53 | read_it = True |
---|
| 54 | |
---|
| 55 | if read_it: |
---|
[8d6440f] | 56 | try: |
---|
[a7a5886] | 57 | datafile = open(filename, 'r') |
---|
[8d6440f] | 58 | except : |
---|
[a7a5886] | 59 | raise RuntimeError,"danse_reader cannot open %s" % (filename) |
---|
| 60 | |
---|
[11a0319] | 61 | # defaults |
---|
| 62 | # wavelength in Angstrom |
---|
| 63 | wavelength = 10.0 |
---|
| 64 | # Distance in meter |
---|
| 65 | distance = 11.0 |
---|
| 66 | # Pixel number of center in x |
---|
| 67 | center_x = 65 |
---|
| 68 | # Pixel number of center in y |
---|
| 69 | center_y = 65 |
---|
| 70 | # Pixel size [mm] |
---|
| 71 | pixel = 5.0 |
---|
| 72 | # Size in x, in pixels |
---|
| 73 | size_x = 128 |
---|
| 74 | # Size in y, in pixels |
---|
| 75 | size_y = 128 |
---|
| 76 | # Format version |
---|
| 77 | fversion = 1.0 |
---|
| 78 | |
---|
[99d1af6] | 79 | output = Data2D() |
---|
| 80 | output.filename = os.path.basename(filename) |
---|
| 81 | detector = Detector() |
---|
| 82 | output.detector.append(detector) |
---|
| 83 | |
---|
| 84 | output.data = numpy.zeros([size_x,size_y]) |
---|
[a7a5886] | 85 | output.err_data = numpy.zeros([size_x, size_y]) |
---|
[99d1af6] | 86 | |
---|
| 87 | data_conv_q = None |
---|
| 88 | data_conv_i = None |
---|
| 89 | |
---|
[ca10d8e] | 90 | if has_converter == True and output.Q_unit != '1/A': |
---|
| 91 | data_conv_q = Converter('1/A') |
---|
[99d1af6] | 92 | # Test it |
---|
| 93 | data_conv_q(1.0, output.Q_unit) |
---|
| 94 | |
---|
[ca10d8e] | 95 | if has_converter == True and output.I_unit != '1/cm': |
---|
| 96 | data_conv_i = Converter('1/cm') |
---|
[99d1af6] | 97 | # Test it |
---|
| 98 | data_conv_i(1.0, output.I_unit) |
---|
| 99 | |
---|
[11a0319] | 100 | read_on = True |
---|
| 101 | while read_on: |
---|
| 102 | line = datafile.readline() |
---|
[a7a5886] | 103 | if line.find("DATA:") >= 0: |
---|
[11a0319] | 104 | read_on = False |
---|
| 105 | break |
---|
| 106 | toks = line.split(':') |
---|
[a7a5886] | 107 | if toks[0] == "FORMATVERSION": |
---|
[11a0319] | 108 | fversion = float(toks[1]) |
---|
[a7a5886] | 109 | if toks[0] == "WAVELENGTH": |
---|
[99d1af6] | 110 | wavelength = float(toks[1]) |
---|
[a7a5886] | 111 | elif toks[0] == "DISTANCE": |
---|
[11a0319] | 112 | distance = float(toks[1]) |
---|
[a7a5886] | 113 | elif toks[0] == "CENTER_X": |
---|
[11a0319] | 114 | center_x = float(toks[1]) |
---|
[a7a5886] | 115 | elif toks[0] == "CENTER_Y": |
---|
[11a0319] | 116 | center_y = float(toks[1]) |
---|
[a7a5886] | 117 | elif toks[0] == "PIXELSIZE": |
---|
[11a0319] | 118 | pixel = float(toks[1]) |
---|
[a7a5886] | 119 | elif toks[0] == "SIZE_X": |
---|
[11a0319] | 120 | size_x = int(toks[1]) |
---|
[a7a5886] | 121 | elif toks[0] == "SIZE_Y": |
---|
[11a0319] | 122 | size_y = int(toks[1]) |
---|
| 123 | |
---|
| 124 | # Read the data |
---|
| 125 | data = [] |
---|
| 126 | error = [] |
---|
[a7a5886] | 127 | if fversion == 1.0: |
---|
[11a0319] | 128 | data_str = datafile.readline() |
---|
| 129 | data = data_str.split(' ') |
---|
| 130 | else: |
---|
| 131 | read_on = True |
---|
| 132 | while read_on: |
---|
| 133 | data_str = datafile.readline() |
---|
[a7a5886] | 134 | if len(data_str) == 0: |
---|
[11a0319] | 135 | read_on = False |
---|
| 136 | else: |
---|
| 137 | toks = data_str.split() |
---|
| 138 | try: |
---|
| 139 | val = float(toks[0]) |
---|
| 140 | err = float(toks[1]) |
---|
[99d1af6] | 141 | if data_conv_i is not None: |
---|
| 142 | val = data_conv_i(val, units=output.y_unit) |
---|
| 143 | err = data_conv_i(err, units=output.y_unit) |
---|
[11a0319] | 144 | data.append(val) |
---|
| 145 | error.append(err) |
---|
| 146 | except: |
---|
[a7a5886] | 147 | logging.info("Skipping line:%s,%s" %( data_str, |
---|
| 148 | sys.exc_value)) |
---|
[11a0319] | 149 | |
---|
| 150 | # Initialize |
---|
| 151 | x_vals = [] |
---|
| 152 | y_vals = [] |
---|
| 153 | ymin = None |
---|
| 154 | ymax = None |
---|
| 155 | xmin = None |
---|
| 156 | xmax = None |
---|
| 157 | |
---|
| 158 | # Qx and Qy vectors |
---|
[4fe4394] | 159 | theta = pixel / distance / 100.0 |
---|
[a7a5886] | 160 | stepq = 4.0 * math.pi/wavelength * math.sin(theta/2.0) |
---|
[11a0319] | 161 | for i_x in range(size_x): |
---|
[a7a5886] | 162 | theta = (i_x - center_x + 1) * pixel / distance / 100.0 |
---|
| 163 | qx = 4.0 * math.pi / wavelength * math.sin(theta/2.0) |
---|
[99d1af6] | 164 | |
---|
[ca10d8e] | 165 | if has_converter == True and output.Q_unit != '1/A': |
---|
[99d1af6] | 166 | qx = data_conv_q(qx, units=output.Q_unit) |
---|
| 167 | |
---|
[11a0319] | 168 | x_vals.append(qx) |
---|
[a7a5886] | 169 | if xmin == None or qx < xmin: |
---|
[11a0319] | 170 | xmin = qx |
---|
[a7a5886] | 171 | if xmax == None or qx > xmax: |
---|
[11a0319] | 172 | xmax = qx |
---|
| 173 | |
---|
| 174 | ymin = None |
---|
| 175 | ymax = None |
---|
| 176 | for i_y in range(size_y): |
---|
[a7a5886] | 177 | theta = (i_y - center_y + 1) * pixel / distance / 100.0 |
---|
| 178 | qy = 4.0 * math.pi/wavelength * math.sin(theta/2.0) |
---|
[99d1af6] | 179 | |
---|
[ca10d8e] | 180 | if has_converter == True and output.Q_unit != '1/A': |
---|
[99d1af6] | 181 | qy = data_conv_q(qy, units=output.Q_unit) |
---|
| 182 | |
---|
[11a0319] | 183 | y_vals.append(qy) |
---|
[a7a5886] | 184 | if ymin == None or qy < ymin: |
---|
[11a0319] | 185 | ymin = qy |
---|
[a7a5886] | 186 | if ymax == None or qy > ymax: |
---|
[11a0319] | 187 | ymax = qy |
---|
| 188 | |
---|
[99d1af6] | 189 | # Store the data in the 2D array |
---|
[b99ac227] | 190 | itot = 0 |
---|
| 191 | i_x = 0 |
---|
| 192 | i_y = -1 |
---|
| 193 | |
---|
[11a0319] | 194 | for i_pt in range(len(data)): |
---|
| 195 | try: |
---|
[99d1af6] | 196 | value = float(data[i_pt]) |
---|
[11a0319] | 197 | except: |
---|
[99d1af6] | 198 | # For version 1.0, the data were still |
---|
| 199 | # stored as strings at this point. |
---|
[a7a5886] | 200 | msg = "Skipping entry (v1.0):%s,%s" %(str(data[i_pt]), |
---|
| 201 | sys.exc_value) |
---|
| 202 | logging.info(msg) |
---|
[11a0319] | 203 | |
---|
| 204 | # Get bin number |
---|
[a7a5886] | 205 | if math.fmod(i_pt, size_x) == 0: |
---|
[11a0319] | 206 | i_x = 0 |
---|
| 207 | i_y += 1 |
---|
| 208 | else: |
---|
| 209 | i_x += 1 |
---|
| 210 | |
---|
[b99ac227] | 211 | output.data[i_y][i_x] = value |
---|
| 212 | #output.data[size_y-1-i_y][i_x] = value |
---|
[11a0319] | 213 | if fversion>1.0: |
---|
[b99ac227] | 214 | output.err_data[i_y][i_x] = error[i_pt] |
---|
| 215 | #output.err_data[size_y-1-i_y][i_x] = error[i_pt] |
---|
[11a0319] | 216 | |
---|
[a7a5886] | 217 | |
---|
[0997158f] | 218 | # Store all data |
---|
[99d1af6] | 219 | # Store wavelength |
---|
[a7a5886] | 220 | if has_converter == True and output.source.wavelength_unit != 'A': |
---|
[99d1af6] | 221 | conv = Converter('A') |
---|
[a7a5886] | 222 | wavelength = conv(wavelength, |
---|
| 223 | units=output.source.wavelength_unit) |
---|
[99d1af6] | 224 | output.source.wavelength = wavelength |
---|
| 225 | |
---|
| 226 | # Store distance |
---|
[a7a5886] | 227 | if has_converter == True and detector.distance_unit != 'm': |
---|
[99d1af6] | 228 | conv = Converter('m') |
---|
| 229 | distance = conv(distance, units=detector.distance_unit) |
---|
| 230 | detector.distance = distance |
---|
| 231 | |
---|
| 232 | # Store pixel size |
---|
[a7a5886] | 233 | if has_converter == True and detector.pixel_size_unit != 'mm': |
---|
[99d1af6] | 234 | conv = Converter('mm') |
---|
| 235 | pixel = conv(pixel, units=detector.pixel_size_unit) |
---|
| 236 | detector.pixel_size.x = pixel |
---|
| 237 | detector.pixel_size.y = pixel |
---|
| 238 | |
---|
| 239 | # Store beam center in distance units |
---|
| 240 | detector.beam_center.x = center_x*pixel |
---|
| 241 | detector.beam_center.y = center_y*pixel |
---|
| 242 | |
---|
| 243 | # Store limits of the image (2D array) |
---|
[a7a5886] | 244 | xmin = xmin - stepq / 2.0 |
---|
| 245 | xmax = xmax + stepq / 2.0 |
---|
| 246 | ymin = ymin - stepq /2.0 |
---|
| 247 | ymax = ymax + stepq / 2.0 |
---|
[4fe4394] | 248 | |
---|
[ca10d8e] | 249 | if has_converter == True and output.Q_unit != '1/A': |
---|
[99d1af6] | 250 | xmin = data_conv_q(xmin, units=output.Q_unit) |
---|
| 251 | xmax = data_conv_q(xmax, units=output.Q_unit) |
---|
| 252 | ymin = data_conv_q(ymin, units=output.Q_unit) |
---|
| 253 | ymax = data_conv_q(ymax, units=output.Q_unit) |
---|
| 254 | output.xmin = xmin |
---|
| 255 | output.xmax = xmax |
---|
| 256 | output.ymin = ymin |
---|
| 257 | output.ymax = ymax |
---|
| 258 | |
---|
| 259 | # Store x and y axis bin centers |
---|
| 260 | output.x_bins = x_vals |
---|
| 261 | output.y_bins = y_vals |
---|
| 262 | |
---|
| 263 | # Units |
---|
| 264 | if data_conv_q is not None: |
---|
[eeaabb1] | 265 | output.xaxis("\\rm{Q_{x}}", output.Q_unit) |
---|
| 266 | output.yaxis("\\rm{Q_{y}}", output.Q_unit) |
---|
[99d1af6] | 267 | else: |
---|
[eeaabb1] | 268 | output.xaxis("\\rm{Q_{x}}", 'A^{-1}') |
---|
| 269 | output.yaxis("\\rm{Q_{y}}", 'A^{-1}') |
---|
[99d1af6] | 270 | |
---|
| 271 | if data_conv_i is not None: |
---|
[0e2aa40] | 272 | output.zaxis("\\rm{Intensity}", output.I_unit) |
---|
[99d1af6] | 273 | else: |
---|
[0e2aa40] | 274 | output.zaxis("\\rm{Intensity}","cm^{-1}") |
---|
[16d8e5f] | 275 | |
---|
[a7a5886] | 276 | if not fversion >= 1.0: |
---|
| 277 | msg = "Danse_reader can't read this file %s" % filename |
---|
| 278 | raise ValueError, msg |
---|
[11a0319] | 279 | else: |
---|
[a7a5886] | 280 | logging.info("Danse_reader Reading %s \n" % filename) |
---|
[fe78c7b] | 281 | |
---|
| 282 | # Store loading process information |
---|
| 283 | output.meta_data['loader'] = self.type_name |
---|
[eadf1f9] | 284 | output = reader2D_converter(output) |
---|
[fe78c7b] | 285 | return output |
---|
[11a0319] | 286 | |
---|
| 287 | return None |
---|
[99d1af6] | 288 | |
---|
| 289 | if __name__ == "__main__": |
---|
| 290 | reader = Reader() |
---|
| 291 | print reader.read("../test/MP_New.sans") |
---|
| 292 | |
---|