/usr/lib/python2.7/dist-packages/ginga/rv/plugins/WCSMatch.py is in python-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# WCSMatch.py -- WCSMatch plugin for Ginga reference viewer
#
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
from ginga import GingaPlugin
from ginga.gw import Widgets
from ginga.util import wcs
from ginga.misc import Bunch
class WCSMatch(GingaPlugin.GlobalPlugin):
"""
*** This plugin is experimental/alpha/testing/preview ***
WCSMatch is a global plugin for the Ginga image viewer that allows
you to roughly align images with different scales and orientations
using WCS for viewing purposes.
To use, simply start the plugin, and from the plugin GUI select a
channel from the drop-down menu labeled "Reference Channel". The
image contained in that channel will be used as a reference for
zooming and orienting the images in the other channels.
The channels will be synchronized in viewing (zoom, pan, rotate,
transform). To "unlock" the synchronization, simply select "None"
from the "Reference Channel" drop-down menu.
Currently there is no way to limit the channels that are affected
by the plugin.
"""
def __init__(self, fv):
# superclass defines some variables for us, like logger
super(WCSMatch, self).__init__(fv)
self.chnames = []
self.ref_channel = None
self.ref_image = None
self.gui_up = False
self._cur_opn_viewer = None
fv.add_callback('add-channel', self.add_channel)
fv.add_callback('delete-channel', self.delete_channel)
def build_gui(self, container):
top = Widgets.VBox()
top.set_border_width(4)
vbox, sw, orientation = Widgets.get_oriented_box(container)
vbox.set_border_width(4)
vbox.set_spacing(2)
self.msg_font = self.fv.get_font("sansFont", 12)
tw = Widgets.TextArea(wrap=True, editable=False)
tw.set_font(self.msg_font)
self.tw = tw
fr = Widgets.Expander("Instructions")
fr.set_widget(tw)
vbox.add_widget(fr, stretch=0)
fr = Widgets.Frame("WCS Match")
captions = ((' Reference Channel:', 'label',
'ref channel', 'combobox'),
)
w, b = Widgets.build_info(captions, orientation=orientation)
self.w = b
b.ref_channel.add_callback('activated', self.set_reference_channel_cb)
fr.set_widget(w)
vbox.add_widget(fr, stretch=0)
spacer = Widgets.Label('')
vbox.add_widget(spacer, stretch=1)
top.add_widget(sw, stretch=1)
btns = Widgets.HBox()
btns.set_spacing(3)
btn = Widgets.Button("Close")
btn.add_callback('activated', lambda w: self.close())
btns.add_widget(btn, stretch=0)
btns.add_widget(Widgets.Label(''), stretch=1)
top.add_widget(btns, stretch=0)
container.add_widget(top, stretch=1)
self.gui_up = True
self._reset_channels_gui()
def add_channel(self, viewer, channel):
chname = channel.name
info = Bunch.Bunch(chinfo=channel)
channel.extdata._wcsmatch_info = info
# Add callbacks to the viewer for all the scale, pan, rotation and
# transform settings changes
chviewer = channel.fitsimage
fitssettings = chviewer.get_settings()
fitssettings.getSetting('scale').add_callback('set',
self.zoomset_cb, chviewer, info)
fitssettings.getSetting('rot_deg').add_callback('set',
self.rotset_cb, chviewer, info)
for name in ('flip_x', 'flip_y', 'swap_xy'):
fitssettings.getSetting(name).add_callback('set',
self.xfmset_cb, chviewer, info)
fitssettings.getSetting('pan').add_callback('set',
self.panset_cb, chviewer, info)
self.fv.gui_do(self._reset_channels_gui)
def delete_channel(self, viewer, channel):
chname = channel.name
self.logger.debug("deleting channel %s" % (chname))
self.active = None
self.info = None
self.fv.gui_do(self._reset_channels_gui)
def _reset_channels_gui(self):
self.chnames = list(self.fv.get_channel_names())
self.chnames.sort()
self.chnames.insert(0, "None")
if not self.gui_up:
return
self.w.ref_channel.clear()
for chname in self.chnames:
self.w.ref_channel.append_text(chname)
# CALLBACKS
def redo(self, channel, image):
info = channel.extdata._wcsmatch_info
self.logger.info("Channel '%s' setting image" % (info.chinfo.name))
if info.chinfo == self.ref_channel:
self.ref_image = image
return True
def set_reference_channel_cb(self, w, idx):
chname = self.chnames[idx]
if chname == 'None':
self.ref_image = None
self.ref_channel = None
chinfo = self.fv.get_channel(chname)
self.ref_channel = chinfo
chviewer = chinfo.fitsimage
self.ref_image = chviewer.get_image()
# reset the scale base to be identical in both axes for the
# reference image
chviewer.set_scale_base_xy(1.0, 1.0)
self.scale_all_relative(chviewer, chinfo)
self.rotate_all_relative(chviewer, chinfo)
self.transform_all_relative(chviewer, chinfo)
self.pan_all_relative(chviewer, chinfo)
self.logger.info("set reference channel to '%s'" % (chname))
def close(self):
self.fv.stop_global_plugin(str(self))
return True
def instructions(self):
self.tw.set_text(WCSMatch.__doc__)
def start(self):
self.instructions()
def stop(self):
self.ref_channel = None
self.ref_image = None
self.fv.showStatus("")
def get_other_channels(self, myname):
return set(self.fv.get_channel_names()) - set([myname])
def zoomset_cb(self, setting, value, chviewer, info):
"""This callback is called when a channel window is zoomed.
"""
# Don't do anything if we are not active
if not self.gui_up or self.ref_image is None:
return
# if this is not a zoom event from the focus window then
# don't do anything
## focus_chviewer = self.fv.getfocus_viewer()
## if chviewer != focus_chviewer:
## return
if self._cur_opn_viewer is not None:
return
self._cur_opn_viewer = chviewer
try:
self.scale_all_relative(chviewer, info.chinfo)
finally:
self._cur_opn_viewer = None
def scale_all_relative(self, chviewer, chinfo):
if self.ref_image is None:
return
# get native scale relative to reference image
image = chviewer.get_image()
ort = wcs.get_relative_orientation(image, self.ref_image)
self.logger.info("scale for channel '%s' relative to ref image %f,%f" % (
chinfo.name, ort.rscale_x, ort.rscale_y))
scale_x, scale_y = chviewer.get_scale_xy()
#scale_x, scale_y = value
chg_x, chg_y = scale_x / ort.rscale_x, scale_y / ort.rscale_y
self.logger.info("scale changed for channel '%s' by %f,%f" % (
chinfo.name, chg_x, chg_y))
# for all other channels except ours
chnames = self.get_other_channels(chinfo.name)
for chname in chnames:
chinfo2 = self.fv.get_channel(chname)
# calculate scale from orientation to reference image
image = chinfo2.fitsimage.get_image()
if image is None:
continue
ort = wcs.get_relative_orientation(image, self.ref_image)
new_scale_x, new_scale_y = (ort.rscale_x * chg_x,
ort.rscale_y * chg_y)
# apply that scale
self.logger.info("changing scale for channel '%s' to %f,%f" % (
chinfo2.name, new_scale_x, new_scale_y))
chinfo2.fitsimage.scale_to(new_scale_x, new_scale_y)
def rotset_cb(self, setting, value, chviewer, info):
"""This callback is called when a channel window is rotated.
"""
# Don't do anything if we are not active
if not self.gui_up or self.ref_image is None:
return
# if this is not a zoom event from the focus window then
# don't do anything
## focus_chviewer = self.fv.getfocus_viewer()
## if chviewer != focus_chviewer:
## return
if self._cur_opn_viewer is not None:
return
self._cur_opn_viewer = chviewer
try:
self.rotate_all_relative(chviewer, info.chinfo)
finally:
self._cur_opn_viewer = None
def rotate_all_relative(self, chviewer, chinfo):
if self.ref_image is None:
return
# get native scale relative to reference image
image = chviewer.get_image()
if self.ref_image is None:
return
ort = wcs.get_relative_orientation(image, self.ref_image)
self.logger.info("rotation for channel '%s' relative to ref image %f" % (
chinfo.name, ort.rrot_deg))
rot_deg = chviewer.get_rotation()
chg_rot_deg = rot_deg + ort.rrot_deg
self.logger.info("rotation changed for channel '%s' by %f" % (
chinfo.name, chg_rot_deg))
# for all other channels except ours
chnames = self.get_other_channels(chinfo.name)
for chname in chnames:
chinfo2 = self.fv.get_channel(chname)
# Get relative rotation of their image
image = chinfo2.fitsimage.get_image()
if image is None:
continue
ort = wcs.get_relative_orientation(image, self.ref_image)
# Apply that rotation
new_rot_deg = ort.rrot_deg + chg_rot_deg
self.logger.info("changing rot for channel '%s' to %f" % (
chinfo2.name, new_rot_deg))
chinfo2.fitsimage.rotate(new_rot_deg)
def panset_cb(self, setting, value, chviewer, info):
"""This callback is called when a channel window is panned.
"""
# Don't do anything if we are not active
if not self.gui_up or self.ref_image is None:
return
# if this is not a zoom event from the focus window then
# don't do anything
## focus_chviewer = self.fv.getfocus_viewer()
## if chviewer != focus_chviewer:
## return
if self._cur_opn_viewer is not None:
return
self._cur_opn_viewer = chviewer
try:
self.pan_all_relative(chviewer, info.chinfo)
finally:
self._cur_opn_viewer = None
def pan_all_relative(self, chviewer, chinfo):
if self.ref_image is None:
return
image = chviewer.get_image()
if self.ref_image is None:
return
pan_ra, pan_dec = chviewer.get_pan(coord='wcs')
# for all other channels except ours
chnames = self.get_other_channels(chinfo.name)
for chname in chnames:
chinfo2 = self.fv.get_channel(chname)
# set pan position on their viewer
image = chinfo2.fitsimage.get_image()
if image is None:
continue
data_x, data_y = image.radectopix(pan_ra, pan_dec)
chinfo2.fitsimage.panset_xy(data_x, data_y)
def xfmset_cb(self, setting, value, chviewer, info):
"""This callback is called when a channel window is transformed
(flipped, or swap axes).
"""
# Don't do anything if we are not active
if not self.gui_up or self.ref_image is None:
return
# if this is not a zoom event from the focus window then
# don't do anything
## focus_chviewer = self.fv.getfocus_chviewer()
## if chviewer != focus_chviewer:
## return
if self._cur_opn_viewer is not None:
return
self._cur_opn_viewer = chviewer
try:
self.transform_all_relative(chviewer, info.chinfo)
finally:
self._cur_opn_viewer = None
def transform_all_relative(self, chviewer, chinfo):
if self.ref_image is None:
return
image = chviewer.get_image()
if self.ref_image is None:
return
flip_x, flip_y, swap_xy = chviewer.get_transforms()
# for all other channels except ours
chnames = self.get_other_channels(chinfo.name)
for chname in chnames:
chinfo2 = self.fv.get_channel(chname)
# set our pan position on their viewer
image = chinfo2.fitsimage.get_image()
if image is None:
continue
chinfo2.fitsimage.transform(flip_x, flip_y, swap_xy)
def redo(self):
if self.ref_image is None:
# no reference image
return
chinfo = self.fv.get_channel_info()
viewer = chinfo.fitsimage
image = viewer.get_image()
## if image == self.ref_image:
## # current image is same as reference image
## return
info = wcs.get_relative_orientation(image, self.ref_image)
self.logger.info("rscale_x=%f rscale_y=%f rrot_deg=%f" % (
info.rscale_x, info.rscale_y, info.rrot_deg))
def __str__(self):
return 'wcsmatch'
#END
|