-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_correct_gui
More file actions
executable file
·659 lines (592 loc) · 15.6 KB
/
auto_correct_gui
File metadata and controls
executable file
·659 lines (592 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/ruby
require 'thread'
require 'timeout'
require 'socket'
require 'resolv'
require 'libglade2'
Thread.abort_on_exception = true
### define the main GUI window
class MainWindow
# SRV routine
def srv_lookup(name)
srv = "_#{name.downcase.gsub(/_/,"-")}._tcp.mmto.arizona.edu"
dns = Resolv::DNS.open
begin
resource = dns.getresource(srv, Resolv::DNS::Resource::IN::SRV)
rescue Resolv::ResolvError
return nil
end
port = resource.port
host = resource.target.to_s
return host, port
end
# end of SRV routine
# MSG routines from msg.rb
def sockopen(host, port)
socket = nil
status = nil
timeout = 5.0
begin
timeout(5) {
socket = TCPSocket.open(host, port)
}
rescue Timeout::Error
status = "Timeout"
return nil
rescue Errno::ECONNREFUSED
status = "Refusing connection"
return nil
rescue => why
status = "Error: #{why}"
return nil
end
return socket
end
def msg_get(socket, par)
return nil unless socket
begin
socket.send("1 get #{par}\n", 0)
result = socket.gets
rescue => why
status = "Error: #{why}"
return status
end
if (result =~ /ack/)
answer = result.split('ack')[1].chomp
else
answer = nil
end
return answer
end
def msg_cmd(socket, command, value)
return nil unless socket
begin
if value
socket.send("1 #{command} #{value}\n", 0)
else
socket.send("1 #{command}\n", 0)
end
answer = socket.gets
rescue => why
status = "Error: #{why}"
return status
end
if (answer =~ /ack/)
return true
else
return false
end
end
def msg_set(socket, par, value)
return nil unless socket
begin
socket.send("1 set #{par} #{value}\n", 0)
answer = socket.gets
rescue => why
status = "Error: #{why}"
return status
end
if (answer =~ /ack/)
return true
else
return false
end
end
# end of MSG routines
def initialize(mode, inst)
@mode = mode
@inst = inst
@hexhost, @hexport = srv_lookup('hexapod')
@mounthost, @mountport = srv_lookup('mount-mini')
@cellhost, @cellport = srv_lookup('cell-mini')
@whost, @wport = srv_lookup('wfs')
@hhost, @hport = srv_lookup('hexapod-msg')
@hostname = "hacksaw"
if ENV['WFSROOT']
@path = ENV['WFSROOT']
@dir = "#{@path}/datadir"
@netdir = "/net/#{@hostname}#{@path}/datadir"
else
@path = "/mmt/shwfs"
@dir = "/mmt/shwfs/datadir"
@netdir = "/net/#{@hostname}/home/mmt/shwfs/datadir"
end
@glade = GladeXML.new("#{@path}/glade/auto_correct.glade") {|handler| method(handler)}
@mainwindow = @glade.get_widget("MainWindow")
@mainwindow.set_title("AutoWFS GUI")
@status = @glade.get_widget("StatusBar")
@menubar = @glade.get_widget("menubar1")
@list = ""
# load reference X and Y values for pupil
@xref = 256.0
@yref = 256.0
@rotoff = Hash.new
@rotoff['F9'] = -225.0
@rotoff['F5'] = 234.0
@hexscale = Hash.new
#@hexscale['F9'] = 6.0*0.12
#@hexscale['F5'] = 0.135/0.297
@hexscale['F9'] = 0.12
@hexscale['F5'] = 0.135
@xparity = Hash.new
@yparity = Hash.new
@xparity['F5'] = -1.0
@yparity['F5'] = 1.0
@xparity['F9'] = 1.0
@yparity['F9'] = -1.0
@centol = 25.0
@abort = false
# exposure configuration
@abort_but = @glade.get_widget("Abort")
@abort_but.set_sensitive(false)
@correct = @glade.get_widget("Correct")
@niter = @glade.get_widget("NIter")
@exptime = @glade.get_widget("ExpTime")
@fileentry = @glade.get_widget("FileEntry")
@fileentry.set_text("auto_wfs")
@tweak = @glade.get_widget("Tweak")
@tweakm2 = @glade.get_widget("TweakM2")
@megacam = @glade.get_widget("MegaCam")
@megacam.child.set_text("Offset Hexapod to #{inst} Center")
@wfsock = nil
end
def wfsopen
unless @wfsock
# report("Before srv_lookup")
# whost, wport = srv_lookup('wfs')
# whost = "localhost"
# wport = 6868
report("Opening connection to #{@whost} on port #{@wport} (wfs via msg)")
@wfsock = sockopen(@whost, @wport)
# report("After srv_lookup")
end
end
def wfsclose
if @wfsock
report("Connection to port #{@wport} (wfs via msg) closed")
@wfsock.close
@wfsock = nil
end
end
def on_Abort_clicked
@abort = true
end
# check to see if there's a pupil in the image and, if so, where it is
def check_pupil(fitsfile)
# report("Before Center")
# center = `#{@path}/get_pupil_center #{fitsfile} #{@mode} 2> /dev/null`
f = IO.popen("#{@path}/get_pupil_center #{fitsfile} #{@mode}")
center = f.gets
# report("After Center")
if center =~ /No pupil/
return false
else
xcen, ycen = center.split()
xcen = xcen.to_f
ycen = ycen.to_f
return [xcen, ycen]
end
end
# given current pupil center, calc hexapod move to center it on WFS
def center_pupil(x, y)
# check if we're close enough
xdist = x - @xref
ydist = y - @yref
xdist = xdist*@xparity[@mode]
ydist = ydist*@yparity[@mode]
dist = Math::sqrt(xdist*xdist + ydist*ydist)
if dist < @centol
return true
else
# get instrument rotator angle
# rotangle = `echo "get rot" | nc #{@mounthost} #{@mountport}`.to_f
f = IO.popen("echo 'get rot' | nc #{@mounthost} #{@mountport}")
rotangle = f.gets.chomp.to_f
angle = Math::PI*(rotangle + @rotoff[@mode])/180.0
daz = @hexscale[@mode]*(xdist*Math::cos(angle) + ydist*Math::sin(angle))
del = @hexscale[@mode]*(-1.0*xdist*Math::sin(angle) +
ydist*Math::cos(angle))
report("Moving hexapod to center pupil....")
puts "X = #{x}, Y = #{y}"
puts "Xcen = #{@xref}, Ycen = #{@yref}"
puts "Del = #{del}, Daz = #{daz}"
# socket = sockopen("hacksaw", 5350)
# msg_cmd(socket, 'tiltxerr_zc', del)
# msg_cmd(socket, 'tiltyerr_zc', daz)
# socket.close
# socket = nil
system("echo \"offset_zc wfs tx #{del}\" | nc -w 5 #{@hexhost} #{@hexport}")
system("echo \"offset_zc wfs ty #{daz}\" | nc -w 5 #{@hexhost} #{@hexport}")
system("echo \"apply_offsets\" | nc -w 5 #{@hexhost} #{@hexport}")
wait4hexapod
wait4hexapod
return false
end
end
# calc hexapod move to center up on megacam
def center_megacam
unless @inst == 'MegaCam' || @inst == 'SWIRC' || @inst == 'Hecto' || @inst == 'Maestro'
return
end
# check if we're close enough
if @inst == 'SWIRC'
# xdist = 256 - 240.7
# ydist = 256 - 240.86
xdist = 256 - 231.5
ydist = 256 - 212.4
else
xdist = 256 - 231.5
ydist = 256 - 212.4
end
if @inst == 'Maestro'
xdist = 0
ydist = 0
end
xdist = xdist*@xparity[@mode]
ydist = ydist*@yparity[@mode]
dist = Math::sqrt(xdist*xdist + ydist*ydist)
# get instrument rotator angle
# rotangle = `echo "get rot" | nc #{@mounthost} #{@mountport}`.to_f
f = IO.popen("echo 'get rot' | nc #{@mounthost} #{@mountport}")
rotangle = f.gets.chomp.to_f
angle = Math::PI*(rotangle + @rotoff[@mode])/180.0
daz = @hexscale[@mode]*(xdist*Math::cos(angle) + ydist*Math::sin(angle))
del = @hexscale[@mode]*(-1.0*xdist*Math::sin(angle) +
ydist*Math::cos(angle))
report("Moving hexapod to center up on #{@inst}....")
#puts "Xcen = #{@xref}, Ycen = #{@yref}"
#puts "Del = #{del}, Daz = #{daz}"
system("echo \"offset_zc wfs tx #{del}\" | nc -w 5 #{@hexhost} #{@hexport}")
system("echo \"offset_zc wfs ty #{daz}\" | nc -w 5 #{@hexhost} #{@hexport}")
if @inst == 'SWIRC'
system("echo \"offset_inc wfs z -30.0\" | nc -w 5 #{@hexhost} #{@hexport}")
end
if @inst == 'Maestro'
#This applied a 800 micron focus shift for MAESTRO that should no longer be needed
#as the instrument was raised. Keeping code here for future use if needed - RJC Sept 2 2014
#system("echo \" offset_inc wfs z -800.0\" | nc -w 5 #{@hexhost} #{@hexport}")
end
system("echo \"apply_offsets\" | nc -w 5 #{@hexhost} #{@hexport}")
wait4hexapod
wait4hexapod
report("Hexapod aligned for #{@inst}.")
return true
end
# kill the main window on a destroy
def on_MainWindow_destroy
wfsclose
Gtk.main_quit
end
# routine to print to statusbar
def report(text)
@status.pop(0)
@status.push(0, text)
end
# menubar callbacks
def on_quit_activate
wfsclose
Gtk.main_quit
end
def hexapod_log
hexlog = "#{@dir}/hexapod.log"
# date = `date -u +'%H_%M_%S'`.chomp
f = IO.popen("date -u +'%H_%M_%S'")
date = f.gets.chomp
celllog = "#{@dir}/#{date}.celllog"
offsetlog = "#{@dir}/#{date}.offsets"
system("touch #{hexlog}")
system("touch #{celllog}")
system("/mmt/shwfs/getm2info >> #{hexlog}")
system("echo \"all\" | nc #{@cellhost} #{@cellport} > #{celllog}")
system("echo \"all\" | nc #{@hexhost} #{@hexport} | grep off_ | grep applied > #{offsetlog}")
end
def disable
@megacam.set_sensitive(false)
@correct.set_sensitive(false)
@tweak.set_sensitive(false)
@tweakm2.set_sensitive(false)
@abort_but.set_sensitive(true)
end
def enable
@abort_but.set_sensitive(false)
@correct.set_sensitive(true)
@tweak.set_sensitive(true)
@tweakm2.set_sensitive(true)
@megacam.set_sensitive(true)
end
def on_about1_activate
report("First cut at a remote WFS interface")
end
def m2correct
report("Applying Hexapod Corrections....")
wfsopen
msg_cmd(@wfsock, "recenter", nil)
msg_cmd(@wfsock, "corr_coma", nil)
msg_cmd(@wfsock, "corr_focus", nil)
wfsclose
wait4hexapod
wait4hexapod
report("Corrections Applied.")
hexapod_log
return true
end
def fullcorrect
report("Applying Cell and Hexapod Corrections....")
wfsopen
msg_cmd(@wfsock, "recenter", nil)
msg_cmd(@wfsock, "corr_coma", nil)
msg_cmd(@wfsock, "corr_focus", nil)
msg_cmd(@wfsock, "corr_primary", nil)
wfsclose
wait4hexapod
wait4hexapod
report("Corrections Applied.")
hexapod_log
return true
end
def wait4hexapod
socket = sockopen(@hhost, @hport)
inmotion = 1
sleep(2)
loop do
inmotion = msg_get(socket, 'motionFlag').to_i
break if inmotion == 0
sleep(1)
end
socket.close if socket
socket = nil
sleep(2)
end
def corr_dialog(type)
dialog = Gtk::Dialog.new
dialog.title = "OK to Apply Corrections?"
#dialog.transient_for = @mainwindow
dialog.set_default_size(100, 100)
dialog.vbox.add(Gtk::Label.new("Apply Corrections?"))
dialog.add_button("OK", Gtk::Dialog::RESPONSE_OK)
dialog.add_button(Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL)
dialog.set_default_response(Gtk::Dialog::RESPONSE_CANCEL)
dialog.signal_connect("response") do |widget, response|
case response
when Gtk::Dialog::RESPONSE_OK
if type == "full"
fullcorrect
else
m2correct
end
dialog.destroy
@continue = true
@cancel = false
report("Corrections Applied.")
when Gtk::Dialog::RESPONSE_CANCEL
report("Correction Application Canceled.")
dialog.destroy
@continue = true
@cancel = true
end
end
dialog.show_all
end
# configure and take exposures
def Expose(nexp)
exptime = @exptime.value.to_f
file = @fileentry.text
n = 1
ngood = 0
ntry = 0
until (ngood == nexp) || (ntry == 2) || @abort
# figure out the filename
if file == "test"
fullfilename = "#{@dir}/#{file}.fits"
netfullfilename = "#{@netdir}/#{file}.fits"
filename = "#{file}.fits"
system("rm -f #{fullfilename}")
else
num = 0
fullfilename = sprintf("%s/%s_%04d.fits", @dir, file, num)
netfullfilename = sprintf("%s/%s_%04d.fits", @netdir, file, num)
filename = sprintf("%s_%04d.fits", file, num)
while test(?e, fullfilename)
num = num + 1
fullfilename = sprintf("%s/%s_%04d.fits", @dir, file, num)
netfullfilename = sprintf("%s/%s_%04d.fits", @netdir, file, num)
filename = sprintf("%s_%04d.fits", file, num)
end
end
# get exposure timer going....
if exptime > 1
count = Thread.new {
left = exptime.to_i
left.times {
report("Exposing \##{n} (#{filename})...#{left} sec left.")
sleep(1)
left = left - 1
}
report("Reading out \##{n} (#{filename})....")
}
else
report("Exposing \##{n} (#{filename})....")
end
# perform exposure
begin
timeout(exptime+30) {
if (@mode == "F5")
system("#{@path}/getimage #{exptime} #{fullfilename}")
else
system("sudo -u mmtop /usr/bin/rsh -n -l tim f9wfs \"/home/tim/get_apogee_image.tcl #{exptime} #{filename} \> /dev/null\"")
system("sudo -u mmtop /usr/bin/rcp tim@f9wfs:/home/tim/#{filename} #{fullfilename}")
system("sudo -u mmtop /usr/bin/rsh -n -l tim f9wfs \"rm -f #{filename} \> /dev/null\"")
system("sudo -u mmtop chmod g+w #{fullfilename}")
end
if test(?e, fullfilename)
system("#{@path}/header.pl #{fullfilename} #{exptime} #{@mode}")
else
report("Image acquisition failed. Check WFS Computer.")
@abort = true
return
end
}
rescue Timeout::Error
report("Exposure Timed Out.")
@abort = true
return
rescue => why
report("Exposure Failed: #{why}.")
@abort = true
return
end
system("/usr/bin/xpaset -p WFS zoom to 1.0")
system("/usr/bin/xpaset -p WFS file #{fullfilename}")
# got the image, now check it to see if anything is there
pupil = check_pupil(fullfilename)
if pupil
# got a pupil, do something
x, y = pupil
centered = center_pupil(x, y)
if centered
if @list == ""
@list = "#{fullfilename}"
else
@list = @list + ",#{fullfilename}"
end
ngood = ngood + 1
else
@list = ""
end
else
if ntry == 0
exptime = 2.0*exptime
report("No pupil found. Doubling exposure time.")
else
exptime = exptime/(ntry+1)
report("Reducing exposure time in case of bad seeing.")
end
sleep(1)
ntry = ntry + 1
end
n = n + 1
end
report("Completed Exposures")
end
def on_Correct_clicked
disable
Thread.new {
iter = @niter.value_as_int
iter.times {
Expose(3)
if @abort || @list == ""
@list = ""
report("Analysis Aborted.")
break
else
centroid
@continue = false
@cancel = false
corr_dialog("full")
loop do
break if @continue
sleep(1)
end
@list = ""
break if @cancel
end
}
if @cancel || @abort
@abort = false
@cancel = false
enable
@list = ""
report("Analysis Aborted.")
else
@list = ""
Expose(3)
if @abort || @list == ""
@list = ""
report("Analysis Aborted.")
else
centroid
@list = ""
end
end
enable
}
end
def on_Tweak_clicked
disable
Thread.new {
fullcorrect
Expose(3)
if @abort || @list == ""
@abort = false
enable
@list = ""
report("Analysis Aborted.")
else
centroid
@list = ""
enable
end
}
end
def on_MegaCam_clicked
disable
Thread.new {
center_megacam
enable
}
end
def on_TweakM2_clicked
disable
Thread.new {
m2correct
Expose(1)
if @abort || @list == ""
@abort = false
enable
@list = ""
report("Analysis Aborted.")
else
centroid
@list = ""
enable
end
}
end
def centroid
report("Centroiding....")
wfsopen
report("Before msg_cmd centroid....")
msg_cmd(@wfsock, "centroid", @list)
# report("After msg_cmd centroid....")
wfsclose
report("Centroiding Complete.")
end
end
if !(ARGV[0] == "F5" || ARGV[0] == "F9")
puts "Specify F5 or F9."
else
# get the ball rolling!
Gtk.init
MainWindow.new(ARGV[0], ARGV[1])
Gtk.main
end