def display(self, frame, width, height, reverse=False): """ NOTE: this is called from the IISRequestHandler """ fb = self.get_frame(frame) self.current_frame = frame if reverse: fb.buffer.reverse() # frames are indexed from 1 in IRAF chname = fb.chname if chname == None: chname = 'Frame%d' % (frame+1) fb.chname = chname self.logger.debug("display to %s" %(chname)) try: data = fb.buffer byteswap = False dims = (fb.height, fb.width) dtype = numpy.uint8 metadata = {} image = IRAF_AstroImage(logger=self.logger) #image.load_buffer(fb.buffer, dims, dtype, byteswap=byteswap, # metadata=metadata) data = numpy.fromstring(fb.buffer, dtype=dtype) data = data.reshape(dims) # Image comes in from IRAF flipped for screen display data = numpy.flipud(data) image.set_data(data, metadata=metadata) # Save coordinate transform info image.set(ct=fb.ct) # make up a name (is there a protocol slot for the name?) fitsname = str(time.time()) # extract path from ref oldref = fb.ct.ref items = oldref.split('!') host = items[0] path = '!'.join(items[1:]) image.set(name=fitsname, path=path, host=host) #image.update_keywords(header) except Exception as e: # Some kind of error unpacking the data errmsg = "Error creating image data for '%s': %s" % ( chname, str(e)) self.logger.error(errmsg) raise GingaPlugin.PluginError(errmsg) # Do the GUI bits as the GUI thread self.fv.gui_do(self._gui_display_image, fitsname, image, chname)
def build_gui(self, container): if not have_samp: raise GingaPlugin.PluginError( "To run this plugin you need to install the astropy.vo.samp module" ) vbox = Widgets.VBox() vbox.set_border_width(4) vbox.set_spacing(2) msgFont = self.fv.getFont("sansFont", 12) tw = Widgets.TextArea(wrap=True, editable=False) tw.set_font(msgFont) self.tw = tw fr = Widgets.Frame("Instructions") fr.set_widget(tw) vbox.add_widget(fr, stretch=0) fr = Widgets.Frame("SAMP") captions = [ ('Start hub', 'checkbutton'), ('Connect client', 'checkbutton'), ] w, b = Widgets.build_info(captions) self.w.update(b) b.start_hub.set_tooltip("Start a SAMP hub") b.start_hub.set_state(self.settings.get('start_hub', True)) b.start_hub.add_callback('activated', self.start_hub_cb) b.connect_client.set_tooltip("Register with a SAMP hub") b.connect_client.set_state(self.settings.get('default_connect', True)) b.connect_client.add_callback('activated', self.connect_client_cb) fr.set_widget(w) vbox.add_widget(fr, stretch=0) # stretch vbox.add_widget(Widgets.Label(''), stretch=1) btns = Widgets.HBox() btns.set_border_width(4) btns.set_spacing(4) btn = Widgets.Button("Close") btn.add_callback('activated', lambda w: self.close()) btns.add_widget(btn) btns.add_widget(Widgets.Label(''), stretch=1) vbox.add_widget(btns, stretch=0) container.add_widget(vbox, stretch=1)
def build_gui(self, container): if not have_samp: raise GingaPlugin.PluginError( "To run this plugin you need to install the " "astropy package") vbox = Widgets.VBox() vbox.set_border_width(4) vbox.set_spacing(2) fr = Widgets.Frame("SAMP") captions = [ ('Start hub', 'checkbutton'), ('Connect client', 'checkbutton'), ] w, b = Widgets.build_info(captions) self.w.update(b) b.start_hub.set_tooltip("Start a SAMP hub") b.start_hub.set_state(self.settings.get('start_hub', True)) b.start_hub.add_callback('activated', self.start_hub_cb) b.connect_client.set_tooltip("Register with a SAMP hub") b.connect_client.set_state(self.settings.get('default_connect', True)) b.connect_client.add_callback('activated', self.connect_client_cb) fr.set_widget(w) vbox.add_widget(fr, stretch=0) # stretch vbox.add_widget(Widgets.Label(''), stretch=1) btns = Widgets.HBox() btns.set_border_width(4) btns.set_spacing(4) btn = Widgets.Button("Close") btn.add_callback('activated', lambda w: self.close()) btns.add_widget(btn) btn = Widgets.Button("Help") btn.add_callback('activated', lambda w: self.help()) btns.add_widget(btn, stretch=0) btns.add_widget(Widgets.Label(''), stretch=1) vbox.add_widget(btns, stretch=0) container.add_widget(vbox, stretch=1) self.gui_up = True
def load_fits_buffer(self, imname, chname, file_buf, num_hdu, metadata): from astropy.io import fits # Unpack the data try: # Decode binary data file_buf = binascii.a2b_base64(file_buf) # Uncompress data if necessary decompress = metadata.get('decompress', None) if decompress == 'bz2': file_buf = bz2.decompress(file_buf) self.logger.info("received data: len=%d num_hdu=%d" % (len(file_buf), num_hdu)) in_f = BytesIO(file_buf) # Create image container with fits.open(in_f, 'readonly') as fits_f: image = AstroImage.AstroImage(metadata=metadata, logger=self.logger) image.load_hdu(fits_f[num_hdu], fobj=fits_f) image.set(name=imname, path=None, idx=num_hdu) except Exception as e: # Some kind of error unpacking the data errmsg = "Error creating image data for '%s': %s" % (imname, str(e)) self.logger.error(errmsg) raise GingaPlugin.PluginError(errmsg) # Display the image channel = self.fv.gui_call(self.fv.get_channel_on_demand, chname) # Note: this little hack needed to let window resize in time for # file to auto-size properly self.fv.gui_do(self.fv.change_channel, channel.name) self.fv.gui_do(self.fv.add_image, imname, image, chname=channel.name) return 0
def display_fitsbuf(self, fitsname, chname, data, dims, dtype, header, metadata, compressed): """Display a FITS image buffer. Parameters: _fitsname_: name of the file _chname_: channel to display the data _data_: ascii encoded numpy containing image data _dims_: image dimensions in pixels (usually a (height, width) tuple) _dtype_: numpy data type of encoding (e.g. 'float32') _header_: fits file header as a dictionary _metadata_: metadata about image to attach to image _compressed_: True if the data is compressed """ # Unpack the data try: # Decode binary data data = binascii.a2b_base64(data) # Uncompress data if necessary if compressed: data = bz2.decompress(data) if dtype == '': dtype = numpy.float32 else: # string to actual type dtype = getattr(numpy, dtype) # Create image container image = AstroImage.AstroImage() image.load_buffer(data, dims, dtype, byteswap=byteswap, metadata=metadata) image.set(name=fitsname) image.update_keywords(header) except Exception, e: # Some kind of error unpacking the data errmsg = "Error creating image data for '%s': %s" % ( fitsname, str(e)) self.logger.error(errmsg) raise GingaPlugin.PluginError(errmsg)
def load_fits_buffer(self, imname, chname, file_buf, num_hdu, metadata): from astropy.io import fits # Unpack the data try: # Decode binary data file_buf = binascii.a2b_base64(file_buf) # Uncompress data if necessary decompress = metadata.get('decompress', None) if decompress == 'bz2': file_buf = bz2.decompress(file_buf) self.logger.info("received data: len=%d num_hdu=%d" % ( len(file_buf), num_hdu)) in_f = BytesIO(file_buf) # Create image container with fits.open(in_f, 'readonly') as fits_f: image = AstroImage.AstroImage(metadata=metadata, logger=self.logger) image.load_hdu(fits_f[num_hdu], fobj=fits_f) image.set(name=imname) except Exception as e: # Some kind of error unpacking the data errmsg = "Error creating image data for '%s': %s" % ( imname, str(e)) self.logger.error(errmsg) raise GingaPlugin.PluginError(errmsg) # Enqueue image to display datasrc self.fv.gui_do(self.fv.add_image, imname, image, chname=chname) return 0
def load_buffer(self, imname, chname, img_buf, dims, dtype, header, metadata, compressed): """Display a FITS image buffer. Parameters ---------- `imname`: string a name to use for the image in Ginga `chname`: string channel in which to load the image `img_buf`: string the image data, encoded as a base64 ascii encoded string `dims`: tuple image dimensions in pixels (usually (height, width)) `dtype`: string numpy data type of encoding (e.g. 'float32') `header`: dict fits file header as a dictionary `metadata`: dict other metadata about image to attach to image `compressed`: bool decompress buffer using "bz2" Returns ------- 0 Notes ----- * Get array dims: data.shape * Get array dtype: str(data.dtype) * Make a string from a numpy array: buf = data.tostring() * Compress the buffer: buf = bz2.compress(buf) * Convert buffer to ascii-encoding: buf = binascii.b2a_base64(buf) """ self.logger.info("received image data len=%d" % (len(img_buf))) # Unpack the data try: # Decode binary data img_buf = binascii.a2b_base64(img_buf) # Uncompress data if necessary decompress = metadata.get('decompress', None) if compressed or (decompress == 'bz2'): img_buf = bz2.decompress(img_buf) # dtype string works for most instances if dtype == '': dtype = numpy.float32 byteswap = metadata.get('byteswap', False) # Create image container image = AstroImage.AstroImage(logger=self.logger) image.load_buffer(img_buf, dims, dtype, byteswap=byteswap, metadata=metadata) image.update_keywords(header) image.set(name=imname, path=None) except Exception as e: # Some kind of error unpacking the data errmsg = "Error creating image data for '%s': %s" % (imname, str(e)) self.logger.error(errmsg) raise GingaPlugin.PluginError(errmsg) # Display the image channel = self.fv.gui_call(self.fv.get_channel_on_demand, chname) # Note: this little hack needed to let window resize in time for # file to auto-size properly self.fv.gui_do(self.fv.change_channel, channel.name) self.fv.gui_do(self.fv.add_image, imname, image, chname=channel.name) return 0
def load_buffer(self, imname, chname, img_buf, dims, dtype, header, wav_buf, wav_dtype, metadata): """ Load and display the 2D slit image. Parameters ---------- imname : :obj:`str` Name to use for the image in Ginga chname : :obj:`str` Channel in which to load the image img_buf : bytes Image data, as a buffer dims : :obj:`tuple` Image dimensions in pixels, typically (height, width) dtype : :obj:`str` numpy data type of encoding (e.g. 'float32') header : :obj:`dict` Fits file header as a dictionary wav_buf : bytes Wavelength image data, as a buffer. Ultimate 2D shape once parsed must match the input image data. wav_dtype : :obj:`str` numpy data type of wav_buf array encoding (e.g. 'float32') metadata : :obj:`dict` other metadata about image to attach to image Returns ------- status : :obj:`int` Load status number. Currently always returns 0. Notes ----- * Get array dims: data.shape * Get array dtype: str(data.dtype) * Make a string from a numpy array: buf = grc.Blob(data.tobytes()) """ self.logger.info("received image data len=%d" % (len(img_buf))) # Unpack the data # TODO: Should trim down what's in this try/except block try: # dtype string works for most instances if dtype == '': dtype = np.float byteswap = metadata.get('byteswap', False) # unpack the auxillary wavelength file data = np.fromstring(wav_buf, dtype=wav_dtype) if byteswap: data.byteswap(True) wav_np = data.reshape(dims) # Create image container image = SlitImage(wav_np=wav_np, logger=self.logger) image.load_buffer(img_buf, dims, dtype, byteswap=byteswap, metadata=metadata) image.update_keywords(header) image.set(name=imname, path=None) except Exception as e: # Some kind of error unpacking the data errmsg = 'Error creating image data for {0}: {1}'.format(imname, e) self.logger.error(errmsg) raise GingaPlugin.PluginError(errmsg) # Display the image channel = self.fv.gui_call(self.fv.get_channel_on_demand, chname) # Note: this little hack needed to let window resize in time for # file to auto-size properly self.fv.gui_do(self.fv.change_channel, channel.name) self.fv.gui_do(self.fv.add_image, imname, image, chname=channel.name) return 0
def load_buffer(self, imname, chname, data, dims, dtype, header, metadata, compressed): """Display a FITS image buffer. Parameters ---------- `imname`: string a name to use for the image in Ginga `chname`: string channel in which to load the image `data`: string the image data, encoded as a base64 ascii encoded string `dims`: tuple image dimensions in pixels (usually (height, width)) `dtype`: string numpy data type of encoding (e.g. 'float32') `header`: dict fits file header as a dictionary `metadata`: dict other metadata about image to attach to image `compressed`: boolean True if `data` is bz2 compressed before ascii encoding Returns ------- 0 Notes ----- * Get array dims: data.shape * Get array dtype: str(data.dtype) * Make a string from a numpy array: buf = data.tostring() * Compress the buffer: buf = bz2.compress(buf) * Convert buffer to ascii-encoding: buf = binascii.b2a_base64(buf) """ # Unpack the data try: # Decode binary data data = binascii.a2b_base64(data) # Uncompress data if necessary if compressed: data = bz2.decompress(data) if dtype == '': dtype = numpy.float32 else: # string to actual type dtype = getattr(numpy, dtype) # Create image container image = AstroImage.AstroImage(logger=self.logger) image.load_buffer(data, dims, dtype, byteswap=byteswap, metadata=metadata) image.set(name=imname) image.update_keywords(header) except Exception as e: # Some kind of error unpacking the data errmsg = "Error creating image data for '%s': %s" % (fitsname, str(e)) self.logger.error(errmsg) raise GingaPlugin.PluginError(errmsg) # Enqueue image to display datasrc self.fv.gui_do(self.fv.add_image, fitsname, image, chname=chname) return 0