summaryrefslogtreecommitdiffstats
path: root/ds9/library/crop.tcl
blob: ddf8a6f9863d957f11896756e748c104f0b63744 (plain)
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
#  Copyright (C) 1999-2017
#  Smithsonian Astrophysical Observatory, Cambridge, MA, USA
#  For conditions of distribution and use, see copyright notice in "copyright"

package provide DS9 1.0

proc CropDef {} {
    global crop
    global icrop

    set icrop(top) .cr
    set icrop(mb) .crmb

    set crop(lock) none

    set crop(system) wcs
    set crop(sky) fk5
    set crop(skyformat) degrees
    set crop(dcoord) wcs
    set crop(dformat) degrees
    set crop(rcoord) wcs
}

proc CropReset {} {
    global current

    if {$current(frame) != {}} {
	$current(frame) crop
	UpdateCrop $current(frame)
    }
}

proc CropButton {which x y} {
    global rgb
    RGBEvalLock rgb(lock,crop) $which [list $which crop begin $x $y]
}

proc CropMotion {which x y} {
    $which crop motion $x $y
}

proc CropRelease {which x y} {
    global rgb

    RGBEvalLock rgb(lock,crop) $which [list $which crop end $x $y]
    UpdateCrop $which
}

proc Crop3dButton {which x y zz} {
    $which crop 3d begin $x $y $zz
}

proc Crop3dMotion {which x y zz} {
    $which crop 3d motion $x $y $zz
}

proc Crop3dRelease {which x y zz} {
    $which crop 3d end $x $y $zz
    UpdateCrop $which
}

proc UpdateCrop {which} {
    global current

    global debug
    if {$debug(tcl,update)} {
	puts stderr "UpdateCrop"
    }

    LockCrop $which
    UpdateCropDialog
    UpdateCubeDialog
    UpdateContourScale
    UpdateContourDialog
    UpdateScaleDialog
    GridUpdateZoom
    UpdateGraphAxis $which
    UpdateInfoBoxBase
    UpdateMain
}

proc CropDialog {} {
    global crop
    global icrop
    global dcrop
    global ds9
    global current

    # see if we already have a window visible
    if {[winfo exists $icrop(top)]} {
	raise $icrop(top)
	return
    }

    # create the window
    set w $icrop(top)
    set mb $icrop(mb)

    Toplevel $w $mb 6 [msgcat::mc {Crop Parameters}] \
	CropDestroyDialog

    # for CoordMenuButton
    set crop(frame) $current(frame)

    $mb add cascade -label [msgcat::mc {File}] -menu $mb.file
    $mb add cascade -label [msgcat::mc {Edit}] -menu $mb.edit

    menu $mb.file
    $mb.file add command -label [msgcat::mc {Apply}] -command CropApplyDialog
    $mb.file add separator
    $mb.file add command -label [msgcat::mc {Reset}] -command CropReset
    $mb.file add separator
    $mb.file add command -label [msgcat::mc {Close}] -command CropDestroyDialog

    EditMenu $mb icrop

    # Param
    set f [ttk::frame $w.param]

    ttk::label $f.title -text [msgcat::mc {Center}]
    ttk::entry $f.x -textvariable dcrop(x) -width 14
    ttk::entry $f.y -textvariable dcrop(y) -width 14
    set dcrop(cb) $f.center 
    CoordMenuButton $dcrop(cb) crop system 1 sky skyformat UpdateCropDialog

    ttk::label $f.stitle -text [msgcat::mc {Size}]
    ttk::entry $f.w -textvariable dcrop(w) -width 14
    ttk::entry $f.h -textvariable dcrop(h) -width 14
    set dcrop(db) $f.size 
    DistMenuButton $dcrop(db) crop dcoord 1 dformat UpdateCropDialog

    ttk::label $f.rtitle -text [msgcat::mc {3D}]
    ttk::entry $f.from -textvariable dcrop(zmin) -width 14
    ttk::entry $f.to -textvariable dcrop(zmax) -width 14
    set dcrop(rb) $f.range
    CoordMenuButton $dcrop(rb) crop rcoord 2 {} {} UpdateCropDialog

    grid $f.title $f.x $f.y $dcrop(cb) -padx 2 -pady 2
    grid $f.stitle $f.w $f.h $dcrop(db) -padx 2 -pady 2

    # Buttons
    set f [ttk::frame $w.buttons]
    ttk::button $f.apply -text [msgcat::mc {Apply}] -command CropApplyDialog
    ttk::button $f.reset -text [msgcat::mc {Reset}] -command CropReset
    ttk::button $f.close -text [msgcat::mc {Close}] \
	-command CropDestroyDialog
    pack $f.apply $f.reset $f.close -side left -expand true -padx 2 -pady 4

    # Fini
    ttk::separator $w.sep -orient horizontal
    pack $w.buttons $w.sep -side bottom -fill x
    pack $w.param -side top -fill both -expand true

    $w.param.x select range 0 end

    UpdateCropDialog
}

