-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproc_gui.py
1760 lines (1611 loc) · 106 KB
/
proc_gui.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
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import copy
import datetime
import operator
import warnings
import numpy as np
import matplotlib.pyplot as plt
import tkcanvas as tkc
if sys.version_info[0] < 3: # Python 2
import Tkinter as Tk
from collections import OrderedDict as dict
else: # Python 3
import tkinter as Tk
import plot_img_spec
from proc_general import *
def file_list_UI(file_list, CONFIGFILE):
from procedures import logger
# Extract a common path
maxlen_files = 0
for entry in file_list:
maxlen_files = max(maxlen_files, len(entry[0].replace('#','').replace(' ','')) )
common_path = ''
if file_list[0][0][2:].find(os.sep) != -1: # it contains subfolders
fnames = []
for entry in file_list:
fnames.append( entry[0].replace('#','').replace(' ','') )
common_path = fnames[0].rsplit(os.sep,1)[0] # just remove the filename
for dummy in range(len(fnames[0].split(os.sep))-1):
found_common_path = True
for entry in fnames:
if entry.find(common_path) == -1: # This path is not the same
found_common_path = False
break
if found_common_path:
common_path += os.sep
break
else:
common_path = common_path.rsplit(os.sep,1) # remove the next subfolder
if len(common_path) == 2: # Another subfolder in the path
common_path = common_path[0]
else: # no more subfolders
common_path = ''
break
# Split the common_path to make the GUI shorter (in x), if necessary
maxlen_files -= len(common_path)
if len(common_path) == 0:
common_path_text = '{0}{1}(no common path)'.format(' '*int(maxlen_files*1.5), os.linesep)
else:
common_path_text = common_path
if len(common_path) > maxlen_files:
common_path_temp = common_path[:-1].split(os.sep) # without the last '/'
common_path_text = [common_path_temp[0]+os.sep] # start with the first entry
ii = 0
for entry in common_path_temp[1:]:
entry += os.sep
if len(common_path_text[ii]+entry) > maxlen_files and len(common_path_text[ii]) > maxlen_files/2.: # If too long to add
common_path_text.append(entry)
ii += 1
else:
common_path_text[ii] += entry
common_path_text = os.linesep.join(common_path_text)
# define widgets
pkwargs = dict()
widgets = dict()
widgets['comment'] = dict(label='Comm-{0}ented{0}out '.format(os.linesep), kind='Label', row=0, column=0, columnspan=1, orientation=Tk.W)
widgets['mid_exp'] = dict(label=' Observation Time {0}(mid exposure){0}UTC'.format(os.linesep), kind='Label', row=0, column=1, orientation=Tk.W)
widgets['exp'] = dict(label='Expo-{0}sure {0}[s] '.format(os.linesep), kind='Label', row=0, column=2, orientation=Tk.W)
widgets['name'] = dict(label='Path and folder{1}{0}'.format(common_path_text, os.linesep), kind='Label', row=0, column=3, orientation=Tk.W)
#widgets['fib1'] = dict(label='Science\nfiber', kind='Label', row=0, column=5)
#widgets['fib2'] = dict(label='Calibration\nfiber', kind='Label', row=0, column=6)
widgets['b'] = dict(label='Bias', kind='Label', row=0, column=6)
widgets['d'] = dict(label='Dark', kind='Label', row=0, column=7)
widgets['a'] = dict(label='Real{0}Flat'.format(os.linesep), kind='Label', row=0, column=8)
widgets['t1'] = dict(label='Sci.{0}tra-{0}ce'.format(os.linesep), kind='Label', row=0, column=9)
widgets['t2'] = dict(label='Cal.{0}tra-{0}ce'.format(os.linesep), kind='Label', row=0, column=10)
widgets['z'] = dict(label='Blaze', kind='Label', row=0, column=11)
widgets['w1'] = dict(label='Wave{0}Sci.'.format(os.linesep), kind='Label', row=0, column=12)
#widgets['w1l'] = dict(label='Wave\nSci.\nlong', kind='Label', row=0, column=12)
#widgets['w1s'] = dict(label='Wave\nSci.\nshort', kind='Label', row=0, column=13)
widgets['w2'] = dict(label='Wave{0}Cal.'.format(os.linesep), kind='Label', row=0, column=14)
#widgets['w2l'] = dict(label='Wave\nCal.\nlong', kind='Label', row=0, column=14)
#widgets['w2s'] = dict(label='Wave\nCal.\nshort', kind='Label', row=0, column=15)
widgets['ws'] = dict(label='Wave{0}shft{0}Sci'.format(os.linesep), kind='Label', row=0, column=16)
widgets['wc'] = dict(label='Wave{0}shft{0}Cal'.format(os.linesep), kind='Label', row=0, column=17)
widgets['e'] = dict(label='Ex-{0}tract'.format(os.linesep), kind='Label', row=0, column=18)
widgets['obname'] = dict(label='Objectname{0}(comma{0}separated)'.format(os.linesep), kind='Label', row=0, column=19)
widgets['extra'] = dict(label='Further usage{0}of files{0}(comma sep.)'.format(os.linesep), kind='Label', row=0, column=20)
for ii, entry in enumerate(file_list):
pkwargs['comment_{0}'.format(ii)] = ( 0 <= entry[0].find('#') < 20 ) # Commented out
widgets['comment_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['comment_{0}'.format(ii)], row=ii+1, column=0)
expstr = '%1.2f'%entry[3] # '%9.2f'%entry[3] has not long enough spaces
if len(expstr) > 7:
expstr = ' '+expstr[:-3] # without fractions at the end
else:
#expstr = ' '*{4:7, 5:5, 6:4, 7:2, 8:1, 9:1, 10:1}[len(expstr)] + expstr # This has the 100s not perfectly alligned
expstr = ' '*{4:6, 5:4, 6:2, 7:1, 8:1, 9:1, 10:1}[len(expstr)] + expstr # This has the 100s and 100s not perfectly alligned
text = '{0}{1} {2}'.format( datetime.datetime.utcfromtimestamp(entry[4]).strftime('%Y-%m-%d %H:%M:%S'),
expstr, entry[0].replace('#','').replace(' ','').replace(common_path,'') )
widgets['name_{0}'.format(ii)] = dict(label=text, kind='Label', row=ii+1, column=1, columnspan=3, orientation=Tk.W)
#fname = entry[0].replace('#','').replace(' ','').replace(common_path,'')
#widgets['mid_exp_{0}'.format(ii)] = dict(label=datetime.datetime.utcfromtimestamp(entry[4]).strftime('%Y-%m-%dT%H:%M:%S'), kind='Label', row=ii+1, column=5)
#widgets['exp_{0}'.format(ii)] = dict(label=entry[3], kind='Label', row=ii+1, column=4)
#widgets['name_{0}'.format(ii)] = dict(label=fname, kind='Label', row=ii+1, column=1, columnspan=1, orientation=Tk.W)
#pkwargs['fib1_{0}'.format(ii)] = entry[1] # Allows modification of the fiber content
#widgets['fib1_{0}'.format(ii)] = dict(kind='TextEntry', minval=None, maxval=None, fmt=str, start=pkwargs['fib1_{0}'.format(ii)], width=8, row=ii+1, column=5) # Allows modification of the fiber content
#pkwargs['fib2_{0}'.format(ii)] = entry[2] # Allows modification of the fiber content
#widgets['fib2_{0}'.format(ii)] = dict(kind='TextEntry', minval=None, maxval=None, fmt=str, start=pkwargs['fib2_{0}'.format(ii)], width=8, row=ii+1, column=6) # Allows modification of the fiber content
flags = entry[6].replace(' ','').split(',')
pkwargs['b_{0}'.format(ii)] = ( 'b' in flags )
widgets['b_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['b_{0}'.format(ii)], row=ii+1, column=6)
pkwargs['d_{0}'.format(ii)] = ( 'd' in flags )
widgets['d_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['d_{0}'.format(ii)], row=ii+1, column=7)
pkwargs['a_{0}'.format(ii)] = ( 'a' in flags )
widgets['a_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['a_{0}'.format(ii)], row=ii+1, column=8)
pkwargs['t1_{0}'.format(ii)] = ( 't1' in flags )
widgets['t1_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['t1_{0}'.format(ii)], row=ii+1, column=9)
pkwargs['t2_{0}'.format(ii)] = ( 't2' in flags )
widgets['t2_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['t2_{0}'.format(ii)], row=ii+1, column=10)
pkwargs['z_{0}'.format(ii)] = ( 'z' in flags )
widgets['z_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['z_{0}'.format(ii)], row=ii+1, column=11)
pkwargs['w1_{0}'.format(ii)] = ( ('w1' in flags) | ('w1l' in flags) | ('w1s' in flags) )
widgets['w1_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['w1_{0}'.format(ii)], row=ii+1, column=12)
#pkwargs['w1l_{0}'.format(ii)] = ( 'w1l' in flags )
#widgets['w1l_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['w1l_{0}'.format(ii)], row=ii+1, column=12)
#pkwargs['w1s_{0}'.format(ii)] = ( 'w1s' in flags )
#widgets['w1s_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['w1s_{0}'.format(ii)], row=ii+1, column=13)
pkwargs['w2_{0}'.format(ii)] = ( ('w2' in flags) | ('w2l' in flags) | ('w2s' in flags) )
widgets['w2_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['w2_{0}'.format(ii)], row=ii+1, column=14)
#pkwargs['w2l_{0}'.format(ii)] = ( 'w2l' in flags )
#widgets['w2l_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['w2l_{0}'.format(ii)], row=ii+1, column=14)
#pkwargs['w2s_{0}'.format(ii)] = ( 'w2s' in flags )
#widgets['w2s_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['w2s_{0}'.format(ii)], row=ii+1, column=15)
pkwargs['ws_{0}'.format(ii)] = ( 'ws' in flags )
widgets['ws_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['ws_{0}'.format(ii)], row=ii+1, column=16)
pkwargs['wc_{0}'.format(ii)] = ( 'wc' in flags )
widgets['wc_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['wc_{0}'.format(ii)], row=ii+1, column=17)
pkwargs['e_{0}'.format(ii)] = ( 'e' in flags )
widgets['e_{0}'.format(ii)] = dict(label=None, kind='CheckBox', start=pkwargs['e_{0}'.format(ii)], row=ii+1, column=18)
pkwargs['obname_{0}'.format(ii)] = entry[5]
widgets['obname_{0}'.format(ii)] = dict(kind='TextEntry', minval=None, maxval=None, fmt=str, start=pkwargs['obname_{0}'.format(ii)], width=15, row=ii+1, column=19)
extra = ''
for flag in flags: # Add extra flags, if necessary
if flag not in ['b', 'd', 'a', 't1', 't2', 'z', 'w2l', 'w2s', 'w2', 'w1l', 'w1s', 'w1', 'ws', 'wc', 'e']:
extra += ','+flag
if len(extra) > 0:
extra = extra[1:]
pkwargs['extra_{0}'.format(ii)] = extra
widgets['extra_{0}'.format(ii)] = dict(kind='TextEntry', minval=None, maxval=None, fmt=str, start=pkwargs['extra_{0}'.format(ii)], width=15, row=ii+1, column=20)
explain = ('Explanation of the columns:{1}'+\
'- Tick first column to not use some files at all{1}'+\
'- Mark the files to be used for calibration:{1}'+\
'-- Bias: These file are combined into a master bias{1}'+\
'-- Dark: exposure time will be automatically taken{1} into account{1}'+\
'-- Real Flat: Evenly exposed detector to calibrate{1} pixel-to-pixel sensitivity variation{1}'+\
'-- Science trace: To trace the science orders{1}'+\
'-- Calibration trace: To trace the calibration orders{1}'+\
'-- Blaze: To derive the blaze function{1}'+\
'-- Wavelength solition for science fiber {1} (long and short expsoure time){1}'+\
'-- Wavelength solution for calibration fiber (*){1}'+\
'-- Wavelength offset Science fiber (**) to correct for{1} wavelength drit{1}'+\
'-- Wavelength offset between the Science fiber and{1} Calibration fiber (*){1}'+\
'-- Object name: List of Names for Object{1}'+\
'-- Extract: Extract these files on an individual basis{1}'+\
'-- Further settings (manual): e.g. to combine files{1} before or after extraction{1}'+\
'(*) not for single fiber spectrographs{1}'+\
'(**) important for unstabilised (single) fiber{1} spectrographs{1}{1}'+\
'The automatic assignment is based on the parameters{1} raw_data_* in {0} (and in procedure{1} add_new_rawfiles_file_list). ').format(CONFIGFILE, os.linesep)
#'- Type of Science and Calibration\n fibers are derived from header or\n filename and can be changed here\n (optional)\n'+\ # Allows modification of the fiber content
for ii, commentii in enumerate(explain.split(os.linesep)):
if len(commentii) > 0:
widgets['explain_{0}'.format(ii)] = dict(label=commentii, kind='Label', row=ii, column=21, rowspan=1, orientation=Tk.W )#, wraplength=100 )
widgets['accept'] = dict(label='Accept', kind='ExitButton', row=ii+1, column=21, rowspan=2)
wprops = dict(fullscreen=False )
#wprops['width_data'] = 800 # not neccssary, as uses the automatic width
if len(file_list) > 100:
logger('Info: A GUI with {0} elements will be created, on some machines that can take up to a few minutes.{1}'.format(len(widgets), os.linesep)+\
'\tIf you want to use an editor instead of the GUI, please kill the process (kill {0}){1}'.format(os.getpid(), os.linesep)+\
'\tand run the script with parameter "nogui", e.g.{1}\t\tpython {0} nogui'.format(sys.argv[0], os.linesep) )
gui3 = tkc.TkCanvasGrid(title='HiFLEx: Asigning Files to extraction steps (What file contains what data)',
kwargs=pkwargs,widgets=widgets, widgetprops=wprops )
gui3.master.mainloop()
# Get the information from the GUI
file_list_new, file_list_full = [], []
for ii in range(len(file_list)):
text = ( {True:'b',False:''}[gui3.data['b_{0}'.format(ii)]] +','+
{True:'d',False:''}[gui3.data['d_{0}'.format(ii)]] +','+
{True:'a',False:''}[gui3.data['a_{0}'.format(ii)]] +','+
{True:'t1',False:''}[gui3.data['t1_{0}'.format(ii)]] +','+
{True:'t2',False:''}[gui3.data['t2_{0}'.format(ii)]] +','+
{True:'z',False:''}[gui3.data['z_{0}'.format(ii)]] +','+
{True:'w2',False:''}[gui3.data['w2_{0}'.format(ii)]] +','+
{True:'w1',False:''}[gui3.data['w1_{0}'.format(ii)]] +','+
{True:'ws',False:''}[gui3.data['ws_{0}'.format(ii)]] +','+
{True:'wc',False:''}[gui3.data['wc_{0}'.format(ii)]] +','+
{True:'e',False:''}[gui3.data['e_{0}'.format(ii)]] +','+
gui3.data['extra_{0}'.format(ii)]
).replace(',,',',').replace(',,',',').replace(',,',',').replace(',,',',').replace(',,',',')
if text[0] == ',':
text = text[1:]
if len(text) > 0:
if text[-1] == ',':
text = text[:-1]
#file_list_full.append([ {True:'#',False:''}[gui3.data['comment_{0}'.format(ii)]] + file_list[ii][0].replace('#','').replace(' ',''),
# gui3.data['fib1_{0}'.format(ii)] , gui3.data['fib2_{0}'.format(ii)], file_list[ii][3], file_list[ii][4],
# text ]) # Allows modification of the fiber content
file_list_full.append([ {True:'#',False:''}[gui3.data['comment_{0}'.format(ii)]] + file_list[ii][0].replace('#','').replace(' ',''),
file_list[ii][1] , file_list[ii][2], file_list[ii][3], file_list[ii][4], gui3.data['obname_{0}'.format(ii)], text ])
if not gui3.data['comment_{0}'.format(ii)]: # Ignore the commented out lines
file_list_new.append(file_list_full[-1])
return file_list_new, file_list_full
def calibration_parameters_coordinates_UI(params, conf_data, object_information_full, object_information_head, CONFIGFILE):
max_length = 1024
def add_widgets(widgets, pkwargs, ii, ftype, doneftype):
if '{0}_rawfiles'.format(ftype) not in conf_data.keys() or ftype in doneftype:
return widgets, pkwargs, ii, doneftype
widgets['type_{0}'.format(ii)] = dict(label=ftype, kind='Label', row=ii, column=0, columnspan=1, orientation=Tk.W)
if 'master_{0}_filename'.format(ftype) in conf_data.keys() and (ftype.find('extract') == -1 or ftype.find('extract_combine') != -1) and ftype.find('wavelenoffset') == -1:
elname = 'master_{0}_filename'.format(ftype)
pkwargs[elname] = conf_data[elname]
widgets[elname] = dict(kind='TextEntry', fmt=str, start=pkwargs[elname], width=30, row=ii, column=1, columnspan=3, orientation=Tk.W)
elname = '{0}_calibs_create'.format(ftype)
pkwargs[elname] = conf_data[elname]
widgets[elname] = dict(kind='TextEntry', fmt=str, start=pkwargs[elname], width=50, row=ii, column=4, columnspan=5, orientation=Tk.W)
label = conf_data['{0}_rawfiles'.format(ftype)]
if len(label) > max_length:
label = label[:max_length-3] + '...' # Otherwise: X Error of failed request: BadAlloc (insufficient resources for operation)
widgets['files_{0}'.format(ii)] = dict(label=label, kind='Label', row=ii, column=9, columnspan=1000, orientation=Tk.W)
doneftype.append(ftype)
ii += 1
return widgets, pkwargs, ii, doneftype
def vfunc_float(xs):
try:
value = float(xs)
return True, value
except:
return False, ('Error, input must be float')
# define widgets
pkwargs = dict()
widgets = dict()
# Add widgets for fits_conf.txt
widgets['type'] = dict(label='Type', kind='Label', row=0, column=0, columnspan=1, orientation=Tk.W)
widgets['name_master'] = dict(label='Name of Master file', kind='Label', row=0, column=1, columnspan=3, orientation=Tk.W)
widgets['calibs'] = dict(label='Calibrations to be applied', kind='Label', row=0, column=4, columnspan=5, orientation=Tk.W)
widgets['files_used'] = dict(label='Files included (see last GUI or {0} to change the assigned files)'.format(params['raw_data_file_list']), kind='Label', row=0, column=9, columnspan=1000, orientation=Tk.W)
darks = []
for entry in sorted(params['exp_darks']):
darks.append('dark{0}'.format(entry))
doneftype = []
ii = 1
for ftype in ['bias'] + darks + ['rflat', 'trace1', 'trace2', 'blazecor', 'cal2_l', 'cal2_s', 'cal1_l', 'cal1_s', 'waveoffsetsci', 'waveoffsetcal']:
widgets, pkwargs, ii, doneftype = add_widgets(widgets, pkwargs, ii, ftype, doneftype)
for entry in sorted(conf_data.keys()):
if entry.find('_rawfiles') >= 0:
ftype = entry.replace('_rawfiles','')
widgets, pkwargs, ii, doneftype = add_widgets(widgets, pkwargs, ii, ftype, doneftype)
widgets['accept1'] = dict(label='Accept', kind='ExitButton', row=ii+1, column=6, rowspan=2, columnspan=2)
ii += 3
# Add widgets for object_list.txt
widgets['name'] = dict(label='Object{0}name'.format(os.linesep), kind='Label', row=ii, column=0, rowspan=2, columnspan=1, orientation=Tk.E)
widgets['header_info1'] = dict(label='Header information', kind='Label', row=ii, column=1, columnspan=2, orientation=Tk.E)
widgets['header_ra'] = dict(label='RA', kind='Label', row=ii+1, column=1)
widgets['header_dec'] = dict(label='DEC', kind='Label', row=ii+1, column=2)
widgets['header_info2'] = dict(label='Header information', kind='Label', row=ii, column=3, columnspan=3)
widgets['header_pmra'] = dict(label='PMRA', kind='Label', row=ii+1, column=3)
widgets['header_pmdec'] = dict(label='PMRA', kind='Label', row=ii+1, column=4)
widgets['header_epoch'] = dict(label='Epoch', kind='Label', row=ii+1, column=5)
widgets['use_header'] = dict(label='Use{0}head'.format(os.linesep), kind='Label', row=ii, column=6, rowspan=2, columnspan=1)
widgets['ra'] = dict(label='RA{0}<:>,< >,float'.format(os.linesep), kind='Label', row=ii, column=7, rowspan=2, columnspan=1)
widgets['dec'] = dict(label='DEC{0}<:>,< >,float'.format(os.linesep), kind='Label', row=ii, column=8, rowspan=2, columnspan=1)
widgets['pmra'] = dict(label='PMRA{0}[mas/yr]'.format(os.linesep), kind='Label', row=ii, column=9, rowspan=2, columnspan=1)
widgets['pmdec'] = dict(label='PMDEC{0}[mas/yr]'.format(os.linesep), kind='Label', row=ii, column=10, rowspan=2, columnspan=1)
widgets['epoch'] = dict(label='Epoch{0}(number)'.format(os.linesep), kind='Label', row=ii, column=11, rowspan=2, columnspan=1)
widgets['simbad_name'] = dict(label='Simbad Name{0}(check that correct)'.format(os.linesep), kind='Label', row=ii, column=12, rowspan=2, columnspan=1)
widgets['mask'] = dict(label='Mask (optional){0}G2,K5,M2'.format(os.linesep), kind='Label', row=ii, column=13, rowspan=2, columnspan=1)
widgets['rot'] = dict(label='rotation (optional){0}[km/s]'.format(os.linesep), kind='Label', row=ii, column=14, rowspan=2, columnspan=1)
ii += 2
jj = 0 # if no object to extract, jj will not exist, but is needed later
for jj, entry in enumerate(object_information_full):
widgets['name_{0}'.format(jj)] = dict(label=entry[0], kind='Label', row=ii+jj, column=0, columnspan=1, orientation=Tk.E)
state = None
if entry[0].lower().find('sun') == 0 or entry[0].lower().find('moon') == 0 or entry[0].lower().find('jupiter') == 0: # Check with get_object_site_from_header()
for i in range(1,7):
entry[i] = ''
state = Tk.DISABLED
pkwargs['ra_{0}'.format(jj)] = entry[2]
widgets['ra_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, start=pkwargs['ra_{0}'.format(jj)], width=13, row=ii+jj, column=7, columnspan=1, state=state)
pkwargs['dec_{0}'.format(jj)] = entry[3]
widgets['dec_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, start=pkwargs['dec_{0}'.format(jj)], width=13, row=ii+jj, column=8, columnspan=1, state=state)
pkwargs['pmra_{0}'.format(jj)] = entry[4]
widgets['pmra_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, valid_function=vfunc_float, start=pkwargs['pmra_{0}'.format(jj)], width=7, row=ii+jj, column=9, columnspan=1, state=state)
pkwargs['pmdec_{0}'.format(jj)] = entry[5]
widgets['pmdec_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, valid_function=vfunc_float, start=pkwargs['pmdec_{0}'.format(jj)], width=7, row=ii+jj, column=10, columnspan=1, state=state)
pkwargs['epoch_{0}'.format(jj)] = entry[9]
widgets['epoch_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, valid_function=vfunc_float, start=pkwargs['epoch_{0}'.format(jj)], width=6, row=ii+jj, column=11, columnspan=1, state=state)
# simbad name at end, next to button (button to search again is missing, therefore state=Tk.DISABLED
pkwargs['simbad_name_{0}'.format(jj)] = entry[1]
widgets['simbad_name_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, start=pkwargs['simbad_name_{0}'.format(jj)], width=13, row=ii+jj, column=12, columnspan=1, orientation=Tk.W, state=Tk.DISABLED)
pkwargs['mask_{0}'.format(jj)] = entry[7]
widgets['mask_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, start=pkwargs['mask_{0}'.format(jj)], width=5, row=ii+jj, column=13, columnspan=1)
pkwargs['rot_{0}'.format(jj)] = entry[8]
widgets['rot_{0}'.format(jj)] = dict(kind='TextEntry', fmt=str, valid_function=vfunc_float, start=pkwargs['rot_{0}'.format(jj)], width=5, row=ii+jj, column=14, columnspan=1)
if object_information_head[jj][0] == '':
pkwargs['use_header_{0}'.format(jj)] = False
widgets['use_header_{0}'.format(jj)] = dict(label=None, kind='CheckBox', start=pkwargs['use_header_{0}'.format(jj)], row=ii+jj, column=6, state=Tk.DISABLED)
continue
entry = object_information_head[jj]
widgets['header_ra_{0}'.format(jj)] = dict(label=entry[0], kind='Label', row=ii+jj, column=1)
widgets['header_dec_{0}'.format(jj)] = dict(label=entry[1], kind='Label', row=ii+jj, column=2)
widgets['header_pmra_{0}'.format(jj)] = dict(label=entry[2], kind='Label', row=ii+jj, column=3)
widgets['header_pmdec_{0}'.format(jj)] = dict(label=entry[3], kind='Label', row=ii+jj, column=4)
widgets['header_epoch_{0}'.format(jj)] = dict(label=entry[4], kind='Label', row=ii+jj, column=5)
pkwargs['use_header_{0}'.format(jj)] = True
widgets['use_header_{0}'.format(jj)] = dict(label=None, kind='CheckBox', start=pkwargs['use_header_{0}'.format(jj)], row=ii+jj, column=6)
ii += jj+1
widgets['accept2'] = dict(label='Accept', kind='ExitButton', row=ii, column=6, rowspan=2, columnspan=2)
ii += 2
explain = 'Explanation of upper half:'+os.linesep+\
' This assigns the calibration that will be applied to files of the different types before creating the master file or before extracting the spectra. The following comma separated options are possible:'+os.linesep+\
' subframe, badpx_mask, bias, dark, flat, background, normalise, combine_mean, combine_sum'+os.linesep+\
' Please check the manual for more information on these options.'+os.linesep+\
' The assigned calibration steps are read from {0} (*_calibs_create_g){1}'.format(CONFIGFILE, os.linesep)+\
' The information from this part of the GUI will be stored in {0}.{1}{1}'.format(params['configfile_fitsfiles'], os.linesep)+\
' Explanation of the lower half:'+os.linesep+\
' For each object to be extracted the coordinates are derived/displayed here. If the header information is available then the information is shown and the user can decide if this information should be used.'+os.linesep+\
' The editable coordinates are taken from the file {0}, for which is also checked in the result and raw data path. If the object does not exist in the file, then Simbad is searched using the Object name.{1}'.format(params['object_file'], os.linesep)+\
' The object name for the results from Simbad is shown and should be the same as the object name derived from header/filename'+os.linesep+\
' The user can modify this information, which should be correct to perform correct barycentric correction. The information is then stored in {0}, overwriting the previous information.{1}'.format(params['object_file'], os.linesep)+\
' The RA and DEC can be given in hour or degree format. The optional parameters "mask" and "rotation speed" are used if RV analysis with the CERES pipeline is performed.'
for jj, commentjj in enumerate(explain.split(os.linesep)):
if len(commentjj) > 0:
widgets['explain_{0}'.format(jj)] = dict(label=commentjj, kind='Label', row=ii+jj, column=0, columnspan=20, orientation=Tk.W )#, wraplength=100 )
wprops = dict(fullscreen=False )
#wprops['width_GUI'] = 800 # not neccssary, as uses the automatic width
gui3 = tkc.TkCanvasGrid(title='HiFLEx: Asigning Calibration and Coordinates',
kwargs=pkwargs,widgets=widgets, widgetprops=wprops )
gui3.master.mainloop()
# Get the information from the GUI
for entry in conf_data.keys(): # For the conf data
if entry in gui3.data.keys():
conf_data[entry] = gui3.data[entry]
if entry.find('_calibs_create') != -1:
if len(conf_data[entry]) > 0:
if conf_data[entry].find('[') + conf_data[entry].find(']') == -2:
conf_data[entry] = '[{0}]'.format(conf_data[entry]) # add brackets
object_information = []
for ii in range(len(object_information_full)):
enabled = {True:0,False:1}[gui3.data['use_header_{0}'.format(ii)]]
if len(object_information_full[ii][1]) >= 3: # The coordinates were read from a file
try:
object_information_full[ii][6] = float(object_information_full[ii][6]) # see if we can make it to a number
except:
True
if type(object_information_full[ii][6]).__name__ != 'str': # it's a number
if abs(object_information_full[ii][6]) < 0.9:
enabled = False # Object was disabled before
if ( len(gui3.data['mask_{0}'.format(ii)]) < 1 and type(gui3.data['rot_{0}'.format(ii)]).__name__ == 'str' ) and ( len(gui3.data['ra_{0}'.format(ii)]) < 3 or len(gui3.data['ra_{0}'.format(ii)]) < 3 ):
continue # Both coordinates are not complete and Mask or rotation is not there -> no useful information
if (len(gui3.data['ra_{0}'.format(ii)]) >= 3 and len(gui3.data['dec_{0}'.format(ii)]) >= 3): # Fill pm* and epoch if coordinates are full
for entry in ['pmra', 'pmdec']:
if type(gui3.data['{1}_{0}'.format(ii, entry)]).__name__ == 'str':
gui3.data['{1}_{0}'.format(ii, entry)] = 0
if type(gui3.data['epoch_{0}'.format(ii)]).__name__ == 'str':
gui3.data['epoch_{0}'.format(ii)] = 2000
object_information.append([ object_information_full[ii][0], gui3.data['ra_{0}'.format(ii)], gui3.data['dec_{0}'.format(ii)],
gui3.data['pmra_{0}'.format(ii)], gui3.data['pmdec_{0}'.format(ii)], gui3.data['epoch_{0}'.format(ii)],
enabled, gui3.data['mask_{0}'.format(ii)], gui3.data['rot_{0}'.format(ii)] ])
return conf_data, object_information
def adjust_binning_UI(im1, binxy, searchlimit_brightness=None, min_separation=None, userinput=True):
"""
Adjusts the binning
"""
if not userinput:
return binxy, searchlimit_brightness, min_separation
from procedures import bin_im
#sim_sflat, dummy, dummy = bin_im(im_sflat, params['bin_search_apertures'] )
# set up plot
fig, frame = plt.subplots(1, 1)
fig.set_size_inches(10, 7.5, forward=True)
# get kwargs
binx, biny = binxy
pkwargs = dict(frame=frame, im1=im1, binx=binx, biny=biny, searchlimit_brightness=searchlimit_brightness, min_separation=min_separation,
im_scale_min=round_sig(np.percentile(im1,10),2), im_scale_max=round_sig(np.percentile(im1,90),2) )
# define update plot function
def plot(frame, im1, binx, biny, searchlimit_brightness, min_separation, im_scale_min, im_scale_max):
frame.clear()
title = ('Adjusting the binning')
im_bin, dummy, dummy = bin_im(im1, [binx,biny] )
frame = plot_img_spec.plot_image(im_bin, 'dummy_filename', pctile=[im_scale_min,im_scale_max], show=False, adjust=[0.07,0.95,0.95,0.07], title=title, return_frame=True, frame=frame, autotranspose=False, colorbar=False, axis_name=['Cross-dispersion axis [px]','Dispersion axis [px]','flux [ADU]'])
# run initial update plot function
plot(**pkwargs)
# define valid_function
# input is one variable (the string input)
# return is either:
# True and values
# or
# False and error message
def vfunc_int(xs):
try:
value = int(xs)
return True, value
except:
return False, ('Error, input must be integer')
def vfunc_float(xs):
try:
value = float(xs)
return True, value
except:
return False, ('Error, input must be float')
# define widgets
widgets = dict()
widgets['im_scale_min'] = dict(label='Scale the image (black)',
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['im_scale_min'], valid_function=vfunc_float,
width=10)
widgets['im_scale_max'] = dict(label='Scale the image (white)',
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['im_scale_max'], valid_function=vfunc_float,
width=10)
widgets['binx'] = dict(label='Binning in{0}Dispersion axis'.format(os.linesep),
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['binx'], valid_function=vfunc_int,
width=10)
widgets['biny'] = dict(label='Binning in{0}Cross-dispersion axis'.format(os.linesep),
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['biny'], valid_function=vfunc_int,
width=10)
if searchlimit_brightness is not None:
widgets['searchlimit_brightness'] = dict(label='Brightest pixel', comment='of the faintest order{0}in binned image to be traced.'.format(os.linesep),
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['searchlimit_brightness'], valid_function=vfunc_float,
width=10)
if min_separation is not None:
widgets['min_separation'] = dict(label='Minumum separation{0}of orders'.format(os.linesep), comment='in unbinned image.{0}Divide by Binning in{0}Cross-dispersion to{0}compare with image.'.format(os.linesep),
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['min_separation'], valid_function=vfunc_float,
width=10)
widgets['accept'] = dict(label='Accept Values',
kind='ExitButton',
position=Tk.BOTTOM)
widgets['update'] = dict(label='Update', kind='UpdatePlot',
position=Tk.BOTTOM)
wprops = dict(orientation='v', position=Tk.RIGHT)
gui3 = tkc.TkCanvas(figure=fig, ax=frame, func=plot, kwargs=pkwargs,
title='HiFlEx: Adjusting the binning', widgets=widgets,
widgetprops=wprops)
gui3.master.mainloop()
#binxy = pkwargs['binxy'] # This doesn't work, pkwargs are only updated within the plot function
binxy = [ gui3.data['binx'], gui3.data['biny'] ]
if 'searchlimit_brightness' in gui3.data.keys():
searchlimit_brightness = gui3.data['searchlimit_brightness']
if 'min_separation' in gui3.data.keys():
min_separation = gui3.data['min_separation']
plt.close()
return binxy, searchlimit_brightness, min_separation
def remove_adjust_orders_UI(im1, pfits, xlows, xhighs, widths=[], shift=0, userinput=True, do_rm=False, do_adj=False, do_shft=False, do_add=False):
"""
Removes orders from the pfits array in a GUI, allows to adjust the width of th extracted area
:param do_adj: Adjust the width of the traces
return fmask: array of bool with the same length as original orders, True for the orders to keep
return pfits: same format as pfits, with adjusted parameters for the polynomial for the left and right border
return widths: same format as widths, with adjusted left and right stop of the trace
"""
from procedures import remove_orders, plot_traces_over_image, scale_image_plot, sort_traces_along_detector, update_tr_poly_width_multiplicate
if not userinput or (not do_rm and not do_adj and not do_shft and not do_add):
return remove_orders(pfits, []), pfits, widths, xlows, xhighs # No orders to remove, pfits stays the same, widths stays the same
# convert to numpy arrays
pfits = np.array(pfits)
xlows, xhighs = np.array(xlows), np.array(xhighs)
im_log = scale_image_plot(im1,'log10')
# Create a list with all data points to refit the orders
data_x = np.linspace(0, im1.shape[0], int(0.01*im1.shape[0]), dtype=int) # about every 100 pixel in dispersion direction
data_order_position = []
for order in range(pfits.shape[0]):
inorder = ( data_x > xlows[order] ) & (data_x < xhighs[order]-1)
xarr = np.hstack(( xlows[order], data_x[inorder], xhighs[order]-1 )) # add the boundaries as points
if len(pfits.shape) == 3:
yarr = np.polyval(pfits[order,0,1:], xarr-pfits[order,0,0])
yarrl = np.polyval(pfits[order,1,1:], xarr-pfits[order,1,0])
yarrr = np.polyval(pfits[order,2,1:], xarr-pfits[order,2,0])
else:
yarr = np.polyval(pfits[order,1:], xarr-pfits[order,0])
yarrl = yarr
yarrr = yarr
data_order_position.append( np.vstack((xarr, yarr, yarrl, yarrr)).T ) # contains the x and y values in one row
# set up plot
fig, frame = plt.subplots(1, 1)
gui3 = tkc.TkCanvas(figure=None, ax=None, func=None, title='HiFLEx: Test', kwargs=dict(), widgets=dict(), widgetprops=dict(orientation='v', position=Tk.RIGHT) ) # Only to get the display size
dpi=100
fig.set_dpi(dpi)
fig.set_size_inches( (int(gui3.screen_w_h[0]) - 400)/dpi, (int(gui3.screen_w_h[1])-90)/dpi, forward=True) # Make the plot as big as possible
tkc.TkCanvas.end(gui3)
# im_log = scale_image_plot(im1,'log10') # Don't do it here
# get kwargs; add more to pkwargs, as otherwise what is changed in for example settings_modify_end() is lost
pkwargs = dict(frame=frame, update_frame=True, im1=im1, pfits=pfits, xlows=xlows, xhighs=xhighs, widths=widths,
w_mult=1.0, rm_orders=[], shift=shift, im_scale_min=round_sig(np.percentile(im_log,10),2),
im_scale_max=round_sig(np.percentile(im_log,90),2), order=np.nan, addpointsorder_lcr=1,
data_order_position=data_order_position, addpoints=False, removepoints=False)
# define update plot function
def plot(frame, update_frame, im1, pfits, xlows, xhighs, widths, w_mult, rm_orders, shift, im_scale_min, im_scale_max, order, addpointsorder_lcr, data_order_position, **kwargs):
#print('addpointsorder_lcr',addpointsorder_lcr)
x_range, y_range = copy.copy(frame.get_xlim()), copy.copy(frame.get_ylim())
#if update_frame: # This will speed up plotting, but copying the old frame doesn't work, hence it always adds plots to the frame, and frame.clear() before loading creates an clear frame, more in https://stackoverflow.com/questions/57351212/matplotlib-how-to-copy-a-contour-plot-to-another-figure
if True:
frame.clear()
title = ''
if do_adj:
title += 'Defining the width of the traces. '
if do_shft:
title += 'Finding the shift of the traces. '
if do_rm:
title += 'Removing bad orders. '
if do_add:
title += 'Adding/modifying orders. '
if do_rm or do_add:
title += '{1}(Largest order number = {0}){1}'.format(pfits.shape[0]-1, os.linesep)
mask = remove_orders(pfits, rm_orders)
pfits_shift = copy.deepcopy(pfits)
if len(pfits_shift.shape) == 3:
pfits_shift[:,:,-1] += shift # shift all traces
else:
pfits_shift[:,-1] += shift # shift all traces
frame = plot_traces_over_image(scale_image_plot(im1,'log10'), 'dummy_filename', pfits_shift, xlows, xhighs, widths, w_mult=w_mult, mask=mask, frame=frame, return_frame=True, imscale=[im_scale_min,im_scale_max])
frame.set_title(title[:-1])
# pkwargs['backup_frame'] = copy.deepcopy(frame)# This will speed up plotting, but copying the old frame doesn't work, hence it always adds plots to the frame, and frame.clear() before loading creates an clear frame
#else:# This will speed up plotting, but copying the old frame doesn't work, hence it always adds plots to the frame, and frame.clear() before loading creates an clear frame
# frame = copy.deepcopy(pkwargs['backup_frame'])# This will speed up plotting, but copying the old frame doesn't work, hence it always adds plots to the frame, and frame.clear() before loading creates an clear frame
if do_add and not np.isnan(pkwargs['order']):
yarr = data_order_position[order][:,addpointsorder_lcr ] + shift
xarr = data_order_position[order][:,0]
frame.plot(yarr, xarr, color='c', linewidth=1, linestyle='dotted', marker="x", markersize=5)
if x_range != (0.0, 1.0) and y_range != (0.0, 1.0):
frame.axis([x_range[0], x_range[1], y_range[0], y_range[1]])
def settings_modify_order():
pkwargs['backup_data_order_position_order'] = copy.copy(pkwargs['data_order_position'][pkwargs['order']])
gui3.ws['addpointsorder'].config(state=Tk.DISABLED)
if 'addpointsorder_lcr' in gui3.ws.keys():
gui3.ws['addpointsorder_lcr'].config(state=Tk.DISABLED)
gui3.ws['addpoints'].config(state=Tk.DISABLED)
gui3.ws['removepoints'].config(state=Tk.DISABLED)
gui3.ws['update'].config(state=Tk.DISABLED)
gui3.ws['stoppoints'].config(state="normal")
gui3.ws['cancelpoints'].config(state="normal")
tkc.TkCanvas.update(gui3)
pkwargs['update_frame'] = False # When clicking only the points are redrawn
def settings_modify_end():
pkwargs['order'] = np.nan
gui3.ws['addpointsorder'].config(state="normal")
if 'addpointsorder_lcr' in gui3.ws.keys():
gui3.ws['addpointsorder_lcr'].config(state="normal")
gui3.ws['addpoints'].config(state="normal")
gui3.ws['removepoints'].config(state="normal")
gui3.ws['update'].config(state="normal")
gui3.ws['stoppoints'].config(state=Tk.DISABLED)
gui3.ws['cancelpoints'].config(state=Tk.DISABLED)
pkwargs['addpoints'] = False
pkwargs['removepoints'] = False
pkwargs['update_frame'] = True
tkc.TkCanvas.update(gui3)
def add_an_order():
pkwargs['xlows'] = np.hstack(( pkwargs['xlows'], im1.shape[0] ))
pkwargs['xhighs'] = np.hstack(( pkwargs['xhighs'], 0 ))
pfits = pkwargs['pfits']
if len(pfits.shape) == 3:
pfnew = np.zeros((1,pfits.shape[1],pfits.shape[2]))
for ii in range(pfits.shape[1]):
pfnew[0,ii,0] = np.median(pfits[:,ii,0]) # cen_px
pfnew[0,ii,-1] = im1.shape[1]/2. # put it in the middle
else:
pfnew = np.zeros((1, pfits.shape[1]))
pfnew[0,0] = np.median(pfits[:,0]) # cen_px
pfnew[0,-1] = im1.shape[1]/2. # put it in the middle
pkwargs['pfits'] = np.concatenate((pfits,pfnew), axis=0)
pkwargs['data_order_position'].append( np.zeros((0,4)) )
widths = pkwargs['widths']
if len(widths) > 0:
widthnew = np.median(widths,axis=0)
#print(widths, widthnew)
if type(widths).__name__ == 'ndarray':
pkwargs['widths'] = np.concatenate( ( widths, widthnew.reshape((1,widths.shape[1])) ), axis=0)
elif type(widths).__name__ == 'list':
pkwargs['widths'].append(list(widthnew))
gui3.prompt_info('Added order with index {0}. Now you need to add points to it.'.format(pkwargs['pfits'].shape[0]-1), width=300)
#print(pfits.shape, pfnew.shape, pkwargs['pfits'].shape, len(pkwargs['widths']), pkwargs['widths'] )
def addpoints_action():
tkc.TkCanvas.update(gui3, updateplot=False)
pkwargs['order'] = gui3.data['addpointsorder']
if type(pkwargs['order']).__name__ not in ['int']:
return
pkwargs['addpoints'] = True
settings_modify_order()
def removepoints_action():
tkc.TkCanvas.update(gui3, updateplot=False)
pkwargs['order'] = gui3.data['addpointsorder']
if type(pkwargs['order']).__name__ not in ['int']:
return
pkwargs['removepoints'] = True
settings_modify_order()
def cancelpoints_action():
# Restore the original points
pkwargs['data_order_position'][pkwargs['order']] = pkwargs['backup_data_order_position_order']
settings_modify_end()
def stoppoints_action():
order = pkwargs['order']
data = pkwargs['data_order_position'][order]
addpointsorder_lcr = pkwargs['addpointsorder_lcr']
nonnan = ~np.isnan(data[:,addpointsorder_lcr])
if len(pkwargs['pfits'].shape) == 3:
poly_orders = pkwargs['pfits'].shape[2]-2
cen_px = pkwargs['pfits'][order,addpointsorder_lcr-1,0]
else: # addpointsorder_lcr will be always 1
poly_orders = pkwargs['pfits'].shape[1]-2
cen_px = pkwargs['pfits'][order,0]
if np.sum(nonnan) < 2*poly_orders:
gui3.prompt_info('Warn: Needs at least {} points to fit the order. Please add more points or cancel.'.format(2*poly_orders), width=300)
return
pkwargs['xlows'][order] = int(np.min(data[:,0]))
pkwargs['xhighs'][order] = int(np.ceil(np.max(data[:,0])+1))
# Fit the center
pfs = np.polyfit(data[nonnan,0] - cen_px, data[nonnan,addpointsorder_lcr], poly_orders)
if len(pkwargs['pfits'].shape) == 3:
pkwargs['pfits'][order,addpointsorder_lcr-1,1:] = pfs
else: # addpointsorder_lcr will be always 1
pkwargs['pfits'][order,1:] = pfs
settings_modify_end()
def onclick(event):
#print('clicked',event.xdata, event.ydata, event.inaxes, im1.shape)
if event.xdata is None or event.ydata is None:
return
if not pkwargs['addpoints'] and not pkwargs['removepoints']: # Not set
return
yy, xx = event.xdata-shift, event.ydata # event.ydata: dispersion axis, xx in dispersion axis
uncertainty = 15
data = pkwargs['data_order_position'][pkwargs['order']] # data[:,0]: dispersion axis
addpointsorder_lcr = pkwargs['addpointsorder_lcr']
if pkwargs['addpoints']:
if xx < 0 or xx > im1.shape[0]-1 or yy < 0 or yy > im1.shape[1]-1: #im1.shape[0]: disersion axis
return # click was outside of the image
datanew = [xx, np.nan, np.nan, np.nan]
datanew[addpointsorder_lcr] = yy
data = np.vstack((data, datanew))
datasortindex = np.argsort(data[:,0])
pkwargs['data_order_position'][pkwargs['order']] = data[datasortindex,:]
elif pkwargs['removepoints']:
yarr = copy.copy(data[:,addpointsorder_lcr])
xarr = copy.copy(data[:,0])
badvalues = np.isnan(yarr)
xarr[badvalues] = -1E6
yarr[badvalues] = -1E6
diff = (xarr-xx)**2 + (yarr-yy)**2
posmin = np.argmin(diff)
#print('smallest distance', np.sqrt(diff[posmin]), data, posmin)
if np.sqrt(diff[posmin]) > uncertainty: # Too many pixel away
return
data[posmin,addpointsorder_lcr] = np.nan
pkwargs['data_order_position'][pkwargs['order']] = data
tkc.TkCanvas.update(gui3, updateplot=True)
# !!!!!!!!!!!! do it for pressing lrc to work on boundaries of the orders?
# define valid_function
# input is one variable (the string input)
# return is either:
# True and values
# or
# False and error message
def vfunc(xs):
try:
xwhole = xs.replace(',', ' ')
new_xs = xwhole.split()
xs = []
for nxs in new_xs:
xs.append(int(nxs))
return True, xs
except:
return False, ('Error, input must consist of integers'+os.linesep+\
'separated by commas or white spaces')
def vfunc_int(xs):
try:
value = int(xs)
return True, value
except:
return False, ('Error, input must be integer'+os.linesep)
def vfunc_float(xs):
try:
value = float(xs)
return True, value
except:
return False, ('Error, input must be float')
def vfunc_lcr(xs):
if xs == 'c':
return True, 1
elif xs == 'l':
return True, 2
elif xs == 'r':
return True, 3
else:
return False, (' Error, input must be either l, c, or r ')
# run initial update plot function
plot(**pkwargs)
# define widgets
widgets = dict()
widgets['im_scale_min'] = dict(label='Scale the image (black)', comment='The image will be shown in log10 scale',
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['im_scale_min'], valid_function=vfunc_float,
width=10)
widgets['im_scale_max'] = dict(label='Scale the image (white)',
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=pkwargs['im_scale_max'], valid_function=vfunc_float,
width=10)
if do_rm:
widgets['rm_orders'] = dict(label='Select orders to remove',
comment='Enter all order numbers to remove'+os.linesep+\
'separated by a whitespace or comma'+os.linesep+\
'to undo just delete the entered number',
kind='TextEntry', minval=None, maxval=None,
fmt=str, start=" ", valid_function=vfunc,
width=40)
if do_adj:
widgets['w_mult'] = dict(label='Multiplier for the'+os.linesep+'width of the traces',
comment='If the results are not as wished,'+os.linesep+\
'a modification of the parameter "width_percentile"'+os.linesep+\
'might help. To do this'+os.linesep+\
'Cancel the script with CTRL+C in the terminal'+os.linesep+\
'and then restart',
kind='TextEntry', minval=None, maxval=None,
fmt=float, start=pkwargs['w_mult'], valid_function=vfunc_float,
width=10)
if do_shft:
widgets['shift'] = dict(label='Shift the traces by:',
#comment='',
kind='TextEntry', minval=None, maxval=None,
fmt=float, start=0.0, valid_function=vfunc_float,
width=10)
if do_add:
widgets['addpointsorder'] = dict(label='Add/remove points from order:',
comment='It might be necessary to zoom before'+os.linesep+\
'adding/removing points will work',
kind='TextEntry', minval=None, maxval=None,
fmt=int, start='', valid_function=vfunc_int,
width=10)
if len(pkwargs['pfits'].shape) == 3:
widgets['addpointsorder_lcr'] = dict(label='Which part of the order?',
comment='c: center (brightest part),'+os.linesep+\
'l or r: left or right boundary of the order',
kind='TextEntry', minval=None, maxval=None,
fmt=str, start='c', valid_function=vfunc_lcr,
width=10)
#widgets['addpoints'] = dict(label='Add or remove points from'+os.linesep+\
# 'the order given below',
# kind='CheckBox', start=False)
#widgets['spacer1'] = dict(kind='Spacer', position=Tk.BOTTOM)
widgets['accept'] = dict(label='Accept', kind='ExitButton',
position=Tk.BOTTOM)
widgets['update'] = dict(label='Update', kind='UpdatePlot',
position=Tk.BOTTOM)
if do_add:
widgets['addorder'] = dict(label='Add a new Order', kind='CommandButton', command=add_an_order,
position=Tk.BOTTOM, width=20)
widgets['stoppoints'] = dict(label='Use added/removed points', kind='CommandButton', command=stoppoints_action,
position=Tk.BOTTOM, state=Tk.DISABLED, width=30)
widgets['cancelpoints'] = dict(label='Cancel adding/removing points', kind='CommandButton', command=cancelpoints_action,
position=Tk.BOTTOM, state=Tk.DISABLED, width=30)
widgets['removepoints'] = dict(label='Remove points from an Order', kind='CommandButton', command=removepoints_action,
position=Tk.BOTTOM, width=30)
widgets['addpoints'] = dict(label='Add points to an Order', kind='CommandButton', command=addpoints_action,
position=Tk.BOTTOM, width=30)
wprops = dict(orientation='v', position=Tk.RIGHT)
gui3 = tkc.TkCanvas(figure=fig, ax=frame, func=plot, kwargs=pkwargs,
title='HiFlEx: Locating orders', widgets=widgets,
widgetprops=wprops)
cid = fig.canvas.callbacks.connect('button_press_event', onclick) # It works with GUI
gui3.master.mainloop()
pfits = pkwargs['pfits']
xlows = pkwargs['xlows']
xhighs = pkwargs['xhighs']
widths = pkwargs['widths']
w_mult = pkwargs['w_mult']
shift = pkwargs['shift']
rm_orders = pkwargs['rm_orders']
# Remove bad orders
good_data = (xhighs - xlows >= 5) # enough data added to the order
if len(pfits.shape) == 3:
pfits = pfits[good_data,:,:]
else:
pfits = pfits[good_data,:]
xlows = xlows[good_data]
xhighs = xhighs[good_data]
if len(widths) > 0:
widths = widths[good_data,:]
# Mask removed orders
fmask = remove_orders(pfits, rm_orders)
# Rescale the widths
if 'w_mult' != 1.0 and len(pfits.shape) == 3 and len(widths) > 0:
pfits, widths = update_tr_poly_width_multiplicate(pfits, widths, [w_mult, w_mult], xlows, xhighs)
# Shift orders
if shift != 0.0:
if len(pfits.shape) == 3:
pfits[:,:,-1] += shift # shift all traces
else:
pfits[:,-1] += shift # shift all traces
# Sort the orders
if len(pfits.shape) == 3:
sort_index = sort_traces_along_detector(pfits[:,0,:], xlows, xhighs)
pfits = pfits[sort_index,:,:]
else:
sort_index = sort_traces_along_detector(pfits, xlows, xhighs)
pfits = pfits[sort_index,:]
xlows = xlows[sort_index]
xhighs = xhighs[sort_index]
if len(widths) > 0:
widths = widths[sort_index,:]
fmask = fmask[sort_index]
plt.close()
return fmask, pfits, widths, xlows, xhighs
def create_new_wavelength_UI( params, cal_l_spec, cal_s_spec, arc_lines_px, reference_lines_dict, adjust=[0.12,0.88,0.85,0.12, 1.0,1.01] ):
"""
:param arc_lines_px: numpy arry of floats: order, pixel, width, height of the line
"""
from procedures import logger, read_text_file, convert_readfile, fit_basic_wavelength_solution, adjust_data_log
reference_catalog, reference_names = reference_lines_dict['reference_catalog'][0], reference_lines_dict['reference_names'][0]
px_to_wave_txt = read_text_file(params['px_to_wavelength_file'], no_empty_lines=True, warn_missing_file=False) # list of strings
if len(px_to_wave_txt) == 0:
px_to_wave_txt = read_text_file(params['logging_path']+'tmp_'+params['px_to_wavelength_file'], no_empty_lines=True, warn_missing_file=False) # list of strings
px_to_wave = np.array( convert_readfile(px_to_wave_txt, [int, int, float, float], delimiter='\t', replaces=[['\n',''],[os.linesep,'']], shorten_input=True, replacewithnan=True )) # order, real order, px, wave
if px_to_wave.shape[0] > 0:
px_to_wave = px_to_wave[~np.isnan(px_to_wave[:,0]),:] # clear the values that don't have order number
nr_entries = len(px_to_wave)
if nr_entries > 0:
order_offset = np.nan
if np.prod( np.isnan(px_to_wave[:,1]) ) == 0:
order_offset = int(np.nanmedian(px_to_wave[:,1] - px_to_wave[:,0]))
px_to_wave = np.hstack(( px_to_wave, np.zeros((nr_entries,2))*np.nan, np.expand_dims(np.arange(nr_entries), axis=1), np.zeros((nr_entries,1))*np.nan )) # order, real order, px, wave, width, height of line, index, nan
order = -1E6
for entry in arc_lines_px:
if entry[0] != order: # So this step is only done when neccessary
order = entry[0]
px_to_wave_sub = px_to_wave[ px_to_wave[:,0] == order, : ]
diff = np.abs(px_to_wave_sub[:,2] - entry[1])
posi = np.where(diff < 2)[0]
if len(posi) >= 1:
index = int(px_to_wave_sub[posi[0], 6]) # first entry as highest signal, not min(diff)
px_to_wave[index,4] = entry[2]
px_to_wave[index,5] = entry[3]
else:
px_to_wave = np.vstack(( px_to_wave, [order, order+order_offset, entry[1], np.nan, entry[2], entry[3], len(px_to_wave), np.nan ] ))
else:
order_offset = np.nan
tmp = arc_lines_px[:,0]*np.nan
px_to_wave = np.vstack(( arc_lines_px[:,0], tmp, arc_lines_px[:,1], tmp, arc_lines_px[:,2], arc_lines_px[:,3], tmp, tmp )).T
px_to_wave[np.isnan(px_to_wave[:,4]),4] = -1
px_to_wave[np.isnan(px_to_wave[:,5]),5] = -1
px_to_wave[:,6] = np.nan
order = int(len(cal_l_spec)/2)
fig, ax = plt.subplots(3, 1, gridspec_kw={'height_ratios': [2, 5, 2]})
gui3 = tkc.TkCanvasGrid(figure=None, ax=None, func=None, title='HiFLEx: Test', kwargs=dict(), widgets=dict(), widgetprops=dict() ) # Only to get the display size
dpi=100
fig.set_dpi(dpi)
fig.set_size_inches( (int(gui3.screen_w_h[0]) - 400)/dpi, (int(gui3.screen_w_h[1])-90)/dpi, forward=True) # Make the plot as big as possible
tkc.TkCanvasGrid.end(gui3)
plt.subplots_adjust(left=adjust[0], right=adjust[1], top=adjust[2], bottom=adjust[3])
pkwargs = dict()
def mark_line_in_GUI(px):
gui3.repopulate()
px_to_wave = pkwargs['px_to_wave']
px_to_wave_sub = px_to_wave[ px_to_wave[:,0] == pkwargs['order'], : ]
match = np.where( np.abs(px_to_wave_sub[:,2] - px) < 20)[0]
if len(match) == 0:
unmark_line_in_GUI()
else:
index = match[0]
for widget in gui3.ws.keys():
if widget.find('wave_') > -1:
try:
number = int(widget.replace('wave_',''))
except:
continue
if number != index:
gui3.ws[widget].config(state=Tk.DISABLED)
def unmark_line_in_GUI():
gui3.repopulate()
def onclick(event):
#print('clicked',event.xdata, event.ydata, event.inaxes)
if event.xdata is None or event.ydata is None:
unmark_line_in_GUI()
else:
px = event.xdata
mark_line_in_GUI(px)
def plot(ax, cal_l_spec, cal_s_spec, px_to_wave, order, **pkwarks):
for ii in range(3):
ax[ii].clear()
ordii = order+ii-1
if ordii < 0 or ordii >= len(cal_l_spec): # outside of useful range
continue
y1 = adjust_data_log( cal_l_spec[ordii,:] )
y2 = adjust_data_log( cal_s_spec[ordii,:] )
x = list(range(len(y1)))
title = None
labels = []
x_title = ''
y_title = 'log ( Flux [ADU] )'
if ii == 0:
title = 'Plot of the spectrum for previous ({0}), actual ({1}), and next order ({2})'.format(order-1, order, order+1)
labels = ['long'+os.linesep+'exp', 'short'+os.linesep+'exp']
if ii == 2:
x_title = 'Dispersion direction [px]'
ax[ii] = plot_img_spec.plot_points([x,x], [y1,y2], labels, [], show=False, adjust=[0.05,0.99,0.97,0.05, 0.92,1.2], title='', return_frame=True, frame=ax[ii], x_title=x_title, y_title=y_title, linestyle="-", marker="")
pctl = 2./len(y1)*100 # Exclude the two highest and two lowest points
xmin, xmax = np.nanmin(x), np.nanmax(x)
ymin1, ymax1 = np.nanpercentile(y1, pctl), np.nanpercentile(y1, 100-pctl)
ymin2, ymax2 = np.nanpercentile(y2, pctl), np.nanpercentile(y2, 100-pctl)
ymin, ymax = min(ymin1, ymin2), max(ymax1, ymax2)
y_range = ymax - ymin
x_range = xmax - xmin
if np.isnan(y_range) or np.isnan(x_range):
continue
ax[ii].set_ylim([ymin-y_range*0.01, ymax+y_range*0.01])
ax[ii].set_xlim([xmin-x_range*0.02, xmax+x_range*0.02])
title = {0:'Previous', 1:'', 2:'Next'}[ii]
ax[ii].set_title('{0} Order: {1}'.format(title, ordii), fontsize=11)
# Plot the wavelengths from the reference list
px_to_wave_sub = px_to_wave[ px_to_wave[:,0] == ordii, : ]
if ii == 1:
goodentries = 0
else:
goodentries = 1E6 # to only plot the identified lines
for jj, entry in enumerate(px_to_wave_sub):
if entry[3] is np.nan or str(entry[3]) == 'nan':