syncmoss 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
src/Calibration.py ADDED
@@ -0,0 +1,757 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ @author: YAROSLAVTSEV S
4
+
5
+ The MIT license follows:
6
+
7
+ Copyright (c) European Synchrotron Radiation Facility (ESRF)
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+
27
+ """
28
+
29
+ import numpy as np
30
+ import minimi_lib as mi
31
+ import models as m5
32
+ import multiprocessing as mp
33
+ import matplotlib.pyplot as plt
34
+ import platform
35
+ import re
36
+ import time
37
+ from constants import number_of_baseline_parameters
38
+ MulCoCMS = 0.28
39
+
40
+ def Calibration(dir_path, Cal_file, pool, VVV, INS, JN, x0, MulCo, Vel_start = 1):
41
+ print('VVV = ', VVV)
42
+ if VVV == 1:
43
+ Li_Lo = 1
44
+ pNorm = np.array([float(0)] * number_of_baseline_parameters)
45
+ pNorm[0] = 1
46
+ Norm = m5.TI(np.array([float(1000)]), pNorm, [], JN, pool, 0.0, MulCoCMS, INS, [0], [0], Met=1)[0]
47
+ def func(x, p):
48
+ #p = np.insert(p, 3, 0)
49
+ #p = np.insert(p, 7, 0)
50
+ return m5.TI(x, p, model, JN, pool, 0.0, MulCoCMS, INS, [], [], Met=1, Norm=Norm)
51
+ # return m5.PV(x, p, model, pool)
52
+ INS_shift = 0
53
+
54
+ if VVV == 3:
55
+ Li_Lo = 2
56
+ pNorm = np.array([float(0)] * number_of_baseline_parameters)
57
+ pNorm[0] = 1
58
+ # JN = 32
59
+ Norm = m5.TI(np.array([float(1000)]), pNorm, [], JN, pool, x0, MulCo, INS, [0], [0])[0]
60
+ def func(x, p):
61
+ #p = np.insert(p, 3, 0)
62
+ #p = np.insert(p, 7, 0)
63
+ return m5.TI(x, p, model, JN, pool, x0, MulCo, INS, [], [], Norm=Norm)
64
+ INS_shift = 0
65
+ for i in range (0, int((len(INS))/3)):
66
+ INS_shift += INS[i*3 + 1] * INS[i*3 + 2]**2
67
+
68
+
69
+ def lin_cal(x, p):
70
+ H1, H2 = [], []
71
+ for i in range(0, len(x)):
72
+ if x[i] <= int(len(xn2)/2):
73
+ H1.append(p[0] - p[2]*x[i])
74
+ else:
75
+ H2.append(p[1] - p[2]*(len(xn2)-1-x[i]))
76
+ H1 = np.array(H1)
77
+ H2 = np.array(H2)
78
+ # print(H1, H2)
79
+ H = np.concatenate((H1, H2), axis=0)
80
+ # H1 = p[0] - p[2]*x[:int(len(x)/2)]
81
+ # H2 = p[1] - p[2]*x[:int(len(x)/2)]
82
+ # H = np.concatenate((H2, H1[::-1]),axis=0)
83
+ return H
84
+ def lin_cal_fin2(x, p):
85
+ # pCAL2 = pCAL
86
+ # pCAL2[number_of_baseline_parameters] = p[9]
87
+ # pCAL2[number_of_baseline_parameters + 11] = p[10]
88
+ # H1 = p[0] - p[2] * x[:int(len(x) / 2)]
89
+ # H2 = p[1] - p[2] * x[:int(len(x) / 2)]
90
+ # Hx1 = np.concatenate((np.array([float(1)]*len(H1)),np.array([float(0)]*len(H2))), axis=0)
91
+ # Hx2 = np.concatenate((np.array([float(0)]*len(H1)),np.array([float(1)]*len(H2))), axis=0)
92
+ # Hx = np.concatenate((H2, H1[::-1]), axis=0)
93
+ # H = p[3]*func(Hx, pCAL2) + (p[5]*(p[4]-xn2)**2 + p[6])*Hx2 + (p[8]*(p[7]-xn2)**2)*Hx1
94
+
95
+ pCAL2 = pCAL
96
+ pCAL2[number_of_baseline_parameters] = p[9] # intensity of main (only) component
97
+ # pCAL2[12] = p[10] # asymmetry parameter
98
+ H1, H2 = [], []
99
+ for i in range(0, len(x)):
100
+ if x[i] <= int(len(xn2) / 2):
101
+ H1.append(p[0] - p[2] * x[i])
102
+ else:
103
+ H2.append(p[1] - p[2] * (len(xn2) - 1 - x[i]))
104
+ H1 = np.array(H1)
105
+ H2 = np.array(H2)
106
+ Hx1 = np.concatenate((np.array([float(1)] * len(H1)), np.array([float(0)] * len(H2))), axis=0)
107
+ Hx2 = np.concatenate((np.array([float(0)] * len(H1)), np.array([float(1)] * len(H2))), axis=0)
108
+ Hx = np.concatenate((H1, H2), axis=0)
109
+ if min(Hx) > -2.95 and max(Hx) < 2.95:
110
+ # pCAL2[number_of_baseline_parameters + 6] = 0.5 # asymmetry parameter
111
+ p[10] = 0.5 # in this case this parameter do not affect the model
112
+ # else:
113
+ # pCAL2[number_of_baseline_parameters + 6] = p[10] # asymmetry parameter
114
+ pCAL2[number_of_baseline_parameters + 6] = p[10] # asymmetry parameter
115
+ pCAL2[0] = p[11]
116
+ if p[12] > p[11]*10:
117
+ p[12] = p[11]*10
118
+ pCAL2[4] = p[12] # * pCAL2[0]
119
+ H = func(Hx, pCAL2) + (p[5] * (p[4] - xn2) ** 2 + p[6]) * Hx1 + (p[8] * (p[7] - xn2) ** 2) * Hx2
120
+ # H = func(Hx, pCAL2) + (p[5] * (p[4] - xn2) ** 2 + p[6]) * np.sign(abs(Hx-Hx2)) + (p[8] * (p[7] - xn2) ** 2) * np.sign(abs(Hx-Hx1))
121
+ # H = p[3] * func(Hx, pCAL2) + (p[5] * (p[4] - xn2) ** 2 + p[6]) * Hx2 + (p[8] * (p[7] - xn2) ** 2) * Hx1
122
+
123
+ return H
124
+
125
+ def sin_cal(x, p):
126
+ H = p[0] / np.pi * len(xn2) * np.sin(np.pi / len(xn2)) * np.cos(p[1] + np.pi / len(xn2) * (2 * x + 1)) + p[2]
127
+ return H
128
+ def sin_cal_fin2(x, p):
129
+ pCAL2 = pCAL
130
+ pCAL2[number_of_baseline_parameters] = p[9] # intensity of main component
131
+
132
+ if VVV == 3:
133
+ pCAL2[number_of_baseline_parameters + 11] = p[11] # intensity of impurity
134
+ Hx = p[0] / np.pi * len(xn2) * np.sin(np.pi / len(xn2)) * np.cos(p[1] + np.pi / len(xn2) * (2 * x + 1)) + p[2]
135
+ if min(Hx) > -2.95 and max(Hx) < 2.95:
136
+ # pCAL2[number_of_baseline_parameters + 6] = 0.5 # asymmetry parameter
137
+ p[10] = 0.5
138
+ # print('very small velocity range - texture could not be defined')
139
+ # else:
140
+ # pCAL2[number_of_baseline_parameters + 6] = p[10] # asymmetry parameter
141
+ pCAL2[number_of_baseline_parameters + 6] = p[10] # asymmetry parameter
142
+ if VVV == 1:
143
+ pCAL2[0] = p[11]
144
+ if p[12] > p[11] * 10:
145
+ p[12] = p[11] * 10
146
+ pCAL2[4] = p[12] # * pCAL2[0]
147
+ Hx1 = np.concatenate((Hx[:int(len(x)/2)],Hx[:int(len(x)/2)]), axis = 0)
148
+ Hx2 = np.concatenate((Hx[int(len(x)/2):],Hx[int(len(x)/2):]), axis = 0)
149
+ H = p[3]*func(Hx, pCAL2) + (p[5]*(p[4]-xn2)**2 + p[6])*np.sign(abs(Hx-Hx2)) + (p[8]*(p[7]-xn2)**2)*np.sign(abs(Hx-Hx1))
150
+ # H = p[3]*TI(Hx, pCAL2, model, 64, -0.01, 3) + (p[5]*(p[4]-xn2)**2 + (p[8]*(p[7]+int(len(xn2)/2))**2 - p[5]*(p[4]+int(len(xn2)/2))**2))*np.sign(abs(Hx.real-Hx2)) + (p[8]*(p[7]-xn2)**2)*np.sign(abs(Hx.real-Hx1))
151
+ return H
152
+ def fix(y, x, p):
153
+ Hx = np.array([float(0)]*len(x))
154
+ for i in range(0, int(len(x) / 2)):
155
+ Hx[i] = y[i] - p[5]*(x[i]-p[4])**2 - p[6] #- p[6]
156
+ # Hx[i] = y[i] - p[5] * (x[i] - p[4]) ** 2 - (p[8]*(p[7]+p[0])**2 - p[5]*(p[4]+p[0])**2)
157
+ for i in range(int(len(x) / 2), len(x)):
158
+ Hx[i] = y[i] - p[8]*(x[i]-p[7])**2 #- p[9]
159
+ # Hx[i] = y[i] - p[8]*(x[i]-p[7])**2 + (p[8] * (p[7] + p[0]) ** 2 - p[5] * (p[4] + p[0]) ** 2)
160
+ return Hx
161
+ a = np.array([0, 1.9 ,1.1, 3.4])
162
+ value = 2.1
163
+ idx = (np.abs(a - value)).argmin()
164
+
165
+ PA = str(Cal_file)
166
+ if PA[-4:] == '.mca' or PA[-5:] == '.cmca':
167
+ LS = len(open(PA, 'r').readlines())
168
+ with open(PA, 'r') as fi:
169
+ id = []
170
+ n = 0
171
+ k = 0
172
+ for i in range(0, LS):
173
+ for ln in fi:
174
+ if ln.startswith("@A"):
175
+ k += 1
176
+ if k > n and ln.startswith("@A"):
177
+ id.append(re.findall(r'[\d.]+', ln[2:]))
178
+ if k > n and ln.startswith("#"):
179
+ break
180
+ if k > n and ln.startswith("@A") == 0:
181
+ id[n].extend(re.findall(r'[\d.]+', ln[0:]))
182
+ n += 1
183
+ if PA[-5:] == '.cmca':
184
+ if (id[0][-1] == 0 and id[0][0] != 0) or (id[0][0] == 0 and id[0][-1] != 0):
185
+ id_half = (id[0][-1] + id[0][0]) / 2
186
+ id[0][-1] = id_half
187
+ id[0][0] = id_half
188
+ if len(id[0])%2 == 1:
189
+ id[0] = id[0][:-1]
190
+ id = np.array(id, dtype=float)
191
+
192
+ xn = np.linspace(0, len(id[0]) / 2 - 1, int(len(id[0]) / 2))
193
+ xn2 = np.linspace(0, len(id[0]) - 1, int(len(id[0])))
194
+ y1 = id[0][:int(len(id[0]) / 2)]
195
+ y2 = id[0][int(len(id[0]) / 2):]
196
+ y_ch = y1 * 2
197
+
198
+ if PA[-4:] == '.ws5' or PA[-4:] == '.w98' or PA[-4:] == '.moe' or PA[-3:] == '.m1' or PA[-4:] == '.mcs' or PA[-4:] == '.Mcs':
199
+ if PA[-4:] == '.mcs' or PA[-4:] == '.Mcs':
200
+ f = open(PA, mode='rb')
201
+ id = []
202
+ entete1 = f.read(256)
203
+ array = np.fromfile(f, dtype=np.uint32)
204
+ print(len(array))
205
+ id.append(array)
206
+ f.close()
207
+ else:
208
+ with open(PA, 'r') as catalog:
209
+ id = []
210
+ id.append([])
211
+ # id[0].append(float(1))
212
+ # id[0].append(float(2))
213
+ # print(id)
214
+ k = 0
215
+ lines = (line.rstrip() for line in catalog)
216
+ lines = (line for line in lines if line) # skipping white lines
217
+ for line in lines:
218
+ if not (line.startswith('#') or line.startswith('<')): # skipping column labels
219
+ if PA[-3:] == '.m1':
220
+ while ' ' in line:
221
+ line = line.replace(' ', ' ')
222
+ column = line.split(' ')
223
+ x = float(column[4])
224
+ if k > 0:
225
+ id[0].append(x)
226
+ k += 1
227
+ else:
228
+ column = line.split()
229
+ x = float(column[0])
230
+ if PA[-4:] != '.moe' or not ('.' in str(column[0])):
231
+ id[0].append(x)
232
+ k += 1
233
+ # if len(id[0]) % 2 == 1:
234
+ # id[0] = id[0][:-1]
235
+ id = np.array(id, dtype=float)
236
+
237
+ xn = np.linspace(0, len(id[0]) / 2 - 1, int(len(id[0]) / 2))
238
+ xn2 = np.linspace(0, len(id[0]) - 1, int(len(id[0])))
239
+ y1 = id[0][:int(len(id[0]) / 2)]
240
+ y2 = id[0][int(len(id[0]) / 2):]
241
+ if len(y2) > len(y1):
242
+ y2 = y2[1:]
243
+ y_ch = y1 * 2
244
+
245
+
246
+ # detecting dephase
247
+ chi2_tmp = np.sum(np.abs(y1-y2[::-1]))/len(y1)
248
+ phase0 = 0
249
+ for i in range(1, int(len(y1)/2)):
250
+ chi2_tmp_n = np.sum(np.abs(y1[i:]-y2[i:][::-1]))/len(y1[i:])
251
+ if chi2_tmp_n <= chi2_tmp:
252
+ chi2_tmp = chi2_tmp_n
253
+ phase0 = -i
254
+ # else:
255
+ # break
256
+ if phase0 == 0:
257
+ for i in range(1, int(len(y1)/2)):
258
+ chi2_tmp_n = np.sum(np.abs(y1[:-i] - y2[:-i][::-1])) / len(y1[:-i])
259
+ if chi2_tmp_n <= chi2_tmp:
260
+ chi2_tmp = chi2_tmp_n
261
+ phase0 = i
262
+ # else:
263
+ # break
264
+ phase0 = - phase0 * 2
265
+ print('PHASE ', phase0)
266
+
267
+
268
+ model = ['Sextet']
269
+ p00 = np.array([(max(id[0]) - 2 * np.sqrt(max(id[0])))*(1-0.4*(VVV==1)), 0, 0, 0, (max(id[0]) - 2 * np.sqrt(max(id[0])))*(0.4*(VVV==1)), 0, 0, 0, 8, 0.0, 0, 33.04, 0.098, 0.0, 0.5, 0, 0, 0, 3])
270
+ sex0 = np.array([-5.3123, -3.0760, -0.8397, 0.8397, 3.0760, 5.3123])
271
+ bounds = np.array([[-np.inf] * len(p00), [np.inf] * len(p00)], dtype=float)
272
+ bounds[0][number_of_baseline_parameters+1] = -0.05
273
+ bounds[1][number_of_baseline_parameters+1] = 0.05
274
+ bounds[0][number_of_baseline_parameters+3] = 32.54
275
+ bounds[1][number_of_baseline_parameters+3] = 33.54
276
+ if Vel_start == 1:
277
+ sex0 = sex0[::-1]
278
+
279
+ start_time = time.time()
280
+ if VVV == 3 or VVV == 1:
281
+ ch_m = int(np.where(id[0] == min(id[0][5:-5]))[0][0])
282
+ # ch_m1 = int(np.where(id[0] == min(id[0][:int(len(id[0]) / 4)]))[0][0])
283
+ # ch_m2 = int(np.where(id[0] == min(id[0][int(len(id[0]) / 4):int(len(id[0]) / 2)]))[0][0])
284
+ ch_m1_arr = (np.where(id[0] == min(id[0][:int(len(id[0]) / 4)]))[0])
285
+ ch_m1 = int(ch_m1_arr[-1])
286
+ for i in range(0, len(ch_m1_arr)):
287
+ if ch_m1_arr[i] < int(len(id[0]) / 4):
288
+ ch_m1 = int(ch_m1_arr[i])
289
+ # print(ch_m1)
290
+ break
291
+ ch_m2_arr = (np.where(id[0] == min(id[0][int(len(id[0]) / 4):int(len(id[0]) / 2)]))[0])
292
+ ch_m2 = int(ch_m2_arr[-1])
293
+ for i in range(0, len(ch_m2_arr)):
294
+ if ch_m2_arr[i] >= int(len(id[0]) / 4) and ch_m2_arr[i] < int(len(id[0]) / 2):
295
+ ch_m2 = int(ch_m2_arr[i])
296
+ # print(ch_m2)
297
+ break
298
+ hi_m = np.array([float(10000)] * 30)
299
+ hi_c = 10000
300
+ hi_sin = hi_c
301
+ k = 0
302
+ print('channels of minimum ', ch_m1, ch_m2)
303
+ for i in range(0, 6):
304
+ for j in range(i + 1, 6):
305
+ # sex0[i] = p[0] / np.pi * len(xn2) * np.sin(np.pi / len(xn2)) * np.cos(np.pi / len(xn2) * (2 * ch_m1 + 1)) + p[2]
306
+ # sex0[j] = p[0] / np.pi * len(xn2) * np.sin(np.pi / len(xn2)) * np.cos(np.pi / len(xn2) * (2 * ch_m2 + 1)) + p[2]
307
+ Vel_max_m = (sex0[i] - sex0[j]) * np.pi / len(xn2) / np.sin(np.pi / len(xn2)) / (np.cos(np.pi / len(xn2) * (2 * ch_m1 + 1)) - np.cos(np.pi / len(xn2) * (2 * ch_m2 + 1)))
308
+ # print((sex0[i] - sex0[j]), len(xn2), np.cos(np.pi / len(xn2)))
309
+ shift_m = sex0[i] - Vel_max_m / np.pi * len(xn2) * np.sin(np.pi / len(xn2)) * np.cos(np.pi / len(xn2) * (2 * ch_m1 + 1) + (np.pi / len(xn2) * phase0))
310
+ x = sin_cal(xn2, [Vel_max_m, (np.pi / len(xn2) * phase0), shift_m]) # p[1] mean different on method
311
+ # A and I1/I3 are 12th and 16th parameters.
312
+ res = mi.minimi_hi(func, x, id[0], p00, fix=np.array([1, 2, 3, 1+int(VVV), 5, 6, 7, number_of_baseline_parameters+2, number_of_baseline_parameters+4, number_of_baseline_parameters+5, number_of_baseline_parameters+6, number_of_baseline_parameters+7, number_of_baseline_parameters+8, number_of_baseline_parameters+9, number_of_baseline_parameters+10]), bounds=bounds, MI=3, MI2=5) # int(11*(VVV==3)+1), int(15*(VVV==3)+1)
313
+ # if res[2] < 10000:
314
+ hi_m[k] = res[2]
315
+ hi_c = hi_m[k]
316
+ # print(Vel_max_m, shift_m, hi_c)
317
+ if all(hi_m >= hi_c) == True:
318
+ Vel_max_sin = Vel_max_m
319
+ shift = shift_m
320
+ p_sin = res[0]
321
+ hi_sin = hi_c
322
+ k += 1
323
+ print(Vel_max_sin,shift, hi_sin)
324
+ x_sin = sin_cal(xn2, [Vel_max_sin, (np.pi / len(xn2) * phase0), shift])
325
+
326
+ # fig = plt.figure(dpi=300)
327
+ # plt.plot(x_sin, id[0])
328
+ # plt.plot(x_sin, func(x_sin, p_sin))
329
+ # # plt.show()
330
+ # fig.savefig(r"C:\Users\yaroslav\Downloads\test_sin.png", bbox_inches='tight')
331
+ # plt.close()
332
+
333
+ method = 0
334
+ x = x_sin
335
+ p0 = p_sin
336
+ Vel_max = Vel_max_sin
337
+
338
+ def cal(x, p):
339
+ return sin_cal(x, p)
340
+
341
+ def cal_fin2(x, p):
342
+ return sin_cal_fin2(x, p)
343
+ print('first sin takes', time.time() - start_time, 'seconds')
344
+
345
+ if VVV == 1:
346
+ phase0 = phase0/2
347
+ # ch_m1 = int(np.where(id[0] == min(id[0][:int(len(id[0])/4)]))[0][0])
348
+ # ch_m2 = int(np.where(id[0] == min(id[0][int(len(id[0])/4):int(len(id[0])/2)]))[0][0])
349
+ ch_m1_arr = (np.where(id[0] == min(id[0][:int(len(id[0]) / 4)]))[0])
350
+ ch_m1 = int(ch_m1_arr[-1])
351
+ for i in range(0, len(ch_m1_arr)):
352
+ if ch_m1_arr[i] < int(len(id[0]) / 4):
353
+ ch_m1 = int(ch_m1_arr[i])
354
+ print(ch_m1)
355
+ break
356
+ ch_m2_arr = (np.where(id[0] == min(id[0][int(len(id[0]) / 4):int(len(id[0]) / 2)]))[0])
357
+ ch_m2 = int(ch_m2_arr[-1])
358
+ for i in range(0, len(ch_m2_arr)):
359
+ if ch_m2_arr[i] >= int(len(id[0]) / 4) and ch_m2_arr[i] < int(len(id[0]) / 2):
360
+ ch_m2 = int(ch_m2_arr[i])
361
+ print(ch_m2)
362
+ break
363
+ hi_m_lin = np.array([float(10000)] * 30)
364
+ hi_c = 10000
365
+ hi_lin = hi_c
366
+ k = 0
367
+ for i in range(0, 6):
368
+ for j in range(i+1, 6):
369
+ velocity_step_m = -(sex0[j]-sex0[i])/(ch_m2-ch_m1)
370
+ Vel_max_m = sex0[i] + velocity_step_m*ch_m1
371
+ x = lin_cal(xn2, [Vel_max_m, Vel_max_m-velocity_step_m*phase0, velocity_step_m]) # p[1] mean different on method
372
+ res = mi.minimi_hi(func, x, id[0], p00, fix=np.array([1, 2, 3, 1+int(VVV), 5, 6, 7, number_of_baseline_parameters+2, number_of_baseline_parameters+4, number_of_baseline_parameters+5, number_of_baseline_parameters+6, number_of_baseline_parameters+7, number_of_baseline_parameters+8, number_of_baseline_parameters+9, number_of_baseline_parameters+10]), bounds=bounds, MI=3, MI2=5)
373
+ # if res[2] < 10000:
374
+ hi_m_lin[k] = res[2]
375
+ hi_c = hi_m_lin[k]
376
+ #print(Vel_max_m, velocity_step_m, hi_m_lin[k])
377
+ if all(hi_m_lin >= hi_c) == True:
378
+ Vel_max_lin = Vel_max_m
379
+ velocity_step = velocity_step_m
380
+ p_lin = res[0]
381
+ hi_lin = hi_c
382
+ k += 1
383
+ print(Vel_max_lin,velocity_step, hi_lin)
384
+ x_lin = lin_cal(xn2, [Vel_max_lin, Vel_max_lin-velocity_step*phase0, velocity_step])
385
+ phase0 = phase0 * 2
386
+ # fig = plt.figure(dpi=300)
387
+ # plt.plot(x_lin, id[0])
388
+ # plt.plot(x_lin, func(x_lin, p_lin))
389
+ # # plt.show()
390
+ # # "C:\Users\yaroslav\Downloads"
391
+ # fig.savefig(r"C:\Users\yaroslav\Downloads\test_lin.png", bbox_inches='tight')
392
+ # plt.close()
393
+
394
+ method = 1
395
+ x = x_lin
396
+ p0 = p_lin
397
+ Vel_max = Vel_max_lin
398
+
399
+ def cal(x, p):
400
+ return lin_cal(x, p)
401
+
402
+ def cal_fin2(x, p):
403
+ return lin_cal_fin2(x, p)
404
+
405
+ if VVV == 1:
406
+ if hi_sin <= hi_lin: # and shift < 1.0
407
+ method = 0
408
+ x = x_sin
409
+ p0 = p_sin
410
+ Vel_max = Vel_max_sin
411
+ def cal(x, p):
412
+ return sin_cal(x, p)
413
+ def cal_fin2(x, p):
414
+ return sin_cal_fin2(x, p)
415
+ print('sinus mode')
416
+ else:
417
+ method = 1
418
+ x = x_lin
419
+ p0 = p_lin
420
+ Vel_max = Vel_max_lin
421
+ phase0 = phase0 / 2
422
+ def cal(x, p):
423
+ return lin_cal(x, p)
424
+ def cal_fin2(x, p):
425
+ return lin_cal_fin2(x, p)
426
+ print('triangular mode')
427
+
428
+ p0[number_of_baseline_parameters+1] = 0
429
+ p0[number_of_baseline_parameters+3] = 33.04
430
+ print('method ', method)
431
+ print(p0)
432
+ # x = sin_cal(xn2, [Vel_max, 0 * np.pi / len(xn2), 0.68])
433
+ # x = sin_cal(xn2, [Vel_max, 0 * np.pi / len(xn2), 0.68]) # p[1] mean different on method
434
+ p00 = np.copy(p0)
435
+ start_time = time.time()
436
+ for i in range(0, 4):
437
+ p = mi.minimi_hi(func, x, id[0], p0, fix=np.array([1, 2, 3, 1+int(VVV), 5, 6, 7, number_of_baseline_parameters+2, number_of_baseline_parameters+3, number_of_baseline_parameters+4, number_of_baseline_parameters+6, number_of_baseline_parameters+7, number_of_baseline_parameters+8, number_of_baseline_parameters+9, number_of_baseline_parameters+10]), MI=3, MI2=5)[0] # int(15*(VVV==3)+1) int(11*(VVV==3)+1)
438
+ # print(p)
439
+ sex0 = np.array([-5.3123, -3.0760, -0.8397, 0.8397, 3.0760, 5.3123, -5.3123, -3.0760, -0.8397, 0.8397, 3.0760, 5.3123])
440
+ if Vel_start == 1:
441
+ sex0[::-1]
442
+ ps0x = np.array([float(0)]*12)
443
+ V = number_of_baseline_parameters
444
+ x1 = x[:int(len(x)/2)]
445
+ x2 = x[int(len(x)/2):]
446
+ p[V+3] = p[V+3] / 3.101
447
+ ps0x[0] = (np.abs(x1 - (p[V + 1] - p[V + 3] / 2 + p[V + 2]) - p[V + 7])).argmin()
448
+ ps0x[1] = (np.abs(x1 - (p[V + 1] - 3.0760 / 5.3123 * p[V + 3] / 2 - p[V + 2]) + p[V + 8])).argmin()
449
+ ps0x[2] = (np.abs(x1 - (p[V + 1] - 0.8397 / 5.3123 * p[V + 3] / 2 - p[V + 2]) - p[V + 8])).argmin()
450
+ ps0x[3] = (np.abs(x1 - (p[V + 1] + 0.8397 / 5.3123 * p[V + 3] / 2 - p[V + 2]) + p[V + 8])).argmin()
451
+ ps0x[4] = (np.abs(x1 - (p[V + 1] + 3.0760 / 5.3123 * p[V + 3] / 2 - p[V + 2]) - p[V + 8])).argmin()
452
+ ps0x[5] = (np.abs(x1 - (p[V + 1] + p[V + 3] / 2 + p[V + 2]) + p[V + 7])).argmin()
453
+ ps0x[6] = int(len(x)/2) + (np.abs(x2 - (p[V + 1] - p[V + 3] / 2 + p[V + 2]) - p[V + 7])).argmin()
454
+ ps0x[7] = int(len(x)/2) + (np.abs(x2 - (p[V + 1] - 3.0760 / 5.3123 * p[V + 3] / 2 - p[V + 2]) + p[V + 8])).argmin()
455
+ ps0x[8] = int(len(x)/2) + (np.abs(x2 - (p[V + 1] - 0.8397 / 5.3123 * p[V + 3] / 2 - p[V + 2]) - p[V + 8])).argmin()
456
+ ps0x[9] = int(len(x)/2) + (np.abs(x2 - (p[V + 1] + 0.8397 / 5.3123 * p[V + 3] / 2 - p[V + 2]) + p[V + 8])).argmin()
457
+ ps0x[10] = int(len(x)/2) + (np.abs(x2 - (p[V + 1] + 3.0760 / 5.3123 * p[V + 3] / 2 - p[V + 2]) - p[V + 8])).argmin()
458
+ ps0x[11] = int(len(x)/2) + (np.abs(x2 - (p[V + 1] + p[V + 3] / 2 + p[V + 2]) + p[V + 7])).argmin()
459
+ p[V + 3] = p[V + 3] * 3.101
460
+ j = 0
461
+ for k in range(0, 12):
462
+ # if ps0x[j] == 0 or ps0x[j] == len(x)-1 or ps0x[j] == len(x1)-1 or ps0x[j] == len(x1) or ps0x[j] == len(x1)-2 or ps0x[j] == len(x) - 2:
463
+ if ps0x[j] < 5 or (ps0x[j] > len(x1)-1-5 and ps0x[j] < len(x1)+5) or ps0x[j] > len(x) - 1 - 5:
464
+ ps0x = np.delete(ps0x, j)
465
+ sex0 = np.delete(sex0, j)
466
+ j = j-1
467
+ j += 1
468
+ # print(ps0x)
469
+ if i == 0:
470
+ if method == 0:
471
+ ps = mi.minimi_hi(sin_cal, ps0x, sex0, p0=np.array([Vel_max, (np.pi / len(xn2) * phase0), shift]), tau0=1)[0]
472
+ if method == 1:
473
+ ps = mi.minimi_hi(lin_cal, ps0x, sex0, p0=np.array([Vel_max, Vel_max-velocity_step*phase0, velocity_step]), tau0=1)[0]
474
+ # print(ps)
475
+ else:
476
+ ps = mi.minimi_hi(cal, ps0x, sex0, p0=ps, eps = 10**-40)[0]
477
+ # print(ps)
478
+ x = cal(xn2, ps)
479
+ p0 = np.copy(p)
480
+ p0[7] = 0
481
+ print('first ps takes', time.time() - start_time, 'seconds')
482
+ # plt.figure(dpi=300)
483
+ # plt.plot(x, id[0])
484
+ # plt.plot(x, func(x, p0))
485
+ # plt.show()
486
+ # plt.close()
487
+
488
+ if VVV == 3:
489
+ JN = 64
490
+ Norm = m5.TI(np.array([float(1000)]), pNorm, [], JN, pool, x0, MulCo, INS, [0], [0])[0]
491
+
492
+ pCAL = np.array([p[0], 0, 0, 0, p[3], 0, 0, 0, 8.2, 0, 0, 33.04, 0.098, 0.0, 0.5, 0, 0, 0, 3])
493
+ ps1 = np.append(ps, 1)
494
+
495
+ def parabolic(x, p):
496
+ H = p[1]*(p[0]-x)**2+p[2]
497
+ return H
498
+ y2 = y2[::-1]
499
+ start_time = time.time()
500
+ parab = mi.minimi_hi(parabolic, xn2[:int(len(xn2)/2)], y1-y2+y1[0], p0=[int(len(xn2)/4), 5, y1[int(len(y1) / 2)]-y2[int(len(y2) / 2)]+y1[0]])[0]
501
+ print('parabola takes', time.time() - start_time, 'seconds')
502
+ parab[2] = parab[2] - y1[0]
503
+ y2 = y2[::-1]
504
+
505
+ ps1[-1] = 1
506
+ ps1 = np.append(ps1, parab[0])
507
+ ps1 = np.append(ps1, parab[1] / 2)
508
+ ps1 = np.append(ps1, parab[2])
509
+ ps1 = np.append(ps1, parab[0] + int(len(xn2)/2))
510
+ ps1 = np.append(ps1, (-1)*parab[1] / 2)
511
+
512
+ # test_par1 = np.array([parab[0] , parab[1] / 2, parab[2]])
513
+ # test_par2 = np.array([parab[0]+ int(len(xn2)/2),-parab[1] / 2, 0])
514
+ # fig = plt.figure(dpi=300)
515
+ # plt.plot(xn2[:int(len(xn2)/2)], p0[0]+p0[3] + parabolic(xn2[:int(len(xn2)/2)], test_par1), 'r')
516
+ # plt.plot(xn2[int(len(xn2)/2):], p0[0]+p0[3] + parabolic(xn2[int(len(xn2)/2):], test_par2), 'r')
517
+ # plt.plot(xn2, id[0], 'm')
518
+ # fig.savefig(r"C:\Users\yaroslav\Downloads\test_parab.png", bbox_inches='tight')
519
+ # plt.close()
520
+
521
+ if method == 0:
522
+ if VVV == 3:
523
+ model = ['Sextet', 'Sextet', 'Doublet']
524
+
525
+ try:
526
+ Be_param = np.genfromtxt(str(dir_path) + str('\\\\parameters\\\\Be.txt')*(platform.system() == 'Windows') + str('/parameters/Be.txt')*(platform.system() != 'Windows'), delimiter='\t', skip_footer=0)
527
+ print('file was read')
528
+ except:
529
+ Be_param = np.array([0.057, 0.066, -0.261, 0.098, 0.375, 0.772, 1])
530
+ print('COULD NOT READ Be.txt')
531
+ pCAL = np.array([p[0], 0, 0, 0, 0, 0, 0, 0, 8.08, 0, 0, 33.04, 0.098, 0, 0.5, 0, 0, 0, 3, 0.451, -0.041, 0.003, 30.88, 0.098, 0.1, 0.5, 0, 0, 0, 3])#, 0.048, 0.103, -0.259, 0.098, 0.105, 0.265, 1])
532
+ pCAL = np.concatenate((pCAL, Be_param))
533
+ if VVV == 1:
534
+ # model = ['Sextet', 'Sextet']
535
+ # pCAL = np.array([p[0], 0, 0, p[3], 0, 0, 8.08, 0, 0, 33.04, 0.098, 0, 0.5, 0, 0, 0, 3, 0.0, -0.041, 0.003, 30.88, 0.098, 0.1, 0.5, 0, 0, 0, 3])#, 0.048, 0.103, -0.259, 0.098, 0.105, 0.265, 1])
536
+ model = ['Sextet']
537
+ pCAL = np.array([p[0], 0, 0, 0, p[3], 0, 0, 0, 8.08, 0, 0, 33.04, 0.098, 0, 0.5, 0, 0, 0, 3])
538
+ print('background ', pCAL[0], pCAL[4], ps1[3])
539
+
540
+ if method == 1:
541
+ model = ['Sextet']
542
+ pCAL = p0
543
+ pCAL[number_of_baseline_parameters+10] = 3
544
+ pCAL[number_of_baseline_parameters+5] = 0
545
+ print('background ', pCAL[0], pCAL[4], ps1[3])
546
+
547
+
548
+ xT = cal(xn2, ps1)
549
+
550
+ ps1 = np.append(ps1, 8)
551
+ ps1 = np.append(ps1, 0.5)
552
+ if VVV == 3:
553
+ ps1 = np.append(ps1, 0.5)
554
+ if VVV == 1:
555
+
556
+ tot = (pCAL[0] + pCAL[4])
557
+ ps1[9] = pCAL[number_of_baseline_parameters] * pCAL[0] / tot / 3 * 5
558
+ pCAL[0] = tot * 0.6
559
+ pCAL[4] = tot * 0.4
560
+ ps1 = np.append(ps1, pCAL[0])
561
+ ps1 = np.append(ps1, pCAL[4])
562
+ print('baseline ', ps1[11], ps1[12])
563
+
564
+ print('parameter set after preliminary fit ', ps1)
565
+ print('model parameteres after preliminary fit ', pCAL)
566
+ time.sleep(1)
567
+
568
+ # tmpy = cal_fin2(xn2, ps1)
569
+ # fig = plt.figure(dpi=300)
570
+ # plt.plot(xn2, tmpy, 'r')
571
+ # plt.plot(xn2, id[0], 'm')
572
+ # plt.plot(xn2, id[0] - tmpy + min(id[0]) - max(id[0] - tmpy), 'b')
573
+ # fig.savefig(r"C:\Users\yaroslav\Downloads\test_calibr1.png", bbox_inches='tight')
574
+ # plt.close()
575
+
576
+
577
+ start_time = time.time()
578
+ res = mi.minimi_hi(cal_fin2, xn2, (id[0]), p0=ps1, MI=20, MI2=20, eps=10**-6)
579
+ if abs(res[0][0]) < 2.95:
580
+ print('very small velocity range - texture could not be defined')
581
+ print('model parameteres ', pCAL)
582
+ print('variable parameters ', res[0])
583
+ print('hi2 ', res[2])
584
+ print('main minimization takes', time.time() - start_time, 'seconds')
585
+ # print('hihi')
586
+ time.sleep(2)
587
+ pS = res[0]
588
+ hi2 = res[2]
589
+ xT = cal(xn2, pS)
590
+
591
+ # tmpy = cal_fin2(xn2, res[0])
592
+ # fig = plt.figure(dpi=300)
593
+ # ax = fig.add_subplot(111)
594
+ # ax2 = ax.twinx()
595
+ # ax.plot(xn2, tmpy, 'r')
596
+ # ax.plot(xn2, id[0], 'm')
597
+ # ax.plot(xn2, id[0]-tmpy + min(id[0])-max(id[0]-tmpy), 'b')
598
+ # ax2.plot(xn2, xT, 'y')
599
+ # ax2.plot([xn2[0], xn2[-1]], [pS[0], pS[0]], 'cyan')
600
+ # ax2.plot([xn2[0], xn2[-1]], [pS[1], pS[1]], 'lime')
601
+ # ax2.plot([xn2[0], xn2[-1]], [pS[0] - int(len(xn2) / 2) * pS[2], pS[0] - int(len(xn2) / 2) * pS[2]], 'cyan')
602
+ # ax2.plot([xn2[0], xn2[-1]], [pS[1] - (int(len(xn2)) / 2 -1) * pS[2], pS[1] - (int(len(xn2) / 2)-1) * pS[2]], 'lime')
603
+ # fig.savefig(r"C:\Users\yaroslav\Downloads\test_calibr2.png", bbox_inches='tight')
604
+ # plt.close()
605
+ # print(xT)
606
+
607
+
608
+ pCAL[0] = pCAL[0] * pS[3]
609
+ if VVV == 1:
610
+ pCAL[4] = pCAL[4] * pS[3]
611
+ # pS[12] = pS[12] * pS[3]
612
+ pS[3] = 1
613
+
614
+ spc = fix(id[0], xn2, pS)
615
+ tmp = cal_fin2(xn2, pS)
616
+
617
+ if method == 0:
618
+ min1 = np.abs(xT[:int(len(xT) / 2)]-pS[2]).argmin()
619
+ min2 = np.abs(xT[int(len(xT) / 2):] - xT[min1]).argmin() + int(len(xT)/2)
620
+
621
+ if min1 == (len(xT)-1-min2):
622
+ n1, n2 = 0, len(xT)-1
623
+ elif min1 > (len(xT)-1-min2):
624
+ n1, n2 = min1 - (len(xT)-min2) + 1, len(xT) - 1
625
+ elif min1 < (len(xT)-1-min2):
626
+ n1, n2 = 0, min2+min1
627
+
628
+ x_1h = (xT[:int(n1 / 2)] + xT[n1 - int(n1 / 2):n1][::-1]) / 2
629
+ spc_1h = id[0][:int(n1 / 2)] + id[0][n1 - int(n1 / 2):n1][::-1]
630
+ fit_1h = tmp[:int(n1 / 2)] + tmp[n1 - int(n1 / 2):n1][::-1]
631
+ delt_1h = id[0][:int(n1 / 2)] - id[0][n1 - int(n1 / 2):n1][::-1]
632
+
633
+ x_2h = (xT[n2+1:n2 + int((len(xT) - 1 - n2) / 2)+1][::-1] + xT[len(xT) - int((len(xT) - 1 - n2) / 2):len(xT)]) / 2
634
+ spc_2h = id[0][n2+1:n2 + int((len(xT) - 1 - n2) / 2)+1][::-1] + id[0][len(xT) - int((len(xT) - 1 - n2) / 2):len(xT)]
635
+ fit_2h = tmp[n2+1:n2 + int((len(xT) - 1 - n2) / 2)+1][::-1] + tmp[len(xT) - int((len(xT) - 1 - n2) / 2):len(xT)]
636
+ delt_2h = id[0][n2+1:n2 + int((len(xT) - 1 - n2) / 2)+1][::-1] - id[0][len(xT) - int((len(xT) - 1 - n2) / 2):len(xT)]
637
+
638
+ x_3h = (xT[n1:n1 + int((n2 - n1 + 1) / 2)] + xT[n2 - int((n2 - n1 + 1) / 2) + 1:n2 + 1][::-1]) / 2
639
+ spc_3h = id[0][n1:n1 + int((n2 - n1 + 1) / 2)] + id[0][n2 - int((n2 - n1 + 1) / 2) + 1:n2 + 1][::-1]
640
+ fit_3h = tmp[n1:n1 + int((n2 - n1 + 1) / 2)] + tmp[n2 - int((n2 - n1 + 1) / 2) + 1:n2 + 1][::-1]
641
+ delt_3h = id[0][n1:n1 + int((n2 - n1 + 1) / 2)] - id[0][n2 - int((n2 - n1 + 1) / 2) + 1:n2 + 1][::-1]
642
+
643
+ delt_4h = id[0][n1+1:n1 + int((n2 - n1 + 1) / 2)+1] - id[0][n2 - int((n2 - n1 + 1) / 2) + 1:n2 + 1][::-1]
644
+
645
+ delt_5h = id[0][n1:n1 + int((n2 - n1 + 1) / 2)] - id[0][n2 - int((n2 - n1 + 1) / 2):n2][::-1]
646
+
647
+ # plt.figure(dpi=300)
648
+ # plt.plot(x_3h, delt_3h, color='r')
649
+ # plt.plot(x_3h, delt_4h, color='b')
650
+ # plt.plot(x_3h, delt_5h, color='m')
651
+ # plt.show()
652
+
653
+ xT2 = np.concatenate((np.concatenate((x_1h, x_2h)), x_3h))
654
+ spc2 = np.concatenate((np.concatenate((spc_1h, spc_2h)), spc_3h))
655
+ spc3 = np.concatenate((np.concatenate((fit_1h, fit_2h)), fit_3h))
656
+ delt = np.concatenate((np.concatenate((delt_1h, delt_2h)), delt_3h))
657
+ print(n1, n2, len(xT2))
658
+ # plt.figure(dpi=300)
659
+ # plt.plot(xT2, spc2)
660
+ # plt.show()
661
+
662
+ if method == 1:
663
+ if pS[0] * pS[2] == pS[1] * pS[2]:
664
+ min1 = 0
665
+ min2 = int(len(xT))-1
666
+ elif pS[0] * pS[2] > pS[1] * pS[2]:
667
+ min1 = np.abs(xT[:int(len(xT) / 2)] - pS[1]).argmin()
668
+ min2 = int(len(xT))-1
669
+ elif pS[0] * pS[2] < pS[1] * pS[2]:
670
+ min1 = 0
671
+ min2 = np.abs(xT[int(len(xT) / 2):] - pS[0]).argmin() + int(len(xT)/2)
672
+ n1, n2 = min1, min2
673
+ n_sh = 2 * (n1 + (int(len(xT))-1-n2))
674
+
675
+ # xT2 = np.array([float(0)] * int((n2-n1+1) / 2))
676
+ # spc2 = np.array([float(0)] * int((n2-n1+1) / 2))
677
+ # spc3 = np.array([float(0)] * int((n2-n1+1) / 2))
678
+ # delt = np.array([float(0)] * int((n2-n1+1) / 2))
679
+ # for i in range(0, int((n2-n1+1) / 2)): # for i in range(0, int(len(xT) / 2) - min1):
680
+ # xT2[i] = (xT[n1 + i] + xT[n2 - i]) / 2
681
+ # spc2[i] = id[0][n1 + i] + id[0][n2 - i]
682
+ # spc3[i] = tmp[n1 + i] + tmp[n2 - i]
683
+ # delt[i] = id[0][n1 + i] - id[0][n2 - i]
684
+
685
+ xT2 = np.array([float(0)] * int((len(xT) - n_sh)/2)) # int((len(xT) - n_sh)/2) - to take into account odd number of points # (511 - 2)/2 = 254,5 -> int = 254
686
+ spc2 = np.array([float(0)] * int((len(xT) - n_sh)/2))
687
+ spc3 = np.array([float(0)] * int((len(xT) - n_sh)/2))
688
+ delt = np.array([float(0)] * int((len(xT) - n_sh)/2))
689
+ for i in range(0, int((len(xT) - n_sh)/2)):
690
+ xT2[i] = (xT[n1 + i] + xT[n2 - i]) / 2
691
+ spc2[i] = id[0][n1 + i] + id[0][n2 - i]
692
+ spc3[i] = tmp[n1 + i] + tmp[n2 - i]
693
+ delt[i] = id[0][n1 + i] - id[0][n2 - i]
694
+
695
+ xT2 = xT2 + INS_shift
696
+
697
+ if VVV == 3: #method == 0
698
+ # plt.rcParams['axes.facecolor'] = '(0, 0, 0)'
699
+ # plt.rcParams['figure.facecolor'] = '(0, 0, 0)'
700
+ # plt.rcParams['axes.labelcolor'] = 'w'
701
+ # plt.rcParams['axes.edgecolor'] = 'w'
702
+ # plt.rcParams['xtick.color'] = 'w'
703
+ # plt.rcParams['ytick.color'] = 'w'
704
+ fig, ax = plt.subplots(dpi=300)
705
+ plt.plot(xn2, (id[0]), 'm')
706
+ plt.plot(xn2, cal_fin2(xn2, pS), 'b')
707
+ plt.plot(xn2, id[0] - tmp + 0.9 * min(id[0]), 'lime')
708
+ plt.plot(xn2[:int(len(xn2)/2)]*2, (id[0] + max(id[0]) - min(spc) + 10 * np.sqrt(max(id[0])))[:int(len(id[0])/2)][::-1], 'r', linestyle = 'None', marker = 'o', markersize = 2)
709
+ plt.plot(xn2[:int(len(xn2)/2)]*2, (id[0] + max(id[0]) - min(spc) + 10 * np.sqrt(max(id[0])))[int(len(id[0])/2):], 'yellow', linestyle = 'None', marker = 'o', markersize = 2)
710
+ sax = ax.twiny()
711
+ ax.get_yaxis().set_ticks([])
712
+ ax.set_xlabel('channel', color='m')
713
+ sax.set_xlabel('Velocity, mm/s', color='r')
714
+ sax.plot(xT[:int(len(spc)/2)], (spc + 2*(max(id[0]) - min(spc) + 10 * np.sqrt(max(id[0]))))[:int(len(spc)/2)], 'r', linestyle = 'None', marker = 'o', markersize = 2)
715
+ sax.plot(xT[int(len(spc)/2):], (spc + 2*(max(id[0]) - min(spc) + 10 * np.sqrt(max(id[0]))))[int(len(spc)/2):], 'yellow', linestyle = 'None', marker = 'o', markersize = 2)
716
+ sax.plot(xT2, delt + 4*(max(id[0]) - min(spc) + 10 * np.sqrt(max(id[0]))), 'lime')
717
+ sax.plot(xT2, spc2/2 + 2*(max(id[0]) - min(spc) + 10 * np.sqrt(max(id[0]))), 'm', marker = 'o', markersize = 1)
718
+
719
+
720
+
721
+ ax.text(0, max(id[0])+4*np.sqrt(max(id[0])), 'Va = %.3f mm/s' % pS[0], color='w', fontsize=8)
722
+ ax.text(len(xn2)/2, max(id[0]) + 4 * np.sqrt(max(id[0])), 'ΔN0 = %.1f ' %(abs(pS[6]) / (pCAL[0] + pS[6]*(1+np.sign(pS[6])) / 2) * 100) +'%', color='w', fontsize=8, horizontalalignment='center')
723
+ ax.text(len(xn2), max(id[0]) + 4 * np.sqrt(max(id[0])), 'impurity %.1f ' %(pS[11]/(pS[9]+pS[11])*100) +'%', color='w', fontsize=8, horizontalalignment='right')
724
+
725
+ ax.text(len(xn2)/2, max(id[0]) + 10 * np.sqrt(max(id[0])), str('lin ')*(method==1) + str('sin ')*(method==0) + str(n1) + str(' ') + str(n2), color='r', fontsize=8, horizontalalignment='center')
726
+
727
+ fig.savefig('calibr.png', bbox_inches='tight')
728
+ plt.close()
729
+
730
+ print('Shift due to instrumental function ', INS_shift)
731
+ print('Velocity range: ', min(xT2), ' to ', max(xT2), 'mm/s, absolute shift ', (max(xT2)+min(xT2))/2 )
732
+ print('Velocity amplitude is', pS[0], 'mm/s')
733
+ print('Source shift is', pS[2], 'mm/s')
734
+ print('difference of N0:', abs(pS[6]) / (pCAL[0] + pS[6]*(1+np.sign(pS[6])) / 2) * 100, '%')
735
+ print('impurity is', pS[11]/(pS[9]+pS[11])*100, '%')
736
+ print('maximum deviation is ', max(np.abs(spc2-spc3)), 'or ', max(np.abs(spc2-spc3))/pCAL[0], '%')
737
+ # if VVV == 3:
738
+ print('Texture parameter is', pS[10], 'should be 0.5 for isotropic')
739
+
740
+
741
+ if platform.system() == 'Windows':
742
+ rpath = str(dir_path) + str('\\\\Calibration.dat')
743
+ else:
744
+ rpath = str(dir_path) + str('/Calibration.dat')
745
+
746
+ f = open(rpath, "w")
747
+ f.write(str('#') + '\t' + str('lin ')*(method==1) + str('sin ')*(method==0) + '\t' + str(n1) + '\t' + str(n2) + '\n')
748
+ for i in range(0, int(len(xT2))):
749
+ f.write(str(xT2[i]) + '\t' + str(spc2[i]) + '\n')
750
+ f.write('\n')
751
+ f.close()
752
+
753
+ pCAL2 = pCAL
754
+ pCAL2[number_of_baseline_parameters] = pS[9]
755
+ pCAL2[number_of_baseline_parameters + 8] = pS[11]
756
+
757
+ return(xT2, spc2, spc3)