-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHG_MAIN.py
380 lines (340 loc) · 16.5 KB
/
SHG_MAIN.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
# -*- coding: utf-8 -*-
"""
@author: Valerie Desnoux
with improvements by Andrew Smith
contributors: Jean-Francois Pittet, Jean-Baptiste Butet, Pascal Berteau, Matt Considine
Version 8 September 2021
--------------------------------------------------------------
Front end of spectroheliograph processing of SER files
- interface able to select one or more files
- call to the solex_recon module which processes the sequence and generates the FITS files
- offers with openCV a display of the resultant image
- wavelength selection with the pixel shift function, including multiple wavelengths and a range of wavelengths
- geometric correction with a fixed Y/X ratio
- if Y/X remains at zero, then this will be calculated automatically
--------------------------------------------------------------
Front end de traitements spectro helio de fichier ser
- interface pour selectionner un ou plusieurs fichiers
- appel au module solex_recon qui traite la sequence et genere les fichiers fits
- propose avec openCV un affichage de l'image resultat ou pas
- decalage en longueur d'onde avec Shift
- ajout d'une zone pour entrer un ratio fixe; si reste à zero alors il sera calculé automatiquement
- ajout de sauvegarde png _protus avec flag disk_display en dur
---------------------------------------------------------------
"""
# import math
import numpy as np
import os
import sys
import Solex_recon as sol
from astropy.io import fits
# import cProfile
import PySimpleGUI as sg
import tkinter as tk
# import ctypes # Modification Jean-Francois: for reading the monitor size
import cv2
import traceback
def usage():
usage_ = "SHG_MAIN.py [-dcfpw] [file(s) to treat]\n"
usage_ += "'d' : 'flag_display', display all pictures (False by default)\n"
usage_ += "'c' : 'clahe_only', only final clahe picture is saved (True by default)\n"
usage_ += "'f' : 'save_fit', all fits are saved (False by default)\n"
usage_ += "'p' : 'disk_display' save protuberance pictures (False by default)\n"
usage_ += "'w' : 'a,b,c' will produce images at a, b and c.\n\tx:y:w will produce images starting at x, finishing at y, every w pixels."
return usage_
def treat_flag_at_cli(arguments):
global options
#reading arguments
i=0
while i < len(argument[1:]): #there's a '-' at first)
character = argument[1:][i]
if character=='h':
print(usage())
sys.exit()
elif character=='w' :
#find characters for shifting
shift=''
stop = False
try :
while not stop :
if argument[1:][i+1].isdigit() or argument[1:][i+1]==':' or argument[1:][i+1]==',' or argument[1:][i+1]=='-':
shift+=argument[1:][i+1]
i+=1
else :
i+=1
stop=True
except IndexError :
i+=1 #the reach the end of arguments.
shift_choice = shift.split(':')
if len(shift_choice) == 1:
options['shift'] = list(map(int, [x.strip() for x in shift.split(',')]))
elif len(shift_choice) == 2:
options['shift'] = list(range(int(shift_choice[0].strip()), int(shift_choice[1].strip())+1))
elif len(shift_choice) == 3:
options['shift'] = list(range(int(shift_choice[0].strip()), int(shift_choice[1].strip())+1, int(shift_choice[2].strip())))
else:
print('invalid shift input')
sys.exit()
else :
try : #all others
options[flag_dictionnary[character]]=True if flag_dictionnary.get(character) else False
i+=1
except KeyError :
print('ERROR !!! At least one argument is not accepted')
print(usage())
print('options %s'%(options))
def UI_SerBrowse (WorkDir, default_graphics, default_fits, default_clahe_only):
"""
Parameters
----------
WorkDir : TYPE string
repertoire par defaut à l'ouverture de la boite de dialogue
Returns
-------
Filenames : TYPE string
liste des fichiers selectionnés, avec leur extension et le chemin complet
Shift : Type string
Ecart en pixel demandé pour reconstruire le disque
sur une longeur d'onde en relatif par rapport au centre de la raie
ratio_fixe : ratio Y/X en fixe, si egal à zéro alors calcul automatique
flag_isplay: affiche ou non la construction du disque en temps réel
"""
sg.theme('Dark2')
sg.theme_button_color(('white', '#500000'))
layout = [
[sg.Text('Video file name(s)', size=(15, 1)), sg.InputText(default_text='',size=(75,1),key='-FILE-'), # MattC
sg.FilesBrowse('Open',file_types=(("SER Files", "*.ser"),("AVI Files", "*.avi"),),initial_folder=WorkDir)], # MattC
[sg.Checkbox('Show graphics', default=default_graphics, key='-DISP-')],
[sg.Checkbox('Save .fits files', default=default_fits, key='-FIT-')],
[sg.Checkbox('Save CLAHE.png only', default=default_clahe_only, key='-CLAHE_ONLY-')],
[sg.Text('Y/X ratio (blank for auto)', size=(25,1)), sg.Input(default_text='', size=(8,1),key='-RATIO-')],
[sg.Text('Tilt angle (blank for auto)',size=(25,1)),sg.Input(default_text='',size=(8,1),key='-SLANT-',enable_events=True)],
[sg.Text('Pixel offset',size=(25,1)),sg.Input(default_text='0',size=(8,1),tooltip= "a,b,c will produce images at a, b and c\n x:y:w will produce images starting at x, finishing at y, every w pixels",key='-DX-',enable_events=True)],
[sg.Text('Protus adjustment', size=(25,1)), sg.Input(default_text='0', size=(8,1), tooltip = 'make the black circle bigger or smaller by inputting an integer', key='-delta_radius-')],
[sg.Text('Bin size', size=(25,1)), sg.Input(default_text='1', size=(8,1), tooltip = 'size of y axis binning', key='-binsize-')], # MattC
[sg.Button('OK'), sg.Cancel()]
]
window = sg.Window('Processing', layout, finalize=True)
window['-FILE-'].update(WorkDir)
window.BringToFront()
while True:
event, values = window.read()
if event==sg.WIN_CLOSED or event=='Cancel':
sys.exit()
if event=='OK':
break
window.close()
FileNames=values['-FILE-']
return FileNames, values['-DX-'], values['-DISP-'], None if values['-RATIO-']=='' else values['-RATIO-'] , None if values['-SLANT-']=='' else values['-SLANT-'], values['-FIT-'], values['-CLAHE_ONLY-'], values['-delta_radius-'], values['-binsize-'] # MattC
"""
-------------------------------------------------------------------------------------------
le programme commence ici !
--------------------------------------------------------------------------------------------
"""
disk_display=True
serfiles = []
options = {
'shift':[0],
'flag_display':False,
'ratio_fixe' : None,
'slant_fix' : None ,
'save_fit' : False,
'clahe_only' : True,
'disk_display' : False, #protus
'delta_radius' : 0,
'binsize' : 1 # MattC
}
flag_dictionnary = {
'd' : 'flag_display', #True False display all pictures
'c' : 'clahe_only', #True/False
'f' : 'save_fit', #True/False
'p' : 'disk_display', #True/False protuberances
'w' : 'shift'
}
# list of files to process
## add a command line argument.
if len(sys.argv)>1 :
for argument in sys.argv :
if '-' in argument: #it's flag options
treat_flag_at_cli(argument)
else : #it's a file or some files
if argument.split('.')[-1].upper()=='SER' or argument.split('.')[-1].upper()=='AVI': #MattC
serfiles.append(argument)
print('theses files are going to be processed : ', serfiles)
#print('Processing will begin with values : \n shift %s, flag_display %s, "%s", slant_fix "%s", save_fit %s, clahe_only %s, disk_display %s' %(options['shift'], options['flag_display'], options['ratio_fixe'], options['slant_fix'], options['save_fit'], options['clahe_only'], options['disk_display']) )
# check for .ini file for working directory
try:
mydir_ini=os.path.dirname(sys.argv[0])+'/SHG.ini'
with open(mydir_ini, "r") as f1:
param_init = f1.read().splitlines()
WorkDir=param_init[0]
default_graphics, default_fits, default_clahe_only = param_init[1] == 'True', param_init[2] == 'True', param_init[3] == 'True'
except:
WorkDir=''
default_graphics, default_fits, default_clahe_only = False, False, False
# if no command line arguments, open GUI interface
if len(serfiles)==0 :
serfiles, shift, flag_display, ratio_fixe, slant_fix, save_fit, clahe_only, delta_radius, binsize =UI_SerBrowse(WorkDir, default_graphics, default_fits, default_clahe_only) #TODO as options is defined as global, only serfiles could be returned # MattC
try :
shift_choice = shift.split(':')
if len(shift_choice) == 1:
options['shift'] = list(map(int, [x.strip() for x in shift.split(',')]))
elif len(shift_choice) == 2:
options['shift'] = list(range(int(shift_choice[0].strip()), int(shift_choice[1].strip())+1))
elif len(shift_choice) == 3:
options['shift'] = list(range(int(shift_choice[0].strip()), int(shift_choice[1].strip())+1, int(shift_choice[2].strip())))
else:
print('invalid shift input')
sys.exit()
except ValueError :
print('invalid shift value')
sys.exit()
options['flag_display'] = flag_display
try :
options['ratio_fixe'] = float(ratio_fixe) if not ratio_fixe is None else None
except ValueError :
print('invalid ratio_fixe value')
sys.exit()
try :
options['slant_fix'] = float(slant_fix) if not slant_fix is None else None
except ValueError :
print('invalid slant_fix value')
sys.exit()
try:
options['delta_radius'] = int(delta_radius)
except ValueError:
print('invalid protus_radius_adjustment')
sys.exit()
options['save_fit'] = save_fit
options['clahe_only'] = clahe_only
options['binsize'] = int(binsize) # MattC
serfiles=serfiles.split(';')
#pour gerer la tempo des affichages des images resultats dans cv2.waitKey
#si plusieurs fichiers à traiter
def do_work():
global options
if len(serfiles)==1:
options['tempo']=60000 #4000
else:
options['tempo']=5000
# boucle sur la liste des fichers
for serfile in serfiles:
if serfile=='':
sys.exit()
print('file %s is processing'%serfile)
WorkDir=os.path.dirname(serfile)+"/"
os.chdir(WorkDir)
base=os.path.basename(serfile)
basefich=os.path.splitext(base)[0]
if base=='':
print('filename ERROR : ',serfile)
sys.exit()
# ouverture du fichier ser
try:
f=open(serfile, "rb")
f.close()
except:
print('ERROR opening file : ',serfile)
sys.exit()
# save working directory
try:
with open(mydir_ini, "w") as f1:
f1.write('\n'.join([WorkDir, str(options['flag_display']), str(options['save_fit']), str(options['clahe_only'])]))
except:
traceback.print_exc()
print('ERROR: couldnt write file ' + mydir_ini)
# appel au module d'extraction, reconstruction et correction
#
# basefich: nom du fichier ser sans extension et sans repertoire
# dx: decalage en pixel par rapport au centre de la raie
try :
frames, header, cercle=sol.solex_proc(serfile,options.copy()) #doesnt know about bw img??
for frame, shift in zip(frames, options['shift']):
base=os.path.basename(serfile)
basefich=os.path.splitext(base)[0] +'_shift=' + str(shift)
flag_result_show = options['flag_display']
# create a CLAHE object (Arguments are optional)
# clahe = cv2.createCLAHE(clipLimit=0.8, tileGridSize=(5,5))
clahe = cv2.createCLAHE(clipLimit=0.8, tileGridSize=(2,2))
cl1 = clahe.apply(frame)
# image leger seuils
frame1=np.copy(frame)
Seuil_bas=np.percentile(frame, 25)
Seuil_haut=np.percentile(frame,99.9999)
print('Seuil bas :', np.floor(Seuil_bas))
print('Seuil haut :', np.floor(Seuil_haut))
fc=(frame1-Seuil_bas)* (65535/(Seuil_haut-Seuil_bas))
fc[fc<0]=0
fc[fc>65535] = 65535
frame_contrasted=np.array(fc, dtype='uint16')
# image seuils serres
frame1=np.copy(frame)
Seuil_bas=(Seuil_haut*0.25)
Seuil_haut=np.percentile(frame1,99.9999)
print('Seuil bas HC :', np.floor(Seuil_bas))
print('Seuil haut HC :', np.floor(Seuil_haut))
fc2=(frame1-Seuil_bas)* (65535/(Seuil_haut-Seuil_bas))
fc2[fc2<0]=0
fc2[fc2>65535] = 65535
frame_contrasted2=np.array(fc2, dtype='uint16')
# image seuils protus
frame1=np.copy(frame)
Seuil_bas=0
Seuil_haut=np.percentile(frame1,99.9999)*0.18
print('Seuil bas protu :', np.floor(Seuil_bas))
print('Seuil haut protu:', np.floor(Seuil_haut))
fc2=(frame1-Seuil_bas)* (65535/(Seuil_haut-Seuil_bas))
fc2[fc2<0]=0
fc2[fc2>65535] = 65535
frame_contrasted3=np.array(fc2, dtype='uint16')
if not cercle == (-1, -1, -1) and disk_display==True:
x0=int(cercle[0])
y0=int(cercle[1])
r=int(cercle[2]) + options['delta_radius']
frame_contrasted3=cv2.circle(frame_contrasted3, (x0,y0),r,80,-1)
Seuil_bas=np.percentile(cl1, 25)
Seuil_haut=np.percentile(cl1,99.9999)*1.05
cc=(cl1-Seuil_bas)*(65535/(Seuil_haut-Seuil_bas))
cc[cc<0]=0
cc[cc>65535] = 65535
cc=np.array(cc, dtype='uint16')
# sauvegarde en png de clahe
cv2.imwrite(basefich+'_clahe.png',cc) # Modification Jean-Francois: placed before the IF for clear reading
if not options['clahe_only']:
# sauvegarde en png pour appliquer une colormap par autre script
cv2.imwrite(basefich+'_disk.png',frame_contrasted)
# sauvegarde en png pour appliquer une colormap par autre script
cv2.imwrite(basefich+'_diskHC.png',frame_contrasted2)
# sauvegarde en png pour appliquer une colormap par autre script
cv2.imwrite(basefich+'_protus.png',frame_contrasted3)
# Modification Jean-Francois: the 4 images are concatenated together in 1 image => 'Sun images'
# The 'Sun images' is scaled for the monitor maximal dimension ... it is scaled to match the dimension of the monitor without
# changing the Y/X scale of the images
if flag_result_show:
im_1 = cv2.hconcat([frame_contrasted, frame_contrasted2])
im_2 = cv2.hconcat([frame_contrasted3, cc])
im_3 = cv2.vconcat([im_1, im_2])
screen = tk.Tk()
screensize = screen.winfo_screenwidth(), screen.winfo_screenheight()
screen.destroy()
scale = min(screensize[0] / im_3.shape[1], screensize[1] / im_3.shape[0])
cv2.namedWindow('Sun images', cv2.WINDOW_NORMAL)
cv2.moveWindow('Sun images', 0, 0)
cv2.resizeWindow('Sun images',int(im_3.shape[1] * scale), int(im_3.shape[0] * scale))
cv2.imshow('Sun images',im_3)
cv2.waitKey(options['tempo']) # affiche et continue
cv2.destroyAllWindows()
frame2=np.copy(frame)
frame2=np.array(cl1, dtype='uint16')
# sauvegarde le fits
if options['save_fit']:
DiskHDU=fits.PrimaryHDU(frame2,header)
DiskHDU.writeto(basefich+ '_clahe.fits', overwrite='True')
except :
print('ERROR ENCOUNTERED')
traceback.print_exc()
cv2.destroyAllWindows()
if 0:
cProfile.run('do_work()', sort='cumtime')
else:
do_work()