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