def buildspline(spindegree):
    MIN_ENERGY = 0.1
    MAX_ENERGY = 15.
    
    POL_FILE_PATH = os.path.join(XIMPOL_CONFIG, 'ascii', 'bh_spin/Polarization_spin%s.txt'%(spindegree))
    # Build the polarization degree as a function of the energy.
    _energy, _pol_degree, _pol_angle = numpy.loadtxt(POL_FILE_PATH, unpack=True)

    #Switch to have degrees
    _pol_degree /= 100.
    
    _mask = (_energy >= MIN_ENERGY)*(_energy <= MAX_ENERGY)
    _energy = _energy[_mask]
    _pol_degree = _pol_degree[_mask]

    fmt = dict(xname='Energy', yname='Polarization degree')
    pol_degree_spline = xInterpolatedUnivariateSpline(_energy, _pol_degree, k=1, **fmt)

    #Pol angle in radians
    _pol_angle = numpy.deg2rad(_pol_angle)

    #Switched to have degrees and not radians
    #_pol_angle = _pol_angle

    #_mask = (_energy >= MIN_ENERGY)*(_energy <= MAX_ENERGY)
    #_energy = _energy[_mask]
    _pol_angle = _pol_angle[_mask]
    fmt = dict(xname='Energy', yname='Polarization angle [rad]')
    pol_angle_spline = xInterpolatedUnivariateSpline(_energy, _pol_angle, k=1, **fmt)

    
    return pol_degree_spline, pol_angle_spline
Exemple #2
0
    def build_eef(self):
        """Build the Encircled Energy Fraction (EEF) as a function of r.

        And, while we're at it, we also calculate and cache the HEW.
        """
        _r = self.x
        _y = numpy.array([self.generator.integral(_r[0], _rp) for _rp in _r])
        _y /= gauss_king_eef_at_infinity(*self.__params)
        hew = 2 * xInterpolatedUnivariateSpline(_y, _r, k=2)(0.5)
        fmt = dict(xname="r", xunits="arcsec", yname="EEF")
        return xInterpolatedUnivariateSpline(_r, _y, k=1, **fmt), hew
Exemple #3
0
# Build the polarization degree as a function of the energy.
_energy, _pol_degree = numpy.loadtxt(POL_DEGREE_FILE_PATH, unpack=True)
_pol_degree /= 100.
#print "Here are the energy and pol degree values"
#print _energy
#print
#print _pol_degree
# Filter the data points to reduce the noise.
#_pol_degree = scipy.signal.wiener(_pol_degree, 5)
_mask = (_energy >= MIN_ENERGY) * (_energy <= MAX_ENERGY)
_energy = _energy[_mask]
_pol_degree = _pol_degree[_mask]
fmt = dict(xname='Energy', yname='Polarization degree')
pol_degree_spline = xInterpolatedUnivariateSpline(_energy,
                                                  _pol_degree,
                                                  k=1,
                                                  **fmt)

# Build the polarization angle as a function of the energy.
_energy, _pol_angle = numpy.loadtxt(POL_ANGLE_FILE_PATH, unpack=True)
_pol_angle = numpy.deg2rad(_pol_angle)
#print "Here are the energy and pol angle values"
#print _energy
#print
#print _pol_angle
# Filter the data points to reduce the noise.
#_pol_angle = scipy.signal.wiener(_pol_angle, 2)
_mask = (_energy >= MIN_ENERGY) * (_energy <= MAX_ENERGY)
_energy = _energy[_mask]
_pol_angle = _pol_angle[_mask]
fmt = dict(xname='Energy', yname='Polarization angle [rad]')
def energy_spectrum(E,t):
    return energy_spectrum_inclination(E,inclination(t))

if ximpol_loaded:
    
    # Build the PL normalization as a function of the phase.
    fmt = dict(xname='Pulsar phase', yname='Flux',
               yunits='keV cm$^{-2}$ s$^{-1}$')

    _phi=numpy.linspace(0,1,23)
    enbin=numpy.linspace(2,10,100)
    esum=_phi*0
    for ii,pp in enumerate(_phi):
        esum[ii]=numpy.trapz(energy_spectrum(enbin,pp),x=enbin)

    pl_normalization_spline = xInterpolatedUnivariateSpline(_phi, esum, k=3, **fmt)

    # Build the polarization angle as a function of the phase.
    _pol_angle = polarization_angle(0,_phi,0,0)
    fmt = dict(xname='Pulsar phase', yname='Polarization angle [rad]')
    pol_angle_spline = xInterpolatedUnivariateSpline(_phi, _pol_angle, k=1, **fmt)


    # Build the polarization degree as a function of the phase.
    _pol_degree = polarization_degree(0,_phi,0,0)
    fmt = dict(xname='Pulsar phase', yname='Polarization degree')
    pol_degree_spline = xInterpolatedUnivariateSpline(_phi, _pol_degree, k=1, **fmt)


    ROI_MODEL = xROIModel(254.457625,  35.3423888889)
    herx1_ephemeris = xEphemeris(0., 0.8064516129,  0, 0)
