def pixtosystem(self, idxs, system=None, coords='data'): if self.coordsys == 'raw': raise common.WCSError("No usable WCS") if system is None: system = 'j2000' # Get a coordinates object based on ra/dec wcs transform ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords) 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)
def pixtocoords(self, idxs, system=None, coords='data'): 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 ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords) self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg)) if not self.new_coords: # convert to astropy coord try: fromclass = self.coord_table[self.coordsys] except KeyError: raise common.WCSError( "No such coordinate system available: '%s'" % (self.coordsys)) coord = fromclass(ra_deg, dec_deg, unit=(units.degree, units.degree)) if (system is None) or (system == self.coordsys): return coord # Now give it back to the user in the system requested try: toclass = self.coord_table[system] except KeyError: raise common.WCSError( "No such coordinate system available: '%s'" % (system)) coord = coord.transform_to(toclass) else: frameClass = coordinates.frame_transform_graph.lookup_name( self.coordsys) coord = frameClass(ra_deg * units.degree, dec_deg * units.degree) toClass = coordinates.frame_transform_graph.lookup_name(system) # Skip in input and output is the same (no realize_frame # call in astropy) if toClass != frameClass: coord = coord.transform_to(toClass) return coord
def datapt_to_wcspt(self, datapt, coords='data', naxispath=None): # force to array representation datapt = np.asarray(datapt) # Kapteyn's WCS needs pixels referenced from 1 if coords == 'data': datapt = datapt + 1.0 if naxispath is not None: n = len(naxispath) if n > 0: datapt = np.hstack((datapt, np.zeros((len(datapt), n)))) try: wcspt = self.wcs.toworld(datapt) except Exception as e: self.logger.error("Error calculating datapt_to_wcspt: %s" % (str(e))) raise common.WCSError(e) # TODO: swap axes if lon/lat reversed? ## if ((self.wcs.lonaxnum is not None) and ## (self.wcs.lataxnum is not None)): return wcspt
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
def wcspt_to_datapt(self, wcspt, coords='data', naxispath=None): # force to array representation wcspt = np.asarray(wcspt) # Starlink works on angles in radians wcspt = np.radians(wcspt) if naxispath is not None: n = len(naxispath) if n > 0: wcspt = np.hstack((wcspt, np.zeros((len(wcspt), n)))) try: # 0 as second arg -> inverse transform datapt = self.wcs.tran(wcspt.T, 0) except Exception as e: self.logger.error( "Error calculating wcspt_to_datapt: %s" % (str(e))) raise common.WCSError(e) if coords == 'data': # Starlink's WCS returns pixels referenced from 1 datapt = datapt - 1.0 return datapt.T
def datapt_to_wcspt(self, datapt, coords='data', naxispath=None): # force to array representation datapt = np.asarray(datapt) # Starlink's WCS needs pixels referenced from 1 if coords == 'data': datapt = datapt + 1.0 if naxispath is not None: n = len(naxispath) if n > 0: datapt = np.hstack((datapt, np.zeros((len(datapt), n)))) try: # 1 as second arg -> regular transform wcspt = self.wcs.tran(datapt.T, 1) if self.coordsys not in ('pixel', 'raw'): # whatever sky coords to icrs coords wcspt = self.icrs_trans.tran(wcspt, 1) except Exception as e: self.logger.error( "Error calculating datapt_to_wcspt: %s" % (str(e))) raise common.WCSError(e) # Starlink returns angles in radians wcspt = np.degrees(wcspt.T) return wcspt
def pixtosystem(self, idxs, system=None, coords='data'): 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 ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords) self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg)) if self.coordsys == 'pixel': # these will actually be x, y pixel values return (ra_deg, dec_deg) # 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 ra_rad, dec_rad = np.radians(ra_deg), np.radians(dec_deg) res = end_trans.tran([[ra_rad], [dec_rad]], 1) lon_rad, lat_rad = res[0][0], res[1][0] lon_deg, lat_deg = np.degrees(lon_rad), np.degrees(lat_rad) return lon_deg, lat_deg
def pixtoradec(self, idxs, coords='data'): # Starlink's WCS needs pixels referenced from 1 if coords == 'data': idxs = np.array(list(map(lambda x: x + 1, idxs))) else: idxs = np.array(idxs) try: # pixel to sky coords (in the WCS specified transform) arrs = [[idxs[i]] for i in range(len(idxs))] res = self.wcs.tran(arrs, 1) if self.coordsys not in ('pixel', 'raw'): # whatever sky coords to icrs coords res = self.icrs_trans.tran(res, 1) # TODO: what if axes are inverted? ra_rad, dec_rad = res[0][0], res[1][0] ra_deg, dec_deg = np.degrees(ra_rad), np.degrees(dec_rad) # print(ra_deg, dec_deg) except Exception as e: self.logger.error("Error calculating pixtoradec: %s" % (str(e))) raise common.WCSError(e) return ra_deg, dec_deg
def datapt_to_system(self, datapt, system=None, coords='data', naxispath=None): """ Map points to given coordinate system. Parameters ---------- datapt : array-like Pixel coordinates in the format of ``[[x0, y0, ...], [x1, y1, ...], ..., [xn, yn, ...]]``. system : str or None, optional, default to 'icrs' Coordinate system name. coords : 'data' or None, optional, default to 'data' Expresses whether the data coordinate is indexed from zero naxispath : list-like or None, optional, defaults to None A sequence defining the pixel indexes > 2D, if any Returns ------- coord : SkyCoord """ 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
def spectral_coord(self, idxs, coords='data'): # NOTE: origin is always 0, coords unused. pixcrd = np.array([idxs], np.float_) try: sky = self.wcs.pixel_to_world( pixcrd[:, 0], pixcrd[:, 1], pixcrd[:, 2]) return sky[1].value[0] except Exception as e: self.logger.error( "Error calculating spectral coordinate: {}".format(str(e))) raise common.WCSError(e)
def radectopix(self, ra_deg, dec_deg, coords='data', naxispath=None): try: x, y = self.wcs.wcs2pix(ra_deg, dec_deg) except Exception as e: self.logger.error("Error calculating radectopix: %s" % (str(e))) raise common.WCSError(e) if coords == 'fits': # Via astWCS.NUMPY_MODE, we've forced pixels referenced from 0 x, y = x+1, y+1 return (x, y)
def pixtoradec(self, idxs, coords='data'): if coords == 'fits': # Via astWCS.NUMPY_MODE, we've forced pixels referenced from 0 idxs = tuple(map(lambda x: x-1, idxs)) try: ra_deg, dec_deg = self.wcs.pix2wcs(idxs[0], idxs[1]) except Exception as e: self.logger.error("Error calculating pixtoradec: %s" % (str(e))) raise common.WCSError(e) return ra_deg, dec_deg
def spectral_coord(self, idxs, coords='data'): if coords == 'data': origin = 0 else: origin = 1 pixcrd = np.array([idxs], np.float_) try: sky = self.wcs.all_pix2world(pixcrd, origin) return float(sky[0, 2]) except Exception as e: self.logger.error("Error calculating spectral coordinate: %s" % (str(e))) raise common.WCSError(e)
def radectopix(self, ra_deg, dec_deg, coords='data', naxispath=None): # NOTE: origin is always 0, coords unused. args = [ra_deg, dec_deg] if naxispath: args += [0] * len(naxispath) try: xy = self.wcs.world_to_pixel_values(*args)[:2] except Exception as e: self.logger.error( "Error calculating radectopix: {}".format(str(e))) raise common.WCSError(e) return xy
def spectral_coord(self, idxs, coords='data'): # Kapteyn's WCS needs pixels referenced from 1 if coords == 'data': idxs = tuple(map(lambda x: x + 1, idxs)) else: idxs = tuple(idxs) try: res = self.wcs.toworld(idxs) if len(res) > 0: return res[self.wcs.specaxnum - 1] except Exception as e: self.logger.error("Error calculating spectral coordinate: %s" % (str(e))) raise common.WCSError(e)
def wcspt_to_datapt(self, wcspt, coords='data', naxispath=None): # NOTE: origin is always 0, coords unused. if naxispath is not None: n = len(naxispath) if n > 0: wcspt = np.hstack((wcspt, np.zeros((len(wcspt), n)))) wcspt = np.asarray(wcspt) try: args = [wcspt[:, i] for i in range(wcspt.shape[1])] # NOTE: Ignores system transformation. datapt = np.asarray(self.wcs.world_to_pixel_values(*args))[:2, :].T except Exception as e: self.logger.error( "Error calculating wcspt_to_datapt: {}".format(str(e))) raise common.WCSError(e) return datapt
def spectral_coord(self, idxs, coords='data'): # Starlink's WCS needs pixels referenced from 1 if coords == 'data': idxs = np.array(map(lambda x: x + 1, idxs)) else: idxs = np.array(idxs) try: # pixel to sky coords (in the WCS specified transform) arrs = [[idxs[i]] for i in range(len(idxs))] res = self.wcs.tran(arrs, 1) return res[2][0] except Exception as e: self.logger.error( "Error calculating spectral coordinate: %s" % (str(e))) raise common.WCSError(e)
def wcspt_to_datapt(self, wcspt, coords='data', naxispath=None): if coords == 'data': origin = 0 else: origin = 1 if naxispath is not None: n = len(naxispath) if n > 0: wcspt = np.hstack((wcspt, np.zeros((len(wcspt), n)))) try: datapt = self.wcs.all_world2pix(wcspt, origin) except Exception as e: self.logger.error("Error calculating wcspt_to_datapt: %s" % (str(e))) raise common.WCSError(e) return datapt[:, :2]
def pixtoradec(self, idxs, coords='data'): # NOTE: origin is always 0, coords unused. try: c = self.wcs.pixel_to_world(*idxs) if (isinstance(c, list) and isinstance(c[0], coordinates.SkyCoord)): # naxis > 2 c = c[0] except Exception as e: self.logger.error( "Error calculating pixtoradec: {}".format(str(e))) raise common.WCSError(e) if isinstance(c, coordinates.SkyCoord): radec = (c.ra.deg, c.dec.deg) else: # list of Quantity (e.g., from primary header) radec = (c[0].value, c[1].value) return radec
def radectopix(self, ra_deg, dec_deg, coords='data', naxispath=None): args = [ra_deg, dec_deg] if naxispath: args += [0] * len(naxispath) args = tuple(args) try: pix = self.wcs.topixel(args) except Exception as e: self.logger.error("Error calculating radectopix: %s" % (str(e))) raise common.WCSError(e) if coords == 'data': # Kapteyn's WCS returns pixels referenced from 1 pix = tuple(map(lambda x: x - 1, pix)) x, y = pix[0], pix[1] return (x, y)
def pixtonative(self, idxs, coords='data'): """ Convert the pixel value to the native coordinate frame of the header """ if coords == 'data': origin = 0 else: origin = 1 pixcrd = np.array([idxs], np.float_) try: sky = self.wcs.all_pix2world(pixcrd, origin)[0] * u.deg except Exception as e: self.logger.error("Error calculating pixtonative: %s" % (str(e))) raise common.WCSError(e) # Update our frame with the new data self.realize_frame_inplace(sky) return self.coordframe
def wcspt_to_datapt(self, wcspt, coords='data', naxispath=None): if naxispath is not None: n = len(naxispath) if n > 0: wcspt = np.hstack((wcspt, np.zeros((len(wcspt), n)))) try: datapt = self.wcs.topixel(wcspt) except Exception as e: self.logger.error( "Error calculating wcspt_to_datapt: %s" % (str(e))) raise common.WCSError(e) 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)
def pixtocoords(self, idxs, system=None, coords='data'): # NOTE: origin is always 0, coords unused. 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 coord = self.wcs.pixel_to_world(*idxs) if isinstance(coord, list): # naxis > 2 coord = coord[0] 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 != coord.name: coord = coord.transform_to(to_class) return coord
def pixtosystem(self, idxs, system=None, coords='data'): 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 ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords) self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg)) if self.coordsys == 'pixel': return (ra_deg, dec_deg) # convert to alternate coord spec = self.conv_d[system] tran = kapwcs.Transformation(self._skyout, spec) lon_deg, lat_deg = tran((ra_deg, dec_deg)) return lon_deg, lat_deg
def pixtoradec(self, idxs, coords='data'): if coords == 'data': origin = 0 else: origin = 1 pixcrd = np.array([idxs], np.float_) try: # sky = self.wcs.wcs_pix2sky(pixcrd, origin) # sky = self.wcs.all_pix2sky(pixcrd, origin) # astropy only? sky = self.wcs.all_pix2world(pixcrd, origin) except Exception as e: self.logger.error("Error calculating pixtoradec: %s" % (str(e))) raise common.WCSError(e) ra_deg = float(sky[0, 0]) dec_deg = float(sky[0, 1]) return ra_deg, dec_deg
def pixtocoords(self, idxs, system=None, coords='data'): 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 ra_deg, dec_deg = self.pixtoradec(idxs, coords=coords) self.logger.debug("ra, dec = %f, %f" % (ra_deg, dec_deg)) frame_class = coordinates.frame_transform_graph.lookup_name( self.coordsys) coord = frame_class(ra_deg * units.degree, dec_deg * units.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
def pixtoradec(self, idxs, coords='data'): # Kapteyn's WCS needs pixels referenced from 1 if coords == 'data': idxs = tuple(map(lambda x: x + 1, idxs)) else: idxs = tuple(idxs) # print("indexes=%s" % (str(idxs))) try: res = self.wcs.toworld(idxs) if ((self.wcs.lonaxnum is not None) and (self.wcs.lataxnum is not None)): ra_deg = res[self.wcs.lonaxnum - 1] dec_deg = res[self.wcs.lataxnum - 1] else: ra_deg, dec_deg = res[0], res[1] except Exception as e: self.logger.error("Error calculating pixtoradec: %s" % (str(e))) raise common.WCSError(e) return ra_deg, dec_deg
def radectopix(self, ra_deg, dec_deg, coords='data', naxispath=None): try: # sky coords to pixel (in the WCS specified transform) ra_rad, dec_rad = np.radians(ra_deg), np.radians(dec_deg) # TODO: what if spatial axes are inverted? args = [ra_rad, dec_rad] if naxispath: args += [0] * len(naxispath) arrs = [[args[i]] for i in range(len(args))] # 0 as second arg -> inverse transform res = self.wcs.tran(arrs, 0) x, y = res[0][0], res[1][0] except Exception as e: self.logger.error("Error calculating radectopix: %s" % (str(e))) raise common.WCSError(e) if coords == 'data': # Starlink's WCS returns pixels referenced from 1 x, y = x - 1, y - 1 return (x, y)
def radectopix(self, ra_deg, dec_deg, coords='data', naxispath=None): if coords == 'data': origin = 0 else: origin = 1 args = [ra_deg, dec_deg] if naxispath: args += [0] * len(naxispath) skycrd = np.array([args], np.float_) try: pix = self.wcs.wcs_world2pix(skycrd, origin) except Exception as e: self.logger.error("Error calculating radectopix: %s" % (str(e))) raise common.WCSError(e) x = float(pix[0, 0]) y = float(pix[0, 1]) return (x, y)