def test_fill_cube():
    filename = gammapy_extra.filename('test_datasets/background/bg_cube_model_test1.fits')
    array = Cube.read(filename, format='table', scheme='bg_cube')
    array.data = Quantity(np.zeros_like(array.data.value), 'u')
    print(type(array.data))

    dir = str(gammapy_extra.dir) + '/datasets/hess-crab4-hd-hap-prod2'
    data_store = DataStore.from_dir(dir)
    ev_list = data_store.load_all('events')

    array.fill_events(ev_list)

    array.write('test_background.fits', format='image', clobber=True)
def test_fill_cube():
    filename = gammapy_extra.filename(
        'test_datasets/background/bg_cube_model_test1.fits')
    array = Cube.read(filename, format='table', scheme='bg_cube')
    array.data = Quantity(np.zeros_like(array.data.value), 'u')
    print(type(array.data))

    dir = str(gammapy_extra.dir) + '/datasets/hess-crab4-hd-hap-prod2'
    data_store = DataStore.from_dir(dir)
    ev_list = data_store.load_all('events')

    array.fill_events(ev_list)

    array.write('test_background.fits', format='image', clobber=True)
Exemple #3
0
from gammapy.utils.fits import get_hdu
from gammapy.background import Cube

import matplotlib.pyplot as plt

irf_path = 'perf_prod2/South_5h/irf_file.fits.gz'

table_hdu_bg = get_hdu(irf_path + '[BACKGROUND]')
# Need to fix that
table_hdu_bg.header['TTYPE7'] = 'Bgd'
table_hdu_bg.header['TUNIT7'] = '1 / (MeV s sr)'
#

cube_bg = Cube.from_fits_table(table_hdu_bg, scheme='bg_cube')

### Bkg integrated on the entire FOV
bkg_rate_diff = cube_bg.integral_images
delta_energy = cube_bg.energy_edges[1:] - cube_bg.energy_edges[:-1]
bkg_rate = bkg_rate_diff * delta_energy.to('MeV')

plt.figure('bkg rate')
plt.plot(cube_bg.energy_edges.log_centers.to('TeV'),
         bkg_rate,
         'o',
         drawstyle='default')
plt.axis([1.e-2, 1.e3, 1.e-8, 1.e2])
plt.loglog()
plt.show(block=False)
"""Plot cube background model and store it in fits.

The 'image' format file can be viewed with ds9.
"""
import matplotlib.pyplot as plt
from astropy.units import Quantity
from astropy.coordinates import Angle
from gammapy.background import Cube
from gammapy import datasets

filename = datasets.get_path('../test_datasets/background/bg_cube_model_test.fits',
                             location='remote')
bg_cube_model = Cube.read(filename, format='table', scheme='bg_cube')

bg_cube_model.plot_image(energy=Quantity(2., 'TeV'))
bg_cube_model.plot_spectrum(coord=Angle([0., 0.], 'degree'))

outname = 'cube_background_model'
bg_cube_model.write('{}_bin_table.fits'.format(outname), format='table', clobber=True)
bg_cube_model.write('{}_image.fits'.format(outname), format='image', clobber=True)

plt.show()
Exemple #5
0
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from astropy.units import Quantity
from astropy.coordinates import Angle
from gammapy.background import Cube
from gammapy.datasets import gammapy_extra

filename = gammapy_extra.filename('test_datasets/background/bg_cube_model_test1.fits')
bg_cube_model = Cube.read(filename, format='table', scheme='bg_cube', hdu='BACKGROUND')

fig, axes = plt.subplots(nrows=1, ncols=3)
fig.set_size_inches(16, 5., forward=True)

# plot images
bg_cube_model.plot_image(energy=Quantity(0.5, 'TeV'), ax=axes[0],
                         style_kwargs=dict(norm=LogNorm()))
bg_cube_model.plot_image(energy=Quantity(50., 'TeV'), ax=axes[1],
                         style_kwargs=dict(norm=LogNorm()))

# plot spectra
bg_cube_model.plot_spectrum(coord=Angle([0., 0.], 'degree'), ax=axes[2],
                            style_kwargs=dict(label='(0, 0) deg'))
bg_cube_model.plot_spectrum(coord=Angle([2., 2.], 'degree'), ax=axes[2],
                            style_kwargs=dict(label='(2, 2) deg'))
axes[2].set_title('')
axes[2].legend()

plt.tight_layout()
plt.show()
Exemple #6
0
from gammapy.utils.fits import get_hdu
from gammapy.background import Cube

import matplotlib.pyplot as plt

irf_path = 'perf_prod2/South_5h/irf_file.fits.gz'

table_hdu_bg = get_hdu(irf_path + '[BACKGROUND]')
# Need to fix that
table_hdu_bg.header['TTYPE7'] = 'Bgd'
table_hdu_bg.header['TUNIT7'] = '1 / (MeV s sr)'
#

cube_bg = Cube.from_fits_table(table_hdu_bg, scheme='bg_cube')

### Bkg integrated on the entire FOV
bkg_rate_diff = cube_bg.integral_images
delta_energy = cube_bg.energy_edges[1:] - cube_bg.energy_edges[:-1]
bkg_rate = bkg_rate_diff * delta_energy.to('MeV')

plt.figure('bkg rate')
plt.plot(cube_bg.energy_edges.log_centers.to('TeV'),
         bkg_rate, 'o', drawstyle='default')
plt.axis([1.e-2, 1.e3, 1.e-8, 1.e2])
plt.loglog()
plt.show(block=False)

Exemple #7
0
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from astropy.units import Quantity
from astropy.coordinates import Angle
from gammapy.background import Cube
from gammapy import datasets

filename = datasets.get_path(
    '../test_datasets/background/bg_cube_model_test1.fits', location='remote')
bg_cube_model = Cube.read(filename, format='table', scheme='bg_cube')

fig, axes = plt.subplots(nrows=1, ncols=3)
fig.set_size_inches(16, 5., forward=True)

# plot images
bg_cube_model.plot_image(energy=Quantity(0.5, 'TeV'),
                         ax=axes[0],
                         style_kwargs=dict(norm=LogNorm()))
bg_cube_model.plot_image(energy=Quantity(50., 'TeV'),
                         ax=axes[1],
                         style_kwargs=dict(norm=LogNorm()))

# plot spectra
bg_cube_model.plot_spectrum(coord=Angle([0., 0.], 'degree'),
                            ax=axes[2],
                            style_kwargs=dict(label='(0, 0) deg'))
bg_cube_model.plot_spectrum(coord=Angle([2., 2.], 'degree'),
                            ax=axes[2],
                            style_kwargs=dict(label='(2, 2) deg'))
axes[2].set_title('')
axes[2].legend()