def plot(save=False):
    """Plot the stuff in the analysis file.
    """

    def draw_time_grid(color='blue'):
        """
        """
        times = [3600., 3600.*24., 3600.*24.*7.]
        labels = ['1 hour', '1 day', '1 week']
        for t, l in zip(times, labels):
            plt.axvline(t, linestyle='dashed', color=color)
            ymin, ymax = plt.gca().get_ylim()
            y = ymin + 1.05*(ymax - ymin)
            plt.text(t, y, l, ha='center', color=color)

    sim_label = 'XIPE'
    mod_label = 'Input model'
    lc_label = 'Light curve'
    _time, _time_errp, _time_errm, _pol_deg, _pol_deg_err, _pol_angle,\
        _pol_angle_err, _index, _index_err, _norm,\
        _norm_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True)
    logger.info(_time)
    logger.info((_time_errp + _time_errm)/3600.)
    _pol_angle = numpy.degrees(_pol_angle)
    _pol_angle_err = numpy.degrees(_pol_angle_err)
    plt.figure('Polarization degree')
    pol_degree_spline.plot(show=False, label=mod_label, logx=True)
    plt.errorbar(_time, _pol_deg, xerr=[_time_errm, _time_errp],
                 yerr=_pol_deg_err, fmt='o', label=sim_label)
    plt.axis([100., 1e6, 0., 0.6])
    plt.legend(bbox_to_anchor=(0.4, 0.95))
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_polarization_degree',
                            OUTPUT_FOLDER, False)
    plt.figure('Polarization angle')
    _x = pol_degree_spline.x
    _y = numpy.full(len(_x), polarization_angle(_x, None, None, None))
    _y = numpy.degrees(_y)
    fmt = dict(xname='Time', xunits='s', yname='Polarization angle',
               yunits=r'$^\circ$')
    _s = xInterpolatedUnivariateSpline(_x, _y, **fmt)
    _s.plot(logx=True, show=False, label=mod_label)
    plt.errorbar(_time, _pol_angle, xerr=[_time_errm, _time_errp],
                 yerr=_pol_angle_err, fmt='o', label=sim_label)
    plt.axis([100., 1e6, None, None])
    plt.legend(bbox_to_anchor=(0.4, 0.95))
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_polarization_angle',
                            OUTPUT_FOLDER, False)
    plt.figure('PL index')
    _y = numpy.full(len(_x), PL_INDEX)
    fmt = dict(xname='Time', xunits='s', yname='PL index')
    _s = xInterpolatedUnivariateSpline(_x, _y, **fmt)
    _s.plot(logx=True, show=False, label=mod_label)
    plt.errorbar(_time, _index, xerr=[_time_errm, _time_errp], yerr=_index_err,
                 fmt='o', label=sim_label)
    plt.axis([100., 1e6, None, None])
    plt.legend(bbox_to_anchor=(0.4, 0.95))
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_pl_index', OUTPUT_FOLDER, False)
    #plt.figure('PL normalization')
    #plt.errorbar(_time, _norm, xerr=[_time_errm, _time_errp], yerr=_norm_err,
    #             fmt='o', label=sim_label)
    #pl_normalization_spline.plot(show=False, label=mod_label)
    #plt.axis([100., 1e6, None, None])
    #plt.legend(bbox_to_anchor=(0.4, 0.95))
    #draw_time_grid()
    #if save:
    #    save_current_figure('grb130427_swift_pl_norm', OUTPUT_FOLDER, False)
    plt.figure('Light curve')
    lc = xBinnedLightCurve(_lc_file_path())
    lc.plot(show=False)
    # This should be implemented in the binned LC class.
    plt.xscale('log')
    plt.yscale('log')
    plt.axis([100., 1e6, None, None])
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_lc', OUTPUT_FOLDER, False)
    plt.show()
