def check_if_flux(ra, dec, radius, catalog=cat_250um): """ Returns flux density in the unit used for the catalog (Jy in the case of the HELMS maps) Returns 'Outside' if the area is outside the image. ra: float Right ascension of the object (degrees). dec: float Declination of the object (degrees). radius: float The radius of the galaxy (degrees). catalog: cat_250um, cat_350um, cat_500um The catalog used in the search. All variables are defined above. """ center = SkyCoord(ra, dec, unit='deg') radius = Quantity(radius, 'deg') region = CircleSkyRegion(center, radius) pixel_region = region.to_pixel(pixel_convert) if pixel_region.radius > 500: pixel_region.radius = 20 mask = pixel_region.to_mask(mode='exact') data = mask.cutout(img_dat) if data is not None: flux_density = max_list_of_lists(data) else: flux_density = 'Outside' return flux_density
def get_flux_density(ra, dec, radius, catalog=cat_250um): """ Returns flux density in the unit used for the catalog (Jy in the case of the HELMS maps) ra: float Right ascension of the object (degrees). dec: float Declination of the object (degrees). radius: float The radius of the galaxy (degrees). catalog: cat_250um, cat_350um, cat_500um The catalog used in the search. All variables are defined above. """ hdul = fits.open(catalog) img_dat = hdul[1].data pixel_convert = wcs.WCS(hdul[1]) center = SkyCoord(ra, dec, unit='deg') radius = Angle(radius, 'deg') region = CircleSkyRegion(center, radius) pixel_region = region.to_pixel(pixel_convert) if pixel_region.radius > 500: pixel_region.radius = 50 mask = pixel_region.to_mask(mode='exact') data = mask.cutout(img_dat) flux_density = max_list_of_lists(data) return flux_density
# In[ ]: jfactory = JFactory(geom=geom, profile=profile, distance=profiles.DMProfile.DISTANCE_GC) jfact = jfactory.compute_jfactor() # In[ ]: jfact_map = WcsNDMap(geom=geom, data=jfact.value, unit=jfact.unit) fig, ax, im = jfact_map.plot(cmap="viridis", norm=LogNorm(), add_cbar=True) plt.title("J-Factor [{}]".format(jfact_map.unit)) # 1 deg circle usually used in H.E.S.S. analyses sky_reg = CircleSkyRegion(center=position, radius=1 * u.deg) pix_reg = sky_reg.to_pixel(wcs=geom.wcs) pix_reg.plot(ax=ax, facecolor="none", edgecolor="red", label="1 deg circle") plt.legend() # In[ ]: # NOTE: https://arxiv.org/abs/1607.08142 quote 2.67e21 without the +/- 0.3 deg band around the plane total_jfact = pix_reg.to_mask().multiply(jfact).sum() print("J-factor in 1 deg circle around GC assuming a " "{} is {:.3g}".format(profile.__class__.__name__, total_jfact)) # ## Gamma-ray spectra at production # # The gamma-ray spectrum per annihilation is a further ingredient for a dark matter analysis. The following annihilation channels are supported. For more info see https://arxiv.org/pdf/1012.4515.pdf # In[ ]:
# get events in AND and XOR compound_and = circle1 & circle2 compound_xor = circle1 ^ circle2 mask_and = compound_and.contains(skycoords, wcs) skycoords_and = skycoords[mask_and] mask_xor = compound_xor.contains(skycoords, wcs) skycoords_xor = skycoords[mask_xor] # plot fig = plt.figure() ax = fig.add_axes([0.15, 0.1, 0.8, 0.8], projection=wcs, aspect='equal') ax.scatter(skycoords.l.value, skycoords.b.value, label='all', transform=ax.get_transform('galactic')) ax.scatter(skycoords_xor.l.value, skycoords_xor.b.value, color='orange', label='xor', transform=ax.get_transform('galactic')) ax.scatter(skycoords_and.l.value, skycoords_and.b.value, color='magenta', label='and', transform=ax.get_transform('galactic')) circle1.to_pixel(wcs=wcs).plot(ax=ax, edgecolor='green', facecolor='none', alpha=0.8, lw=3) circle2.to_pixel(wcs=wcs).plot(ax=ax, edgecolor='red', facecolor='none', alpha=0.8, lw=3) ax.legend(loc='lower right') ax.set_xlim(-0.5, dataset.config['shape'][1] - 0.5) ax.set_ylim(-0.5, dataset.config['shape'][0] - 0.5) plt.show()
from astropy.coordinates import SkyCoord, Angle from regions import ( make_example_dataset, CircleSkyRegion, ) import matplotlib.pyplot as plt config = dict(crpix=(18, 9), cdelt=(-10, 10), shape=(18, 36)) dataset = make_example_dataset(data='simulated', config=config) wcs = dataset.wcs fig = plt.figure() ax = fig.add_axes([0.15, 0.1, 0.8, 0.8], projection=wcs) ax.set_xlim(-0.5, dataset.config['shape'][1] - 0.5) ax.set_ylim(-0.5, dataset.config['shape'][0] - 0.5) ax.imshow(dataset.image.data, cmap='gray', vmin=0, vmax=1, interpolation='nearest', origin='lower') for source in dataset.source_table: # Plot a sky circle around each source center = SkyCoord(source['GLON'], source['GLAT'], unit='deg', frame='galactic') radius = Angle(20, 'deg') region = CircleSkyRegion(center=center, radius=radius) pix_region = region.to_pixel(wcs=wcs) pix_region.plot(ax=ax, edgecolor='yellow', facecolor='yellow', alpha=0.5, lw=3) plt.show()
bkg_maker = ReflectedRegionsBackgroundMaker(exclusion_mask=exclusion_mask) safe_mask_masker = SafeMaskMaker(methods=["aeff-max"], aeff_percent=10) # In[ ]: get_ipython().run_cell_magic( 'time', '', 'datasets = []\n\nfor observation in observations:\n dataset = dataset_maker.run(dataset_empty, observation)\n dataset_on_off = bkg_maker.run(dataset, observation)\n dataset_on_off = safe_mask_masker.run(dataset_on_off, observation)\n datasets.append(dataset_on_off)' ) # In[ ]: plt.figure(figsize=(8, 8)) _, ax, _ = images["counts"].smooth("0.03 deg").plot(vmax=8) on_region.to_pixel(ax.wcs).plot(ax=ax, edgecolor="white") plot_spectrum_datasets_off_regions(datasets, ax=ax) # ### Model fit # # The next step is to fit a spectral model, using all data (i.e. a "global" fit, using all energies). # In[ ]: get_ipython().run_cell_magic( 'time', '', 'spectral_model = PowerLawSpectralModel(\n index=2, amplitude=1e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV\n)\nmodel = SkyModel(spectral_model=spectral_model)\nfor dataset in datasets:\n dataset.models = model\n\nfit = Fit(datasets)\nresult = fit.run()\nprint(result)' ) # ### Spectral points #
on_ellipse_annulus = EllipseAnnulusSkyRegion( center=position, inner_width=1.5 * u.deg, outer_width=2.5 * u.deg, inner_height=3 * u.deg, outer_height=4 * u.deg, angle=130 * u.deg, ) another_position = SkyCoord(80.3, 22.0, unit="deg") on_rectangle = RectangleSkyRegion(center=another_position, width=2.0 * u.deg, height=4.0 * u.deg, angle=50 * u.deg) # Now we plot those regions. We first create an empty map empty_map = WcsNDMap.create(skydir=position, width=10 * u.deg, binsz=0.1 * u.deg, proj="TAN") empty_map.data += 1.0 empty_map.plot(cmap="gray", vmin=0, vmax=1) # To plot the regions, we convert them to PixelRegion with the map wcs on_circle.to_pixel(empty_map.geom.wcs).plot() on_rectangle.to_pixel(empty_map.geom.wcs).plot() on_ellipse_annulus.to_pixel(empty_map.geom.wcs).plot() plt.show()
ax = fig.add_subplot(111, projection=wcs) #ax.set_title("Gaussian smoothed image") norm_xmm = ImageNormalize(smoothed_data_g2, interval=ManualInterval(vmin=0.01, vmax=100.0), stretch=LogStretch()) # norm_xmm = ImageNormalize(smoothed_data_g2,interval=PercentileInterval(pp), stretch=AsinhStretch()) ax.imshow(smoothed_data_g2, cmap=plt.cm.hot, norm=norm_xmm, origin='lower', interpolation='nearest') ax.xlabel = 'RA' ax.ylabel = 'Dec' ax.set_xlabel('RA') ax.set_ylabel('DEC') # # only show the last aperture # circle_sky = CircleSkyRegion(center=center, radius=r_end) pix_reg = circle_sky.to_pixel(wcs) pix_reg.plot(ax=ax, edgecolor='yellow') plt.savefig('/home/aaranda/tfm/results_v2/{}/{}/smoothed_g2_image_low.png'.format(target, obsid)) plt.close(fig) (x, y, yerr) = calc_radial_profile(fits_image, center, r_start, r_end, r_step, verbose=False, detmaskfile=detmask_file, plot=False) energy = 1000 # at 1 keV box = 2 * int(r_end.to(u.arcsec).value) + 1 psf_out = '{}/psf_ellbeta_{}_{}pix.fits'.format(odf_dir, energy, box) psf_gen(center, energy, box, psf_out) fig = plt.figure(figsize=(10, 10), dpi=100) pp = 99.9 # colour cut percentage
fig = plt.figure() ax = fig.add_axes([0.15, 0.1, 0.8, 0.8], projection=wcs) ax.set_xlim(-0.5, dataset.config['shape'][1] - 0.5) ax.set_ylim(-0.5, dataset.config['shape'][0] - 0.5) ax.imshow(dataset.image.data, cmap='gray', vmin=0, vmax=1, interpolation='nearest', origin='lower') for source in dataset.source_table: # Plot a sky circle around each source center = SkyCoord(source['GLON'], source['GLAT'], unit='deg', frame='galactic') radius = Angle(20, 'deg') region = CircleSkyRegion(center=center, radius=radius) pix_region = region.to_pixel(wcs=wcs) pix_region.plot(ax, edgecolor='yellow', facecolor='yellow', alpha=0.5, lw=3) plt.show()
# get events in AND and XOR compound_and = circle1 & circle2 compound_xor = circle1 ^ circle2 mask_and = compound_and.contains(skycoords, wcs) skycoords_and = skycoords[mask_and] mask_xor = compound_xor.contains(skycoords, wcs) skycoords_xor = skycoords[mask_xor] # plot fig = plt.figure() ax = fig.add_axes([0.15, 0.1, 0.8, 0.8], projection=wcs, aspect='equal') ax.scatter(skycoords.l.value, skycoords.b.value, label='all', transform=ax.get_transform('galactic')) ax.scatter(skycoords_xor.l.value, skycoords_xor.b.value, color='orange', label='xor', transform=ax.get_transform('galactic')) ax.scatter(skycoords_and.l.value, skycoords_and.b.value, color='magenta', label='and', transform=ax.get_transform('galactic')) circle1.to_pixel(wcs=wcs).plot(ax, edgecolor='green', facecolor='none', alpha=0.8, lw=3) circle2.to_pixel(wcs=wcs).plot(ax, edgecolor='red', facecolor='none', alpha=0.8, lw=3) ax.legend(loc='lower right') ax.set_xlim(-0.5, dataset.config['shape'][1] - 0.5) ax.set_ylim(-0.5, dataset.config['shape'][0] - 0.5) plt.show()
"""Example how to compute and plot reflected regions.""" from astropy.coordinates import SkyCoord, Angle from regions import CircleSkyRegion from gammapy.image import SkyMask from gammapy.background import find_reflected_regions mask = SkyMask.empty(name='Exclusion Mask', nxpix=801, nypix=701, binsz=0.01, coordsys='CEL', xref=83.2, yref=22.7) mask.fill_random_circles(n=8, min_rad=30, max_rad=80) pos = SkyCoord(80.2, 23.5, unit='deg') radius = Angle(0.4, 'deg') test_region = CircleSkyRegion(pos, radius) center = SkyCoord(82.8, 22.5, unit='deg') regions = find_reflected_regions(test_region, center, mask) import matplotlib.pyplot as plt fig = plt.figure(figsize=(8, 5), dpi=80) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], projection=mask.wcs) mask.plot(ax, fig) for reg in regions: reg.to_pixel(mask.wcs).plot(ax, facecolor='red') test_region.to_pixel(mask.wcs).plot(ax, facecolor='blue') plt.show()