예제 #1
0
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 SkyImage, SkyMask

    log.info('Reading {0}'.format(infile))
    hdu = get_hdu(infile)

    out_hdus = fits.HDUList()

    if make_coordinate_maps:
        image = SkyImage.empty_like(hdu)
        log.info('Computing LON and LAT maps')
        lon, lat = image.coordinates()
        out_hdus.append(fits.ImageHDU(lon, hdu.header, 'LON'))
        out_hdus.append(fits.ImageHDU(lat, hdu.header, 'LAT'))

    if make_distance_map:
        excl = SkyMask.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)
예제 #2
0
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 = SkyImage.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 = SkyMask.read(
        '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits')
    exclusion_mask = exclusion_mask.reproject(reference=refheader)
    mosaic_images = StackedObsImageMaker(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.,
                              make_psf=True,
                              region_center=center)
    filename = 'fov_bg_images.fits'
    log.info('Writing {}'.format(filename))
    mosaic_images.images.write(filename, clobber=True)
예제 #3
0
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 = SkyImage.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 = SkyMask.read('$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits')
    exclusion_mask = exclusion_mask.reproject(reference=refheader)
    # Pb with the load psftable for one of the run that is not implemented yet...
    data_store.hdu_table.remove_row(14)
    mosaic = 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.make_images(make_background_image=True, for_integral_flux=True, radius=10.)
    assert_allclose(mosaic.maps['counts'].data.sum(), 2334.0, atol=3)
    assert_allclose(mosaic.maps['bkg'].data.sum(), 1987.1513636663785, atol=3)
    assert_allclose(mosaic.maps['exposure'].data.sum(), 54190569251987.68, atol=3)
    assert_allclose(mosaic.maps['significance'].lookup(center), 33.707901541600634, atol=3)
    assert_allclose(mosaic.maps['excess'].data.sum(), 346.8486363336217, atol=3)
예제 #4
0
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 = SkyImage.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 = SkyMask.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., make_psf = True,
                              region_center=center)
    filename = 'fov_bg_maps.fits'
    log.info('Writing {}'.format(filename))
    mosaic_images.maps.write(filename, clobber=True)
예제 #5
0
"""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.plot(ax, facecolor='red')
test_region.plot(ax, facecolor='blue')

plt.show()
예제 #6
0
"""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.plot(ax, facecolor='red')
test_region.plot(ax, facecolor='blue')

plt.show()
#name_source = "J1813"
#name_source = "RXJ1713"
# name_source = "G21p5"
# name_source = "2155"

#name_method_fond = "coszenbinning_zen_0_34_49_61_72_eff"
# name_method_fond = "coszenbinning_zen_0_27_39_49_57_65_72_15binE"
name_method_fond = "coszenbinning_zen_0_34_49_61_72_sansLMC"

config_name = "Mpp_Std"
outdir_data = make_outdir_data(name_source, name_method_fond, len(energy_bins),
                               config_name)
outdir_plot = make_outdir_plot(name_source, name_method_fond, len(energy_bins),
                               config_name)

exclusion_mask = SkyMask.read('tevcat_exclusion_radius_0p5.fits')
#exclusion_mask = SkyMask.read('exclusion_large.fits')
for i_E, E in enumerate(energy_bins[:-2]):
    E1 = energy_bins[i_E].value
    E2 = energy_bins[i_E + 1].value
    significance_map = SkyImageList.read(outdir_data + "/fov_bg_maps" +
                                         str(E1) + "_" + str(E2) +
                                         "_TeV.fits")["significance"]
    #coord=significance_map.coordinates()
    #center=significance_map.center
    #offset=center.separation(coord)
    #i=np.where(offset>Angle(2,"deg"))
    #significance_map.data[i]=-1000
    refheader = significance_map.to_image_hdu().header
    exclusion_mask = exclusion_mask.reproject(reference=refheader)
    pt.figure(i_E)
