This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main_V7.py
1966 lines (1602 loc) · 79 KB
/
Main_V7.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
#! /usr/bin/env python
#
# example2_tk.py -- Simple, configurable FITS viewer.
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import sys
#sys.path.append('/opt/anaconda3/envs/samos_env/lib/python3.10/site-packages')
import os
from os.path import exists as file_exists
import time
from argparse import ArgumentParser
import threading
import pandas as pd
from ginga.tkw.ImageViewTk import CanvasView
from ginga.canvas.CanvasObject import get_canvas_types
from ginga.misc import log
from ginga.util.loader import load_data
from ginga import colors
from ginga.util.ap_region import astropy_region_to_ginga_canvas_object as r2g
from ginga.util.ap_region import ginga_canvas_object_to_astropy_region as g2r
from ginga.canvas import CompoundMixin as CM
from ginga.util import ap_region
from ginga.AstroImage import AstroImage
img = AstroImage()
from PIL import Image,ImageTk,ImageOps
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfile
#import regions
from regions import Regions
from regions import PixCoord, RectanglePixelRegion, PointPixelRegion, RegionVisual
from astropy import units as u
from astropy.io import fits, ascii
from astropy.stats import sigma_clipped_stats, SigmaClip
import astropy.wcs as wcs
from photutils.background import Background2D, MedianBackground
from photutils.detection import DAOStarFinder
from ginga.util import iqcalc
iq = iqcalc.IQCalc()
import matplotlib.pyplot as plt
import csv
### Needed to run ConvertSIlly by C. Loomis
import math
import pathlib
import numpy as np
import glob
import re
#import sewpy #to run sextractor wrapper
STD_FORMAT = '%(asctime)s | %(levelname)1.1s | %(filename)s:%(lineno)d (%(funcName)s) | %(message)s'
# =============================d================================================
#
# from Astrometry import tk_class_astrometry
# Astrometry = tk_class_astrometry
#
# Astrometry.return_from_astrometry()
#
# =============================================================================
from pathlib import Path
#define the local directory, absolute so it is not messed up when this is called
path = Path(__file__).parent.absolute()
local_dir = str(path.absolute())
sys.path.append(local_dir)
#print("line 48 main local",local_dir)
os.sys.path.append(local_dir)
os.sys.path.append(local_dir+"/Astrometry")
os.sys.path.append(local_dir+"/SAMOS_CCD_dev")
os.sys.path.append(local_dir+"/SAMOS_DMD_dev")
os.sys.path.append(local_dir+"/SAMOS_MOTORS_dev")
os.sys.path.append(local_dir+"/SAMOS_SOAR_dev")
os.sys.path.append(local_dir+"/SAMOS_CONFIG_dev")
from SAMOS_CONFIG_dev.CONFIG_GUI import Config
#print(Config.return_directories)
from SAMOS_Astrometry_dev.tk_class_astrometry_V4 import Astrometry
from SAMOS_CCD_dev.GUI_CCD_dev import GUI_CCD
from SAMOS_CCD_dev.Class_CCD_dev import Class_Camera as CCD
from SAMOS_MOTORS_dev.Class_PCM import Class_PCM
Motors = Class_PCM()
from SAMOS_MOTORS_dev.SAMOS_MOTORS_GUI_dev import Window as SM_GUI
from SAMOS_DMD_dev.Class_DMD import DigitalMicroMirrorDevice as DMD
from SAMOS_DMD_dev.Class_DMD_dev import DigitalMicroMirrorDevice
DMD = DigitalMicroMirrorDevice()#config_id='pass')
from SAMOS_DMD_dev.SAMOS_DMD_GUI_dev import GUI_DMD
from SAMOS_SOAR_dev.tk_class_SOAR_V0 import SOAR as SOAR
from SAMOS_system_dev.SAMOS_Functions import Class_SAMOS_Functions as SF
from SAMOS_DMD_dev.CONVERT.CONVERT_class import CONVERT
convert = CONVERT()
from SlitTableViewer import SlitTableView as STView
#from ginga.misc import widgets
#import PCM_module_GUI as Motors
#text format for writing new info to header. Global var
param_entry_format = '[Entry {}]\nType={}\nKeyword={}\nValue="{}"\nComment="{}\n"'
#SlitTabView = STView()
class SAMOS_Main(object):
def __init__(self, logger):
self.logger = logger
self.drawcolors = colors.get_colors()
# self.drawcolors = ['white', 'black', 'red', 'yellow', 'blue', 'green']
self.canvas_types = get_canvas_types()
# table widget to keep track of slit regions
root = tk.Tk()
root.title("SAMOS")
root.geometry("1280x900")
#root.set_border_width(2)
#root.connect("delete_event", lambda w, e: self.quit(w))
self.root = root
# keep track of the entry number for header keys that need to be added
# will be used to write "OtherParameters.txt"
self.extra_header_params = 0
self.header_entry_string = '' #keep string of entries to write to a file after acquisition.
# =============================================================================
# #
# # Menu Bar goes into the mac header...
# #
# =============================================================================
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Motors Setup", command=self.load_Motors_module_GUI)
filemenu.add_command(label="DMD Setup", command=self.load_DMD_module_GUI)
filemenu.add_command(label="SOAR comm Setup", command=self.load_SOAR_module_GUI)
filemenu.add_command(label="CCD Acquisition", command=self.load_CCD_module_GUI)
filemenu.add_command(label="Astrometry", command=self.load_Astrometry)
# filemenu.add_command(label="Config", command=self.CONFIG_GUI)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=self.donothing)
helpmenu.add_command(label="About...", command=self.donothing)
menubar.add_cascade(label="Help", menu=helpmenu)
root.config(menu=menubar)
# =============================================================================
#
# # FILTER STATUS Label Frame
#
# =============================================================================
self.frame0l = tk.Frame(root,background="cyan")#, width=400, height=800)
self.frame0l.place(x=4, y=0, anchor="nw", width=220, height=110)
labelframe_Filters = tk.LabelFrame(self.frame0l, text="Filter Status", font=("Arial", 24))
labelframe_Filters.pack(fill="both", expand="yes")
# label_FW1 = tk.Label(labelframe_Filters, text="Filters")
# label_FW1.place(x=4,y=10)
all_dirs = SF.read_dir_user()
filter_data= ascii.read(local_dir+all_dirs['dir_Motors']+'/IDG_Filter_positions.txt')
filter_names = list(filter_data[0:9]['Filter'])
print(filter_names)
self.FW1_filter = tk.StringVar()
# initial menu text
self.FW1_filter.set(filter_names[2])
# Create Dropdown menu
self.optionmenu_FW1 = tk.OptionMenu(labelframe_Filters, self.FW1_filter, *filter_names)
self.optionmenu_FW1.place(x=5, y=8)
button_SetFW1 = tk.Button(labelframe_Filters, text="Set Filter", bd=3, command=self.set_filter)
button_SetFW1.place(x=110,y=4)
# self.Current_Filter = tk.StringVar()
# self.Current_Filter.set(self.FW1_filter.get())
self.Label_Current_Filter = tk.Text(labelframe_Filters,font=('Georgia 20'),width=8,height=1,bg='white', fg='green')
#self.Label_Current_Filter.insert(tk.END,"",#self.FW1_Filter)
self.Label_Current_Filter.insert(tk.END,self.FW1_filter.get())
self.Label_Current_Filter.place(x=30,y=45)
# =============================================================================
# entry_FW1 = tk.Entry(labelframe_Filters, width=11, bd =3)
# entry_FW1.place(x=100, y=10)
# # =============================================================================
# =============================================================================
# label_FW1_template = tk.Label(labelframe_Filters, text="HH:MM:SS.xx")
# label_FW1_template.place(x=200,y=10)
#
# =============================================================================
# =============================================================================
# label_FW2 = tk.Label(labelframe_Filters, text="FW 2")
# label_FW2.place(x=4,y=40)
# # Dropdown menu options
# FW2_options = [
# "[OIII]",
# "Ha",
# "[SII]",
# "blank",
# "open"
# ]
# # datatype of menu text
# self.FW2_filter = tk.StringVar()
# # initial menu text
# self.FW2_filter.set(FW2_options[4])
# # Create Dropdown menu
# self.optionmenu_FW2 = tk.OptionMenu(labelframe_Filters, self.FW2_filter, *FW2_options)
# self.optionmenu_FW2.place(x=40, y=38)
# button_SetFW2 = tk.Button(labelframe_Filters, text="Set FW2", bd=3)
# button_SetFW2.place(x=125,y=34)
#
# =============================================================================
# =============================================================================
# entry_FW2 = tk.Entry(labelframe_Filters, width=11, bd =3)
# entry_FW2.place(x=100, y=40)
# # =============================================================================
# =============================================================================
# label_FW1_template = tk.Label(labelframe_Filters, text="2213DD:MM:SS.xx")
# label_FW1_template.place(x=200,y=10)
#
# =============================================================================
# button_HomeFW1 = tk.Button(labelframe_Filters, text="Home FW1", bd=3)
# button_HomeFW1.place(x=4,y=70)
# button_HomeFW2 = tk.Button(labelframe_Filters, text="Home FW2", bd=3)
# button_HomeFW2.place(x=105,y=70)
# =============================================================================
#
# # GRISM STATUS Label Frame
#
# =============================================================================
self.frame1l = tk.Frame(root,background="cyan")#, width=400, height=800)
self.frame1l.place(x=4, y=120, anchor="nw", width=220, height=110)
labelframe_Grating = tk.LabelFrame(self.frame1l, text="Grism Status", font=("Arial", 24))
labelframe_Grating.pack(fill="both", expand="yes")
# labelframe_Grating.place(x=4, y=10)
all_dirs = SF.read_dir_user()
Grating_data= ascii.read(local_dir+all_dirs['dir_Motors']+'/IDG_Filter_positions.txt')
self.Grating_names = list(Grating_data[14:18]['Filter'])
self.Grating_positions= list(Grating_data[14:18]['Position'])
# print(Grating_names)
#
self.Grating_Optioned = tk.StringVar()
# initial menu text
index=2
self.Grating_Optioned.set(self.Grating_names[index])
# Create Dropdown menu
self.optionmenu_GR = tk.OptionMenu(labelframe_Grating, self.Grating_Optioned, *self.Grating_names)
self.optionmenu_GR.place(x=5, y=8)
button_SetGR = tk.Button(labelframe_Grating, text="Set Grating", bd=3, command=self.set_grating)
button_SetGR.place(x=110,y=4)
# =============================================================================
# self.Grating_int = tk.IntVar()
# self.Grating_int.set(2)
# self.optionmenu_GR = tk.OptionMenu(labelframe_Grating, self.Grating_int, *self.Grating_names)
# self.optionmenu_GR.place(x=5, y=8)
# button_SetGR = tk.Button(labelframe_Grating, text="Set Grating", bd=3, command=self.set_grating)
# button_SetGR.place(x=110,y=4)
#
# =============================================================================
self.Label_Current_Grating = tk.Text(labelframe_Grating,font=('Georgia 20'),width=8,height=1,bg='white', fg='green')
#self.Label_Current_Filter.insert(tk.END,"",#self.FW1_Filter)
self.Label_Current_Grating.insert(tk.END,self.Grating_names[index])
self.Label_Current_Grating.place(x=30,y=45)
# =============================================================================
# # Dropdown menu options
# options = [
# "Low Blue",
# "Low Red",
# "High Blue",
# "High Red"
# ]
# # datatype of menu text
# self.grating = tk.StringVar()
# # initial menu text
# self.grating.set(options[2])
# # Create Dropdown menu
# self.optionmenu_grating = tk.OptionMenu(labelframe_Grating, self.grating, *options)
# self.optionmenu_grating.place(x=4, y=0)
#
# button_HomeGrating= tk.Button(labelframe_Grating, text="Home Grating", bd=3)
# button_HomeGrating.place(x=4,y=35)
#
# =============================================================================
# =============================================================================
# =============================================================================
# label_FW1 = tk.Label(labelframe_Filters, text="Grism")
# label_FW1.place(x=4,y=10)
# entry_FW1 = tk.Entry(labelframe_Filters, width=5, bd =3)
# entry_FW1.place(x=100, y=10)
# label_FW2 = tk.Label(labelframe_Filters, text="Filter Wheel 2")
# label_FW2.place(x=4,y=40)
# entry_FW2 = tk.Entry(labelframe_Filters, width=5, bd =3)
# entry_FW2.place(x=100, y=40)
#
# button_HomeFW1 = tk.Button(labelframe_Filters, text="Home FW1", bd=3)
# button_HomeFW1.place(x=0,y=70)
# button_HomeFW2 = tk.Button(labelframe_Filters, text="Home FW2", bd=3)
# button_HomeFW2.place(x=105,y=70)
#
# =============================================================================
# self.frame0r = tk.Frame(root,background="cyan")#, width=400, height=800)
# self.frame0r.place(x=601, y=0, anchor="nw", width=500, height=800)
#
#
# vbox = tk.Frame(self.frame0l, relief=tk.RAISED, borderwidth=1)
# vbox.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# =============================================================================
# =============================================================================
#
# # ACQUIRE IMAGE Label Frame
#
# =============================================================================
self.frame2l = tk.Frame(root,background="cyan")#, width=400, height=800)
self.frame2l.place(x=0, y=240, anchor="nw", width=370, height=280)
# root = tk.Tk()
# root.title("Tab Widget")
tabControl = ttk.Notebook(self.frame2l)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tab4 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='Image')
tabControl.add(tab2, text ='Bias')
tabControl.add(tab3, text ='Dark')
tabControl.add(tab4, text ='Flat')
tabControl.pack(expand = 1, fill ="both")
# =============================================================================
# SCIENCE
# =============================================================================
labelframe_Acquire = tk.LabelFrame(tab1, text="Acquire Image", font=("Arial", 24))
labelframe_Acquire.pack(fill="both", expand="yes")
# labelframe_Grating.place(x=4, y=10)
label_ExpTime = tk.Label(labelframe_Acquire, text="Exp. Time (s)")
label_ExpTime.place(x=4,y=10)
self.Light_ExpT=tk.StringVar()
self.Light_ExpT.set("0.01")
entry_ExpTime = tk.Entry(labelframe_Acquire, textvariable=self.Light_ExpT, width=5, bd =3)
entry_ExpTime.place(x=100, y=10)
label_ObjectName = tk.Label(labelframe_Acquire, text="Object Name:")
label_ObjectName.place(x=4,y=40)
entry_ObjectName = tk.Entry(labelframe_Acquire, width=11, bd =3)
entry_ObjectName.place(x=100, y=38)
label_Comment = tk.Label(labelframe_Acquire, text="Comment:")
label_Comment.place(x=4,y=70)
# scrollbar = tk.Scrollbar(orient="horizontal")
entry_Comment = tk.Entry(labelframe_Acquire, width=11, bd =3)# , xscrollcommand=scrollbar.set)
entry_Comment.place(x=100, y=68)
button_ExpStart= tk.Button(labelframe_Acquire, text="READ", bd=3, bg='#0052cc',font=("Arial", 24),
command=self.expose_light)
button_ExpStart.place(x=75,y=95)
label_Display = tk.Label(labelframe_Acquire, text="Subtract for Display:")
label_Display.place(x=4,y=135)
self.subtract_Bias = tk.IntVar()
check_Bias = tk.Checkbutton(labelframe_Acquire, text='Bias',variable=self.subtract_Bias, onvalue=1, offvalue=0)
check_Bias.place(x=4, y=155)
self.subtract_Dark = tk.IntVar()
check_Dark = tk.Checkbutton(labelframe_Acquire, text='Dark',variable=self.subtract_Dark, onvalue=1, offvalue=0)
check_Dark.place(x=60,y=155)
self.subtract_Flat = tk.IntVar()
check_Flat = tk.Checkbutton(labelframe_Acquire, text='Flat',variable=self.subtract_Flat, onvalue=1, offvalue=0)
check_Flat.place(x=120,y=155)
# =============================================================================
# BIAS
# =============================================================================
labelframe_Bias = tk.LabelFrame(tab2, text="Bias",
width=300, height=170,
font=("Arial", 24))
labelframe_Bias.pack(fill="both", expand="yes")
# labelframe_Bias.place(x=5,y=5)
label_Bias_ExpT = tk.Label(labelframe_Bias, text="Exposure time (s):")
label_Bias_ExpT.place(x=4,y=10)
self.Bias_ExpT = tk.StringVar(value="0.00")
entry_Bias_ExpT = tk.Entry(labelframe_Bias, width=6, bd =3, textvariable=self.Bias_ExpT)
entry_Bias_ExpT.place(x=120, y=6)
label_Bias_NofFrames = tk.Label(labelframe_Bias, text="Nr. of Frames:")
label_Bias_NofFrames.place(x=4,y=40)
self.Bias_NofFrames = tk.StringVar(value="10")
entry_Bias_NofFrames = tk.Entry(labelframe_Bias, width=5, bd =3, textvariable=self.Bias_NofFrames)
entry_Bias_NofFrames.place(x=100, y=38)
self.var_Bias_saveall = tk.IntVar()
r1_Bias_saveall = tk.Radiobutton(labelframe_Bias, text = "Save single frames", variable=self.var_Bias_saveall, value=1)
r1_Bias_saveall.place(x=160, y=38)
label_Bias_MasterFile = tk.Label(labelframe_Bias, text="Master Bias File:")
label_Bias_MasterFile.place(x=4,y=70)
self.Bias_MasterFile = tk.StringVar(value="Bias")
entry_Bias_MasterFile = tk.Entry(labelframe_Bias, width=11, bd =3, textvariable=self.Bias_MasterFile)
entry_Bias_MasterFile.place(x=120, y=68)
button_ExpStart= tk.Button(labelframe_Bias, text="START", bd=3, bg='#0052cc',font=("Arial", 24),
command=self.expose_bias)
button_ExpStart.place(x=75,y=95)
# root.mainloop()
# =============================================================================
# Dark
# =============================================================================
labelframe_Dark = tk.LabelFrame(tab3, text="Dark",
width=300, height=170,
font=("Arial", 24))
labelframe_Dark.pack(fill="both", expand="yes")
label_Dark_ExpT = tk.Label(labelframe_Dark, text="Exposure time (s):")
label_Dark_ExpT.place(x=4,y=10)
self.Dark_ExpT = tk.StringVar(value="0.00")
entry_Dark_ExpT = tk.Entry(labelframe_Dark, width=6, bd =3, textvariable=self.Dark_ExpT)
entry_Dark_ExpT.place(x=120, y=6)
label_Dark_NofFrames = tk.Label(labelframe_Dark, text="Nr. of Frames:")
label_Dark_NofFrames.place(x=4,y=40)
self.Dark_NofFrames = tk.StringVar(value="10")
entry_Dark_NofFrames = tk.Entry(labelframe_Dark, width=5, bd =3, textvariable=self.Dark_NofFrames)
entry_Dark_NofFrames.place(x=100, y=38)
self.var_Dark_saveall = tk.IntVar()
r1_Dark_saveall = tk.Radiobutton(labelframe_Dark, text = "Save single frames", variable=self.var_Dark_saveall, value=1)
r1_Dark_saveall.place(x=160, y=38)
label_Dark_MasterFile = tk.Label(labelframe_Dark, text="Master Dark File:")
label_Dark_MasterFile.place(x=4,y=70)
self.Dark_MasterFile = tk.StringVar(value="Dark")
entry_Dark_MasterFile = tk.Entry(labelframe_Dark, width=11, bd =3, textvariable=self.Dark_MasterFile)
entry_Dark_MasterFile.place(x=120, y=68)
button_ExpStart = tk.Button(labelframe_Dark, text="START", bd=3, bg='#0052cc',font=("Arial", 24),
command=self.expose_dark)
button_ExpStart.place(x=75,y=95)
# =============================================================================
# Flat
# =============================================================================
labelframe_Flat = tk.LabelFrame(tab4, text="Flat",
width=300, height=170,
font=("Arial", 24))
labelframe_Flat.pack(fill="both", expand="yes")
label_Flat_ExpT = tk.Label(labelframe_Flat, text="Exposure time (s):")
label_Flat_ExpT.place(x=4,y=10)
self.Flat_ExpT = tk.StringVar(value="0.00")
entry_Flat_ExpT = tk.Entry(labelframe_Flat, width=6, bd =3, textvariable=self.Flat_ExpT)
entry_Flat_ExpT.place(x=120, y=6)
label_Flat_NofFrames = tk.Label(labelframe_Flat, text="Nr. of Frames:")
label_Flat_NofFrames.place(x=4,y=40)
self.Flat_NofFrames = tk.StringVar(value="10")
entry_Flat_NofFrames = tk.Entry(labelframe_Flat, width=5, bd =3, textvariable=self.Flat_NofFrames)
entry_Flat_NofFrames.place(x=100, y=38)
self.var_Flat_saveall = tk.IntVar()
r1_Flat_saveall = tk.Radiobutton(labelframe_Flat, text = "Save single frames", variable=self.var_Flat_saveall, value=1)
r1_Flat_saveall.place(x=160, y=38)
label_Flat_MasterFile = tk.Label(labelframe_Flat, text="Master Flat File:")
label_Flat_MasterFile.place(x=4,y=70)
self.Flat_MasterFile = tk.StringVar(value="Flat")
entry_Flat_MasterFile = tk.Entry(labelframe_Flat, width=11, bd =3, textvariable=self.Flat_MasterFile)
entry_Flat_MasterFile.place(x=120, y=68)
button_ExpStart= tk.Button(labelframe_Flat, text="START", bd=3, bg='#0052cc',font=("Arial", 24),
command=self.expose_flat)
button_ExpStart.place(x=75,y=95)
# =============================================================================
#
# # FITS manager
#
# =============================================================================
self.frame_FITSmanager = tk.Frame(root,background="pink")#, width=400, height=800)
self.frame_FITSmanager.place(x=0, y=500, anchor="nw", width=220, height=250)
labelframe_FITSmanager = tk.LabelFrame(self.frame_FITSmanager, text="FITS manager", font=("Arial", 24))
labelframe_FITSmanager.pack(fill="both", expand="yes")
# =============================================================================
#
#
#
# label_FW1 = tk.Label(labelframe_Filters, text="Filter Wheel 1")
# label_FW1.place(x=4,y=10)
# entry_FW1 = tk.Entry(labelframe_Filters, width=5, bd =3)
# entry_FW1.place(x=100, y=10)
# label_FW2 = tk.Label(labelframe_Filters, text="Filter Wheel 2")
# label_FW2.place(x=4,y=40)
# entry_FW2 = tk.Entry(labelframe_Filters, width=5, bd =3)
# entry_FW2.place(x=100, y=40)
#
# =============================================================================
button_FITS_Load = tk.Button(labelframe_FITSmanager, text="FITS Load", bd=3,
command=self.load_last_file)
button_FITS_Load.place(x=0,y=25)
self.stop_it = 0
button_FITS_start = tk.Button(labelframe_FITSmanager, text="FITS start", bd=3,
command=self.check_for_file_existence)#start_the_loop)
button_FITS_start.place(x=0,y=50)
# =============================================================================
button_Astrometry = tk.Button(labelframe_FITSmanager, text="Astrometry", bd=3,
# command=Astrometry)
command=self.load_Astrometry)
button_Astrometry.place(x=0,y=110)
#
# =============================================================================
button_run_Sextractor = tk.Button(labelframe_FITSmanager, text="run DaoFind", bd=3,
command=self.run_DaoFind)
button_run_Sextractor.place(x=0,y=80)
label_sigma = tk.Label(labelframe_FITSmanager, text="sigma")
label_sigma.place(x=120,y=82)
self.sigma=tk.StringVar()
entry_sigma = tk.Entry(labelframe_FITSmanager, width=3, bd =3, textvariable=self.sigma)
entry_sigma.place(x=160, y=80)
self.sigma.set('25')
#
# =============================================================================
button_show_slits = tk.Button(labelframe_FITSmanager, text="Show slits", bd=3,
# command=Astrometry)
command=self.show_slits)
button_show_slits.place(x=0,y=140)
# =============================================================================
#
# GINGA DISPLAY
#
# =============================================================================
vbox = tk.Frame(root, relief=tk.RAISED, borderwidth=1)
# vbox.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
vbox.pack(side=tk.TOP)
vbox.place(x=350, y=0, anchor="nw")#, width=500, height=800)
#self.vb = vbox
# canvas = tk.Canvas(vbox, bg="grey", height=514, width=522)
canvas = tk.Canvas(vbox, bg="grey", height=516, width=528)
canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
fi = CanvasView(logger) #=> ImageViewTk -- a backend for Ginga using a Tk canvas widget
fi.set_widget(canvas) #=> Call this method with the Tkinter canvas that will be used for the display.
#fi.set_redraw_lag(0.0)
fi.enable_autocuts('on')
fi.set_autocut_params('zscale')
fi.enable_autozoom('on')
#fi.enable_draw(False)
# tk seems to not take focus with a click
fi.set_enter_focus(True)
fi.set_callback('cursor-changed', self.cursor_cb)
fi.set_bg(0.2, 0.2, 0.2)
fi.ui_set_active(True)
fi.show_pan_mark(True)
# add little mode indicator that shows keyboard modal states
fi.show_mode_indicator(True, corner = 'ur')
self.fitsimage = fi
bd = fi.get_bindings()
bd.enable_all(True)
# canvas that we will draw on
# DrawingCanvas = fi.getDrawClasses('drawingcanvas')
canvas = self.canvas_types.DrawingCanvas()
canvas.enable_draw(True)
canvas.enable_edit(True)
canvas.set_drawtype('point', color='red')
canvas.register_for_cursor_drawing(fi)
canvas.add_callback('draw-event', self.draw_cb)
canvas.set_draw_mode('draw')
# without this call, you can only draw with the right mouse button
# using the default user interface bindings
#canvas.register_for_cursor_drawing(fi)
canvas.set_surface(fi)
canvas.ui_set_active(True)
self.canvas = canvas
# # add canvas to viewers default canvas
fi.get_canvas().add(canvas)
self.drawtypes = canvas.get_drawtypes()
self.drawtypes.sort()
# fi.configure(516, 528) #height, width
fi.set_window_size(514,522)
hbox = tk.Frame(root)
hbox.pack(side=tk.BOTTOM, fill=tk.X, expand=0)
self.readout = tk.Label(root, text='')
self.readout.pack(side=tk.BOTTOM, fill=tk.X, expand=0)
self.drawtypes = canvas.get_drawtypes()
## wdrawtype = ttk.Combobox(root, values=self.drawtypes,
## command=self.set_drawparams)
## index = self.drawtypes.index('ruler')
## wdrawtype.current(index)
wdrawtype = tk.Entry(hbox, width=12)
wdrawtype.insert(0, 'point')
wdrawtype.bind("<Return>", self.set_drawparams)
self.wdrawtype = wdrawtype
self.vslit = tk.IntVar()
wslit = tk.Checkbutton(hbox, text="Slit", variable=self.vslit)
self.wslit = wslit
wdrawcolor = ttk.Combobox(hbox, values=self.drawcolors)#,
# command=self.set_drawparams)
index = self.drawcolors.index('lightblue')
wdrawcolor.current(index)
wdrawcolor.bind("<<ComboboxSelected>>", self.set_drawparams)
#wdrawcolor = tk.Entry(hbox, width=12)
#wdrawcolor.insert(0, 'blue')
#wdrawcolor.bind("<Return>", self.set_drawparams)
self.wdrawcolor = wdrawcolor
self.vfill = tk.IntVar()
wfill = tk.Checkbutton(hbox, text="Fill", variable=self.vfill)
self.wfill = wfill
walpha = tk.Entry(hbox, width=12)
walpha.insert(0, '1.0')
walpha.bind("<Return>", self.set_drawparams)
self.walpha = walpha
wrun = tk.Button(hbox, text="Run code",
command=self.run_code)
wclear = tk.Button(hbox, text="Clear Canvas",
command=self.clear_canvas)
wsave = tk.Button(hbox, text="Save Canvas",
command=self.save_canvas)
wopen = tk.Button(hbox, text="Open File",
command=self.open_file)
# pressing quit button freezes application and forces kernel restart.
wquit = tk.Button(hbox, text="Quit",
command=lambda: self.quit(root))
for w in (wquit, wsave, wclear, wrun, walpha, tk.Label(hbox, text='Alpha:'),
wfill, wdrawcolor, wslit, wdrawtype, wopen):
w.pack(side=tk.RIGHT)
mode = self.canvas.get_draw_mode() #initially set to draw by line >canvas.set_draw_mode('draw')
hbox1 = tk.Frame(hbox)
hbox1.pack(side=tk.BOTTOM, fill=tk.X, expand=0)
self.setChecked = tk.StringVar(None,"draw")
btn1 = tk.Radiobutton(hbox1,text="Draw",padx=20,variable=self.setChecked,value="draw", command=self.set_mode_cb).pack(anchor=tk.SW)
btn2 = tk.Radiobutton(hbox1,text="Edit",padx=20,variable=self.setChecked,value="edit", command=self.set_mode_cb).pack(anchor=tk.SW)
btn3 = tk.Radiobutton(hbox1,text="Pick",padx=20,variable=self.setChecked,value="pick", command=self.set_mode_cb).pack(anchor=tk.SW)
# =============================================================================
#
# # DMD Handler Label Frame
#
# =============================================================================
self.frame0r = tk.Frame(root,background="cyan")#, width=400, height=800)
self.frame0r.place(x=900, y=10, anchor="nw", width=360, height=500)
labelframe_DMD = tk.LabelFrame(self.frame0r, text="DMD", font=("Arial", 24))
labelframe_DMD.pack(fill="both", expand="yes")
#1) Set the x size of the default slit
#2) Set the y size of the default slit
#3) save slit pattern to file
#4) save and push slit pattern
#5) load slit pattern
#6) shift slit pattern
#7) analyze point source
#8) remove slit
# =============================================================================
#3) write slit pattern
# =============================================================================
regfname_entry = tk.Entry(labelframe_DMD)
regfname_entry.place(x=0,y=25, width=150)
regfname_entry.config(fg='grey',bg='white') # default text is greyed out
regfname_entry.insert(tk.END,"enter pattern name")
regfname_entry.bind("<FocusIn>", self.regfname_handle_focus_in)
#regfname_entry.bind("<FocusOut>", self.regfname_handle_focus_out)
self.regfname_entry = regfname_entry
# click in entry box deletes default text and allows entry of new text
button_write_slits = tk.Button(labelframe_DMD, text="SAVE: Slits -> .reg file", bd=3, command=self.write_slits)
button_write_slits.place(x=155,y=25)
button_read_slits = tk.Button(labelframe_DMD, text="LOAD: .reg file -> Slits", bd=3, command=self.read_slits)
button_read_slits.place(x=155,y=50)
button_push_slits = tk.Button(labelframe_DMD, text="Slits -> DMD", bd=3, font=("Arial", 24), relief=tk.RAISED, command=self.push_slits)
button_push_slits.place(x=80,y=85)
####
# LOAD BUTTONS
###
button_load_map = tk.Button(labelframe_DMD,
text = "Load DMD Map",
command = self.LoadMap)
button_load_map.place(x=4,y=162)
label_filename = tk.Label(labelframe_DMD, text="Current DMD Map")
label_filename.place(x=4,y=190)
self.str_filename = tk.StringVar()
self.textbox_filename = tk.Text(labelframe_DMD, height = 1, width = 22)
self.textbox_filename.place(x=120,y=190)
button_load_slits = tk.Button(labelframe_DMD,
text = "Load Slit Grid",
command = self.LoadSlits)
button_load_slits.place(x=4,y=222)
label_filename_slits = tk.Label(labelframe_DMD, text="Current Slit Grid")
label_filename_slits.place(x=4,y=250)
self.str_filename_slits = tk.StringVar()
self.textbox_filename_slits = tk.Text(labelframe_DMD, height = 1, width = 22)
self.textbox_filename_slits.place(x=120,y=250)
def regfname_handle_focus_out(self,_):
current_text = self.regfname_entry.get()
if current_text.strip(" ") == "":
#self.regfname_entry.delete(0, tk.END)
self.regfname_entry.config(fg='grey')
self.regfname_entry.config(bg='white')
self.regfname_entry.insert(0, "enter pattern name")
def regfname_handle_focus_in(self,_):
current_text = self.regfname_entry.get()
if current_text == "enter pattern name":
self.regfname_entry.delete(0, tk.END)
self.regfname_entry.config(fg="black")
def write_slits(self):
# when writing a new DMD pattern, put it in the designated directory
# don't want to clutter working dir.
# At SOAR, this should be cleared out every night for the next observer
created_patterns_path = path / Path("Astropy Regions/")
pattern_name = self.regfname_entry.get()
#check if pattern name has been proposed
if (pattern_name.strip(" ") == "") or (pattern_name == "enter pattern name"):
# if there is no pattern name provided, use a default based on
# number of patterns already present
num_patterns_thus_far = len(os.listdir(created_patterns_path))
pattern_name = "pattern_reg{}.reg".format(num_patterns_thus_far)
pattern_path = created_patterns_path / Path(pattern_name)
#create astropy regions and save them after checking that there is something to save...
slits = CM.CompoundMixin.get_objects_by_kind(self.canvas,'rectangle')
list_slits = list(slits)
if len(list_slits) != 0:
RRR=Regions([g2r(list_slits[0])])
for i in range(1,len(list_slits)):
RRR.append(g2r(list_slits[i]))
RRR.write(str(pattern_path)+'.reg', overwrite=True)
print("\nSlits written to region file\n")
def read_slits(self):
reg = askopenfilename(filetypes=[("region files", "*.reg")],initialdir=local_dir+'/Astropy Regions')
print("trying to read region file")
if isinstance(reg, tuple):
regfileName = reg[0]
else:
regfileName = str(reg)
if len(regfileName) != 0:
self.display_region_file(regfileName)
pass
def display_region_file(self, regfileName):
regfile = open(regfileName, "r")
loaded_regions = Regions.read(regfileName, format='ds9')
[ap_region.add_region(self.canvas, reg) for reg in loaded_regions]
pass
def push_slits(self):
# push selected slits to DMD pattern
#Export all Ginga objects to Astropy region
#1. list of ginga objects
objects = CM.CompoundMixin.get_objects(self.canvas)
counter = 0
slit_shape = np.ones((1080,2048)) # This is the size of the DC2K
for obj in objects:
ccd_x0,ccd_y0,ccd_x1,ccd_y1 = obj.get_llur()
x1,y1 = convert.CCD2DMD(ccd_x0,ccd_y0)
x1,y1 = int(np.floor(x1)), int(np.floor(y1))
x2,y2 = convert.CCD2DMD(ccd_x1,ccd_y1)
x2,y2 = int(np.ceil(x2)), int(np.ceil(y2))
#dmd_corners[:][1] = corners[:][1]+500
####
#x1 = round(dmd_corners[0][0])
#y1 = round(dmd_corners[0][1])+400
#x2 = round(dmd_corners[2][0])
#y2 = round(dmd_corners[2][1])+400
#3 load the slit pattern
slit_shape[x1:x2,y1:y2]=0
DMD.initialize()
DMD._open()
DMD.apply_shape(slit_shape)
#DMD.apply_invert()
print("check")
pass
# IPs = Config.load_IP_user(self)
#print(IPs)
# =============================================================================
# def open_Astrometry(self):
# btn = tk.Button(master,
# text ="Click to open a new window",
# command = openNewWindow)
# btn.pack(pady = 10)ƒ
# return self.Astrometry(master)
# =============================================================================
def set_filter(self):
print(self.FW1_filter.get())
print('moving to filter:',self.FW1_filter.get())
# self.Current_Filter.set(self.FW1_filter.get())
filter = self.FW1_filter.get()
print(filter)
t = Motors.move_filter_wheel(filter)
#self.Echo_String.set(t)
print(t)
self.Label_Current_Filter.delete("1.0","end")
self.Label_Current_Filter.insert(tk.END,self.FW1_filter.get())
self.extra_header_params+=1
entry_string = param_entry_format.format(self.extra_header_params,'String','FILTER',
filter,'Selected filter')
self.header_entry_string+=entry_string
def set_grating(self):
print(self.Grating_names,self.Grating_Optioned.get())
i_selected = self.Grating_names.index(self.Grating_Optioned.get())
print(i_selected)
# Grating_Position_Optioned
GR_pos = self.Grating_positions[i_selected]
print(GR_pos)
# print('moving to grating',Grating_Position_Optioned)
# self.Current_Filter.set(self.FW1_filter.get())
# grating = str(Grating_Position_Optioned)
# print(grating)
# t = Motors.move_grism_rails(grating)
# GR_pos = self.selected_GR_pos.get()
# print(type(GR_pos),type(str(GR_pos)),type("GR_B1"))
t = Motors.move_grism_rails(GR_pos)
# self.Echo_String.set(t)
print(t)
#self.Label_Current_Filter.insert(tk.END,"",#self.FW1_Filter)
self.Label_Current_Grating.delete("1.0","end")
self.Label_Current_Grating.insert(tk.END,self.Grating_Optioned.get())
self.extra_header_params+=1
entry_string = param_entry_format.format(self.extra_header_params,'String','GRISM',
i_selected,'Grism position')
self.header_entry_string+=entry_string
print(entry_string)
def get_widget(self):
return self.root
def set_drawparams(self, evt):
kind = self.wdrawtype.get()
color = self.wdrawcolor.get()
alpha = float(self.walpha.get())
fill = self.vfill.get() != 0
params = {'color': color,
'alpha': alpha,
#'cap': 'ball',
}
if kind in ('circle', 'rectangle', 'polygon', 'triangle',
'righttriangle', 'ellipse', 'square', 'box'):
params['fill'] = fill
params['fillalpha'] = alpha
self.canvas.set_drawtype(kind, **params)
def save_canvas(self):
regs = ap_region.export_regions_canvas(self.canvas, logger=self.logger)
#self.canvas.save_all_objects()
def clear_canvas(self):
# CM.CompoundMixin.delete_all_objects(self.canvas)#,redraw=True)
self.canvas.delete_all_objects()
#ConvertSIlly courtesy of C. Loomis
def convertSIlly(self,fname, outname=None):
FITSblock = 2880
# If no output file given, just prepend "fixed"
if outname is None:
fname = pathlib.Path(fname)
dd = fname.parent
outname = pathlib.Path(fname.parent, 'fixed'+fname.name)
with open(fname, "rb") as in_f:
buf = in_f.read()
# Two fixes:
# Header cards:
buf = buf.replace(b'SIMPLE = F', b'SIMPLE = T')
buf = buf.replace(b'BITPIX = -16', b'BITPIX = 16')
buf = buf.replace(b"INSTRUME= Spectral Instruments, Inc. 850-406 camera ", b"INSTRUME= 'Spectral Instruments, Inc. 850-406 camera'")
# Pad to full FITS block:
blocks = len(buf) / FITSblock
pad = round((math.ceil(blocks) - blocks) * FITSblock)
buf = buf + (b'\0' * pad)
with open(outname, "wb+") as out_f:
out_f.write(buf)
# =============================================================================
# # Expose_light
#