def create_gammapy_skymap(smooth=3): """ Parameters ---------- smooth : 3 Gaussian smoothing width in pixel. """ # create logo cutout filename = os.environ.get('GAMMAPY_EXTRA') + '/logo/gammapy_logo.png' gammapy_logo = (imread(filename, flatten=True) > 1).astype('float') shape = gammapy_logo.shape gammapy_cutout = SkyMap.empty(nxpix=shape[1], nypix=shape[0], binsz=0.02) # flip upside down gammapy_cutout.data = np.flipud(gammapy_logo) # reproject to larger sky map gammapy_skymap = SkyMap.empty(nxpix=320, nypix=180, binsz=0.08) gammapy_skymap = gammapy_cutout.reproject(gammapy_skymap) gammapy_skymap.name = 'gp_logo' gammapy_skymap.data = np.nan_to_num(gammapy_skymap.data) gammapy_skymap.data = gaussian_filter(gammapy_skymap.data, smooth) # normalize gammapy_skymap.data /= gammapy_skymap.data.sum() return gammapy_skymap
def background_skyimage_2fhl(counts): log.info('Computing background map.') images = GammaImages(counts.data, header=counts.wcs.to_header()) source_kernel = Tophat2DKernel(5) source_kernel.normalize('peak') background_kernel = Ring2DKernel(20, 20) background_kernel.normalize('peak') ikbe = IKBE( images=images, source_kernel=source_kernel.array, background_kernel=background_kernel.array, significance_threshold=5, mask_dilation_radius=3, ) mask_data, background_data = ikbe.run() mask = SkyMap.empty_like(counts) mask.data = mask_data background = SkyMap.empty_like(counts) background.data = background_data return mask, background
def make_image(): table = Table.read('acceptance_curve.fits') table.pprint() center = SkyCoord(83.63, 22.01, unit='deg').galactic counts_image = SkyMap.empty(nxpix=1000, nypix=1000, binsz=0.01, xref=center.l.deg, yref=center.b.deg, proj='TAN').to_image_hdu() bkg_image = counts_image.copy() data_store = DataStore.from_dir( '$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2') for events in data_store.load_all("events"): center = events.pointing_radec.galactic livetime = events.observation_live_time_duration solid_angle = Angle(0.01, "deg")**2 counts_image.data += bin_events_in_image(events, counts_image).data # interp_param = dict(bounds_error=False, fill_value=None) acc_hdu = fill_acceptance_image(bkg_image.header, center, table["offset"], table["Acceptance"]) acc = Quantity(acc_hdu.data, table["Acceptance"].unit) * solid_angle * livetime bkg_image.data += acc.decompose() print(acc.decompose().sum()) counts_image.writeto("counts_image_save.fits", clobber=True) bkg_image.writeto("bkg_image_save.fits", clobber=True)
def make_image_from_2d_bg(): center = SkyCoord(83.63, 22.01, unit='deg').galactic energy_band = Energy([1, 10], 'TeV') offset_band = Angle([0, 2.49], 'deg') data_store = DataStore.from_dir( '/Users/jouvin/Desktop/these/temp/bg_model_image/') # TODO: fix `binarize` implementation # exclusion_mask = exclusion_mask.binarize() image = SkyMap.empty(nxpix=250, nypix=250, binsz=0.02, xref=center.l.deg, yref=center.b.deg, proj='TAN', coordsys='GAL') refheader = image.to_image_hdu().header exclusion_mask = ExclusionMask.read( '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits') exclusion_mask = exclusion_mask.reproject(reference=refheader) mosaic_images = MosaicImage(image, energy_band=energy_band, offset_band=offset_band, data_store=data_store, obs_table=data_store.obs_table, exclusion_mask=exclusion_mask) mosaic_images.make_images(make_background_image=True, for_integral_flux=True, radius=10.) filename = 'fov_bg_maps_test.fits' log.info('Writing {}'.format(filename)) mosaic_images.maps.write(filename, clobber=True)
def make_image(): table = Table.read('acceptance_curve.fits') table.pprint() center = SkyCoord(83.63, 22.01, unit='deg').galactic counts_image = SkyMap.empty(nxpix=1000, nypix=1000, binsz=0.01, xref=center.l.deg, yref=center.b.deg, proj='TAN').to_image_hdu() bkg_image = counts_image.copy() data_store = DataStore.from_dir('$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2') for events in data_store.load_all("events"): center = events.pointing_radec.galactic livetime = events.observation_live_time_duration solid_angle = Angle(0.01, "deg") ** 2 counts_image.data += bin_events_in_image(events, counts_image).data # interp_param = dict(bounds_error=False, fill_value=None) acc_hdu = fill_acceptance_image(bkg_image.header, center, table["offset"], table["Acceptance"]) acc = Quantity(acc_hdu.data, table["Acceptance"].unit) * solid_angle * livetime bkg_image.data += acc.decompose() print(acc.decompose().sum()) counts_image.writeto("counts_image.fits", clobber=True) bkg_image.writeto("bkg_image.fits", clobber=True)
def image_coordinates(infile, outfile, make_coordinate_maps, make_distance_map, overwrite): """Make maps that can be used to create profiles. The following images can be created: * LON -- Longitude coordinate * LAT -- Latitude coordinate * DIST -- Distance to mask * SOLID_ANGLE -- Solid angle """ from astropy.io import fits from gammapy.utils.fits import get_hdu from gammapy.image import SkyMap, ExclusionMask log.info('Reading {0}'.format(infile)) hdu = get_hdu(infile) out_hdus = fits.HDUList() if make_coordinate_maps: skymap = SkyMap.empty_like(hdu) log.info('Computing LON and LAT maps') lon, lat = skymap.coordinates() out_hdus.append(fits.ImageHDU(lon, hdu.header, 'LON')) out_hdus.append(fits.ImageHDU(lat, hdu.header, 'LAT')) if make_distance_map: excl = ExclusionMask.from_image_hdu(hdu) log.info('Computing DIST map') dist = excl.exclusion_distance out_hdus.append(fits.ImageHDU(dist, hdu.header, 'DIST')) log.info('Writing {0}'.format(outfile)) out_hdus.writeto(outfile, clobber=overwrite)
def test_image_pipe(tmpdir): tmpdir = str(tmpdir) from subprocess import call outdir = tmpdir outdir2 = outdir + '/background' cmd = 'mkdir -p {}'.format(outdir2) print('Executing: {}'.format(cmd)) call(cmd, shell=True) ds = DataStore.from_dir("$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2") ds.copy_obs(ds.obs_table, tmpdir) data_store = DataStore.from_dir(tmpdir) bgmaker = OffDataBackgroundMaker(data_store, outdir=outdir2) bgmaker.select_observations(selection='all') bgmaker.group_observations() bgmaker.make_model("2D") bgmaker.save_models("2D") fn = outdir2 + '/group-def.fits' hdu_index_table = bgmaker.make_total_index_table( data_store=data_store, modeltype='2D', out_dir_background_model=outdir2, filename_obs_group_table=fn ) fn = outdir + '/hdu-index.fits.gz' hdu_index_table.write(fn, overwrite=True) tmpdir = str(tmpdir) center = SkyCoord(83.63, 22.01, unit='deg').galactic energy_band = Energy([1, 10], 'TeV') offset_band = Angle([0, 2.49], 'deg') data_store = DataStore.from_dir(tmpdir) # TODO: fix `binarize` implementation # exclusion_mask = exclusion_mask.binarize() image = SkyMap.empty(nxpix=250, nypix=250, binsz=0.02, xref=center.l.deg, yref=center.b.deg, proj='TAN', coordsys='GAL') refheader = image.to_image_hdu().header exclusion_mask = ExclusionMask.read('$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits') exclusion_mask = exclusion_mask.reproject(reference=refheader) images = ImageAnalysis(image, energy_band=energy_band, offset_band=offset_band, data_store=data_store, obs_table=data_store.obs_table, exclusion_mask=exclusion_mask) images.make_maps(radius=10., bkg_norm=True, spectral_index=2.3, for_integral_flux=True)
def make_image_from_2d_bg(): center = SkyCoord(83.63, 22.01, unit='deg').galactic energy_band = Energy([1, 10], 'TeV') offset_band = Angle([0, 2.49], 'deg') data_store = DataStore.from_dir('/Users/jouvin/Desktop/these/temp/bg_model_image/') # TODO: fix `binarize` implementation # exclusion_mask = exclusion_mask.binarize() image = SkyMap.empty(nxpix=250, nypix=250, binsz=0.02, xref=center.l.deg, yref=center.b.deg, proj='TAN', coordsys='GAL') refheader = image.to_image_hdu().header exclusion_mask = ExclusionMask.read('$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits') exclusion_mask = exclusion_mask.reproject(reference=refheader) images = ImageAnalysis(image, energy_band=energy_band, offset_band=offset_band, data_store=data_store, obs_table=data_store.obs_table, exclusion_mask=exclusion_mask) images.make_maps(radius=10., bkg_norm=True, spectral_index=2.3, for_integral_flux = True) # images.make_total_exposure() filename = 'fov_bg_maps.fits' log.info('Writing {}'.format(filename)) images.maps.write(filename, clobber=True)
def image_coordinates(infile, outfile, make_coordinate_maps, make_distance_map, overwrite): """Make maps that can be used to create profiles. The following images can be created: * LON -- Longitude coordinate * LAT -- Latitude coordinate * DIST -- Distance to mask * SOLID_ANGLE -- Solid angle """ from astropy.io import fits from gammapy.utils.fits import get_hdu from gammapy.image import SkyMap, ExclusionMask log.info('Reading {0}'.format(infile)) hdu = get_hdu(infile) out_hdus = fits.HDUList() if make_coordinate_maps: skymap = SkyMap.empty_like(hdu) log.info('Computing LON and LAT maps') lon, lat = skymap.coordinates() out_hdus.append(fits.ImageHDU(lon, hdu.header, 'LON')) out_hdus.append(fits.ImageHDU(lat, hdu.header, 'LAT')) if make_distance_map: excl = ExclusionMask.read(hdu) log.info('Computing DIST map') dist = excl.exclusion_distance out_hdus.append(fits.ImageHDU(dist, hdu.header, 'DIST')) log.info('Writing {0}'.format(outfile)) out_hdus.writeto(outfile, clobber=overwrite)
mygaus_G0p9.xpos, mygaus_G0p9.ypos = skycoord_to_pixel(source_center_G0p9, on.wcs) mygaus_G0p9.xpos.val += 0.5 mygaus_G0p9.ypos.val += 0.5 #Arc_source arc_source=SkyCoord(0.130, -0.139, unit='deg', frame="galactic") mygaus_arcsource = Gauss2D("Arc Source") mygaus_arcsource.xpos, mygaus_arcsource.ypos = skycoord_to_pixel(arc_source, on.wcs) # Modele pour le bkg base sur la carte de fond load_table_model("bkg", "off_maps" + str(E1) + "_" + str(E2) + "_TeV.fits") bkg.ampl = 1 load_table_model("exposure", "exp_maps" + str(E1) + "_" + str(E2) + "_TeV.fits") exposure.ampl = 1 # Modele de CS CS_map = SkyMap.read("CStot.fits") cs_reproj = CS_map.reproject(on) cs_reproj.data[np.where(np.isnan(cs_reproj.data))] = 0 cs_reproj.data[np.where(cs_reproj.data < 30)] = 0 cs_reproj.write("cs_map_reproj.fits", clobber=True) load_table_model("CS", "cs_map_reproj.fits") #set_full_model(bkg + psf_SgrA(mygaus_SgrA)+psf_G0p9(mygaus_G0p9)) #set_full_model(bkg + psf_SgrA(exposure*mygaus_SgrA)+psf_G0p9(exposure*mygaus_G0p9)) #set_full_model(bkg + psf_SgrA(exposure*(mygaus_SgrA+large_gaus * CS))+psf_G0p9(exposure*mygaus_G0p9)) set_full_model(bkg + psf_SgrA(mygaus_SgrA + large_gaus * CS) + psf_G0p9(mygaus_G0p9)) #set_full_model(bkg + psf_SgrA(mygaus_SgrA + large_gaus * CS + central_gauss) + psf_G0p9(mygaus_G0p9)) #set_full_model(bkg + psf_SgrA(mygaus_SgrA + large_gaus * CS + mygaus_arcsource) + psf_G0p9(mygaus_G0p9)) mygaus_SgrA.fwhm = 1 freeze(mygaus_SgrA.fwhm) mygaus_G0p9.fwhm = 1
def counts_skyimage_2fhl(**kwargs): log.info('Computing counts map.') events = EventList.read('2fhl_events.fits.gz') counts = SkyMap.empty('Counts', **kwargs) counts.fill(events) return counts
"""Simulates a galaxy of point sources and produces an image. """ import numpy as np import matplotlib.pyplot as plt import astropy.units as u from aplpy import FITSFigure from gammapy.astro import population from gammapy.datasets import FermiGalacticCenter from gammapy.image import SkyMap, catalog_image from gammapy.irf import EnergyDependentTablePSF from gammapy.utils.random import sample_powerlaw # Create image of defined size reference = SkyMap.empty(nxpix=300, nypix=100, binsz=1).to_image_hdu() psf_file = FermiGalacticCenter.filenames()['psf'] psf = EnergyDependentTablePSF.read(psf_file) # Simulation Parameters # source density at the sun (sources kpc^-1) rho_sun = 3 # number of sources n_sources = int(5e2) # Spatial distribution using Lorimer (2006) model rad_dis = 'L06' # Velocity dispersion vel_dis = 'F06B' # Includes spiral arms spiralarms = True # Creates table
def test_image_pipe(tmpdir): tmpdir = str(tmpdir) from subprocess import call outdir = tmpdir outdir2 = outdir + '/background' cmd = 'mkdir -p {}'.format(outdir2) print('Executing: {}'.format(cmd)) call(cmd, shell=True) ds = DataStore.from_dir("$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2") ds.copy_obs(ds.obs_table, tmpdir) data_store = DataStore.from_dir(tmpdir) bgmaker = OffDataBackgroundMaker(data_store, outdir=outdir2) bgmaker.select_observations(selection='all') bgmaker.group_observations() bgmaker.make_model("2D") bgmaker.save_models("2D") fn = outdir2 + '/group-def.fits' hdu_index_table = bgmaker.make_total_index_table( data_store=data_store, modeltype='2D', out_dir_background_model=outdir2, filename_obs_group_table=fn) fn = outdir + '/hdu-index.fits.gz' hdu_index_table.write(fn, overwrite=True) tmpdir = str(tmpdir) center = SkyCoord(83.63, 22.01, unit='deg').galactic energy_band = Energy([1, 10], 'TeV') offset_band = Angle([0, 2.49], 'deg') data_store = DataStore.from_dir(tmpdir) # TODO: fix `binarize` implementation # exclusion_mask = exclusion_mask.binarize() image = SkyMap.empty(nxpix=250, nypix=250, binsz=0.02, xref=center.l.deg, yref=center.b.deg, proj='TAN', coordsys='GAL') refheader = image.to_image_hdu().header exclusion_mask = ExclusionMask.read( '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits') exclusion_mask = exclusion_mask.reproject(reference=refheader) images = ImageAnalysis(image, energy_band=energy_band, offset_band=offset_band, data_store=data_store, obs_table=data_store.obs_table, exclusion_mask=exclusion_mask) images.make_maps(radius=10., bkg_norm=True, spectral_index=2.3, for_integral_flux=True)
def counts_skyimage_2fhl(**kwargs): log.info("Computing counts map.") events = EventList.read("2fhl_events.fits.gz") counts = SkyMap.empty("Counts", **kwargs) counts.fill(events) return counts