.. note:: To get WCS functionality, please install one of the 3rd party python WCS modules referred to at the top of this module. """ def __init__(self, logger): super(BareBonesWCS, self).__init__(logger) self.kind = 'barebones' def load_header(self, header, fobj=None): self.coordsys = 'pixel' def spectral_coord(self, idxs, coords='data'): raise common.WCSError("This feature not supported by BareBonesWCS") def pixtoradec(self, idxs, coords='data'): px_x, px_y = idxs[:2] px_x, px_y = px_x + 1.0, px_y + 1.0 return (px_x, px_y) def radectopix(self, px_x, px_y, coords='data', naxispath=None): # px_x, px_y = px_x - 1.0, px_y - 1.0 return (px_x, px_y) def pixtosystem(self, idxs, system=None, coords='data'): return self.pixtoradec(idxs, coords=coords) # register our WCS with ginga common.register_wcs('barebones', BareBonesWCS, coord_types)
if self.coordsys == 'raw': raise common.WCSError("No usable WCS") if system is None: system = 'icrs' wcspt = self.datapt_to_wcspt(datapt, coords=coords, naxispath=naxispath) if not self.new_coords: raise NotImplementedError else: frameClass = coordinates.frame_transform_graph.lookup_name( self.coordsys) ra_deg = wcspt[:, 0] dec_deg = wcspt[:, 1] coord = frameClass(ra_deg * units.degree, dec_deg * units.degree) toClass = coordinates.frame_transform_graph.lookup_name(system) # Skip if input and output is the same (no realize_frame # call in astropy) if toClass != frameClass: coord = coord.transform_to(toClass) return coord # register our WCS with ginga common.register_wcs('astropy', AstropyWCS, coord_types)
self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg)) # convert to alternate coord try: fromsys = self.coordsys.upper() if fromsys == 'PIXEL': # these are really pixel values return (ra_deg, dec_deg) tosys = system.upper() if fromsys == 'B1950': equinox = 1950.0 else: equinox = 2000.0 lon_deg, lat_deg = astCoords.convertCoords(fromsys, tosys, ra_deg, dec_deg, equinox) except Exception as e: raise common.WCSError( "Error converting between coordinate systems " "'%s' and '%s': %s" % (fromsys, tosys, str(e))) return (lon_deg, lat_deg) # register our WCS with ginga common.register_wcs('astlib', AstLibWCS, coord_types)
return datapt def datapt_to_system(self, datapt, system=None, coords='data', naxispath=None): if self.coordsys == 'raw': raise common.WCSError("No usable WCS") if system is None: system = 'icrs' wcspt = self.datapt_to_wcspt(datapt, coords=coords, naxispath=naxispath) if self.coordsys == 'pixel': return wcspt # convert to alternate coord spec = self.conv_d[system] tran = kapwcs.Transformation(self._skyout, spec) return tran(wcspt) # register our WCS with ginga common.register_wcs('kapteyn', KapteynWCS, coord_types)
def datapt_to_system(self, datapt, system=None, coords='data', naxispath=None): if self.coordsys == 'raw': raise common.WCSError("No usable WCS") if system is None: system = 'icrs' wcspt = self.datapt_to_wcspt(datapt, coords=coords, naxispath=naxispath) if self.coordsys == 'pixel': return wcspt # define a transform from reference (icrs/j2000) to user's end choice refframe = self.icrs_trans.getframe(2) toframe = Ast.SkyFrame("System=%s, Epoch=2000.0" % (system.upper())) end_trans = refframe.convert(toframe) # convert to alternate coord wcspt = np.radians(wcspt) wcspt = end_trans.tran(wcspt.T, 1) wcspt = np.degrees(wcspt) return wcspt.T # register our WCS with ginga common.register_wcs('starlink', StarlinkWCS, coord_types)
------- coord : SkyCoord """ if self.coordsys == 'raw': raise common.WCSError("No usable WCS") if system is None: system = 'icrs' # Get a coordinates object based on ra/dec wcs transform wcspt = self.datapt_to_wcspt(datapt, coords=coords, naxispath=naxispath) frame_class = coordinates.frame_transform_graph.lookup_name( self.coordsys) ra_deg = wcspt[:, 0] dec_deg = wcspt[:, 1] coord = frame_class(ra_deg * u.degree, dec_deg * u.degree) to_class = coordinates.frame_transform_graph.lookup_name(system) # Skip in input and output is the same (no realize_frame # call in astropy) if to_class != frame_class: coord = coord.transform_to(to_class) return coord # register our WCS with ginga common.register_wcs('astropy_ape14', AstropyWCS, coord_types)
if coords == 'data': # Kapteyn's WCS returns pixels referenced from 1 datapt = datapt - 1.0 return datapt def datapt_to_system(self, datapt, system=None, coords='data', naxispath=None): if self.coordsys == 'raw': raise common.WCSError("No usable WCS") if system is None: system = 'icrs' wcspt = self.datapt_to_wcspt(datapt, coords=coords, naxispath=naxispath) if self.coordsys == 'pixel': return wcspt # convert to alternate coord spec = self.conv_d[system] tran = kapwcs.Transformation(self._skyout, spec) return tran(wcspt) # register our WCS with ginga common.register_wcs('kapteyn', KapteynWCS, coord_types)
self.wcs_ximage = np.outer(np.linspace(4000., 8000., num=shape[0]), np.ones(shape[1])) def pixtoradec(self, idxs, coords='data'): """Convert a (x, y) pixel coordinate on the image to a (ra, dec) coordinate in space. Parameter (coords): - if 'data' then x, y coordinates are interpreted as 0-based - otherwise coordinates are interpreted as 1-based (traditional FITS) """ if self.wcs_ximage is None: return 0., 0. x, y = idxs[:2] # account for DATA->FITS coordinate space if coords == 'data': x, y = int(round(x)), int(round(y)) ra_deg = self.wcs_ximage[y, x] dec_deg = 0. return ra_deg, dec_deg def pixtosystem(self, idxs, system=None, coords='data'): return self.pixtoradec(idxs, coords=coords) # register our WCS with ginga common.register_wcs('imgwcs', ImgWCS, coord_types)
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, as a bytes object `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) """ self.logger.info("received image data len=%d" % (len(img_buf))) # Unpack the data try: # 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) # WCS? if 'WCS-XIMG' in header.keys(): from ginga.util import wcsmod from ginga.util.wcsmod.common import register_wcs from ginga.util.wcsmod.wcs_img import ImgWCS register_wcs('img', ImgWCS, ['pixel']) wcsmod.use('img') metadata = header.copy() # 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