Example #1
0
    def build_mdp_table(self, energy_binning, modulation_factor):
        """Calculate the MDP values in energy bins, given the modulation
        factor of the instrument as a function of the energy.

        Arguments
        ---------
        energy_binning : array
            The energy binning
        modulation_factor : ximpol.irf.mrf.xModulationFactor instance
            The instrument modulation factor as a function of the energy.
        """
        # Build the time-integrated spectrum.
        time_integrated_spectrum = self.build_time_integral()
        # Build the modulation-factor spectrum, i.e. the object that we
        # integrate to calculate the effective modulation factor in a
        # given energy bin for a given spectrum.
        _x = time_integrated_spectrum.x
        _y = time_integrated_spectrum.y*modulation_factor(_x)
        mu_spectrum = xInterpolatedUnivariateSplineLinear(_x, _y)
        # Loop over the energy bins and calculate the actual MDP values.
        # Note that we also calculate the MDP on the entire energy range.
        observation_time = self.scale_factor*(self.xmax() - self.xmin())
        mdp_table = xMDPTable(observation_time)
        ebins = zip(energy_binning[:-1], energy_binning[1:])
        if len(energy_binning) > 2:
            ebins.append((energy_binning[0], energy_binning[-1]))
        for _emin, _emax in ebins:
            num_counts = self.num_expected_counts(emin=_emin, emax=_emax)
            mu_eff = mu_spectrum.integral(_emin, _emax)/num_counts
            mdp = mdp99(mu_eff, num_counts)
            mdp_table.add_row(mdp, _emin, _emax, mu_eff, num_counts)
        return mdp_table
Example #2
0
 def bin_(self):
     """Overloaded method.
     """
     from ximpol.irf import load_mrf
     from ximpol.irf.mrf import mdp99
     modf = load_mrf(self.event_file.irf_name())
     evt_header = self.event_file.hdu_list['PRIMARY'].header
     if self.get('mc'):
         energy = self.event_data['MC_ENERGY']
     else:
         energy = self.event_data['ENERGY']
     phi = self.event_data['PE_ANGLE']
     phi_hist, xedges, yedges = numpy.histogram2d(energy, phi,
                                                  bins=self.make_binning())
     primary_hdu = self.build_primary_hdu()
     emin, emax = xedges[:-1], xedges[1:]
     emean = []
     effmu = []
     ncounts = []
     mdp = []
     # If there's more than one bin in energy, we're also interested in all
     # the basic quantities in the entire energy range. This involves
     # extending the emin and emax arrays and summing up all the
     # phi histograms across the various energy slices.
     if len(emin) > 1:
         emin = numpy.append(emin, emin[0])
         emax = numpy.append(emax, emax[-1])
         _d1, _d2 = phi_hist.shape
         phi_hist_sum = numpy.sum(phi_hist, axis=0).reshape((1, _d2))
         phi_hist = numpy.append(phi_hist, phi_hist_sum, axis=0)
     for _emin, _emax in zip(emin, emax):
         _mask = (energy > _emin)*(energy < _emax)
         _energy = energy[_mask]
         _emean = numpy.mean(_energy)
         _effmu = modf.weighted_average(_energy)
         _ncounts = len(_energy)
         _mdp = mdp99(_effmu, _ncounts)
         emean.append(_emean)
         effmu.append(_effmu)
         ncounts.append(_ncounts)
         mdp.append(_mdp)
     data = [emin, emax, emean, effmu, ncounts, mdp, phi_hist]
     xBinTableHDUMCUBE.set_phi_spec(self.get('phibins'))
     mcube_hdu = xBinTableHDUMCUBE(data)
     mcube_hdu.setup_header(self.event_file.primary_keywords())
     gti_hdu = self.event_file.hdu_list['GTI']
     hdu_list = fits.HDUList([primary_hdu, mcube_hdu, gti_hdu])
     hdu_list.info()
     logger.info('Writing binned MCUBE data to %s...' % self.get('outfile'))
     hdu_list.writeto(self.get('outfile'), clobber=True)
     logger.info('Done.')
Example #3
0
 def bin_(self):
     """Overloaded method.
     """
     from ximpol.irf import load_mrf
     from ximpol.irf.mrf import mdp99
     modf = load_mrf(self.event_file.irf_name())
     evt_header = self.event_file.hdu_list['PRIMARY'].header
     if self.get('mc'):
         energy = self.event_data['MC_ENERGY']
     else:
         energy = self.event_data['ENERGY']
     phi = self.event_data['PE_ANGLE']
     phi_hist, xedges, yedges = numpy.histogram2d(energy,
                                                  phi,
                                                  bins=self.make_binning())
     primary_hdu = self.build_primary_hdu()
     emin, emax = xedges[:-1], xedges[1:]
     emean = []
     effmu = []
     ncounts = []
     mdp = []
     for _emin, _emax in zip(emin, emax):
         _mask = (energy > _emin) * (energy < _emax)
         _energy = energy[_mask]
         _emean = numpy.mean(_energy)
         _effmu = modf.weighted_average(_energy)
         _ncounts = len(_energy)
         _mdp = mdp99(_effmu, _ncounts)
         emean.append(_emean)
         effmu.append(_effmu)
         ncounts.append(_ncounts)
         mdp.append(_mdp)
     data = [emin, emax, emean, effmu, ncounts, mdp, phi_hist]
     xBinTableHDUMCUBE.set_phi_spec(self.get('phibins'))
     mcube_hdu = xBinTableHDUMCUBE(data)
     mcube_hdu.setup_header(self.event_file.primary_keywords())
     gti_hdu = self.event_file.hdu_list['GTI']
     hdu_list = fits.HDUList([primary_hdu, mcube_hdu, gti_hdu])
     hdu_list.info()
     logger.info('Writing binned MCUBE data to %s...' % self.get('outfile'))
     hdu_list.writeto(self.get('outfile'), clobber=True)
     logger.info('Done.')
