def to_region(**kwargs): """Model outline (`~regions.EllipseSkyRegion`).""" return EllipseSkyRegion( center=SkyCoord(np.nan * u.deg, np.nan * u.deg), height=np.nan * u.deg, width=np.nan * u.deg, angle=np.nan * u.deg, **kwargs, )
def to_region(self, **kwargs): """Model outline (`~regions.EllipseSkyRegion`).""" minor_axis = Angle(self.sigma.quantity * np.sqrt(1 - self.e.quantity**2)) return EllipseSkyRegion(center=self.position, height=2 * self.sigma.quantity, width=2 * minor_axis, angle=self.phi.quantity, **kwargs)
def position_error(self): """Get 95% containment position error as (`~regions.EllipseSkyRegion`)""" if self.covariance is None: return EllipseSkyRegion( center=self.position, height=np.nan * u.deg, width=np.nan * u.deg, angle=np.nan * u.deg, ) pars = self.parameters sub_covar = self.covariance.get_subcovariance(["lon_0", "lat_0"]).data.copy() cos_lat = np.cos(self.lat_0.quantity.to_value("rad")) sub_covar[0, 0] *= cos_lat**2.0 sub_covar[0, 1] *= cos_lat sub_covar[1, 0] *= cos_lat eig_vals, eig_vecs = np.linalg.eig(sub_covar) lon_err, lat_err = np.sqrt(eig_vals) y_vec = eig_vecs[:, 0] phi = (np.arctan2(y_vec[1], y_vec[0]) * u.rad).to("deg") + self.phi_0 err = np.sort([lon_err, lat_err]) scale_r95 = Gauss2DPDF(sigma=1).containment_radius(0.95) err *= scale_r95 if err[1] == lon_err * scale_r95: phi += 90 * u.deg height = 2 * err[1] * pars["lon_0"].unit width = 2 * err[0] * pars["lat_0"].unit else: height = 2 * err[1] * pars["lat_0"].unit width = 2 * err[0] * pars["lon_0"].unit return EllipseSkyRegion(center=self.position, height=height, width=width, angle=phi)
def catalog_to_regions(catalog, ra='RA', dec='DEC', majax='Maj', minax='Min', PA='PA'): ''' Convert catalog to a list of regions Keyword arguments: catalog -- Input catalog ra, dec, majax, minax, PA -- Column names of containing required variables ''' regions = Regions([ EllipseSkyRegion(center=SkyCoord(source[ra], source[dec], unit='deg'), height=source[majax] * u.deg, width=source[minax] * u.deg, angle=source[PA] * u.deg) for source in catalog ]) return regions
def create_gal_region(galaxy_cat, row_index=None, pgcname=None, d25scale=1.): """ Similar to ellreg above but can take a galaxy name as input. Create galaxy ellipse region using an input galaxy catalog_table (likely need to change this API as it heavily follows the RC3 catalog format) Parameters ---------- galaxy_catalog_table: an astropy table table of nearby galaxy positions and sizes. Must have a central coordinate and the SemiMajorAxis, SemiMinorAxis, Position Angle info as table column names row_index: int a row index in the galaxy catalog to create the ellipse for pgcname: str the PGCNAME in the RC3 cat. This is a string. eg. "PGC 43255" for NGC 4707 d25scale: float how many times D25 should to scale the region """ if row_index is not None: index = row_index elif pgcname is not None: index = np.where(galaxy_cat['PGC'] == pgcname)[0][0] coords = SkyCoord(galaxy_cat['Coords'][index], frame='icrs') # The ellipse region uses the major and minor axes, so we have to multiply by # two first, before applying any user scaling. major = (galaxy_cat['SemiMajorAxis'][index] ) * np.float64(2.) * d25scale * u.arcmin minor = (galaxy_cat['SemiMinorAxis'][index] ) * np.float64(2.) * d25scale * u.arcmin pa = (galaxy_cat['PositionAngle'][index]) * u.deg ellipse_reg = EllipseSkyRegion(center=coords, height=major, width=minor, angle=pa) return ellipse_reg
def to_region(self, x_r_0=1, **kwargs): """Model outline at a given number of r_0. Parameters ---------- x_r_0 : float Number of r_0 (Default is 1). Returns ------- region : `~regions.EllipseSkyRegion` Model outline. """ minor_axis = Angle(self.r_0.quantity * np.sqrt(1 - self.e.quantity ** 2)) return EllipseSkyRegion( center=self.position, height=2 * x_r_0 * self.r_0.quantity, width=2 * x_r_0 * minor_axis, angle=self.phi.quantity, **kwargs, )
def to_region(self, x_sigma=1.5, **kwargs): """Model outline at a given number of :math:`\sigma`. Parameters ---------- x_sigma : float Number of :math:`\sigma` Default is :math:`1.5\sigma` which corresonds to about 68% containment for a 2D symetric Gaussian. Returns ------- region : `~regions.EllipseSkyRegion` Model outline. """ minor_axis = Angle(self.sigma.quantity * np.sqrt(1 - self.e.quantity ** 2)) return EllipseSkyRegion( center=self.position, height=2 * x_sigma * self.sigma.quantity, width=2 * x_sigma * minor_axis, angle=self.phi.quantity, **kwargs, )
def create_ellreg(t, index, d25scale=1.0): """ Creates an elliptical sky region from astropy.regions, with info from RC3 catalog t - a table of galaxy regions, similar to that found in read_rc3_tables index - the value of the table to be used scale - a scaling factor. Leaving at 1.0 means to use the D25 isophote, and it's likely we will want to scale this larger. """ coords = SkyCoord(t["Coords"][index], frame="icrs") # The ellipse region uses the major and minor axes, so we have to multiply by # two first, before applying any user scaling. major = (t["SemiMajorAxis"][index]) * np.float64(2.0) * d25scale * u.arcmin minor = (t["SemiMinorAxis"][index]) * np.float64(2.0) * d25scale * u.arcmin pa = (t["PositionAngle"][index]) * u.deg ellipse_reg = EllipseSkyRegion(center=coords, height=major, width=minor, angle=pa) return ellipse_reg
outer_height=0.6 * u.deg, angle=130 * u.deg, ) finder.region = on_ellipse_annulus finder.reference_map = None finder.run() regions = finder.reflected_regions assert len(regions) == 5 center = SkyCoord(0.5, 0.0, unit="deg") other_region_finder_param = [ (RectangleSkyRegion(center, 0.5 * u.deg, 0.5 * u.deg, angle=0 * u.deg), 3), (RectangleSkyRegion(center, 0.5 * u.deg, 1 * u.deg, angle=0 * u.deg), 1), (RectangleSkyRegion(center, 0.5 * u.deg, 1 * u.deg, angle=90 * u.deg), 0), (EllipseSkyRegion(center, 0.1 * u.deg, 1 * u.deg, angle=0 * u.deg), 2), (EllipseSkyRegion(center, 0.1 * u.deg, 1 * u.deg, angle=60 * u.deg), 3), (EllipseSkyRegion(center, 0.1 * u.deg, 1 * u.deg, angle=90 * u.deg), 0), ] @pytest.mark.parametrize("region, nreg", other_region_finder_param) def test_non_circular_regions(region, nreg): pointing = SkyCoord(0.0, 0.0, unit="deg") finder = ReflectedRegionsFinder(center=pointing, region=region, min_distance_input="0 deg") finder.run() regions = finder.reflected_regions assert len(regions) == nreg
''' from astropy.io import fits import astropy.units as u import astropy.constants as c from astropy.coordinates import Angle, SkyCoord from astropy.coordinates import ICRS from regions import EllipseSkyRegion from astropy.wcs import WCS import numpy as np # Use regions to get ellipse covering ONC onc_coords = SkyCoord('5h35m13.9s -5d22m51s',ICRS) a_maj = 310.*u.arcsec a_min = 210.*u.arcsec a_pa = 105.*u.deg ellipse_sky = EllipseSkyRegion(onc_coords,a_maj, a_min, angle=a_pa) # Use wcs from Orion A map to convert to pixels, and mask: OrionA_image = 'nh2_regridded/OrionA_NH2_regrid.fits' hdulist = fits.open(OrionA_image) wcs = WCS(hdulist[0].header) ellipse_pix = ellipse_sky.to_pixel(wcs) # First plot on Orion data to see if have the mask right. Looks good. fig, ax = plt.subplots(1,1) ax.imshow(np.log10(hdulist[0].data),origin='lower',cmap='Greys_r') patch = ellipse_pix.as_patch(facecolor='none',edgecolor='red',lw=2) ax.add_patch(patch) fig.savefig('figures/OrionA_mask.pdf') plt.close('all') # Then make mask. mask = ellipse_pix.to_mask()