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 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 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)
"""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
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