-
Notifications
You must be signed in to change notification settings - Fork 7
/
filehandling.py
361 lines (299 loc) · 11 KB
/
filehandling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from typing import Union, Iterable, List, Tuple
from pathlib import Path
import tarfile
import time
import numpy as np
from numpy import ndarray
from scipy.ndimage import gaussian_filter1d
def mama_read(filename: str) -> Union[Tuple[ndarray, ndarray],
Tuple[ndarray, ndarray, ndarray]]:
"""Read 1d and 2d mama spectra/matrices
Args:
filename (str): Filename of matrix/spectrum
Returns:
2 or 3 eleement tuple containing
- **counts** (*ndarray*): array of counts.
- **x_array** (*ndarray*): mid-bin energies of x axis.
- **y_array** (*ndarray, optional*): Returned only if input is 2d.
Mid-bin energies of y-axis.
Raises:
ValueError: If format is wrong, ie. if the calibrations line is
not as expected.
"""
counts = np.genfromtxt(filename, skip_header=10, skip_footer=1,
encoding="latin-1")
cal = {}
with open(filename, 'r', encoding='latin-1') as datafile:
calibration_line = datafile.readlines()[6].split(",")
if len(calibration_line) == 4:
ndim = 1
cal = {
"a0x": float(calibration_line[1]),
"a1x": float(calibration_line[2]),
"a2x": float(calibration_line[3]),
}
elif len(calibration_line) == 7:
ndim = 2
cal = {
"a0x": float(calibration_line[1]),
"a1x": float(calibration_line[2]),
"a2x": float(calibration_line[3]),
"a0y": float(calibration_line[4]),
"a1y": float(calibration_line[5]),
"a2y": float(calibration_line[6])
}
else:
raise ValueError("File format must be wrong or not implemented.\n"
"Check calibration line of the Mama file")
if ndim == 1:
Nx = counts.shape[0]
x_array = np.linspace(0, Nx - 1, Nx)
# Make arrays in center-bin calibration:
x_array = cal["a0x"] + cal["a1x"] * x_array + cal["a2x"] * x_array**2
# counts, E array
return counts, x_array
elif ndim == 2:
Ny, Nx = counts.shape
y_array = np.linspace(0, Ny - 1, Ny)
x_array = np.linspace(0, Nx - 1, Nx)
# Make arrays in center-bin calibration:
x_array = cal["a0x"] + cal["a1x"] * x_array + cal["a2x"] * x_array**2
y_array = cal["a0y"] + cal["a1y"] * y_array + cal["a2y"] * y_array**2
# counts, Eg array, Ex array
return counts, x_array, y_array
def mama_write(mat, filename, comment=""):
ndim = mat.values.ndim
if ndim == 1:
mama_write1D(mat, filename, comment)
elif ndim == 2:
mama_write2D(mat, filename, comment)
else:
NotImplementedError("Mama cannot read ojects with more then 2D.")
def mama_write1D(mat, filename, comment=""):
assert(mat.shape[0] <= 8192),\
"Mama cannot handle vectors with dimensions > 8192. "\
"Rebin before saving."
# Calculate calibration coefficients.
calibration = mat.calibration()
cal = {
"a0x": calibration['a0'],
"a1x": calibration['a1'],
"a2x": 0,
}
# Write mandatory header:
header_string = '!FILE=Disk \n'
header_string += '!KIND=Spectrum \n'
header_string += '!LABORATORY=Oslo Cyclotron Laboratory (OCL) \n'
header_string += '!EXPERIMENT= oslo_method_python \n'
header_string += '!COMMENT={:s} \n'.format(comment)
header_string += '!TIME=DATE:' + time.strftime("%d-%b-%y %H:%M:%S",
time.localtime()) + ' \n'
header_string += (
'!CALIBRATION EkeV=6, %12.6E, %12.6E, %12.6E \n'
% (
cal["a0x"],
cal["a1x"],
cal["a2x"],
))
header_string += '!PRECISION=16 \n'
header_string += "!DIMENSION=1,0:{:4d} \n".format(
mat.shape[0] - 1)
header_string += '!CHANNEL=(0:%4d) ' % (mat.shape[0] - 1)
footer_string = "!IDEND=\n"
# Write matrix:
np.savetxt(
filename,
mat.values,
fmt="%-17.8E",
delimiter=" ",
newline="\n",
header=header_string,
footer=footer_string,
comments="")
def mama_write2D(mat, filename, comment=""):
assert(mat.shape[0] <= 2048 and mat.shape[1] <= 2048),\
"Mama cannot handle matrixes with any of the dimensions > 2048. "\
"Rebin before saving."
# Calculate calibration coefficients.
calibration = mat.calibration()
cal = {
"a0x": calibration['a0y'],
"a1x": calibration['a1y'],
"a2x": 0,
"a0y": calibration['a0x'],
"a1y": calibration['a1x'],
"a2y": 0
}
# Write mandatory header:
header_string = '!FILE=Disk \n'
header_string += '!KIND=Spectrum \n'
header_string += '!LABORATORY=Oslo Cyclotron Laboratory (OCL) \n'
header_string += '!EXPERIMENT= oslo_method_python \n'
header_string += '!COMMENT={:s} \n'.format(comment)
header_string += '!TIME=DATE:' + time.strftime("%d-%b-%y %H:%M:%S",
time.localtime()) + ' \n'
header_string += (
'!CALIBRATION EkeV=6, %12.6E, %12.6E, %12.6E, %12.6E, %12.6E, %12.6E \n'
% (
cal["a0x"],
cal["a1x"],
cal["a2x"],
cal["a0y"],
cal["a1y"],
cal["a2y"],
))
header_string += '!PRECISION=16 \n'
header_string += "!DIMENSION=2,0:{:4d},0:{:4d} \n".format(
mat.shape[1] - 1, mat.shape[0] - 1)
header_string += '!CHANNEL=(0:%4d,0:%4d) ' % (mat.shape[1] - 1,
mat.shape[0] - 1)
footer_string = "!IDEND=\n"
# Write matrix:
np.savetxt(
filename,
mat.values,
fmt="%-17.8E",
delimiter=" ",
newline="\n",
header=header_string,
footer=footer_string,
comments="")
def read_response(fname_resp_mat, fname_resp_dat):
# Import response matrix
R, cal_R, Eg_array_R, tmp = mama_read(fname_resp_mat)
# We also need info from the resp.dat file:
resp = []
with open(fname_resp_dat) as file:
# Read line by line as there is crazyness in the file format
lines = file.readlines()
for i in range(4, len(lines)):
try:
row = np.array(lines[i].split(), dtype="double")
resp.append(row)
except:
break
resp = np.array(resp)
# Name the columns for ease of reading
FWHM = resp[:, 1] # *6.8 # Correct with fwhm @ 1.33 MeV?
eff = resp[:, 2]
pf = resp[:, 3]
pc = resp[:, 4]
ps = resp[:, 5]
pd = resp[:, 6]
pa = resp[:, 7]
return R, FWHM, eff, pc, pf, ps, pd, pa, Eg_array_R
def save_tar(objects: Union[np.ndarray, Iterable[np.ndarray]],
path: Union[str, Path]) -> None:
if isinstance(path, str):
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
tarpath = str(path) if path.suffix == '.tar' else str(path) + '.tar'
tar = tarfile.open(tarpath, 'w')
for num, object in enumerate(objects):
npath = Path(str(path)[:-len(path.suffix)] + str(num) + '.npy')
np.save(npath, object)
tar.add(npath)
npath.unlink()
tar.close()
def load_tar(path: Union[str, Path]) -> List[np.ndarray]:
if isinstance(path, str):
path = Path(path)
tarpath = str(path) if path.suffix == '.tar' else str(path) + '.tar'
tar = tarfile.open(tarpath)
objects = []
for name in tar.getnames():
tar.extract(name)
objects.append(np.load(name))
Path(name).unlink()
return objects
def save_numpy_2D(matrix: np.ndarray, Eg: np.ndarray,
Ex: np.ndarray, path: Union[str, Path]):
mat = np.empty((matrix.shape[0] + 1, matrix.shape[1] + 1))
mat[0, 1:] = Eg
mat[1:, 0] = Ex
mat[1:, 1:] = matrix
np.save(path, mat)
def load_numpy_2D(path: Union[str, Path]
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
mat = np.load(path)
return mat[1:, 1:], mat[0, 1:], mat[1:, 0]
def save_txt_2D(matrix: np.ndarray, Eg: np.ndarray,
Ex: np.ndarray, path: Union[str, Path],
header=None):
if header is None:
header = ("Format:\n"
" Eg0 Eg1 Eg2 ...\n"
"Ex0 val00 val01 val02\n"
"Ex1 val10 val11 ...\n"
"Ex2 ...\n"
"...")
elif header is False:
header = None
mat = np.empty((matrix.shape[0] + 1, matrix.shape[1] + 1))
mat[0, 0] = -0
mat[0, 1:] = Eg
mat[1:, 0] = Ex
mat[1:, 1:] = matrix
np.savetxt(path, mat, header=header)
def load_txt_2D(path: Union[str, Path]
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
mat = np.loadtxt(path)
return mat[1:, 1:], mat[0, 1:], mat[1:, 0]
def load_numpy_1D(path: Union[str, Path]) -> Tuple[np.ndarray, np.ndarray]:
vec = np.load(path)
E = vec[:, 0]
values = vec[:, 1]
return values, E
def save_numpy_1D(values: np.ndarray, E: np.ndarray,
path: Union[str, Path]) -> None:
mat = np.column_stack((E, values))
np.save(path, mat)
def load_txt_1D(path: Union[str, Path]) -> Tuple[np.ndarray, np.ndarray]:
vec = np.loadtxt(path)
E = vec[:, 0]
values = vec[:, 1]
return values, E
def save_txt_1D(values: np.ndarray, E: np.ndarray,
path: Union[str, Path], header='E[keV] values') -> None:
""" E default in keV """
mat = np.column_stack([E, values])
np.savetxt(path, mat, header=header)
def filetype_from_suffix(path: Path) -> str:
suffix = path.suffix
if suffix == '.tar':
return 'tar'
elif suffix == '.npy':
return 'numpy'
elif suffix == '.txt':
return 'txt'
elif suffix == '.m':
return 'mama'
else:
return "unknown"
def load_discrete(path: Union[str, Path], energy: ndarray,
resolution: float = 0.1) -> Tuple[ndarray, ndarray]:
"""Load discrete levels and apply smoothing
Assumes linear equdistant binning
Args:
path (Union[str, Path]): The file to load
energy (ndarray): The binning to use
resolution (float, optional): The resolution (FWHM) to apply to the
gaussian smoothing. Defaults to 0.1.
Returns:
Tuple[ndarray, ndarray]
"""
energies = np.loadtxt(path)
energies /= 1e3 # convert to MeV
if len(energies) > 1:
assert energies.mean() < 5, "Probably energies are not in keV"
binsize = energy[1] - energy[0]
bin_edges = np.append(energy, energy[-1] + binsize)
bin_edges -= binsize / 2
hist, _ = np.histogram(energies, bins=bin_edges)
hist = hist.astype(float) / binsize # convert to levels/MeV
if resolution > 0:
resolution /= 2.3548
smoothed = gaussian_filter1d(hist, sigma=resolution / binsize)
else:
smoothed = None
return hist, smoothed