-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
312 lines (249 loc) · 8.41 KB
/
main2.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 10 10:26:59 2022
@author: berej
"""
from genericpath import isfile
from symtable import Symbol
from wsgiref import simple_server
import numpy as np
import os
import sympy
from matplotlib import pyplot as plt
from matplotlib import image
from astropy.io import fits
from mpmath import *
from natsort import natsorted
from mpl_toolkits.mplot3d import Axes3D
lamb = 630e-9
lamb_micro = 0.630
c = 3.00e8
h = 6.63e-34
nu = c/lamb
k = 1.38e-23
mp.dps = 300
lamb_1 = 1/lamb_micro
lamb_5 = 1/lamb_micro**5
def average(l):
return sum(l)/len(l)
def path(decon,year,idstr=''):
path = os.getcwd()
if decon:
path = os.path.join(path, 'decondata'+idstr+year)
else:
path = os.path.join(path, 'data'+idstr+year)
return path
def trapint(y,x,a,b):
intsum = 0
for i in range(a,b):
trapval = (x[i]-x[i-1])*0.5*(y[i]+y[i-1])
intsum += trapval
return intsum
def blamb(T, h=h,c=c,lamb=lamb,nu=nu, k=k):
return ((2*h*nu**3)/c**2)*(1/(exp(h*nu/(k*T))-1))
def matrix_max(N):
maxval = 0
for i in N:
for j in i:
if j > maxval:
maxval = j
return maxval
intensity = []
bpl = []
iupfit = []
bupfit = []
idownfit = []
bdownfit = []
angle = []
avint = []
avinthighmag = []
P_corr = []
N = 49
z = []
k = [[] for i in range(11)]
taulist = [[] for i in range(11)]
B = []
I = []
T = []
G = [i for i in range(200,1300,100)]
print('Available data sets :')
print('2008')
print('2014')
year = input('select data set : ')
if year == '2014':
alpha = 0.83 #calculating alpha dynamically is pointless in this application, we only have two sets of data.
else:
alpha = 1
print('Deconvoluted?')
decon_choice = input('yes or no? (defaults to yes) : ')
if decon_choice == 'no':
decon = False
else:
decon = True
wpath = path(decon,year)
files = [f for f in os.listdir(wpath) if os.path.isfile(os.path.join(wpath, f))]
for q in range(len(files)):
if decon:
angle_value = float(files[q].lstrip('image-muD').rstrip('.fits').strip('='))
else:
angle_value = float(files[q].lstrip('image-mu').rstrip('.fits').strip('='))
angle.append(angle_value)
print('possible angles : ', angle)
imagenum = input('select mu value (mu = cos(theta), closer to the center is closer to 1) : ')
mu = abs(float(imagenum))
if decon:
current_file='imageD-mu='+imagenum+'.fits'
else:
current_file='image-mu='+imagenum+'.fits'
readuplimit = input('B mode limit? (leave empty for 1250G) : ')
if readuplimit == '':
uplimit = 1250
else:
uplimit = int(readuplimit)
P_mag = uplimit**2/(8*np.pi)
write = input('write data? (defaults to no) : ')
if write == 'yes':
writef = True
else:
writef = False
imname = os.path.join(wpath, current_file)
hdul = fits.open(imname)
hdul.info()
idata = hdul[0].data
x_var, y_var = idata.shape
x_arr = [i for i in range(x_var)]
y_arr = [i for i in range(y_var)]
plt.figure('Heatmap of fits data for '+year+' '+current_file)
plt.title('Heatmap of fits data for '+year+' '+current_file)
plt.imshow(idata, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.show()
hdul.close()
wpath = path(decon, year, 'mag')
if decon:
current_file='BFe2D-mu='+imagenum+'.fits'
else:
current_file='BFe2-mu='+imagenum+'.fits'
imname = os.path.join(wpath, current_file)
hdul = fits.open(imname)
hdul.info()
magdata = np.absolute(hdul[0].data)
xmax, ymax = magdata.shape
#print(xmax, ymax)
#print(xmax//2,ymax//2)
#print(magdata)
plt.figure('Heatmap of magnetic field data for '+year+' '+current_file)
plt.title('Heatmap of magnetic field data for '+year+' '+current_file)
plt.imshow(magdata, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.show()
hdul.close()
for i in range(xmax):
for j in range(ymax):
mag = magdata[i][j]
its = idata[i][j]*(1/alpha)
if mag <= 100:
avint.append(its)
if mag >= 2000:
avinthighmag.append(its)
elif mag > 200 and mag <= uplimit:
iupfit.append(its)
bupfit.append(mag)
elif mag > uplimit:
idownfit.append(its)
bdownfit.append(mag*(np.absolute(1/float(imagenum))))
bpl.append(mag)
intensity.append(its)
plt.figure('I(B) ' +current_file)
plt.title('Intensity according to parallel B for '+year+' '+current_file)
plt.scatter(bpl,intensity)
plt.xlabel('B parallel (in Gauss)')
plt.ylabel('I (in hits per pixel)')
plt.show()
# =============================================================================
# plt.figure('hist of B '+current_file)
# plt.title('Histogram of parallel B for '+year+' '+current_file)
# plt.yscale('log')
# plt.hist(bpl,bins=1000)
# plt.show()
# =============================================================================
# =============================================================================
# plt.figure('linear fit '+current_file)
# plt.title('Linear fit for intensity according to parallel B for '+year+' '+current_file)
# =============================================================================
(m1, b1), cov1 = np.polyfit(bupfit, iupfit, 1, cov=True)
u1 = np.sqrt(np.diag(cov1))[0]
x1 = np.sort(np.array(bupfit))
try:
(m2, b2), cov2 = np.polyfit(bdownfit, idownfit, 1, cov=True)
u2 = np.sqrt(np.diag(cov2))[0]
x2 = np.sort(np.array(bdownfit))
#plt.scatter(bdownfit, idownfit, color='lightsalmon', marker='o')
#plt.plot(x2, m2*x2+b2, 'r', label = str(m2)+'*B+'+str(b2))
rm2 = str(round(m2,3))
ru2 = str(round(u2,3))
except:
rm2 = 'N/A'
ru2 = 'N/A'
y1 = m1*x1+b1
average_int = average(avint)
max_int = min(y1)
# =============================================================================
# for i in range(200,1300,100):
# G.append(i)
# =============================================================================
f=open('model-ProjetL3.txt','r')
text = f.readlines()
for i in range(1,N+1):
line = text[i].split()
z.insert(0,float(line[1]))
T.insert(0,float(line[2]))
f.close()
wpath = os.path.join(os.getcwd(),'k')
files = natsorted([f for f in os.listdir(wpath) if os.path.isfile(os.path.join(wpath, f))])
for i in range(len(files)):
f = open(os.path.join(wpath,files[i]),'r')
text = f.readlines()
for j in range(N):
k[i].insert(0,float(text[j]))
f.close()
for i in range(len(k)):
for h in range(1,N+1):
taulist[i].append(trapint(k[i],z,h,N))
for tau in taulist:
intsum = 0
for i in reversed(range(0,N-1)):
trapval = 0.5*((tau[i]-tau[i+1])*(blamb(T[i])*exp(-1*tau[i]/mu)+blamb(T[i+1])*exp(-1*tau[i+1]/mu)))/mu
intsum += trapval
I.append(intsum)
plt.figure('I(B) theoretical')
plt.title('I(B) theoretical')
plt.scatter(bupfit, iupfit/max_int, color='lightblue', marker='o')
plt.plot(x1, y1/max_int, 'b', label = str(m1)+'*B+'+str(b1))
plt.plot(G,np.array(I)/min(I), label = 'theoretical I(B)')
plt.xlabel('Parallel B (in Gauss)')
plt.ylabel('Normalised intensity')
plt.legend()
plt.show()
#(os.getcwd())
if writef:
f = open('results'+year+'.txt','a')
f.write('mu = {mu}, upcoef = {m1:.3f}, u(upcoef) = {u1:.3f}, downcoef = {rm2}, u(downcoef) = {ru2} | avint = {avint:.3f}\n'.format(mu = imagenum, m1 = float(m1), u1 = float(u1), rm2 = rm2, ru2 = ru2, avint = float(average(avint))))
f.close()
print('intensity average between -100 and 100 Gauss: '+str(average(avint)))
print('intesnity average higher than 2000 Gauss : '+str(average(avinthighmag)))
print('high/low field coefficient : '+str(average(avinthighmag)/average(avint)))
# =============================================================================
# [yy,xx] = np.meshgrid(y_arr, x_arr)
#
# for i in range(len(idata)):
# for j in range(len(idata[0])):
# if idata[i][j] < 24000:
# idata[i][j] = 24000
#
# fig = plt.figure('magdata peaks and valleys')
# ax = fig.gca(projection='3d')
# ax.set_box_aspect([len(x_arr)/len(y_arr),1,1])
# ax.plot_surface(xx, yy, idata/matrix_max(idata), cmap = 'hot')
# ax.plot_surface(xx, yy, magdata/matrix_max(magdata), cmap = 'gnuplot')
# =============================================================================