-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSHG_Solex_recon.py
More file actions
726 lines (600 loc) · 27.4 KB
/
Copy pathDSHG_Solex_recon.py
File metadata and controls
726 lines (600 loc) · 27.4 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# -*- coding: utf-8 -*-
"""
@author: Valerie Desnoux
with improvements by Andrew Smith
contributors: Jean-Francois Pittet, Jean-Baptiste Butet, Pascal Berteau, Matt Considine
Version 13 August 2021
------------------------------------------------------------------------
reconstruction on an image from the deviations between the minimum of the line and a reference line
calcul sur une image des ecarts simples entre min de la raie et une ligne de reference
-------------------------------------------------------------------------
"""
from DSHG_solex_util import *
from DSHG_ser_read_video import *
from DSHG_ellipse_to_circle_JFP import ellipse_to_circle, correct_image
#import numpy_indexed as npi #MattC
from scipy import signal, fftpack #MattC
import astropy.units as u #MattC
from astropy.time import Time #MattC
from astropy.coordinates import SkyCoord, EarthLocation, get_sun, AltAz #MattC
#import xlsxwriter #MattC
# read video and return constructed image of sun using fit and LineRecal
'''
def read_video_improved(serfile, fit, LineRecal, options):
rdr = ser_reader(serfile)
ih, iw = rdr.ih, rdr.iw
if options['flag_display']:
cv2.namedWindow('disk', cv2.WINDOW_NORMAL)
FrameMax=rdr.FrameCount
cv2.resizeWindow('disk', FrameMax//3, ih//3)
cv2.moveWindow('disk', 200, 0)
#initialize le tableau qui va recevoir la raie spectrale de chaque trame
Disk=np.zeros((ih,FrameMax), dtype=rdr.infiledatatype) #MattC
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.moveWindow('image', 0, 0)
cv2.resizeWindow('image', int(iw), int(ih))
else:
#Disk=np.zeros((ih,1), dtype='uint16')
FrameMax=rdr.FrameCount
Disk=np.zeros((ih,FrameMax), dtype=rdr.infiledatatype) #MattC
shift = options['shift']
ind_l = (np.asarray(fit)[:, 0] + np.ones(ih) * (LineRecal + shift )).astype(int) #MattC
#CLEAN if fitting goes too far
ind_l[ind_l < 0] = 0
ind_l[ind_l > iw - 2] = iw - 2
#print('ind l ',ind_l[0:49]) #MattC
#print('ind l shape ',ind_l.shape) #MattC
ind_r = (ind_l + np.ones(ih)).astype(int)
#print('ind r ',ind_r[0:49]) #MattC
#print('ind r shape ',ind_r.shape) #MattC
left_weights = np.ones(ih) - np.asarray(fit)[:, 1]
right_weights = np.ones(ih) - left_weights
# lance la reconstruction du disk a partir des trames
#print('L Weights : ', left_weights.shape) #MattC
#print('R Weights : ', right_weights.shape) #MattC
#print('reader num frames:', rdr.FrameCount) #MattC
##an_array = np.arange(0,iw)
##repetitions = ih
##testmask = np.tile(an_array, (repetitions))#.reshape(ih,iw)
##print('testmask shape',testmask.shape)
col_mask = np.tile(range(0,options['pixel_bandwidth']),ih).reshape(ih,options['pixel_bandwidth']) #MattC
#print('col mask shape',col_mask.shape) #MattC
#print('col mask data ',col_mask[0:49,:])
while rdr.has_frames():
img = rdr.next_frame()
#img2 = img.reshape(ih*iw)
if options['flag_display'] and rdr.FrameIndex % 10 == 0 :
cv2.imshow('image', img)
if cv2.waitKey(1)==27:
cv2.destroyAllWindows()
sys.exit()
if options['pixel_bandwidth'] >= 0: #MattC TODO: need to test this against prior default
left_banda = np.repeat(ind_l, options['pixel_bandwidth'],axis=0).reshape(ih,options['pixel_bandwidth'])
left_band = left_banda - col_mask
mx1 = np.take_along_axis(img,left_band.reshape(ih,options['pixel_bandwidth']),1)
left_col = np.mean(mx1,axis=1)
right_banda = np.repeat(ind_r, options['pixel_bandwidth'],axis=0).reshape(ih,options['pixel_bandwidth'])
right_band = right_banda + col_mask
mx2 = np.take_along_axis(img,right_band.reshape(ih,options['pixel_bandwidth']),1)
right_col = np.mean(mx2,axis=1)
#
#if rdr.FrameIndex == 2000: #MattC
# print('FRAMEINDEX ',rdr.FrameIndex)
# #print('mx1 shape ',mx1.shape)
# #print('mx2 shape ',mx2.shape)
# #print('mx1 type ',type(mx1))
# #print('mx2 type ',type(mx2))
# print('img data ',img[0:49,:])
# print('left banda data ',left_banda[0:49,:])
# print('left band data ',left_band[0:49,:])
# print('mx1 data ',mx1[0:49,:])
# print('left col data ',left_col[0:49])
# print('right banda data ',right_banda[0:49,:])
# print('right band data ',right_band[0:49,:])
# print('mx2 data ',mx2[0:49,:])
# print('right col data ',right_col[0:49])
#
mx3 = np.hstack((mx1,mx2))
#
#if rdr.FrameIndex == 2000: #MattC
# print('mx3 shape ',mx3.shape)
# print('mx3 data ',mx3[0:49,:])
#
IntensiteRaie = np.mean(mx3, axis=1) #MattC could be any function, techically
#
#if rdr.FrameIndex == 2000: #MattC
# print('Intens shape ',IntensiteRaie.shape)
# print('Intens data ',IntensiteRaie[0:49].astype(rdr.infiledatatype))
#
#else:
# left_col = img[np.arange(ih), ind_l]
# right_col = img[np.arange(ih), ind_r]
# IntensiteRaie = left_col*left_weights + right_col*right_weights
#ajoute au tableau disk
Disk[:,rdr.FrameIndex]=IntensiteRaie.astype(rdr.infiledatatype) #MattC
if options['flag_display'] and rdr.FrameIndex % 10 ==0:
cv2.imshow ('disk', Disk)
if cv2.waitKey(1) == 27: # exit if Escape is hit
cv2.destroyAllWindows()
sys.exit()
if rdr.infiledatatype == 'uint8': #MattC deal with AVI scale
Disk = (Disk*(65535/255))
return Disk, ih, iw, rdr.FrameCount
'''
def read_video_improved(serfile, fit, LineRecal, options):
rdr = ser_reader(serfile)
ih, iw = rdr.ih, rdr.iw
FrameMax=rdr.FrameCount
disk_list = [np.zeros((ih,FrameMax), dtype='uint16') for _ in options['shift']]
if options['flag_display']:
cv2.namedWindow('disk', cv2.WINDOW_NORMAL)
cv2.resizeWindow('disk', FrameMax//3, ih//3)
cv2.moveWindow('disk', 200, 0)
#initialize le tableau qui va recevoir la raie spectrale de chaque trame
#Disk=np.zeros((ih,FrameMax), dtype=rdr.infiledatatype) #MattC
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.moveWindow('image', 0, 0)
cv2.resizeWindow('image', int(iw), int(ih))
#else: #MattC
# #Disk=np.zeros((ih,1), dtype='uint16')
# FrameMax=rdr.FrameCount
# Disk=np.zeros((ih,FrameMax), dtype=rdr.infiledatatype) #MattC
col_indeces = []
#shift = options['shift']
#ind_l = (np.asarray(fit)[:, 0] + np.ones(ih) * (LineRecal + shift )).astype(int) #MattC
for shift in options['shift']:
ind_l = (np.asarray(fit)[:, 0] + np.ones(ih) * (LineRecal + shift)).astype(int)
#CLEAN if fitting goes too far
ind_l[ind_l < 0] = 0
ind_l[ind_l > iw - 2] = iw - 2
ind_r = (ind_l + np.ones(ih)).astype(int)
col_indeces.append((ind_l, ind_r))
left_weights = np.ones(ih) - np.asarray(fit)[:, 1]
right_weights = np.ones(ih) - left_weights
# lance la reconstruction du disk a partir des trames
print('reader num frames:', rdr.FrameCount)
col_mask = np.tile(range(0,options['pixel_bandwidth']),ih).reshape(ih,options['pixel_bandwidth']) #MattC
if rdr.scalemax == 255: #MattC
ascalefactor = 255
options['scalemax'] = 255
else:
ascalefactor = 1
options['scalemax'] = 64000
while rdr.has_frames():
img = rdr.next_frame()
if options['flag_display'] and rdr.FrameIndex % 10 == 0 :
cv2.imshow('image', img)
if cv2.waitKey(1)==27:
cv2.destroyAllWindows()
sys.exit()
for i in range(len(options['shift'])):
ind_l, ind_r = col_indeces[i]
if options['pixel_bandwidth'] > 1: #MattC TODO: need to test this against prior default
left_banda = np.repeat(ind_l, options['pixel_bandwidth'],axis=0).reshape(ih,options['pixel_bandwidth'])
left_band = left_banda - col_mask
mx1 = np.take_along_axis(img,left_band.reshape(ih,options['pixel_bandwidth']),1)
left_col = np.mean(mx1,axis=1)
right_banda = np.repeat(ind_r, options['pixel_bandwidth'],axis=0).reshape(ih,options['pixel_bandwidth'])
right_band = right_banda + col_mask
mx2 = np.take_along_axis(img,right_band.reshape(ih,options['pixel_bandwidth']),1)
right_col = np.mean(mx2,axis=1)
mx3 = np.hstack((mx1,mx2))
IntensiteRaie = np.mean(mx3, axis=1) #MattC could be any function, techically
else:
left_col = img[np.arange(ih), ind_l]
right_col = img[np.arange(ih), ind_r]
IntensiteRaie = left_col*left_weights + right_col*right_weights
#ajoute au tableau disk
#Disk[:,rdr.FrameIndex]=IntensiteRaie.astype(rdr.infiledatatype) #MattC
#if options['hflip']:
# disk_list[i][:,rdr.FrameCount-rdr.FrameIndex-1]=IntensiteRaie.astype(rdr.infiledatatype) #MattC
#else:
if 1:
disk_list[i][:,rdr.FrameIndex]=IntensiteRaie.astype(rdr.infiledatatype) #MattC
if options['flag_display'] and rdr.FrameIndex % 10 ==0:
cv2.imshow ('disk', disk_list[1]*ascalefactor) # disk_list[1] is always shift = 0 #MattC
if cv2.waitKey(1) == 27: # exit if Escape is hit
cv2.destroyAllWindows()
sys.exit()
if rdr.infiledatatype == 'uint8': #MattC deal with AVI scale
for i in range(len(options['shift'])):
disk_list[i] = (disk_list[i]*(64000/255)).astype('uint16')
return disk_list, ih, iw, rdr.FrameCount
def make_header(rdr):
# initialisation d'une entete fits (etait utilisé pour sauver les trames individuelles)
hdr= fits.Header()
hdr['SIMPLE']='T'
hdr['BITPIX']=32
hdr['NAXIS']=2
hdr['NAXIS1'] = rdr.iw
hdr['NAXIS2'] = rdr.ih
hdr['BZERO']=0
hdr['BSCALE']=1
hdr['BIN1']=1
hdr['BIN2']=1
hdr['EXPTIME']=0
return hdr
# compute mean image of video
def compute_mean(serfile, options): #MattC
"""IN : serfile path"
OUT :numpy array
"""
rdr = ser_reader(serfile)
logme('Width, Height : '+str(rdr.Width)+' '+str(rdr.Height))
logme('Number of frames : '+str(rdr.FrameCount))
my_data = np.zeros((rdr.ih, rdr.iw),dtype='uint64')
if options['phasecorr'] == True: #MattC I'm pretty sure this isn't even close to working. Shifts seem reasonable
#but I haven't been successful in reflecting them in the image. Getting tripped up by ... something.
my_shift_data = np.zeros((rdr.FrameCount),dtype='int16') #NOTE: need a signed int here
testcount = 0
while rdr.has_frames():
img = rdr.next_frame()
#phase correlation for shifts: https://stackoverflow.com/questions/4688715/find-time-shift-between-two-similar-waveforms
if testcount == 0: #MattC https://stackoverflow.com/questions/35567906/how-to-apply-phase-correlation-in-1d-signal
#establish the slice from which movement is judged
baseimg = img[:,rdr.iw//2]
fft_sig1 = fftpack.fft(img[:,rdr.iw//2])
Ar = -fft_sig1.conjugate()
#fft_sig1 = np.fft.fft(img[:,rdr.iw//2])
my_shift_data[testcount] = 0
shift_img = img
else:
'''
fft_sig2 = np.fft.fft(img[:,rdr.iw//2])
fft_sig2_conj = np.conj(fft_sig2)
R = (fft_sig1 * fft_sig2_conj) / abs(fft_sig1 * fft_sig2_conj)
r = np.fft.ifft(R)
pix_shift = np.argmax(r)
if testcount % 500 == 0:
print('pixel shift = ', testcount, pix_shift)
my_shift_data[testcount] = pix_shift
'''
fft_sig2 = fftpack.fft(img[:,rdr.iw//2])
Br = -fft_sig2.conjugate()
pix_shift = np.argmax(np.abs(fftpack.ifft(Ar*fft_sig2)))
print(pix_shift)
#pix_shift2 = np.argmax(np.abs(fftpack.ifft(fft_sig1*Br)))
my_shift_data[testcount] = pix_shift
#NOTE: I have no idea yet if this is really working; shift data still needs to go to Disk
shift_img = np.zeros_like(img) #We're going to shift the entire image
shift_img = np.roll(img,-pix_shift,axis=0) #shift up/down #TODO: preserve rolled rows
fft_sig1 = fft_sig2 #fftpack.fft(shift_img[:,rdr.iw//2]) #????
Ar = -fft_sig1.conjugate()
testcount += 1 #MattC
my_data += shift_img #MattC shift data should be passed back so that smile calc/fit can accomodate
else:
while rdr.has_frames():
img = rdr.next_frame()
my_data += img
#workbook = xlsxwriter.Workbook('arrays.xlsx')
#worksheet = workbook.add_worksheet()
#row = 0
#for col, data in enumerate(my_shift_data.T):
# worksheet.write_column(row, col, data)
#workbook.close()
return (my_data / rdr.FrameCount).astype(rdr.infiledatatype) #MattC
def compute_mean_return_fit(serfile, options, LineRecal = 1):
global hdr, ih, iw
"""
----------------------------------------------------------------------------
Reconstuit l'image du disque a partir de l'image moyenne des trames et
des trames extraite du fichier ser avec un fit polynomial
Corrige de mauvaises lignes et transversallium
basefich: nom du fichier de base de la video sans extension, sans repertoire
shift: ecart en pixel par rapport au centre de la raie pour explorer longueur d'onde decalée
----------------------------------------------------------------------------
"""
flag_display = options['flag_display']
# first compute mean image
# rdr is the ser_reader object
mean_img= compute_mean(serfile, options) #MattC
"""
----------------------------------------------------------------------------
Calcul polynome ecart sur une image au centre de la sequence
----------------------------------------------------------------------------
"""
#savefich=basefich+'_mean'
if options['save_fit']:
DiskHDU=fits.PrimaryHDU(mean_img,header=hdr)
DiskHDU.writeto(basefich0+'_mean.fits', overwrite='True')
#affiche image moyenne
if flag_display:
cv2.namedWindow('Video mean', cv2.WINDOW_NORMAL) #MattC
cv2.resizeWindow('Video mean', iw, ih) #MattC
cv2.moveWindow('Video mean', 100, 0) #MattC
cv2.imshow ('Video mean', mean_img) #MattC
if cv2.waitKey(2000) == 27: # exit if Escape is hit
cv2.destroyAllWindows()
sys.exit()
cv2.destroyAllWindows()
y1,y2=detect_bord(mean_img, axis=1, offset=5)
logme('Vertical limits y1, y2 : '+str(y1)+' '+str(y2))
PosRaieHaut=y1
PosRaieBas=y2
"""
-----------------------------------------------------------
Trouve les min intensité de la raie
-----------------------------------------------------------
"""
# construit le tableau des min de la raie a partir du haut jusqu'en bas
MinOfRaie=[]
for i in range(PosRaieHaut,PosRaieBas):
line_h=mean_img[i,:]
MinX=line_h.argmin()
MinOfRaie.append([MinX,i])
#print('MinOfRaie x,y', MinX,i)
#best fit d'un polynome degre 2, les lignes y sont les x et les colonnes x sont les y
np_m=np.asarray(MinOfRaie)
xm,ym=np_m.T
#LineRecal=xm.min()
p=np.polyfit(ym,xm,2)
#calcul des x colonnes pour les y lignes du polynome
a=p[0]
b=p[1]
c=p[2]
fit=[]
#ecart=[]
for y in range(0,ih):
x=a*y**2+b*y+c
deci=x-int(x)
fit.append([int(x)-LineRecal,deci,y])
return fit, a, b, c
def correct_bad_lines_and_geom(Disk, options, not_fake):
global hdr, basefich
iw=Disk.shape[1]
ih=Disk.shape[0]
img=Disk
y1,y2=detect_bord (img, axis=1,offset=5) # bords verticaux
#detection de mauvaises lignes
# somme de lignes projetées sur axe Y
ysum=np.mean(img,1)
#plt.plot(ysum)
#plt.show()
# ne considere que les lignes du disque avec marge de 15 lignes
ysum=ysum[y1+15:y2-15]
# filtrage sur fenetre de 31 pixels, polynome ordre 3 (etait 101 avant)
yc=savgol_filter(ysum,31, 3)
# divise le profil somme par le profil filtré pour avoir les hautes frequences
hcol=np.divide(ysum,yc)
# met à zero les pixels dont l'intensité est inferieur à 1.03 (3%)
hcol[abs(hcol-1)<=0.03]=0
# tableau de zero en debut et en fin pour completer le tableau du disque
a=[0]*(y1+15)
b=[0]*(ih-y2+15)
hcol=np.concatenate((a,hcol,b))
#plt.plot(hcol)
#plt.show()
# creation du tableau d'indice des lignes a corriger
l_col=np.where(hcol!=0)
listcol=l_col[0]
# correction de lignes par filtrage median 13 lignes, empririque
img_copy = np.copy(img)
for c in listcol:
m=img[c-7:c+6,]
s=np.median(m,0)
img_copy[c-1:c,]=s
#sauvegarde le fits
if options['save_fit'] and not_fake:
DiskHDU=fits.PrimaryHDU(img_copy,header=hdr)
DiskHDU.writeto(basefich+'_corr.fits', overwrite='True')
return img_copy
def correct_transversalium(img, flag_nobords, options, not_fake):
global hdr, ih, basefich
frame = img
newiw=img.shape[1]
ih=img.shape[0]
flag_nobords = False
# on cherche la projection de la taille max du soleil en Y
y1,y2=detect_bord(frame, axis=1,offset=0)
#print ('flat ',y1,y2)
# si mauvaise detection des bords en x alors on doit prendre toute l'image
if flag_nobords:
ydisk=np.median(img,1)
else:
#plt.hist(frame.ravel(),bins=1000,)
#plt.show()
#plt.hist(frame.ravel(),bins=1000,cumulative=True)
# plt.show()
seuil_bas=np.percentile(frame,25)
seuil_haut=np.percentile(frame,97)
#print ('Seuils de flat: ',seuil_bas, seuil_haut)
#print ('Seuils bas x: ',seuil_bas*4)
#print ('Seuils haut x: ',seuil_haut*0.25)
#myseuil=seuil_haut*0.2
myseuil=seuil_haut*0.5
# filtre le profil moyen en Y en ne prenant que le disque
ydisk=np.empty(ih+1)
for j in range(0,ih):
temp=np.copy(frame[j,:])
temp=temp[temp>myseuil]
if len(temp)!=0:
ydisk[j]=np.median(temp)
else:
ydisk[j]=1
y1=y1
y2=y2
ToSpline= ydisk[y1:y2]
Smoothed2=savgol_filter(ToSpline,301, 3) # window size, polynomial order
Smoothed2[ToSpline==np.min(ToSpline)]=0.01 #MattC
#best fit d'un polynome degre 4
np_m=np.asarray(ToSpline)
ym=np_m.T
xm=np.arange(y2-y1)
p=np.polyfit(xm,ym,4)
#calcul des x colonnes pour les y lignes du polynome
a=p[0]
b=p[1]
c=p[2]
d=p[3]
e=p[4]
'''
Smoothed=[]
for x in range(0,y2-y1):
y=a*x**4+b*x**3+c*x**2+d*x+e
Smoothed.append(y)
'''
x = np.arange(y2-y1)
Smoothed=a*x**4+b*x**3+c*x**2+d*x+e
# divise le profil reel par son filtre ce qui nous donne le flat
hf=np.divide(ToSpline,Smoothed2)
# elimine possible artefact de bord
hf=hf[5:-5]
#reconstruit le tableau du pofil complet
a=[1]*(y1+5)
b=[1]*(ih-y2+5)
hf=np.concatenate((a,hf,b))
Smoothed=np.concatenate((a,Smoothed,b))
ToSpline=np.concatenate((a,ToSpline,b))
Smoothed2=np.concatenate((a,Smoothed2,b))
# genere tableau image de flat
flat=[]
hf = np.array(hf) / max(0.9, min(hf)) # don't make things bigger
hf[hf==0] = 1
for i in range(0,newiw):
flat.append(hf)
np_flat=np.asarray(flat)
flat = np_flat.T
#print(hf, sum(hf)/len(hf), max(hf), min(hf))
# divise image par le flat
BelleImage=np.divide(frame,flat)
frame=np.array(BelleImage, dtype='uint16')
# sauvegarde de l'image deflattée
if options['save_fit'] and not_fake:
DiskHDU=fits.PrimaryHDU(frame,header=hdr)
DiskHDU.writeto(basefich+'_flat.fits', overwrite='True')
return frame
def solex_proc(serfile, options):
global hdr, ih, iw, basefich0, basefich
clearlog()
#plt.gray() #palette de gris si utilise matplotlib pour visu debug
logme('Using pixel shift : ' + str(options['shift']))
options['shift'] = [10, 0] + options['shift'] # 10, 0 are "fake"
WorkDir=os.path.dirname(serfile)+"/"
os.chdir(WorkDir)
base=os.path.basename(serfile)
basefich0=os.path.splitext(base)[0]
LineRecal=1
rdr = ser_reader(serfile)
hdr = make_header(rdr)
ih = rdr.ih
iw = rdr.iw
fit, a, b, c = compute_mean_return_fit(serfile, options, LineRecal)
# Modification Jean-Francois: correct the variable names: A0, A1, A2
logme('Coeff A0, A1, A2 : '+str(a)+' '+str(b)+' '+str(c))
disk_list, ih, iw, FrameCount = read_video_improved(serfile, fit, LineRecal, options)
hdr['NAXIS1']=iw # note: slightly dodgy, new width
#sauve fichier disque reconstruit
'''
Start of angle/shear test ... MattC
'''
'''
print(rdr.DTime)
print(rdr.DTimeUTC)
the_location = EarthLocation.from_geodetic(lat=45.2*u.deg, lon=-70*u.deg, height=330*u.m)
utcoffset = -4*u.hour
startobstime = Time('2021-07-22 10:58:23') - utcoffset
timeframe = AltAz(obstime=startobstime, location=the_location)
startsunaltaz = get_sun(startobstime).transform_to(timeframe)
print("Sun's altitude = {0.alt:.8}".format(startsunaltaz))
print("Sun's alzimuth = {0.az:.8}".format(startsunaltaz))
endobstime = startobstime + 2*u.min
timeframe = AltAz(obstime=endobstime, location=the_location)
endsunaltaz = get_sun(endobstime).transform_to(timeframe)
print("Sun's altitude = {0.alt:.8}".format(endsunaltaz))
print("Sun's azimuth = {0.az:.8}".format(endsunaltaz))
print(endsunaltaz.alt-startsunaltaz.alt)
print(endsunaltaz.az-startsunaltaz.az)
the_slope = ((endsunaltaz.alt-startsunaltaz.alt)/(endsunaltaz.az-startsunaltaz.az))/2
slope_rad=math.atan(the_slope)
slope_deg=math.degrees(slope_rad)
print(the_slope)
print(slope_deg)
print("strip width in pixels : ",disk_list[0].shape[1])
print("strip height in pixels : ",disk_list[0].shape[0])
plt.imshow(disk_list[0])
plt.show()
rows, cols = disk_list[0].shape
angle_shs = 2*slope_deg #rotation about center; undo shear then add angle
shearx_shs = 0*the_slope #positive leans to left; anchors on bottom
sheary_shs = the_slope #positive skews down from left; anchors on left
transl_shs = 0
type_border = cv2.BORDER_CONSTANT
color_border = (255,255,255)
###Calc space needed for rotation
##M=cv2.getRotationMatrix2D((cols/2,rows/2), 45, 1) #45 would be max
##cos_part = np.abs(M[0,0])
##sin_part = np.abs(M[0,1])
##new_cols = int((rows * sin_part) + (cols * cos_part))
##new_rows = int((rows * cos_part) + (cols * sin_part))
###Calc space needed for shear
##new_cols = new_cols + (shearx_shs*new_cols)
##new_rows = new_rows + (sheary_shs*new_rows)
new_rows = max(rows,cols)
new_cols = max(rows,cols)+2*rows
#Calc space needed for border
up_down = int((new_rows-rows)/2)
left_right = int((new_cols-cols)/2)
sheared_Disk = cv2.copyMakeBorder(disk_list[0], up_down, up_down, left_right, left_right, type_border, value=color_border)
plt.axis('off')
plt.imshow(sheared_Disk)
plt.show()
rows, cols = sheared_Disk.shape
#Apply transform
M_rot = cv2.getRotationMatrix2D((cols/2, rows/2),angle_shs,1)
translat_center_x = 0
translat_center_y = 0
translat_center_x = -(shearx_shs*cols)/2
translat_center_y = -(sheary_shs*rows)/2
M = M_rot + np.float64([[0,shearx_shs,transl_shs + translat_center_x], [sheary_shs,0,transl_shs + translat_center_y]])
sheared_Disk = cv2.warpAffine(sheared_Disk, M, (cols,rows), borderMode=type_border, borderValue=color_border)
sheared_Disk = cv2.flip(sheared_Disk, 1)
plt.axis('off')
plt.imshow(sheared_Disk)
plt.show()
#options['slant_fix']=slope_deg
# ... end of angle test MattC
'''
if options['flag_display']:
cv2.destroyAllWindows()
cercle = (-1, -1, -1)
frames_circularized = []
for i in range(len(disk_list)):
basefich = basefich0 + '_hbw='+str(options['pixel_bandwidth']) + '_shift='+str(options['shift'][i]) #MattC
#disk_list[i] = np.fliplr(disk_list[i]) #MattC
if options['save_fit'] and i >= 2:
DiskHDU=fits.PrimaryHDU(disk_list[i],header=hdr)
DiskHDU.writeto(basefich+'_img.fits', overwrite='True')
"""
--------------------------------------------------------------------
--------------------------------------------------------------------
Badlines and geometry
--------------------------------------------------------------------
--------------------------------------------------------------------
"""
img = correct_bad_lines_and_geom(disk_list[i], options, i >= 2)
"""
--------------------------------------------------------------
transversallium correction
--------------------------------------------------------------
"""
flag_nobords = False
frame_flatted = correct_transversalium(img,flag_nobords, options, i >= 2)
"""
We now apply ellipse_fit to apply the geometric correction
"""
# disk_list[0] is always shift = 10, for more contrast for ellipse fit
if options['ratio_fixe'] is None and options['slant_fix'] is None:
frame_circularized, cercle, options['ratio_fixe'], phi = ellipse_to_circle(frame_flatted, options)
options['slant_fix'] = math.degrees(phi) # in options angles are stored as degrees for some reason
frames_circularized.append(frame_circularized)
else:
ratio = options['ratio_fixe'] if not options['ratio_fixe'] is None else 1.0
phi = math.radians(options['slant_fix']) if not options['slant_fix'] is None else 0.0
frames_circularized.append(correct_image(frame_flatted / 65536, phi, ratio, np.array([-1.0, -1.0]), print_log = i == 0)[0]) # Note that we assume 16-bit
# sauvegarde en fits de l'image finale
if options['save_fit'] and i >= 2: # first two shifts are not user specified
DiskHDU=fits.PrimaryHDU(frames_circularized[-1],header=hdr)
DiskHDU.writeto(basefich + '_recon.fits', overwrite='True')
with open(basefich0+'_log.txt', "w") as logfile:
logfile.writelines(mylog)
return frames_circularized[2:], hdr, cercle