def test_region(self): pos = SkyCoord(81, 21, unit='deg', frame='icrs') radius = Angle(1, 'deg') circ = CircleSkyRegion(pos, radius) idx = circ.contains(self.events.radec) table = self.events.table[idx] assert_allclose(table[4]['RA'], 81, rtol=1) assert_allclose(table[2]['DEC'], 21, rtol=1) assert len(table) == 5
def test_EventList_region(): filename = gammapy_extra.filename("test_datasets/unbundled/hess/run_0023037_hard_eventlist.fits.gz") event_list = EventList.read(filename, hdu="EVENTS") pos = SkyCoord(81, 21, unit="deg", frame="icrs") radius = Angle(1, "deg") circ = CircleSkyRegion(pos, radius) idx = circ.contains(event_list.radec) filtered_list = event_list[idx] assert_allclose(filtered_list[4]["RA"], 81, rtol=1) assert_allclose(filtered_list[2]["DEC"], 21, rtol=1) assert len(filtered_list) == 5
def test_EventList_region(): filename = gammapy_extra.filename( 'test_datasets/unbundled/hess/run_0023037_hard_eventlist.fits.gz') event_list = EventList.read(filename, hdu='EVENTS') pos = SkyCoord(81, 21, unit='deg', frame='icrs') radius = Angle(1, 'deg') circ = CircleSkyRegion(pos, radius) idx = circ.contains(event_list.radec) filtered_list = event_list[idx] assert_allclose(filtered_list[4]['RA'], 81, rtol=1) assert_allclose(filtered_list[2]['DEC'], 21, rtol=1) assert len(filtered_list) == 5
# We first create a circular region centered on a given SkyCoord with a given radius. # In[30]: from regions import CircleSkyRegion #center = SkyCoord.from_name("M31") center = SkyCoord(ra='0h42m44.31s', dec='41d16m09.4s') circle_region = CircleSkyRegion(center=center, radius=Angle(50, 'deg')) # We now use the contains method to search objects in this circular region in the sky. # In[31]: in_region = circle_region.contains(coord) # In[32]: fig = plt.figure(figsize=(14, 10)) ax = fig.add_subplot(111, projection="aitoff") ax.scatter(coord[in_region].l.radian, coord[in_region].b.radian) # ### Tables and pandas # # [pandas](http://pandas.pydata.org/) is one of the most-used packages in the scientific Python stack. Numpy provides the `ndarray` object and functions that operate on `ndarray` objects. Pandas provides the `Dataframe` and `Series` objects, which roughly correspond to the Astropy `Table` and `Column` objects. While both `pandas.Dataframe` and `astropy.table.Table` can often be used to work with tabular data, each has features that the other doesn't. When Astropy was started, it was decided to not base it on `pandas.Dataframe`, but to introduce `Table`, mainly because `pandas.Dataframe` doesn't support multi-dimensional columns, but FITS does and astronomers use sometimes. # # But `pandas.Dataframe` has a ton of features that `Table` doesn't, and is highly optimised, so if you find something to be hard with `Table`, you can convert it to a `Dataframe` and do your work there. As explained in the [interfacing with the pandas package](http://docs.astropy.org/en/stable/table/pandas.html) page in the Astropy docs, it is easy to go back and forth between Table and Dataframe: # # table = Table.from_pandas(dataframe) # dataframe = table.to_pandas()
def num_rings(t, index, tdetect, rings): """ Find the total number of HETDEX sources from tdetect inside a set of scaled ellipsoidal rings. Input t - the RC3 catalog table index - the index corresponding to the specific galaxy tdetect - the HETDEX ctalog rings = np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.5]) """ # Read in specific galaxy parameters from the table. c1 = SkyCoord(t["Coords"][index], frame="icrs") pgc = t["PGC"][index] name = t["Name1"][index] major = t["SemiMajorAxis"][index] * u.arcmin minor = t["SemiMinorAxis"][index] * u.arcmin pa = t["PositionAngle"][index] * u.deg source_scale = rings[-1] rlimit = major * source_scale nrings = len(rings) temprings = np.zeros(nrings) # Now, create a list of sources near the galaxy from the detect table. catalog = SkyCoord(tdetect["ra"], tdetect["dec"], unit=(u.deg, u.deg)) d2d = c1.separation(catalog) # Find all of separations catalogmsk = d2d < rlimit # Create Boolean mask of objects inside of this near_sources = catalog[catalogmsk] # Create fake WCS, centered on the galaxy mywcs = create_dummy_wcs(c1) # Loop over all but the last ring, and find the number of sources within each scaled ellipse for i in range(0, nrings - 1): scale = rings[i] ellipse = create_ellreg(t, index, d25scale=scale) nellipse = 0 for skycoord in near_sources: if ellipse.contains(skycoord, mywcs): nellipse += 1 temprings[i] = nellipse # Finally, for the last ring, find the number of sources within the large outer circle # Create the outerRing circle, which shows the radius that we searched to. ntotal = 0 outerRing = CircleSkyRegion(center=c1, radius=rlimit) for skycoord in near_sources: if outerRing.contains(skycoord, mywcs): ntotal += 1 temprings[-1] = ntotal outrings = temprings return outrings