Example #1
0
def plot_swift_lc(grb_list,show=True):
    """Plots Swift GRB light curves.
    """
    plt.figure(figsize=(10, 8), dpi=80)
    plt.title('Swift XRT light curves')
    num_grb = 0
    for grb_name in grb_list:
        flux_outfile = download_swift_grb_lc_file(grb_name, min_obs_time=21600)
        if flux_outfile is not None:
            integral_flux_spline = parse_light_curve(flux_outfile)
            if integral_flux_spline is not None:
                if grb_name == 'GRB 130427A':
                    integral_flux_spline.plot(num_points=1000,logx=True,\
                                              logy=True,show=False,\
                                              color="red",linewidth=1.0)
                    num_grb += 1
                else:
                    c = random.uniform(0.4,0.8)
                    integral_flux_spline.plot(num_points=1000,logx=True,\
                                              logy=True,show=False,\
                                              color='%f'%c,linewidth=1.0)
                    num_grb += 1
        else:
            continue
    logger.info('%i GRBs included in the plot.'%num_grb)
    if show:
        plt.show()
Example #2
0
def get_grb_mdp(grb_name, repointing=21600., obs_time=100000, \
                aeff_file_path=os.path.join(XIMPOL_IRF,'fits',\
                                            'xipe_baseline.arf'), \
                mu_file_path=os.path.join(XIMPOL_IRF,'fits',\
                                             'xipe_baseline.mrf')):
    """Calculate the MDP, given GRB name, the repointing elapsed time [sec]
       after the trigger, and the oservationrange of time [sec]
    """
    file_path = download_swift_grb_lc_file(grb_name)
    if file_path is not None:
        index = get_grb_spec_index(file_path)
        E_light_curve = parse_light_curve(file_path,num_min_data=10.)
        #E_light_curve.plot(logx=True,logy=True)
        if E_light_curve is not None:
            scale_factor = (2. - index)/(numpy.power(MAX_ENERGY, 2. - index) - \
                                     numpy.power(MIN_ENERGY, 2. - index))
            scale_factor *= 6.242e8
            light_curve = E_light_curve.scale(scale_factor)
            #light_curve.plot(logx=True,logy=True)
            t_min = repointing
            if t_min < light_curve.xmin():
                logger.info('Repointing time < to the minimum time of the burst...')
                return None
            else:
                t_max = t_min + obs_time
                if t_max > light_curve.xmax():
                    t_max = light_curve.xmax()
                norm = light_curve.integral(t_min,t_max)
                _energy = numpy.linspace(2.,MAX_ENERGY,1000)
                _modf = get_eff_mu(_energy,mu_file_path)
                _spectrum = get_spectrum(_energy,norm,index,aeff_file_path)
                energy_spectrum = xInterpolatedUnivariateSplineLinear(_energy,\
                                                                      _spectrum)
                #plt.xlim(1.,10.)
                #energy_spectrum.plot(logx=True,logy=True)
                
                num_evt = energy_spectrum.integral(MIN_ENERGY,MAX_ENERGY)
                if not math.isnan(num_evt) and num_evt != 0.:
                    logger.info('Total estimated number of events: %i'\
                                %int(num_evt))
                    mu = numpy.average(_modf,weights=_spectrum)
                    mdp = mdp99(mu,int(num_evt))
                    if not math.isnan(mdp):
                        logger.info('%.2f--%.2f keV: %i counts (%.1e s), mu %.2f, MDP %.2f%%'\
                                    %(MIN_ENERGY,MAX_ENERGY,int(num_evt),\
                                      obs_time,mu,mdp*100))
                        return mdp
                    else:
                        return None
                else:
                    logger.info('Total number of events cannot be estimated...')
                    return None
        else:
            return None
    else:
        return None
Example #3
0
def get_integral_flux(grb_name,delta_t=600):
    file_path = download_swift_grb_lc_file(grb_name)
    if file_path is not None:
        index = get_grb_spec_index(file_path)
        E_light_curve = parse_light_curve(file_path,num_min_data=10.)
        if E_light_curve is not None:
            scale_factor = (2. - index)/(numpy.power(MAX_ENERGY, 2. - index) - \
                                     numpy.power(MIN_ENERGY, 2. - index))
            scale_factor *= 6.242e8
            light_curve = E_light_curve.scale(scale_factor)
            t_min = light_curve.xmin()
            t_max = t_min + delta_t
            flux = light_curve.integral(t_min,t_max)
            return flux, light_curve.xmin()
        else:
            return None, None
    else:
        return None, None
Example #4
0
def process_grb(grb_name, tstart=21600., duration=30000., prompt_duration=600):
    """
    """
    file_path = download_swift_grb_lc_file(grb_name)
    if file_path is None:
        return None
    pl_index = get_grb_spec_index(file_path)
    ra, dec = get_grb_position(file_path)
    light_curve = parse_light_curve(file_path, num_min_data=5.)
    if light_curve is None:
        return None
    t = light_curve.x
    grb_start, prompt_tstart = t[0], t[0]
    grb_stop = t[-1]
    prompt_tstop = t[0] + prompt_duration
    prompt_flux = light_curve.integral(prompt_tstart, prompt_tstop)
    logger.info('Integral energy flux in %.3f--%.3f s: %.3e erg cm^{-2}' %\
                (prompt_tstart, prompt_tstop, prompt_flux))
    tstart = max(tstart, t[0])
    tstop = min(tstart + duration, t[-1])
    logger.info('Effective time interval for the MDP: %.3f--%.3f s' %\
                (tstart, tstop))
    t = t[(t >= tstart)*(t <= tstop)]
    if len(t) < 2:
        return None
    scale_factor = int_eflux2pl_norm(1., 0.3, 10., pl_index, erg=True)
    pl_norm = light_curve.scale(scale_factor)# Fix the label.
    def energy_spectrum(E, t):
        return pl_norm(t)*numpy.power(E, -pl_index)
    count_spectrum = xCountSpectrum(energy_spectrum, aeff, t)
    mdp_table = count_spectrum.build_mdp_table(ENERGY_BINNING, modf)
    logger.info(mdp_table)
    mdp = mdp_table.mdp_values()[0]
    eff_mu = [row.mu_effective for row in mdp_table.rows]
    counts = [row.num_signal for row in mdp_table.rows]
    grb_values = [ra, dec, pl_index, tstart, tstop, prompt_flux, prompt_tstart,\
                  prompt_tstop, grb_start, grb_stop, eff_mu[0], counts[0], mdp]
    return grb_values