proc CropApplyDialog {} {
    global crop
    global icrop
    global dcrop
    global current

    if {$current(frame) != {}} {
	$current(frame) crop center $dcrop(x) $dcrop(y) \
	    $crop(system) $crop(sky) \
	    $dcrop(w) $dcrop(h) $crop(dcoord) $crop(dformat)

	if {[$current(frame) has fits cube]} {
	    $current(frame) crop 3d $dcrop(zmin) $dcrop(zmax) $crop(rcoord)
	}
	UpdateCrop $current(frame)
    }
}

proc CropDestroyDialog {} {
    global icrop
    global dcrop

    if {[winfo exists $icrop(top)]} {
	destroy $icrop(top)
	destroy $icrop(mb)
    }

    unset dcrop
}

proc UpdateCropMenu {} {
    # can be changed by wcs
    SetCoordSystem crop system sky skyformat
}

proc UpdateCropDialog {} {
    global crop
    global icrop
    global dcrop
    global current

    global debug
    if {$debug(tcl,update)} {
	puts stderr "UpdateCropDialog"
    }

    if {![winfo exists $icrop(top)]} {
	return
    }

    set w $icrop(top)

    if {$current(frame) != {}} {
	set crop(frame) $current(frame)

	if {[$current(frame) has fits]} {
	    # now make sure we have the coord systems
	    AdjustCoordSystem crop system
	    CoordMenuEnable $dcrop(cb).menu crop system 1 sky skyformat
	    CoordMenuButtonCmd crop system sky {}

	    AdjustCoordSystem crop dcoord
	    DistMenuEnable $dcrop(db).menu crop dcoord 1 dformat
	    DistMenuButtonCmd crop dcoord dformat {}

	    AdjustCoordSystem3d crop rcoord
	    CoordMenuEnable $dcrop(rb).menu crop rcoord 2 {} {}
	    CoordMenuButtonCmd crop rcoord {} {}

	    set rr [$current(frame) get crop center \
			$crop(system) $crop(sky) $crop(skyformat) \
			$crop(dcoord) $crop(dformat)]
	    set dcrop(x) [lindex $rr 0]
	    set dcrop(y) [lindex $rr 1]
	    set dcrop(w) [lindex $rr 2]
	    set dcrop(h) [lindex $rr 3]

	    if {[$current(frame) has fits cube]} {
		set ss [$current(frame) get crop 3d $crop(rcoord)]
		set dcrop(zmin) [lindex $ss 0]
		set dcrop(zmax) [lindex $ss 1]

		grid $w.param.rtitle $w.param.from $w.param.to $dcrop(rb) \
		    -padx 2 -pady 2
	    } else {
		set dcrop(zmin) {}
		set dcrop(zmax) {}

		grid forget $w.param.rtitle $w.param.from $w.param.to $dcrop(rb)
	    }

	    return
	}
    }

    grid forget $w.param.rtitle $w.param.from $w.param.to $dcrop(rb)

    CoordMenuReset $dcrop(cb).menu crop system 1 sky skyformat
    DistMenuReset $dcrop(db).menu crop dcoord 1 dformat
    CoordMenuReset $dcrop(rb).menu crop rcoord 2 {} {}

    set dcrop(x) {}
    set dcrop(y) {}
    set dcrop(w) {}
    set dcrop(h) {}
    set dcrop(zmin) {}
    set dcrop(zmax) {}
}

proc MatchCropCurrent {sys} {
    global current

    if {$current(frame) != {}} {
	MatchCrop $current(frame) $sys
    }
}

