def main(): """Simple test code. """ import os from ximpol.srcmodel.spectrum import power_law from ximpol.srcmodel.polarization import constant from ximpol import XIMPOL_CONFIG src_base_model = dict(energy_spectrum = power_law(1., 2.), polarization_degree = constant(0.1), polarization_angle = constant(20.), column_density = 1.0e-22, redshift = 0.0012, identifier = 0) s1 = xModelComponentBase('Source 1', **src_base_model) print(s1) s2 = xPointSource('Source 2', ra=10., dec=15., **src_base_model) print(s2) _ephem = xEphemeris(10., 0.033, min_validity_time=10., max_validity_time=10000.) s3 = xPeriodicPointSource('Source 3', ra=10., dec=15., ephemeris=_ephem, **src_base_model) print(s3) s4 = xUniformDisk('Source 4', ra=10., dec=15., radius=0.01, **src_base_model) print(s4) s5 = xGaussianDisk('Source 5', ra=10., dec=15., sigma=0.01, **src_base_model) print(s5) img_file_path = os.path.join(XIMPOL_CONFIG, 'fits', 'crab_0p3_10p0_keV.fits') s6 = xExtendedSource('Source 6', img_file_path, **src_base_model) print(s6)
def mdp_table(self, column_density, index, exposure_time, eflux): """Return the MDP table for a point source with a power-law spectral shape with a given set of parameters and for a given observation time. There's a slight complication, here, due to the fact that the sensitivity calculator is rescaling the absorbed fluxes so that the input energy flux (in the web form) is that at the observer instead of that at the source. Therefore we need to do the same here. """ tsamples = numpy.linspace(0.0, exposure_time, 2) norm = int_eflux2pl_norm(eflux, self.emin, self.emax, index, erg=True) energy_spectrum = power_law(norm, index) ism_trans = self.ism_model.transmission_factor(column_density) _x = numpy.linspace(self.emin, self.emax, 1000) _y = _x * energy_spectrum(_x, 0.0) * ism_trans(_x) absorbed_energy_spectrum = xInterpolatedUnivariateSplineLinear(_x, _y) absorbed_eflux = keV2erg(absorbed_energy_spectrum.norm()) scale = eflux / absorbed_eflux count_spectrum = xCountSpectrum(energy_spectrum, self.aeff, tsamples, column_density, scale_factor=scale) mdp_table = count_spectrum.build_mdp_table(self.ebinning, self.modf) return mdp_table
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import numpy from ximpol.srcmodel.roi import xGaussianDisk, xROIModel from ximpol.srcmodel.spectrum import power_law from ximpol.srcmodel.polarization import constant ROI_MODEL = xROIModel(10., 15.) energy_spectrum = power_law(10., 2.) polarization_degree = constant(0.5) polarization_angle = constant(numpy.radians(65.)) src = xGaussianDisk('Gaussian disk', 10., 15., 0.005, energy_spectrum, polarization_degree, polarization_angle) ROI_MODEL.add_source(src) if __name__ == '__main__': print(ROI_MODEL)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import numpy from ximpol.srcmodel.roi import xPointSource, xROIModel from ximpol.srcmodel.spectrum import power_law from ximpol.srcmodel.polarization import constant ROI_MODEL = xROIModel(10., 10.) energy_spectrum1 = power_law(10., 2.) polarization_degree1 = constant(1.0) polarization_angle1 = constant(numpy.radians(65.)) src1 = xPointSource('Point source 1', 9.9750, 9.9833, energy_spectrum1, polarization_degree1, polarization_angle1) energy_spectrum2 = power_law(15., 3.) polarization_degree2 = constant(0.0) polarization_angle2 = constant(numpy.radians(0)) src2 = xPointSource('Point source 2', 10., 10., energy_spectrum2, polarization_degree2, polarization_angle2) ROI_MODEL.add_sources(src1, src2) if __name__ == '__main__': print(ROI_MODEL)
"name": name, "flux_min": flux_min, "flux_max": flux_max, "p_opt_max": p_opt_max, "p_opt_min": p_opt_min, } src_list.append(src) pl_norm_ref = int_eflux2pl_norm(FLUX_REF, E_MIN, E_MAX, PL_INDEX) logger.info("PL normalization @ %.3e erg cm^-2 s^-1: %.3e keV^-1 cm^-2 s^-1" % (FLUX_REF, pl_norm_ref)) aeff = load_arf(DEFAULT_IRF_NAME) modf = load_mrf(DEFAULT_IRF_NAME) tsamples = numpy.array([0, OBS_TIME_REF]) ebinning = numpy.array([E_MIN, E_MAX]) energy_spectrum = power_law(pl_norm_ref, PL_INDEX) count_spectrum = xCountSpectrum(energy_spectrum, aeff, tsamples) mdp_table = count_spectrum.build_mdp_table(ebinning, modf) mdp_ref = mdp_table.mdp_values()[-1] logger.info("Reference MDP for %s s: %.3f" % (OBS_TIME_REF, mdp_ref)) blazar_list = parse_blazar_list(PRIORITY_ONLY) mirror_list = [1, 3, 9, 17, 18] numpy.random.seed(10) _color = numpy.random.random((3, len(blazar_list))) # numpy.random.seed(1) # _disp = numpy.random.uniform(0.7, 2., len(blazar_list)) plt.figure("Average polarization degree", (11, 8)) _x = numpy.logspace(-13, -7, 100)
# Location of the ROI: ROI_MODEL = xROIModel(83.633083, 22.014500) # Image: img_file_path = os.path.join(XIMPOL_CONFIG, 'fits', 'crab_0p3_10p0_keV.fits') # Spectrum # The idea is to have multiple extended sources, one per region. # For example to create multiple region polarization map, from the bin diurectory: #./xppolmap.py --image ../config/fits/crab_0p3_10p0_keV.fits --region ../config/fits/crab_nebula_complex.reg --output ../config/fits/crab_nebula_complex.fits ############################## base_names=['crab_nebula_complex_pmax020_reg000','crab_nebula_complex_pmax017_reg001','crab_nebula_complex_pmax010_reg002'] polarization_maps=[] for i,base_name in enumerate(base_names): energy_spectrum = power_law(9.59/len(base_names), 2.108) # This ineeds to be checked! pol_mapx_path = os.path.join(XIMPOL_CONFIG, 'fits', '%s_x.fits' % base_name) pol_mapy_path = os.path.join(XIMPOL_CONFIG, 'fits', '%s_y.fits' % base_name) polarization_map = xPolarizationMap(pol_mapx_path, pol_mapy_path) polarization_maps.append(polarization_map) def nebula_polarization_angle(E, t, ra, dec): return polarization_map.polarization_angle(ra, dec) def nebula_polarization_degree(E, t, ra, dec): return polarization_map.polarization_degree(ra, dec) _source=xExtendedSource('Crab Nebula (%d)' % i, img_file_path, energy_spectrum, nebula_polarization_degree,
IRF_NAME = 'xipe_baseline' T_MIN = 0. T_MAX = 1000. OUTPUT_FOLDER = XIMPOL_DOC_FIGURES aeff, psf, modf, edisp = load_irfs(IRF_NAME) def C(t): return 1. - 0.9*t/(T_MAX - T_MIN) def Gamma(t): return 1.5 + t/(T_MAX - T_MIN) spectrum = power_law(C, Gamma) plt.figure('Source spectrum') _t = numpy.linspace(T_MIN, T_MAX, 100) _e = aeff.x fmt = dict(xname='Time', xunits='s', yname='Energy', yunits='keV', zname='dN/dE', zunits='cm$^{-2}$ s$^{-1}$ keV$^{-1}$') spectrum_spline = xInterpolatedBivariateSplineLinear(_t, _e, spectrum, **fmt) spectrum_spline.plot(show=False, logz=True) save_current_figure('source_spectrum.png', OUTPUT_FOLDER, clear=False) plt.figure('Source spectrum slices') for t in [T_MIN, 0.5*(T_MIN + T_MAX), T_MAX]: spectrum_spline.vslice(t).plot(show=False, logx=True, logy=True, label='t = %d s' % t) plt.legend(bbox_to_anchor=(0.75, 0.95))
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import numpy import os from ximpol.srcmodel.roi import xExtendedSource, xROIModel from ximpol.srcmodel.spectrum import power_law from ximpol.srcmodel.polarization import constant from ximpol import XIMPOL_CONFIG ROI_MODEL = xROIModel(83.633083, 22.014500) img_file_path = os.path.join(XIMPOL_CONFIG, 'fits', 'crab_0p3_10p0_keV.fits') energy_spectrum = power_law(10., 2.) polarization_degree = constant(0.157) polarization_angle = constant(numpy.radians(161.1)) crab_nebula = xExtendedSource('Crab nebula', img_file_path, energy_spectrum, polarization_degree, polarization_angle) ROI_MODEL.add_source(crab_nebula) if __name__ == '__main__': print(ROI_MODEL)
# Fit the PL index as a function of the phase with a sinusoid. def ffit(x, *pars): """Fit function for the PL index as function of the phase. """ return pars[0] + pars[1]*numpy.cos(pars[2]*x + pars[3]) p0 = [1., 1., 1., 1.] popt, pcov = curve_fit(ffit, _phi, _index, p0, _index_err) # And use tho best-fit parameters to build a proper spline. fmt = dict(xname='Pulsar phase', yname='PL index') pl_index_spline = xInterpolatedUnivariateSpline(_phi, ffit(_phi, *popt), k=2, **fmt) # Build the actual energy spectrum. energy_spectrum = power_law(pl_normalization_spline, pl_index_spline) # Build the polarization angle as a function of the phase. _phi, _pol_angle = numpy.loadtxt(PANG_FILE_PATH, unpack=True) _pol_angle = numpy.deg2rad(_pol_angle) # Filter the data points to reduce the noise. _pol_angle = scipy.signal.wiener(_pol_angle, 7) fmt = dict(xname='Pulsar phase', yname='Polarization angle [rad]') pol_angle_spline = xInterpolatedUnivariateSpline(_phi, _pol_angle, k=1, **fmt) # Mind that you have to wrap this into a function to be used. # This could be done in a more general way with some sort of library function. def polarization_degree(E, t, ra, dec): return pol_degree_spline(t) # Build the polarization degree as a function of the phase.
# # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import numpy from ximpol.srcmodel.roi import xPointSource, xROIModel from ximpol.srcmodel.spectrum import power_law from ximpol.srcmodel.polarization import constant ROI_MODEL = xROIModel(10.0, 10.0) energy_spectrum1 = power_law(10.0, 2.0) polarization_degree1 = constant(1.0) polarization_angle1 = constant(numpy.radians(65.0)) src1 = xPointSource("Point source 1", 9.9750, 9.9833, energy_spectrum1, polarization_degree1, polarization_angle1) energy_spectrum2 = power_law(15.0, 3.0) polarization_degree2 = constant(0.0) polarization_angle2 = constant(numpy.radians(0)) src2 = xPointSource("Point source 2", 10.0, 10.0, energy_spectrum2, polarization_degree2, polarization_angle2) ROI_MODEL.add_sources(src1, src2) if __name__ == "__main__": print(ROI_MODEL)
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import numpy import os from ximpol.srcmodel.roi import xExtendedSource, xROIModel from ximpol.srcmodel.spectrum import power_law from ximpol.srcmodel.polarization import constant from ximpol import XIMPOL_CONFIG ROI_MODEL = xROIModel(83.633083, 22.014500) img_file_path = os.path.join(XIMPOL_CONFIG, 'fits', 'crab_0p3_10p0_keV.fits') energy_spectrum = power_law(9.59, 2.108) polarization_degree = constant(0.157) polarization_angle = constant(numpy.radians(161.1)) crab_nebula = xExtendedSource('Crab nebula', img_file_path, energy_spectrum, polarization_degree, polarization_angle) ROI_MODEL.add_source(crab_nebula) if __name__ == '__main__': print(ROI_MODEL)
""" return pars[0] + pars[1] * numpy.cos(pars[2] * x + pars[3]) p0 = [1., 1., 1., 1.] popt, pcov = curve_fit(ffit, _phi, _index, p0, _index_err) # And use tho best-fit parameters to build a proper spline. fmt = dict(xname='Pulsar phase', yname='PL index') pl_index_spline = xInterpolatedUnivariateSpline(_phi, ffit(_phi, *popt), k=2, **fmt) # Build the actual energy spectrum. energy_spectrum = power_law(pl_normalization_spline, pl_index_spline) # Build the polarization angle as a function of the phase. _phi, _pol_angle = numpy.loadtxt(PANG_FILE_PATH, unpack=True) _pol_angle = numpy.deg2rad(_pol_angle) # Filter the data points to reduce the noise. _pol_angle = scipy.signal.wiener(_pol_angle, 7) fmt = dict(xname='Pulsar phase', yname='Polarization angle [rad]') pol_angle_spline = xInterpolatedUnivariateSpline(_phi, _pol_angle, k=1, **fmt) # Mind that you have to wrap this into a function to be used. # This could be done in a more general way with some sort of library function. def polarization_degree(E, t, ra, dec): return pol_degree_spline(t)
# You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import numpy from ximpol.srcmodel.roi import xPointSource, xROIModel from ximpol.srcmodel.spectrum import power_law from ximpol.srcmodel.polarization import constant ROI_MODEL = xROIModel(90., 0.) PL_NORM = 10. PL_INDEX = 2.0 POL_DEGREE = 0.5 POL_ANGLE = numpy.radians(65.) energy_spectrum = power_law(PL_NORM, PL_INDEX) polarization_degree = constant(POL_DEGREE) polarization_angle = constant(POL_ANGLE) src = xPointSource('Point source', ROI_MODEL.ra, ROI_MODEL.dec, energy_spectrum, polarization_degree, polarization_angle) ROI_MODEL.add_source(src) if __name__ == '__main__': print(ROI_MODEL)