Exemple #1
0
def make_psf_model(outdir, E1, E2, data_image, source_name):
    """

    Parameters
    ----------
    outdir: str
        directory chere are stored the data
    E1: float
        energy min
    E2: float
        energy max
    data_image: SkyImage
        on map to reproject the psf
    source_name: str
        name of the source to load the psf file

    Returns
    -------

    """
    psf_file = Table.read(outdir + "/psf_table_" + source_name + "_" + str(E1) + "_" + str(E2) + ".fits")
    header = data_image.to_image_hdu().header
    psf_image = fill_acceptance_image(header, data_image.center, psf_file["theta"].to("deg"),
                                      psf_file["psf_value"].data, psf_file["theta"].to("deg")[-1])
    psf_image.writeto(outdir + "/psf_image.fits", clobber=True)
    load_psf("psf", outdir + "/psf_image.fits")
    return psf
Exemple #2
0
def make_image():
    table = Table.read('acceptance_curve.fits')
    table.pprint()
    center = SkyCoord(83.63, 22.01, unit='deg').galactic

    counts_image = make_empty_image(nxpix=1000, nypix=1000, binsz=0.01, xref=center.l.deg, yref=center.b.deg,
                                    proj='TAN')
    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)
Exemple #3
0
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)
Exemple #4
0
    def bkg_image(self, bkg_norm=True):
        """
        Make the background image for one observation from a bkg model.

        Parameters
        ----------
        bkg_norm : bool
            If true, apply the scaling factor from the number of counts
            outside the exclusion region to the bkg image
        """
        bkg_image = SkyImage.empty_like(self.empty_image)
        table = self.bkg.acceptance_curve_in_energy_band(
            energy_band=self.energy_band)
        center = self.obs_center.galactic
        bkg_hdu = fill_acceptance_image(self.header, center, table["offset"],
                                        table["Acceptance"],
                                        self.offset_band[1])
        bkg_image.data = Quantity(
            bkg_hdu.data,
            table["Acceptance"].unit) * bkg_image.solid_angle() * self.livetime
        bkg_image.data = bkg_image.data.decompose()
        bkg_image.data = bkg_image.data.value
        if bkg_norm:
            scale, counts = self.background_norm_factor(
                self.images["counts"], bkg_image)
            bkg_image.data = scale * bkg_image.data
            if self.save_bkg_scale:
                self.table_bkg_scale.add_row([self.obs_id, scale, counts])

        self.images["bkg"] = bkg_image
def make_psf_cube(image_size,
                  energy_cube,
                  source_name,
                  center_maps,
                  center,
                  ObsList,
                  outdir,
                  spectral_index=2.3):
    """
    Compute the mean psf for a set of observation for different energy bands
    Parameters
    ----------
    image_size:int, Total number of pixel of the 2D map
    energy: Tuple for the energy axis: (Emin,Emax,nbins)
    source_name: name of the source you want to compute the image
    center_maps: SkyCoord
            center of the images
    center: SkyCoord 
            position where we want to compute the psf
    ObsList: ObservationList to use to compute the psf (could be different that the data_store for G0p9 for the GC for example)
    outdir: directory where the fits image will go
    spectral_index: assumed spectral index to compute the psf

    Returns
    -------

    """
    ref_cube = make_empty_cube(image_size, energy_cube, center_maps)
    header = ref_cube.sky_image_ref.to_image_hdu().header
    energy_bins = ref_cube.energies(mode="edges")
    for i_E, E in enumerate(energy_bins[0:-1]):
        energy_band = Energy(
            [energy_bins[i_E].value, energy_bins[i_E + 1].value],
            energy_bins.unit)
        energy = EnergyBounds.equal_log_spacing(energy_band[0].value,
                                                energy_band[1].value, 100,
                                                energy_band.unit)
        # Here all the observations have a center at less than 2 degrees from the Crab so it will be ok to estimate the mean psf on the Crab source postion (the area is define for offset equal to 2 degrees...)
        psf_energydependent = ObsList.make_mean_psf(center, energy, theta=None)
        try:
            psf_table = psf_energydependent.table_psf_in_energy_band(
                energy_band, spectral_index=spectral_index)
        except:
            psf_table = TablePSF(
                psf_energydependent.offset,
                Quantity(np.zeros(len(psf_energydependent.offset)), u.sr**-1))
        ref_cube.data[i_E, :, :] = fill_acceptance_image(
            header, center_maps, psf_table._offset.to("deg"),
            psf_table._dp_domega,
            psf_table._offset.to("deg")[-1]).data
    ref_cube.write(outdir + "/mean_psf_cube_" + source_name + ".fits",
                   format="fermi-counts")