Exemple #6
0
def plot(save=False):
    """Plot the stuff in the analysis file.
    """
    def draw_time_grid(color='blue'):
        """
        """
        times = [3600., 3600. * 24., 3600. * 24. * 7.]
        labels = ['1 hour', '1 day', '1 week']
        for t, l in zip(times, labels):
            plt.axvline(t, linestyle='dashed', color=color)
            ymin, ymax = plt.gca().get_ylim()
            y = ymin + 1.05 * (ymax - ymin)
            plt.text(t, y, l, ha='center', color=color)

    sim_label = 'XIPE'
    mod_label = 'Input model'
    lc_label = 'Light curve'
    _time, _time_errp, _time_errm, _pol_deg, _pol_deg_err, _pol_angle,\
        _pol_angle_err, _index, _index_err, _norm,\
        _norm_err = numpy.loadtxt(ANALYSIS_FILE_PATH, unpack=True)
    logger.info(_time)
    logger.info((_time_errp + _time_errm) / 3600.)
    _pol_angle = numpy.degrees(_pol_angle)
    _pol_angle_err = numpy.degrees(_pol_angle_err)
    plt.figure('Polarization degree')
    pol_degree_spline.plot(show=False, label=mod_label, logx=True)
    plt.errorbar(_time,
                 _pol_deg,
                 xerr=[_time_errm, _time_errp],
                 yerr=_pol_deg_err,
                 fmt='o',
                 label=sim_label)
    plt.axis([100., 1e6, 0., 0.6])
    plt.legend(bbox_to_anchor=(0.4, 0.95))
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_polarization_degree',
                            OUTPUT_FOLDER, False)
    plt.figure('Polarization angle')
    _x = pol_degree_spline.x
    _y = numpy.full(len(_x), polarization_angle(_x, None, None, None))
    _y = numpy.degrees(_y)
    fmt = dict(xname='Time',
               xunits='s',
               yname='Polarization angle',
               yunits=r'$^\circ$')
    _s = xInterpolatedUnivariateSpline(_x, _y, **fmt)
    _s.plot(logx=True, show=False, label=mod_label)
    plt.errorbar(_time,
                 _pol_angle,
                 xerr=[_time_errm, _time_errp],
                 yerr=_pol_angle_err,
                 fmt='o',
                 label=sim_label)
    plt.axis([100., 1e6, None, None])
    plt.legend(bbox_to_anchor=(0.4, 0.95))
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_polarization_angle',
                            OUTPUT_FOLDER, False)
    plt.figure('PL index')
    _y = numpy.full(len(_x), PL_INDEX)
    fmt = dict(xname='Time', xunits='s', yname='PL index')
    _s = xInterpolatedUnivariateSpline(_x, _y, **fmt)
    _s.plot(logx=True, show=False, label=mod_label)
    plt.errorbar(_time,
                 _index,
                 xerr=[_time_errm, _time_errp],
                 yerr=_index_err,
                 fmt='o',
                 label=sim_label)
    plt.axis([100., 1e6, None, None])
    plt.legend(bbox_to_anchor=(0.4, 0.95))
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_pl_index', OUTPUT_FOLDER, False)
    #plt.figure('PL normalization')
    #plt.errorbar(_time, _norm, xerr=[_time_errm, _time_errp], yerr=_norm_err,
    #             fmt='o', label=sim_label)
    #pl_normalization_spline.plot(show=False, label=mod_label)
    #plt.axis([100., 1e6, None, None])
    #plt.legend(bbox_to_anchor=(0.4, 0.95))
    #draw_time_grid()
    #if save:
    #    save_current_figure('grb130427_swift_pl_norm', OUTPUT_FOLDER, False)
    plt.figure('Light curve')
    lc = xBinnedLightCurve(_lc_file_path())
    lc.plot(show=False)
    # This should be implemented in the binned LC class.
    plt.xscale('log')
    plt.yscale('log')
    plt.axis([100., 1e6, None, None])
    draw_time_grid()
    if save:
        save_current_figure('grb130427_swift_lc', OUTPUT_FOLDER, False)
    plt.show()
Exemple #7
0
in 10 phase bins.