예제 #8
0
            add_bkgmodel_to_indextable(bkg_model_directory, source_name,
                                       obsdir)

    # Make the images and psf model for different energy bands
    #Energy binning
    energy_reco_bins = EnergyBounds.equal_log_spacing(
        input_param["energy binning"]["Emin"],
        input_param["energy binning"]["Emax"],
        input_param["energy binning"]["nbin"], 'TeV')

    outdir = make_outdir(source_name, name_bkg, len(energy_reco_bins),
                         config_name, image_size, for_integral_flux, use_cube,
                         use_etrue)
    offset_band = Angle([0, 2.49], 'deg')
    data_store = DataStore.from_dir(obsdir)
    exclusion_mask = SkyMask.read(input_param["general"]["exclusion_mask"])
    obs_table_subset = data_store.obs_table[0:nobs]
    # Cette observetion n est pas utilise par gammapy car cette observation est centre sur le Crab donc ne peut pas trouve de reflected region...
    if ((source_name == "Crab") | (source_name == "Crab_Test")):
        i_remove = np.where(obs_table_subset["OBS_ID"] == 18373)
        if len(i_remove[0]) != 0:
            obs_table_subset.remove_row(i_remove[0][0])

    if use_cube:
        energy_reco = [
            Energy(input_param["energy binning"]["Emin"], "TeV"),
            Energy(input_param["energy binning"]["Emax"], "TeV"),
            input_param["energy binning"]["nbin"]
        ]
        if use_etrue:
            energy_true = [
예제 #9
0
def make_cubes(ereco, etrue, use_etrue, center):
    tmpdir = os.path.expandvars('$GAMMAPY_EXTRA') + "/test_datasets/cube/data"
    outdir = tmpdir
    outdir2 = os.path.expandvars(
        '$GAMMAPY_EXTRA') + '/test_datasets/cube/background'

    if os.path.isdir("data"):
        shutil.rmtree("data")
    if os.path.isdir("background"):
        shutil.rmtree("background")
    Path(outdir2).mkdir()

    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)
    # Create a background model from the 4 crab run for the counts ouside the exclusion region. it's just for test, normaly you take 8000 thousands AGN runs to build this kind of model
    axes = [ObservationGroupAxis('ZEN_PNT', [0, 49, 90], fmt='edges')]
    obs_groups = ObservationGroups(axes)
    obs_table_with_group_id = obs_groups.apply(data_store.obs_table)
    obs_groups.obs_groups_table.write(outdir2 + "/group-def.fits",
                                      overwrite=True)
    # Exclusion sources table
    cat = SourceCatalogGammaCat()
    exclusion_table = cat.table
    exclusion_table.rename_column('ra', 'RA')
    exclusion_table.rename_column('dec', 'DEC')
    radius = exclusion_table['morph_sigma']
    radius.value[np.isnan(radius)] = 0.3
    exclusion_table['Radius'] = radius
    exclusion_table = Table(exclusion_table)

    bgmaker = OffDataBackgroundMaker(data_store,
                                     outdir2,
                                     run_list=None,
                                     obs_table=obs_table_with_group_id,
                                     ntot_group=obs_groups.n_groups,
                                     excluded_sources=exclusion_table)
    bgmaker.make_model("2D")
    bgmaker.smooth_models("2D")
    bgmaker.save_models("2D")
    bgmaker.save_models(modeltype="2D", smooth=True)

    shutil.move(str(outdir2), str(outdir))
    fn = outdir + '/background/group-def.fits'
    hdu_index_table = bgmaker.make_total_index_table(
        data_store=data_store,
        modeltype='2D',
        out_dir_background_model="background",
        filename_obs_group_table=fn,
        smooth=True)
    fn = outdir + '/hdu-index.fits.gz'
    hdu_index_table.write(fn, overwrite=True)

    offset_band = Angle([0, 2.49], 'deg')

    ref_cube_images = make_empty_cube(image_size=50,
                                      energy=ereco,
                                      center=center)
    ref_cube_exposure = make_empty_cube(image_size=50,
                                        energy=etrue,
                                        center=center,
                                        data_unit="m2 s")

    data_store = DataStore.from_dir(tmpdir)

    refheader = ref_cube_images.sky_image_ref.to_image_hdu().header
    exclusion_mask = SkyMask.read(
        '$GAMMAPY_EXTRA/datasets/exclusion_masks/tevcat_exclusion.fits')
    exclusion_mask = exclusion_mask.reproject(reference=refheader)

    # Pb with the load psftable for one of the run that is not implemented yet...
    data_store.hdu_table.remove_row(14)

    cube_maker = StackedObsCubeMaker(empty_cube_images=ref_cube_images,
                                     empty_exposure_cube=ref_cube_exposure,
                                     offset_band=offset_band,
                                     data_store=data_store,
                                     obs_table=data_store.obs_table,
                                     exclusion_mask=exclusion_mask,
                                     save_bkg_scale=True)
    cube_maker.make_cubes(make_background_image=True, radius=10.)
    obslist = [data_store.obs(id) for id in data_store.obs_table["OBS_ID"]]
    ObsList = ObservationList(obslist)
    mean_psf_cube = make_mean_psf_cube(image_size=50,
                                       energy_cube=etrue,
                                       center_maps=center,
                                       center=center,
                                       ObsList=ObsList,
                                       spectral_index=2.3)
    if use_etrue:
        mean_rmf = make_mean_rmf(energy_true=etrue,
                                 energy_reco=ereco,
                                 center=center,
                                 ObsList=ObsList)

    filename_mask = 'exclusion_mask.fits'
    filename_counts = 'counts_cube.fits'
    filename_bkg = 'bkg_cube.fits'
    filename_significance = 'significance_cube.fits'
    filename_excess = 'excess_cube.fits'
    if use_etrue:
        filename_exposure = 'exposure_cube_etrue.fits'
        filename_psf = 'psf_cube_etrue.fits'
        filename_rmf = 'rmf.fits'
        mean_rmf.write(filename_rmf, clobber=True)
    else:
        filename_exposure = 'exposure_cube.fits'
        filename_psf = 'psf_cube.fits'
    exclusion_mask.write(filename_mask, clobber=True)
    cube_maker.counts_cube.write(filename_counts,
                                 format="fermi-counts",
                                 clobber=True)
    cube_maker.bkg_cube.write(filename_bkg,
                              format="fermi-counts",
                              clobber=True)
    cube_maker.significance_cube.write(filename_significance,
                                       format="fermi-counts",
                                       clobber=True)
    cube_maker.excess_cube.write(filename_excess,
                                 format="fermi-counts",
                                 clobber=True)
    cube_maker.exposure_cube.write(filename_exposure,
                                   format="fermi-counts",
                                   clobber=True)
    mean_psf_cube.write(filename_psf, format="fermi-counts", clobber=True)