Exemple #6
0
def make_mean_psf_cube(image_size,
                       energy_cube,
                       center_maps,
                       center,
                       ObsList,
                       spectral_index=2.3):
    """
    Compute the mean psf for a set of observation for different energy bands
    Parameters
    ----------
    image_size:int, Total number of pixel of the 2D map
    energy: Tuple for the energy axis: (Emin,Emax,nbins)
    center_maps: SkyCoord
            center of the images
    center: SkyCoord 
            position where we want to compute the psf
    ObsList: ObservationList to use to compute the psf (could be different that the data_store for G0p9 for the GC for example)
    spectral_index: assumed spectral index to compute the psf

    Returns
    -------
    ref_cube : `~gammapy.cube.SkyCube`
             PSF mean cube

    """
    ref_cube = make_empty_cube(image_size, energy_cube, center_maps)
    header = ref_cube.sky_image_ref.to_image_hdu().header
    energy_bins = ref_cube.energies()
    for i_E, E in enumerate(energy_bins[0:-1]):
        energy_band = Energy(
            [energy_bins[i_E].value, energy_bins[i_E + 1].value],
            energy_bins.unit)
        energy = EnergyBounds.equal_log_spacing(energy_band[0].value,
                                                energy_band[1].value, 100,
                                                energy_band.unit)
        psf_energydependent = ObsList.make_psf(center, energy, theta=None)
        try:
            psf_table = psf_energydependent.table_psf_in_energy_band(
                energy_band, spectral_index=spectral_index)
        except:
            psf_table = TablePSF(
                psf_energydependent.offset,
                Quantity(np.zeros(len(psf_energydependent.offset)), u.sr**-1))
        ref_cube.data[i_E, :, :] = fill_acceptance_image(
            header, center_maps, psf_table._offset.to("deg"),
            psf_table._dp_domega,
            psf_table._offset.to("deg")[-1]).data
    return ref_cube
    on.write("on_maps" + str(E1) + "_" + str(E2) + "_TeV.fits", clobber=True)
    # bkgmap.data=bkgmap.data/bkgmap.data.sum()
    bkgmap.write("off_maps" + str(E1) + "_" + str(E2) + "_TeV.fits", clobber=True)
    exp.write("exp_maps" + str(E1) + "_" + str(E2) + "_TeV.fits", clobber=True)

    data = fits.open("on_maps" + str(E1) + "_" + str(E2) + "_TeV.fits")
    load_image(1, data)
    # image_data()

    psf_file_SgrA = Table.read("psf_table_SgrA_" + str(E1) + "_" + str(E2) + ".fits")
    psf_file_G0p9 = Table.read("psf_table_G0p9" + str(E1) + "_" + str(E2) + ".fits")

    header = on.to_image_hdu().header
    # Todo:voir pour la normalisation normalement il le fait tout seul mais pas sur...
    psf_image_SgrA = fill_acceptance_image(header, on.center(), psf_file_SgrA["theta"].to("deg"),
                                           psf_file_SgrA["psf_value"].data, psf_file_SgrA["theta"].to("deg")[-1])
    source_center_SgrA = SkyCoord(359.9442, -0.0462, unit='deg', frame="galactic")
    # source_center_SgrA = SkyCoord.from_name("SgrA*")
    source_center_G0p9 = SkyCoord(0.868, 0.075, unit='deg', frame="galactic")
    psf_image_G0p9 = fill_acceptance_image(header, on.center(), psf_file_G0p9["theta"].to("deg"),
                                           psf_file_G0p9["psf_value"].data, psf_file_G0p9["theta"].to("deg")[-1])
    psf_image_SgrA.writeto("psf_image_SgrA_" + str(E1) + "_" + str(E2) + ".fits", clobber=True)
    psf_image_G0p9.writeto("psf_image_G0p9_" + str(E1) + "_" + str(E2) + ".fits", clobber=True)
    load_psf("psf_SgrA", "psf_image_SgrA_" + str(E1) + "_" + str(E2) + ".fits")
    load_psf("psf_G0p9", "psf_image_G0p9_" + str(E1) + "_" + str(E2) + ".fits")

    # modele gauss pour sgrA centre sur SgrA
    mygaus_SgrA = normgauss2dint("SgrA")
    mygaus_SgrA.xpos, mygaus_SgrA.ypos = skycoord_to_pixel(source_center_SgrA, on.wcs)
    mygaus_SgrA.xpos.val += 0.5
    mygaus_SgrA.ypos.val += 0.5
Exemple #8
0
                           str(E2) + "_TeV.fits")["counts"]
    on.write(outdir_data + "/on_maps" + str(E1) + "_" + str(E2) + "_TeV.fits",
             clobber=True)
    counts[i_E, :, :] = on.data
    exposure_data[i_E, :, :] = SkyImageList.read(
        outdir_data + "/fov_bg_maps" + str(E1) + "_" + str(E2) +
        "_TeV.fits")["exposure"].data * 1e4
    #exposure_data[i_E,:,:] = SkyImageList.read(outdir_data + "/fov_bg_maps" + str(E1) + "_" + str(E2) + "_TeV.fits")["exposure"].data
    bkg_data[i_E, :, :] = SkyImageList.read(outdir_data + "/fov_bg_maps" +
                                            str(E1) + "_" + str(E2) +
                                            "_TeV.fits")["bkg"].data
    psf_file = Table.read(outdir_data + "/psf_table_" + source_name + "_" +
                          str(E1) + "_" + str(E2) + ".fits")
    header = on.to_image_hdu().header
    psf_data[i_E, :, :] = fill_acceptance_image(
        header, on.center, psf_file["theta"].to("deg"),
        psf_file["psf_value"].data, psf_file["theta"].to("deg")[-1]).data

#logenergy_axis=LogEnergyAxis(energy_bins[0:imax+1],mode='edges')
logenergy_axis = LogEnergyAxis(energy_bins, mode='edges')
counts_3D = SkyCube(name="counts3D",
                    data=Quantity(counts, " "),
                    wcs=on.wcs,
                    energy_axis=logenergy_axis)
cube = counts_3D.to_sherpa_data3d(dstype='Data3DInt')
logenergy_axis = LogEnergyAxis(energy_bins[0:imax + 1], mode='edges')
psf_image_3D = SkyCube(name="counts3D",
                       data=Quantity(psf_data, " "),
                       wcs=on.wcs,
                       energy_axis=logenergy_axis)
exposure_image_3D = SkyCube(name="counts3D",