Skip to content

Commit 5a269c9

Browse files
committed
Add basic cursor display
1 parent 858c77d commit 5a269c9

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

astrowidgets/bqplot.py

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

374431
def _interval_and_stretch(self):
375432
"""

0 commit comments

Comments
 (0)