Despite the fact that the input values are most likely the average fluxes
within each phase bin, we just add 0.05 and consider them as the values
at the bin center. In addition, in order not to have discontinuities at the
boundaries, we  set the values at 0 and 1 as the average values of the first
and last point. We might want to be smarter in the long run, but this should
be a sensible first step.
"""
phasogram_file_path = _full_path("gk_per_flux_2_8_keV_ph10.txt")
_bin, _phase, _eflux, _dummy = numpy.loadtxt(phasogram_file_path, unpack=True)
# Duplicate the values at phi=0 for phi=1.
_phase = numpy.append(_phase, 1.0)
_eflux = numpy.append(_eflux, _eflux[0])
_fmt = dict(xname="Phase", yname="Integral energy flux 2--8 keV", yunits="erg s$^{-1}$ cm$^{-2}$")
phasogram_spline = xInterpolatedUnivariateSpline(_phase, _eflux, k=3, **_fmt)


"""Parse the input polarization degree and angle.

This is provided in a text file in ten phase bins, and we exactly the same
magic that we do for the integral flux above.
"""
pol_degree_file_path = _full_path("gk_per_pol_degree_angle_ph10.txt")
_dummy, _phase, _degree, _angle = numpy.loadtxt(pol_degree_file_path, unpack=True)
_degree /= 100.0
# Duplicate the values at phi=0 for phi=1.
_phase = numpy.append(_phase, 1.0)
_degree = numpy.append(_degree, _degree[0])
_angle = numpy.append(_angle, _angle[0])
_fmt = dict(xname="Phase", yname="Polarization degree")
Exemple #8
0
_energy, _pol_degree = numpy.loadtxt(POL_DEGREE_FILE_PATH, unpack=True)

#Switch to have degrees
_pol_degree /= 100.
#_pol_degree = _pol_degree
#print "Here are the energy and pol degree values"
#print _energy
#print
#print _pol_degree
# Filter the data points to reduce the noise.
#_pol_degree = scipy.signal.wiener(_pol_degree, 5)
_mask = (_energy >= MIN_ENERGY)*(_energy <= MAX_ENERGY)
_energy = _energy[_mask]
_pol_degree = _pol_degree[_mask]
fmt = dict(xname='Energy', yname='Polarization degree')
pol_degree_spline = xInterpolatedUnivariateSpline(_energy, _pol_degree, k=1, **fmt)


# Build the polarization angle as a function of the energy.
_energy, _pol_angle = numpy.loadtxt(POL_ANGLE_FILE_PATH, unpack=True)
_pol_angle = numpy.deg2rad(_pol_angle)

#Switched to have degrees and not radians

#_pol_angle = _pol_angle
#print "Here are the energy and pol angle values"
#print _energy
#print
#print _pol_angle
# Filter the data points to reduce the noise.
#_pol_angle = scipy.signal.wiener(_pol_angle, 2)
    return os.path.join(XIMPOL_CONFIG, 'ascii', file_name)

# Grab all the relevant files.
SPEC_FILE_PATH = _full_path('Crab_PhaseResolvedSpectrum.txt')
PDEG_FILE_PATH = _full_path('Crab_Polarization_Degree.txt')
PANG_FILE_PATH = _full_path('Crab_Polarization_Angle.txt')

# Read the file with the phase-resolved spectral parameters.
_phi0, _phi1, _index, _index_err, _norm,\
    _norm_err = numpy.loadtxt(SPEC_FILE_PATH, unpack=True)
_phi = 0.5*(_phi0 + _phi1)

# Build the PL normalization as a function of the phase.
fmt = dict(xname='Pulsar phase', yname='PL normalization',
           yunits='cm$^{-2}$ s$^{-1}$ keV$^{-1}$')
pl_normalization_spline = xInterpolatedUnivariateSpline(_phi, _norm, k=3, **fmt)

# 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)
Exemple #10
0
# Grab all the relevant files.
SPEC_FILE_PATH = _full_path('Crab_PhaseResolvedSpectrum.txt')
PDEG_FILE_PATH = _full_path('Crab_Polarization_Degree.txt')
PANG_FILE_PATH = _full_path('Crab_Polarization_Angle.txt')

# Read the file with the phase-resolved spectral parameters.
_phi0, _phi1, _index, _index_err, _norm,\
    _norm_err = numpy.loadtxt(SPEC_FILE_PATH, unpack=True)
_phi = 0.5 * (_phi0 + _phi1)

# Build the PL normalization as a function of the phase.
fmt = dict(xname='Pulsar phase',
           yname='PL normalization',
           yunits='cm$^{-2}$ s$^{-1}$ keV$^{-1}$')
pl_normalization_spline = xInterpolatedUnivariateSpline(_phi,
                                                        _norm,
                                                        k=3,
                                                        **fmt)


# 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')