forked from ngergihun/NeaReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeaSpectra.py
484 lines (397 loc) · 19.8 KB
/
NeaSpectra.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import numpy as np
import os
import copy
from scipy import signal
from scipy.fft import fft, fftshift
from matplotlib import pyplot as plt
from scipy.interpolate import CubicSpline, interp1d
import copy
class NeaSpectrum:
def __init__(self) -> None:
self.filename = None # Full path with name
# data from all the channels
self.data = {}
# Other parameters from info txt - Dictionary
self.parameters = {}
def readNeaSpectrum(self,filename):
# reader tested for neascan version 2.1.10719.0
self.filename = filename
fid = open(filename,errors='replace')
data = {}
params = {}
linestring = fid.readline()
Nlines = 1
while 'Row' not in linestring:
Nlines += 1
linestring = fid.readline()
if Nlines > 1:
ct = linestring.split('\t')
fieldname = ct[0][2:-1]
fieldname = fieldname.replace(' ', '')
if 'Scanner Center Position' in linestring:
fieldname = fieldname[:-5]
params[fieldname] = [float(ct[2]), float(ct[3])]
elif 'Scan Area' in linestring:
fieldname = fieldname[:-7]
params[fieldname] = [float(ct[2]), float(ct[3]), float(ct[4])]
elif 'Pixel Area' in linestring:
fieldname = fieldname[:-7]
params[fieldname] = [int(ct[2]), int(ct[3]), int(ct[4])]
elif 'Interferometer Center/Distance' in linestring:
fieldname = fieldname.replace('/', '')
params[fieldname] = [float(ct[2]), float(ct[3])]
elif 'Regulator' in linestring:
fieldname = fieldname[:-7]
params[fieldname] = [float(ct[2]), float(ct[3]), float(ct[4])]
elif 'Q-Factor' in linestring:
fieldname = fieldname.replace('-', '')
params[fieldname] = float(ct[2])
else:
fieldname = ct[0][2:-1]
fieldname = fieldname.replace(' ', '')
val = ct[2]
val = val.replace(',','')
try:
params[fieldname] = float(val)
except:
params[fieldname] = val.strip()
channels = linestring.split('\t')
self.parameters = params
fid.close()
if "PTE+" in params['Scan']:
C_data = np.genfromtxt(filename, skip_header=Nlines, encoding='utf-8')
else:
C_data = np.genfromtxt(filename, skip_header=Nlines)
for i in range(len(channels)-2):
if params['PixelArea'][1] == 1 and params['PixelArea'][0] == 1:
if "PTE+" in params['Scan']:
data[channels[i]] = np.reshape(C_data[:,i], (params['PixelArea'][2]))
else:
data[channels[i]] = np.reshape(C_data[:,i], (params['PixelArea'][2]*2))
else:
if "PTE+" in params['Scan']:
data[channels[i]] = np.reshape(C_data[:,i], (params['PixelArea'][0], params['PixelArea'][1], params['PixelArea'][2]))
else:
data[channels[i]] = np.reshape(C_data[:,i], (params['PixelArea'][0], params['PixelArea'][1], params['PixelArea'][2]*2))
self.data = data
def SaveSpectraToDAT(self,channelname):
fname = f'{self.filename[0:-4]}.dat'
M = np.array([self.data["Wavenumber"],self.data[channelname]])
np.savetxt(fname, M.T)
def linearSubtract(self,channelname,wn1,wn2):
if self.parameters['PixelArea'][0] == 1 and self.parameters['PixelArea'][1] == 1:
wnaxis = self.data["Wavenumber"]
data = self.data[channelname]
wn1idx = np.argmin(abs(wnaxis-wn1))
wn2idx = np.argmin(abs(wnaxis-wn2))
m = (data[wn2idx]-data[wn1idx])/(wnaxis[wn2idx]-wnaxis[wn1idx])
C = data[wn1idx]-m*wnaxis[wn1idx]
self.data[channelname] = data - (m*wnaxis + C)
else:
for i in range(self.parameters['PixelArea'][0]):
for k in range(self.parameters['PixelArea'][1]):
wnaxis = self.data["Wavenumber"][i,k,:]
data = self.data[channelname][i,k,:]
wn1idx = np.argmin(abs(wnaxis-wn1))
wn2idx = np.argmin(abs(wnaxis-wn2))
m = (data[wn2idx]-data[wn1idx])/(wnaxis[wn2idx]-wnaxis[wn1idx])
C = data[wn1idx]-m*wnaxis[wn1idx]
self.data[channelname][i,k,:] = data - (m*wnaxis + C)
def normalizeSpectrum(self, ref_spectrum, order = None, dounwrap = "True"):
# if order is not defined, go trough all the orders
if order == None:
for i in [0,1,2,3,4,5]:
channelA = f"O{i}A"
channelP = f"O{i}P"
for i in range(self.parameters['PixelArea'][0]):
for k in range(self.parameters['PixelArea'][1]):
self.data[channelA][i,k,:] = self.data[channelA][i,k,:]/ref_spectrum.data[channelA]
self.data[channelP][i,k,:] = self.data[channelP][i,k,:]-ref_spectrum.data[channelP]
if dounwrap:
self.data[channelP][i,k,:] = np.unwrap(self.data[channelP][i,k,:])
else:
channelA = f"O{order}A"
channelP = f"O{order}P"
# Check if this is juts a point spectrum
if self.parameters['PixelArea'][0] == 1 and self.parameters['PixelArea'][1] == 1:
self.data[channelA] = self.data[channelA]/ref_spectrum.data[channelA]
self.data[channelP] = self.data[channelP]-ref_spectrum.data[channelP]
if dounwrap:
self.data[channelP] = np.unwrap(self.data[channelP])
else:
for i in range(self.parameters['PixelArea'][0]):
for k in range(self.parameters['PixelArea'][1]):
self.data[channelA][i,k,:] = self.data[channelA][i,k,:]/ref_spectrum.data[channelA]
self.data[channelP][i,k,:] = self.data[channelP][i,k,:]-ref_spectrum.data[channelP]
if dounwrap:
self.data[channelP][i,k,:] = np.unwrap(self.data[channelP][i,k,:])
class NeaInterferogram:
def __init__(self) -> None:
self.filename = None # Full path with name
# data from all the channels
self.data = {}
# Other parameters from info txt - Dictionary
self.parameters = {}
def readNeaInterferogram(self,filename):
# reader tested for neascan version 2.1.10719.0
self.filename = filename
fid = open(filename,errors='replace')
data = {}
params = {}
linestring = fid.readline()
Nlines = 1
while 'Row' not in linestring:
Nlines += 1
linestring = fid.readline()
if Nlines > 1:
ct = linestring.split('\t')
fieldname = ct[0][2:-1]
fieldname = fieldname.replace(' ', '')
if 'Scanner Center Position' in linestring:
fieldname = fieldname[:-5]
params[fieldname] = [float(ct[2]), float(ct[3])]
elif 'Scan Area' in linestring:
fieldname = fieldname[:-7]
params[fieldname] = [float(ct[2]), float(ct[3]), float(ct[4])]
elif 'Pixel Area' in linestring:
fieldname = fieldname[:-7]
params[fieldname] = [int(ct[2]), int(ct[3]), int(ct[4])]
elif 'Averaging' in linestring:
# fieldname = fieldname[:-7]
params[fieldname] = int(ct[2])
elif 'Interferometer Center/Distance' in linestring:
fieldname = fieldname.replace('/', '')
params[fieldname] = [float(ct[2]), float(ct[3])]
elif 'Regulator' in linestring:
fieldname = fieldname[:-7]
params[fieldname] = [float(ct[2]), float(ct[3]), float(ct[4])]
elif 'Q-Factor' in linestring:
fieldname = fieldname.replace('-', '')
params[fieldname] = float(ct[2])
else:
fieldname = ct[0][2:-1]
fieldname = fieldname.replace(' ', '')
val = ct[2]
val = val.replace(',','')
try:
params[fieldname] = float(val)
except:
params[fieldname] = val.strip()
channels = linestring.split('\t')
self.parameters = params
fid.close()
C_data = np.genfromtxt(filename, skip_header=Nlines)
for i in range(len(channels)-2):
if params['PixelArea'][1] == 1 and params['PixelArea'][0] == 1:
data[channels[i]] = C_data[:,i]
else:
data[channels[i]] = np.reshape(C_data[:,i], (int(params['PixelArea'][0]), int(params['PixelArea'][1]), int(params['PixelArea'][2]*params['Averaging'])))
self.data = data
def reshapeSinglePointFromChannel(self,channel):
interferograms = self.data[channel]
# TODO error handling if params['PixelArea'][1] or params['PixelArea'][0] =! 1
return np.reshape(interferograms,(int(self.parameters['Averaging']),self.parameters['PixelArea'][2]))
def asymmetricWindow(self, npoints = 2048, centerindex = 1700, windowtype = "blackmanharris"):
"""
Calculates an asymmetric blackmann-harris window function with length given by *npoints*.
The center of the window is defined by *centerindex*
Parameters
----------
npoints : int
Length of the final window function
centerindex : int
Location index of the maximum of the window function.
If `n` is smaller than the length of the input, the input is cropped.
If it is larger, the input is padded with zeros. If `n` is not given,
the length of the input along the axis specified by `axis` is used.
Returns
-------
out : real-valued ndarray of the window function
Raises
------
See Also
--------
Notes
-----
References
----------
Examples
--------
"""
# Calculate the length of the two sides
length1 = (centerindex)*2
length2 = (npoints-centerindex)*2
# Generate the two parts of the window
windowfunc = getattr(signal.windows,windowtype)
windowFull1 = windowfunc(length1)
windowFull2 = windowfunc(length2)
# Construct the asymetric window from the two sides
asymWindow1 = windowFull1[0:int(len(windowFull1)/2)]
asymWindow2 = windowFull2[int(len(windowFull2)/2):int(len(windowFull2))]
asymWindowFull = np.concatenate((asymWindow1, asymWindow2))
return asymWindowFull
def processSingleInterferogram(self, ifg, maxis, windowtype = "blackmanharris", wlpindex = 1024, nzeros = 4, apod = True, autoidx = True):
# Find the location index of the WLP
if autoidx:
wlpindex = np.argmax(np.abs(ifg))
else:
pass
# Create apodization window
if apod:
w = self.asymmetricWindow(npoints = len(ifg), centerindex = wlpindex, windowtype = windowtype)
else:
w = np.ones(np.shape(ifg))
# if np.iscomplex(ifg).any():
# real_ifg = np.real(ifg)
# imag_ifg = np.imag(ifg)
# real_ifg = real_ifg - np.mean(real_ifg)
# imag_ifg = imag_ifg - np.mean(imag_ifg)
# ifg = real_ifg + imag_ifg*complex(1j)
# else:
ifg = ifg - np.mean(ifg)
# Calculate FFT
complex_spectrum = fftshift(fft(ifg*w, nzeros*len(ifg)))
# amplitude = np.abs(complex_spectrum)
# phase = np.angle(complex_spectrum)
# Calculate frequency axis
stepsizes = np.mean(np.diff(maxis*1e6))
Fs = 1/np.mean(stepsizes)
faxis = (Fs/2)*np.linspace(-1,1,len(complex_spectrum))*10000/2
# return amplitude[int(len(faxis)/2)-1:-1], phase[int(len(faxis)/2)-1:-1], faxis[int(len(faxis)/2)-1:-1]
return complex_spectrum[int(len(faxis)/2)-1:-1], faxis[int(len(faxis)/2)-1:-1]
def processPointInterferogram(self, simpleoutput = False, order = 2, windowtype = "blackmanharris", nzeros = 4, apod = True, method = "complex", interpmethod = "spline"):
# Load amplitude and phase of the given channel
channelA = f"O{order}A"
channelP = f"O{order}P"
ifgA = self.reshapeSinglePointFromChannel(channelA)
ifgP = self.reshapeSinglePointFromChannel(channelP)
# Load position data
Maxis = self.reshapeSinglePointFromChannel("M")
# Calculate the interferogram to process based on the given method
match method:
case "abs":
IFG = np.abs(ifgA*np.exp(ifgP*complex(1j)))
case "real":
IFG = np.real(ifgA*np.exp(ifgP*complex(1j)))
case "imag":
IFG = np.imag(ifgA*np.exp(ifgP*complex(1j)))
case "complex":
IFG = ifgA*np.exp(ifgP*complex(1j))
case "simple":
IFG = ifgA
# Interpolate
match method:
case "complex":
IFG, Maxis = self.interpolateSingleInterferogram(IFG,Maxis,method = interpmethod)
# realIFG, Maxis = self.interpolateSingleInterferogram(np.real(IFG),Maxis,method = interpmethod)
# imagIFG, Maxis = self.interpolateSingleInterferogram(np.imag(IFG),Maxis,method = interpmethod)
# IFG = realIFG + complex(1j)*imagIFG
case _:
IFG, Maxis = self.interpolateSingleInterferogram(IFG,Maxis,method = interpmethod)
# PROCESS IFGs
# Check if it is multiple interferograms or just a single one
if len(np.shape(IFG)) == 1:
complex_spectrum, f = self.processSingleInterferogram(IFG, Maxis, windowtype = windowtype, nzeros = nzeros, apod = apod, autoidx = True)
amp = np.abs(complex_spectrum)
phi = np.angle(complex_spectrum)
else:
# Allocate variables
spectraAll = complex(1j)*np.zeros((np.shape(IFG)[0],int(nzeros*np.shape(IFG)[1]/2)))
fAll = np.zeros(np.shape(spectraAll))
# Go trough all
for i in range(np.shape(IFG)[0]):
spectraAll[i,:], fAll[i,:] = self.processSingleInterferogram(IFG[i,:], Maxis[i,:], windowtype = windowtype, nzeros = nzeros, apod = apod, autoidx = True)
# Average the complex spectra
complex_spectrum = np.mean(spectraAll, axis = 0)
# Extract amplitude and phase from the averaged complex spectrum
amp = np.abs(complex_spectrum)
phi = np.angle(complex_spectrum)
f = np.mean(fAll, axis=0)
if simpleoutput:
return amp, phi, f
else:
spectrum = NeaSpectrum()
spectrum.parameters = copy.deepcopy(self.parameters)
spectrum.parameters["ScanArea"] = [self.parameters["ScanArea"][0],self.parameters["ScanArea"][1],len(amp)]
spectrum.data[channelA] = amp
spectrum.data[channelP] = phi
spectrum.data["Wavenumber"] = f
return spectrum
def processAllPoints(self, order = 2, windowtype = "blackmanharris", nzeros = 4, apod = True, method = "complex", interpmethod = "spline"):
if self.parameters['PixelArea'][0] == 1 and self.parameters['PixelArea'][1] == 1:
spectra = self.processPointInterferogram(order = order, windowtype = windowtype, nzeros = nzeros, apod = apod, method = method, interpmethod = interpmethod)
else:
spectra = NeaSpectrum()
spectra.parameters = copy.deepcopy(self.parameters)
spectra.parameters["PixelArea"] = [self.parameters["PixelArea"][0],self.parameters["PixelArea"][1],int(nzeros*self.parameters['PixelArea'][2]/2)]
ampFullData = np.zeros((spectra.parameters['PixelArea'][0], spectra.parameters['PixelArea'][1],spectra.parameters["PixelArea"][2]))
phiFullData = np.zeros(np.shape(ampFullData))
fFullData = np.zeros(np.shape(ampFullData))
singlePointIFG = NeaInterferogram()
singlePointIFG.parameters = copy.deepcopy(self.parameters)
singlePointIFG.parameters["PixelArea"] = [1,1,self.parameters["PixelArea"][2]]
singlePointIFG.data = dict()
channelA = f"O{order}A"
channelP = f"O{order}P"
for i in range(self.parameters['PixelArea'][0]):
for k in range(self.parameters['PixelArea'][1]):
singlePointIFG.data[channelA] = self.data[channelA][i,k,:]
singlePointIFG.data[channelP] = self.data[channelP][i,k,:]
singlePointIFG.data["M"] = self.data["M"][i,k,:]
ampFullData[i,k,:], phiFullData[i,k,:], fFullData[i,k,:] = singlePointIFG.processPointInterferogram(order = order, simpleoutput = True, windowtype = windowtype, nzeros = nzeros, apod = apod, method = method, interpmethod = interpmethod)
spectra.data[channelA] = ampFullData
spectra.data[channelP] = phiFullData
spectra.data["Wavenumber"] = fFullData
return spectra
def interpolateSingleInterferogram(self, ifg, maxis, method = "spline"):
"""
Re-interpolates the an interferogram to have a uniform coordinate spacing.
First, recalculates the space coordinates from sampling coordinates givan by *maxis*.
The interferogram is then re-interpolated for the new space grid.
Parameters
----------
ifg : 2d array or list
Containing the interferograms row-wise (first index)
maxis : 2d array or list
Containing the position coordinates for each interferograms row-wise (first index)
"""
if np.iscomplex(ifg).any():
newifg = np.zeros(np.shape(ifg))*complex(1j)
else:
newifg = np.zeros(np.shape(ifg))
newmaxis = np.zeros(np.shape(maxis))
# startM = np.min(maxis)
# stopM = np.max(maxis)
startM = np.min(np.median(maxis,axis=0))
stopM = np.max(np.median(maxis,axis=0))
try:
newcoords = np.linspace(startM,stopM,num=np.shape(maxis)[1])
for i in range(np.shape(ifg)[0]):
spl = CubicSpline(maxis[i][:], ifg[i][:])
newifg[i][:] = spl(newcoords)
newmaxis[i][:] = newcoords
except:
newcoords = np.linspace(startM,stopM,num = len(maxis))
match method:
case "spline":
interp_object = CubicSpline(maxis, ifg)
newifg = interp_object(newcoords)
case "linear":
interp_object = interp1d(maxis, ifg)
newifg = interp_object(newcoords)
newmaxis = newcoords
return newifg, newmaxis
def analyseRealSteps(self, maxis, plotoption = False):
# maxis = self.reshapeSinglePointFromChannel("M")*1e6
stepsize = np.zeros((np.shape(maxis)[0],1))
stepspread = np.zeros((np.shape(maxis)[0],1))
for i in range(np.shape(maxis)[0]):
stepsize[i] = np.mean(np.diff(maxis[i,:]))
stepspread[i] = np.std(np.diff(maxis[i,:]))
if plotoption:
# plt.figure()
plt.hist(np.diff(maxis[0,:]), bins = 300)
plt.show()
else:
pass
return stepsize, stepspread