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