Skip to content

Commit 19cfcb0

Browse files
committed
Add basic cursor display
1 parent 54fe7ee commit 19cfcb0

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

astrowidgets/bqplot.py

+58-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from astropy.table import Table, vstack
77
from astropy import units as u
88
import astropy.visualization as apviz
9+
from astropy.wcs import WCS
910

1011
from bqplot import Figure, LinearScale, Axis, ColorScale, PanZoom, ScatterGL
1112
from bqplot_image_gl import ImageGL
@@ -177,6 +178,10 @@ def center(self, value):
177178
self._scales['y'].max = y_c + width_y / 2
178179
self._scales['y'].min = y_c - width_y / 2
179180

181+
@property
182+
def interaction(self):
183+
return self._figure.interaction
184+
180185
def set_color(self, colors):
181186
# colors here means a list of hex colors
182187
self._image.scales['image'].colors = colors
@@ -363,12 +368,64 @@ def __init__(self, *args, image_width=500, image_height=500):
363368
viewer_aspect_ratio=viewer_aspect)
364369
self._interval = None
365370
self._stretch = None
366-
self._colormap = 'Grays'
371+
self.set_colormap('Greys_r')
367372
self._marker_table = MarkerTableManager()
368373
self._data = None
369374
self._wcs = None
370375
self._is_marking = False
376+
371377
self.marker = {'color': 'red', 'radius': 20, 'type': 'square'}
378+
self.cuts = apviz.AsymmetricPercentileInterval(1, 99)
379+
380+
self._cursor = ipw.HTML('Coordinates show up here')
381+
self._init_mouse_callbacks()
382+
self.children = [self._astro_im, self._cursor]
383+
384+
def _init_mouse_callbacks(self):
385+
386+
def on_mouse_message(interaction, event_data, buffers):
387+
"""
388+
This function simply detects the event type then dispatches
389+
to the method that handles that event.
390+
391+
The ``event_data`` contains all of the information we need.
392+
"""
393+
if event_data['event'] == 'mousemove':
394+
self._mouse_move(event_data)
395+
396+
self._astro_im.interaction.on_msg(on_mouse_message)
397+
398+
def _mouse_move(self, event_data):
399+
if self._data is None:
400+
# Nothing to display, so exit
401+
return
402+
403+
xc = event_data['domain']['x']
404+
yc = event_data['domain']['y']
405+
406+
# get the array indices into the data so that we can get data values
407+
x_index = int(np.trunc(xc + 0.5))
408+
y_index = int(np.trunc(yc + 0.5))
409+
410+
# Check that the index is in the array.
411+
in_image = (self._data.shape[1] > x_index >= 0) and (self._data.shape[0] > y_index >= 0)
412+
if in_image:
413+
val = self._data[y_index, x_index]
414+
else:
415+
val = None
416+
417+
if val is not None:
418+
value = f'value: {val:8.3f}'
419+
else:
420+
value = 'value: N/A'
421+
422+
pixel_location = f'X: {xc:.2f} Y: {yc:.2f}'
423+
if self._wcs is not None:
424+
sky = self._wcs.pixel_to_world(xc, yc)
425+
ra_dec = f'RA: {sky.icrs.ra:3.7f} Dec: {sky.icrs.dec:3.7f}'
426+
else:
427+
ra_dec = ''
428+
self._cursor.value = ', '.join([pixel_location, ra_dec, value])
372429

373430
def _interval_and_stretch(self):
374431
"""

0 commit comments

Comments
 (0)