proc MatchCrop {which sys} {
    global ds9
    global rgb

    # make sure matrices have been updated
    RealizeDS9

    set tt [$which has crop]
    set datasec [$which get datasec]

    if {$tt} {
	switch -- $sys {
	    image -
	    physical -
	    amplifier -
	    detector {
		set rr [$which get crop center $sys fk5 degrees $sys degrees]
		set r(x) [lindex $rr 0]
		set r(y) [lindex $rr 1]
		set r(w) [lindex $rr 2]
		set r(h) [lindex $rr 3]
		set qq [$which get crop 3d image]

		foreach ff $ds9(frames) {
		    if {$ff != $which} {
			RGBEvalLock rgb(lock,crop) $ff [list $ff datasec $datasec]
			RGBEvalLock rgb(lock,crop) $ff [list $ff crop center $r(x) $r(y) $sys fk5 $r(w) $r(h) $sys degrees]
			RGBEvalLock rgb(lock,crop) $ff [list $ff crop 3d $qq image]
		    }
		}
	    }
	    wcs {
		set ss [lindex [$which get wcs] 0]
		if {[$which has wcs $ss]} {
		    set rr [$which get crop center $ss fk5 degrees $ss degrees]
		    set r(x) [lindex $rr 0]
		    set r(y) [lindex $rr 1]
		    set r(w) [lindex $rr 2]
		    set r(h) [lindex $rr 3]
		    set qq [$which get crop 3d $ss]

		    foreach ff $ds9(frames) {
			if {$ff != $which} {
			    if {[$ff has wcs $ss]} {
				RGBEvalLock rgb(lock,crop) $ff [list $ff crop center $r(x) $r(y) $ss fk5 $r(w) $r(h) $ss degrees]
				RGBEvalLock rgb(lock,crop) $ff [list $ff crop 3d $qq $ss]
			    }
			}
		    }
		}
	    }
	}
    } else {
	foreach ff $ds9(frames) {
	    if {$ff != $which} {
		RGBEvalLock rgb(lock,crop) $ff [list $ff crop]
		RGBEvalLock rgb(lock,crop) $ff [list $ff crop 3d]
	    }
	}
    }
}

proc LockCropCurrent {} {
    global current
    
    if {$current(frame) != {}} {
	LockCrop $current(frame)
    }
}

proc LockCrop {which} {
    global crop

    switch -- $crop(lock) {
	none {}
	default {MatchCrop $which $crop(lock)}
    }
}

proc CropBackup {ch which} {
    switch [$which get type] {
	base -
	3d {CropBackupBase $ch $which}
	rgb {CropBackupRGB $ch $which}
    }
}

proc CropBackupBase {ch which} {
    if {[$which has crop]} {
	if {[$which has fits]} {
	    set rr [$which get crop physical fk5 degrees] 
	    puts $ch "$which crop $rr physical fk5"

	    if {[$which has fits cube]} {
		set ss [$which get crop 3d image]
		puts $ch "$which crop 3d $ss image"
	    }
	}
    }
}

proc CropBackupRGB {ch which} {
    set sav [$which get rgb channel]
    foreach cc {red green blue} {
	$which rgb channel $cc
	puts $ch "$which rgb channel $cc"
	CropBackupBase $ch $which
    }
    $which rgb channel $sav
    puts $ch "$which rgb channel $sav"
}

# Process Cmds

proc ProcessCropCmd {varname iname} {
    upvar $varname var
    upvar $iname i

    # we need to be realized
    ProcessRealizeDS9

    global crop
    global current
    switch -- [string tolower [lindex $var $i]] {
	match {
	    incr i
	    MatchCropCurrent [lindex $var $i]
	}
	lock {
	    incr i
	    set crop(lock) [lindex $var $i]
	    LockCropCurrent
	}
	open {CropDialog}
	close {CropDestroyDialog}
	reset {CropReset}
	3d {
	    incr i 1
	    set zmin [lindex $var [expr $i+0]]
	    set zmax [lindex $var [expr $i+1]]
	    set sys [lindex $var [expr $i+2]]

	    incr i 1
	    incr i [FixSpecSystem sys physical]

	    $current(frame) crop 3d $zmin $zmax $sys
	}
	default {
	    set x [lindex $var [expr $i+0]]
	    set y [lindex $var [expr $i+1]]
	    set w [lindex $var [expr $i+2]]
	    set h [lindex $var [expr $i+3]]
	    set sys [lindex $var [expr $i+4]]
	    set sky [lindex $var [expr $i+5]]
	    set dformat [lindex $var [expr $i+6]]

	    incr i 3
	    incr i [FixSpec sys sky dformat physical fk5 degrees]

	    $current(frame) crop center $x $y $sys $sky $w $h $sys $dformat
	}
    }
}

proc ProcessSendCropCmd {proc id param} {
    global crop
    global current

    switch -- [string tolower [lindex $param 0]] {
	lock {$proc $id "$crop(lock)\n"}
	3d {
	    set sys [lindex $param 1]
	    FixSpecSystem sys physical

	    if {$current(frame) != {}} {
		$proc $id "[$current(frame) get crop 3d $sys]\n"
	    }
	}
	default {
	    set sys [lindex $param 0]
	    set sky [lindex $param 1]
	    set format [lindex $param 2]
	    set dformat [lindex $param 3]
	    FixSpec sys sky format physical fk5 degrees
	    
	    if {$current(frame) != {}} {
		$proc $id "[$current(frame) get crop center $sys $sky $format $sys $dformat]\n"
	    }
	}
    }
}