Example #4
0
 def bin_(self):
     """Overloaded method.
     """
     from ximpol.irf import load_mrf
     from ximpol.irf.mrf import mdp99
     modf = load_mrf(self.event_file.irf_name())
     evt_header = self.event_file.hdu_list['PRIMARY'].header
     if self.get('mc'):
         energy = self.event_data['MC_ENERGY']
     else:
         energy = self.event_data['ENERGY']
     phi = self.event_data['PE_ANGLE']
     phi_hist, xedges, yedges = numpy.histogram2d(energy, phi,
                                                  bins=self.make_binning())
     primary_hdu = self.build_primary_hdu()
     emin, emax = xedges[:-1], xedges[1:]
     emean = []
     effmu = []
     ncounts = []
     mdp = []
     for _emin, _emax in zip(emin, emax):
         _mask = (energy > _emin)*(energy < _emax)
         _energy = energy[_mask]
         _emean = numpy.mean(_energy)
         _effmu = modf.weighted_average(_energy)
         _ncounts = len(_energy)
         _mdp = mdp99(_effmu, _ncounts)
         emean.append(_emean)
         effmu.append(_effmu)
         ncounts.append(_ncounts)
         mdp.append(_mdp)
     data = [emin, emax, emean, effmu, ncounts, mdp, phi_hist]
     xBinTableHDUMCUBE.set_phi_spec(self.get('phibins'))
     mcube_hdu = xBinTableHDUMCUBE(data)
     mcube_hdu.setup_header(self.event_file.primary_keywords())
     gti_hdu = self.event_file.hdu_list['GTI']
     hdu_list = fits.HDUList([primary_hdu, mcube_hdu, gti_hdu])
     hdu_list.info()
     logger.info('Writing binned MCUBE data to %s...' % self.get('outfile'))
     hdu_list.writeto(self.get('outfile'), clobber=True)
     logger.info('Done.')
Example #5
0
def run():
    outfile = PIPELINE.chandra2ximpol(
        INPUT_FILE_PATH, acis=ACIS, seed=SEED, duration=TIME, regfile=REG_FILE_PATH, configfile=CONFIG_FILE_PATH
    )
    cmap_file = PIPELINE.xpbin(outfile, algorithm=ALGORITHM[1], nxpix=512, nypix=512, binsz=1.25)
    # outfile = PIPELINE.xpselect(outfile, ra=RA, dec=DEC, rad=RAD)
    # outfile = PIPELINE.xpselect(outfile, ra=RA_CORE, dec=DEC_CORE, rad=RAD_PSF)
    outfile = PIPELINE.xpselect(outfile, ra=RA_JET, dec=DEC_JET, rad=RAD_PSF)
    mod_file = PIPELINE.xpbin(outfile, algorithm=ALGORITHM[0], emin=EMIN, emax=EMAX, ebins=EBINS)

    mod_cube = xBinnedModulationCube(mod_file)
    mod_cube.fit()

    event = xEventFile(outfile)
    energy = event.event_data["ENERGY"]
    src_id = event.event_data["MC_SRC_ID"]
    jet_tot = 0
    core_tot = 0
    bkg_tot = 0
    mu_tot = 0.0
    for i in range(0, len(mod_cube.emax)):
        mask_jet = (energy > mod_cube.emin[i]) * (energy < mod_cube.emax[i]) * (src_id == 1)
        mask_bkg = (energy > mod_cube.emin[i]) * (energy < mod_cube.emax[i]) * (src_id == 0)
        mask_core = (energy > mod_cube.emin[i]) * (energy < mod_cube.emax[i]) * (src_id == 2)
        cnts_jet = len(energy[mask_jet])
        cnts_bkg = len(energy[mask_bkg])
        cnts_core = len(energy[mask_core])
        jet_tot += cnts_jet
        bkg_tot += cnts_bkg
        core_tot += cnts_core
        cnts_tot = cnts_jet + cnts_bkg + cnts_core
        mu_tot += mod_cube.effective_mu[i] * cnts_tot
        mdp = mdp99(mod_cube.effective_mu[i], cnts_jet, cnts_bkg + cnts_core)
        logger.info(
            "%.2f--%.2f keV: %d jet counts (%.1f%%) in %d s, mu %.3f, MDP %.2f%%"
            % (
                mod_cube.emin[i],
                mod_cube.emax[i],
                cnts_jet,
                100 * cnts_jet / float(cnts_tot),
                TIME,
                mod_cube.effective_mu[i],
                100 * mdp,
            )
        )
        # print cnts_jet, cnts_bkg, cnts_core
    if EBINS > 1:
        cnts_tot = jet_tot + bkg_tot + core_tot
        mu_tot /= cnts_tot
        mdp_tot = mdp99(mu_tot, jet_tot, bkg_tot + core_tot)
        logger.info(
            "%.2f--%.2f keV: %d jet counts (%.1f%%) in %d s, mu %.3f, MDP %.2f%%"
            % (
                mod_cube.emin[0],
                mod_cube.emax[len(mod_cube.emax) - 1],
                jet_tot,
                100 * jet_tot / float(cnts_tot),
                TIME,
                mu_tot,
                100 * mdp_tot,
            )
        )
        # print jet_tot, bkg_tot, core_tot
    if PLOT is True:
        plot(cmap_file)
        plt.show()
    logger.info("Done.")