Exemplo n.º 1
0
def parse_eph(filenm):
    global period, time
    suffix = filenm.split(".")[-1]
    if suffix=="bestprof":
        x = bestprof.bestprof(filenm)
        fs = pu.p_to_f(x.p0_bary, x.p1_bary, x.p2_bary)
        epoch = x.epochi_bary + x.epochf_bary
        T = x.T
    elif suffix=="par":
        x = parfile.psr_par(filenm)
        # Try to see how many freq derivs we have
        fs = [x.F0]
        for ii in range(1, 20):  # hopefully 20 is an upper limit!
            attrib = "F%d"%ii
            if hasattr(x, attrib):
                fs.append(getattr(x, attrib))
            else:
                break
        epoch = x.PEPOCH
        T = (x.FINISH - x.START) * 86400.0
    else:
        print("I don't recognize the file type for", filenm)
        sys.exit()
    newts = epoch + num.arange(int(T/10.0+0.5), dtype=num.float)/8640.0
    time = num.concatenate((time, newts))
    newps = 1.0 / pu.calc_freq(newts, epoch, *fs)
    period = num.concatenate((period, newps))
    print("%13.7f (%0.1f sec): " % (epoch, T), fs)
Exemplo n.º 2
0
def parse_eph(filenm):
    global period, time
    suffix = filenm.split(".")[-1]
    if suffix == "bestprof":
        x = bestprof.bestprof(filenm)
        fs = pu.p_to_f(x.p0_bary, x.p1_bary, x.p2_bary)
        epoch = x.epochi_bary + x.epochf_bary
        T = x.T
    elif suffix == "par":
        x = parfile.psr_par(filenm)
        # Try to see how many freq derivs we have
        fs = [x.F0]
        for ii in range(1, 20):  # hopefully 20 is an upper limit!
            attrib = "F%d" % ii
            if hasattr(x, attrib):
                fs.append(getattr(x, attrib))
            else:
                break
        epoch = x.PEPOCH
        T = (x.FINISH - x.START) * 86400.0
    else:
        print "I don't recognize the file type for", filenm
        sys.exit()
    newts = epoch + num.arange(int(T / 10.0 + 0.5), dtype=num.float) / 8640.0
    time = num.concatenate((time, newts))
    newps = 1.0 / pu.calc_freq(newts, epoch, *fs)
    period = num.concatenate((period, newps))
    print "%13.7f (%0.1f sec): " % (epoch, T), fs
Exemplo n.º 3
0
def fold_event_prof(ph_mjd, par_file, nbins=16):
    params = read_par(par_file, file_format='tempo2')
    # Fold data with psr_utils by first calculating phases for each MJD based on par file's F0, F1, ... only good for isolated puslar
    ph_phase = pu.calc_phs(ph_mjd, params['pepoch'], params['f'][0], params['f'][1], params['f'][2], params['f'][3])
    # Cast all negative phases to be between 0 and 1
    while (len(np.where(ph_phase <= 0.)[0]) > 0 ): # Need the [0] because output is a tuple...
        below_zero_ind = np.where(ph_phase <= 0.)
        ph_phase[below_zero_ind] += 1.0
    # Now do the same to ensure anything greater than 1.0 is between (0., 1.0)
    while (len(np.where(ph_phase > 1.0)[0]) > 0 ): # Need the [0] because output is a tuple...
        above_zero_ind = np.where(ph_phase > 1.0)
        ph_phase[below_zero_ind] -= 1.0


    # Now histogram phases to create profile for a given number of bins (and thus bin size), and give user option to split data into N equal parts
    prof, bin_edges = np.histogram(ph_phase, bins=nbins, range=(0.,1.))
    bin_size = (bin_edges[1] - bin_edges[0])
    bin_val = bin_edges[0: len(bin_edges)-1] + bin_size/2.
    prof_err = np.sqrt(prof)
    # Calculate central MJD and MJD span for profile from min/max event MJDs
    if(len(ph_mjd) > 0):
        mjd_mean = 0.5*(np.min(ph_mjd) + np.max(ph_mjd))
        mjd_span = np.max(ph_mjd) - np.min(ph_mjd)
        # spin phase at representative MJD of profile
        ref_phase = pu.calc_phs(mjd_mean, params['pepoch'], params['f'][0], params['f'][1], params['f'][2], params['f'][3])
        # Cast to be between 0 and 1
        while(ref_phase <= 0.):
            ref_phase += 1.0
        while(ref_phase > 1.0):
            ref_phase -= 1.0
        ref_freq  = pu.calc_freq(mjd_mean, params['pepoch'], params['f'][0], params['f'][1], params['f'][2], params['f'][3])
    else:
        mjd_mean = 0.
        ref_phase = 0.
        ref_freq = 0.
    profile = {'i':prof, 'i_err':prof_err, 'phase':bin_val, 'mjd':mjd_mean, 'mjd_span': mjd_span, 'psrname':params['psr'], 'ref_phase':ref_phase, 'ref_freq':ref_freq}
    return profile
Exemplo n.º 4
0
def plot_file_panel(in_file, params):
    period = []
    time = []
    if in_file.endswith('.par'):
        (epoch, T, fs) = read_par(in_file)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = minute / 1440.0
            time.append(t)
            period.append(1.0 / psr_utils.calc_freq(epoch + t, epoch, *fs))
    else:
        (epoch, T, p0, p1, p2) = read_bestprof(in_file)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = minute * 60.0
            time.append(minute / 1440.0)
            period.append(p0 + t * (p1 + 0.5 * t * p2))
    print "Plotting: file,  epoch, Tobs", in_file, epoch, T
    period = asarray(period)
    time = asarray(time)
    plot(time, period * 1000.0, 'o')
    xlabel('Time (s)')
    ylabel('Period (ms)')
    title("%.3f" % (epoch, ))
    model_time = arange(epoch - 0.1, epoch + max(time) + 0.1, 0.001)
    plot(model_time - epoch, doppler_period(params, model_time) * 1000.0, 'r')
Exemplo n.º 5
0
def plot_file_panel(in_file, params):
    period = []
    time = []
    if in_file.endswith(".par"):
        (epoch, T, fs) = read_par(in_file)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = minute / 1440.0
            time.append(t)
            period.append(1.0 / psr_utils.calc_freq(epoch + t, epoch, *fs))
    else:
        (epoch, T, p0, p1, p2) = read_bestprof(in_file)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = minute * 60.0
            time.append(minute / 1440.0)
            period.append(p0 + t * (p1 + 0.5 * t * p2))
    print "Plotting: file,  epoch, Tobs", in_file, epoch, T
    period = asarray(period)
    time = asarray(time)
    plot(time, period * 1000.0, "o")
    xlabel("Time (s)")
    ylabel("Period (ms)")
    title("%.3f" % (epoch,))
    model_time = arange(epoch - 0.1, epoch + max(time) + 0.1, 0.001)
    plot(model_time - epoch, doppler_period(params, model_time) * 1000.0, "r")
Exemplo n.º 6
0
        pcs = None
        fold.phs0 = 0.0
        (fold.f0, fold.f1, fold.f2) = psr_utils.p_to_f(fold.p0, fold.p1, fold.p2)

    #
    # Calculate the TOAs
    #

    for ii in range(numtoas):

        # The .pfd file was generated using -nosearch and a specified
        # folding period, p-dot, and p-dotdot (or f, f-dot, and f-dotdot).
        if (pcs is None):
            # Time at the middle of the interval in question
            midtime = fold.epoch + (ii+0.5)*timestep_day
            p = 1.0/psr_utils.calc_freq(midtime, fold.epoch, fold.f0, fold.f1, fold.f2)
            t0 = psr_utils.calc_t0(midtime, fold.epoch, fold.f0, fold.f1, fold.f2)
            t0i= int(t0 + 1e-9)
            t0f = t0 - t0i
        # The .pfd file was folded using polycos
        else:
            # Time at the middle of the interval in question
            mjdf = fold.epochf + (ii+0.5)*timestep_day
            (phs, f0) = pcs.get_phs_and_freq(fold.epochi, mjdf)
            phs -= fold.phs0
            p = 1.0/fold.f0
            t0f = mjdf - phs*p/SECPERDAY
            t0i = fold.epochi

        for jj in range(numsubbands):
            prof = profs[ii][jj]
Exemplo n.º 7
0
    #
    # Calculate the TOAs
    #

    if t2format:
        print "FORMAT 1"

    for ii in range(numtoas):

        # The .pfd file was generated using -nosearch and a specified
        # folding period, p-dot, and p-dotdot (or f, f-dot, and f-dotdot).
        if (pcs is None):
            # Time at the middle of the interval in question
            midtime = fold.epoch + (ii + 0.5) * timestep_day
            p = 1.0 / psr_utils.calc_freq(midtime, fold.epoch, fold.f0,
                                          fold.f1, fold.f2)
            t0 = psr_utils.calc_t0(midtime, fold.epoch, fold.f0, fold.f1,
                                   fold.f2)
            t0i = int(t0 + 1e-9)
            t0f = t0 - t0i
        # The .pfd file was folded using polycos
        else:
            # Time at the middle of the interval in question
            mjdf = fold.epochf + (ii + 0.5) * timestep_day
            (phs, f0) = pcs.get_phs_and_freq(fold.epochi, mjdf)
            phs -= fold.phs0
            p = 1.0 / f0
            if (phs < 0.0): phs += 1.0  # Consistent with pat
            t0f = mjdf - phs * p / SECPERDAY
            t0i = fold.epochi
Exemplo n.º 8
0
def get_ml_toa(fits_fn, prof_mod, parfile, scope='swift', print_offs=None, 
               frequency=None, epoch=None,  sim=False, bg_counts=0, Emin=None, 
               Emax=None, gauss_err=False, tempo2=False, debug=False, 
               correct_pf=False, split_num=None, split_orbits=False):

    print_timings = False # if want to print summary of runtime

    fits = pyfits.open(fits_fn)
    t = smu.fits2times(fits_fn, scope=scope, Emin=Emin, Emax=Emax)

    #if scope != 'chandra':
    #    exposure = fits[0].header['EXPOSURE']

    try:
        obsid = fits[0].header['OBS_ID']
    except KeyError:
        obsid = os.path.basename(fits_fn)

    if bg_counts < 0:
        bg_scale = -1.0*bg_counts
        bg_fits_fn = fits_fn.replace('reg','bgreg')
        bg_fits = pyfits.open(bg_fits_fn)
        bg_counts = int(bg_fits[1].header['NAXIS2'] * bg_scale)
        print 'BG Counts:',bg_counts
        bg_fits.close()
    if frequency and epoch:
        par = lambda: None
        par.epoch = epoch
        par.f0 = frequency
        par.fdots = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
    else:
        par = PSRpar(parfile)

    # split times into multiple arrays if needed
    if split_orbits:
        dt = t[1:] - t[:-1]
        splits = np.where(dt > 0.0116)[0] # 1 ks in days
        if len(splits):
            ts = [ t[:splits[0]] ]
            for i in range(len(splits)-1):
                ts.append(t[splits[i]+1:splits[i+1]])
            ts.append(t[splits[-1]+1:])
        else:
            ts = np.atleast_2d(t)

    elif split_num:
        remainder = len(t) % split_num
        if remainder:
            sys.stderr.write("Warning: Number of events in %s not divisable by %d. " \
                             "Dropping last %d events.\n" % (obsid, split_num, remainder))
            ts = np.split(t[:-remainder],split_num)
        else:
            ts = np.split(t,split_num)

    else:
        ts = np.atleast_2d(t)

    if len(ts) > 1 and debug:
        plt.figure()
        for t in ts:
            nbins = int((t[-1] - t[0]) * 8640.0)
            hist = np.histogram(t,bins=nbins)
            plt.plot(hist[1][:-1],hist[0],c='b')
            plt.axvline(t[0],ls='--',c='k',lw=2)
            plt.axvline(t[-1],ls='-',c='k',lw=2)
        plt.show()
           

    for i,t in enumerate(ts):
        sys.stderr.write('Measuring TOA #%d for %s\n' % (i+1,obsid))

        phases = smu.times2phases(t, parfile)

        if correct_pf:
            old_model, new_model, corr_folded = correct_model(phases,prof_mod)
        maxoff, error = calc_toa_offset(phases,prof_mod.prof_mod,sim_err=sim,bg_counts=bg_counts, gauss_err=gauss_err, debug=debug)
        midtime = (t[-1]+t[0])/2.0
        p_mid = 1.0/psr_utils.calc_freq(midtime, par.epoch, par.f0, par.fdots[0], par.fdots[1], par.fdots[2], par.fdots[3],
                                        par.fdots[4], par.fdots[5], par.fdots[6], par.fdots[7], par.fdots[8]) 

        t0 = psr_utils.calc_t0(midtime, par.epoch, par.f0, par.fdots[0], par.fdots[1], par.fdots[2], par.fdots[3],
                               par.fdots[4], par.fdots[5], par.fdots[6], par.fdots[7], par.fdots[8]) 
        t0i = int(t0)
        t0f = t0 - t0i

        toaf = t0f + maxoff*p_mid / SECPERDAY
        newdays = int(np.floor(toaf))
        
 
        if tempo2:
            psr_utils.write_tempo2_toa(t0i+newdays, toaf-newdays, error*p_mid*1.0e6, 0000, 0.0, name=obsid) 
        else:
            psr_utils.write_princeton_toa(t0i+newdays, toaf-newdays, error*p_mid*1.0e6, 0000, 0.0, name=obsid) 

        if print_offs:
            offs_file = open(print_offs,'a')
            #print "\t",error*p_mid*1.0e6,"\t",exposure # this was for checking uncertainties scaling with exposure time
            offs_file.write(fits_fn + "\t" + str(maxoff) + "\t" + str(error) + "\n")
            #print obsid,"\tOffset:",maxoff,"+/-",error 
            offs_file.close()

        fits.close()

        
        #double check PF correction with measuring binned model pulsed fraction
        if correct_pf and debug:
            plt.figure()
            nbins = len(corr_folded[0])
            uncertainties = np.sqrt(corr_folded[0])
            area = np.sum(corr_folded[0],dtype='float')/nbins
            plt.step(corr_folded[1][:-1],np.roll(corr_folded[0]/area,int(1.0-maxoff*nbins)),where='mid')
            plt.errorbar(corr_folded[1][:-1],np.roll(corr_folded[0]/area,int(1.0-maxoff*nbins)),uncertainties/area,fmt='ko')
            model_x = np.linspace(0,1,100)
            plt.plot(model_x,old_model(model_x),label='uncorrected')
            plt.plot(model_x,new_model(model_x),label='corrected')
            plt.legend()
            plt.show()

    if print_timings:
        global calcprobtime
        global logsumtime 
        global integratetime

        sys.stderr.write('\tCalc Prob: %f s\n' % calcprobtime)
        sys.stderr.write('\tLog Sum: %f s\n' % logsumtime)
        sys.stderr.write('\tIntegrate Norm: %f s\n' % integratetime)
Exemplo n.º 9
0
pepochs = []
p0s = []
p1s = []
# get the observed periods and times from the .bestprof files
for in_file in in_files:
    if in_file.endswith('.par'):
        (epoch, T, fs) = read_par(in_file)
        if (fs[1] != 0.0):
            p0tmp, p1tmp = psr_utils.p_to_f(fs[0], fs[1])
            p0s.append(p0tmp)
            p1s.append(p1tmp)
            pepochs.append(epoch)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = epoch + minute / 1440.0
            time.append(t)
            period.append(1.0 / psr_utils.calc_freq(t, epoch, *fs))
    else:
        (epoch, T, p0, p1, p2) = read_bestprof(in_file)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = minute * 60.0
            time.append(epoch + minute / 1440.0)
            period.append(p0 + t * (p1 + 0.5 * t * p2))
            if p1 != 0.0:
                p0s.append(p0)
                p1s.append(p1)
                pepochs.append(epoch)

Torb = min(time)
period = asarray(period)
time = asarray(time)
p0s = asarray(p0s)
Exemplo n.º 10
0
pepochs = []
p0s = []
p1s = []
# get the observed periods and times from the .bestprof files
for in_file in in_files:
    if in_file.endswith(".par"):
        (epoch, T, fs) = read_par(in_file)
        if fs[1] != 0.0:
            p0tmp, p1tmp = psr_utils.p_to_f(fs[0], fs[1])
            p0s.append(p0tmp)
            p1s.append(p1tmp)
            pepochs.append(epoch)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = epoch + minute / 1440.0
            time.append(t)
            period.append(1.0 / psr_utils.calc_freq(t, epoch, *fs))
    else:
        (epoch, T, p0, p1, p2) = read_bestprof(in_file)
        for minute in arange(int(T / 60.0 + 0.5)):
            t = minute * 60.0
            time.append(epoch + minute / 1440.0)
            period.append(p0 + t * (p1 + 0.5 * t * p2))
            if p1 != 0.0:
                p0s.append(p0)
                p1s.append(p1)
                pepochs.append(epoch)

Torb = min(time)
period = asarray(period)
time = asarray(time)
p0s = asarray(p0s)
Exemplo n.º 11
0
def add_xte_prof(profile_in, params, days_add=0.5, mjd_start=None, mjd_finish=None, align=False, template=None, filter_files=None):

    # Assume we are not adding all profs together to start
    add_all = False

    if((mjd_start != None) & (mjd_finish != None) & (mjd_start > mjd_finish)):
        print 'WARNING: mjd_start is not suppoed to be greater than mjd_finish'

    n_prof_in = len(profile_in)
    prof = []
    prof_mjd = []
    for prof_in in profile_in:
        prof.append(prof_in['i'])
        prof_mjd.append(prof_in['mjd'])

    prof = np.array(prof)
    prof_mjd = np.array(prof_mjd)

    # Sort profs by MJD
    sort_ind = np.argsort(prof_mjd)
    prof_mjd = prof_mjd[sort_ind]
    prof = prof[sort_ind]

    # If only certain MJDs are specified in arguments, then filter out those MJDs only for the rest of the function.  It will
    # then just think these were all the input files, and if all are added together then it would behave the same, but now only selected
    # files would be used.  This is the easiest way to allow selected MJD range.
    # So, add an if statement here that if we are only doing selected dates then no need for while loop; just add all within specified MJDs
    #if(mjd_start  != None): prof = prof[prof_mjd >= mjd_start]
    #if(mjd_finish != None): prof = prof[prof_mjd <= mjd_finish]


    # Initialize starting index
    if(mjd_start != None):
        # Get mjd differences between prof_mjd and mjd_start, take abs of negative ones, get smallest one.
        i_first = np.argmin(np.abs(prof_mjd-mjd_start))
        if ((prof_mjd[i_first] > mjd_start) & (i_first > 0)): i_first -= 1
        #i_first = np.where(prof_mjd == mjd_start)[0]
    else:
        i_first = 0

    if(mjd_finish != None):
        i_last = np.argmin(np.abs(prof_mjd-mjd_finish))
        if ( (prof_mjd[i_last] < mjd_start) & (i_last< (len(prof_mjd) - 1)) ): i_last += 1
 #       i_last = np.where(prof_mjd == mjd_finish)[0]
    else:
        i_last = len(prof_mjd) - 1

    print ''
    print 'MJD start  = ', mjd_start,  ' -- prof_mjd[i_first] = ', prof_mjd[i_first], ' -- i_first = ', i_first
    print 'MJD finish = ', mjd_finish, ' -- prof_mjd[i_last]  = ', prof_mjd[i_last],  ' -- i_last = ', i_last
    print ''

    # If days_add is negative, then add everything in list
    if(days_add < 0):
###        days_add = np.max(prof_mjd) - np.min(prof_mjd) + 10.0 # add some extra days just to be safe
        days_add = prof_mjd[i_last] - prof_mjd[i_first]  # add some extra days just to be safe
#        print "DAYS ADD = ", days_add, ' = ', days_add/365.0, ' years'
        add_all = True


    # Same number of phase bins for all profiles, pre- and post-added
    prof_phase_out, bin_size = np.linspace(0., 1., num=np.shape(prof)[1], endpoint=False, retstep=True)
    prof_phase_out += bin_size/2.

#    print 'prof_mjd = ', prof_mjd

#    print 'new_mjd = '
    # While we are still before the end of the MJD array
    profile_out = []
####    while(i_first < len(prof_mjd)):
    while(i_first < i_last+1):
        bin_inds = (prof_mjd >= prof_mjd[i_first]) & ( prof_mjd < (prof_mjd[i_first] + days_add) )
        n_prof_out = len(np.where(bin_inds)[0]) # Number of profs going into added prof
        #print prof_mjd[bin_inds]

        current_profs = prof[bin_inds]
        #print np.shape(current_profs), np.shape(prof), n_prof_out
        # Now, if we want to align profiles before adding, do so, rotating profiles in place:
        if(align==True):
            # In this case, choose the profile with the best 'signal', which I will take to be the largest
            # difference between peak and minimum
            if(template==None):
                i_template = 0
                max_peak = -1.
                for i_prof in range(n_prof_out):
                    peak_height = np.max(current_profs[i_prof]) - np.min(current_profs[i_prof])
                    if(peak_height > max_peak):
                        max_peak = peak_height
                        i_template = i_prof
                # Now run through and align profiles with our found template.  Skip if profile is template.
                for i_prof in range(n_prof_out):
                    if(i_prof != i_template):
                        current_profs[i_prof] = prof_align(current_profs[i_prof], current_profs[i_template])
            # If we have a template, just use it:
            else:
                for i_prof in range(n_prof_out):
                    current_profs[i_prof] = prof_align(current_profs[i_prof], template)

        # Now, just add!
        prof_out = np.sum(current_profs, 0)  # I think 0 dim is time, and 1 is phase bins.  This way, preserve phase bins.\
        prof_err_out = np.sqrt(prof_out)
        # Calculate central MJD based on min and max (central) MJD in sub-array
        prof_mjd_out = 0.5*(np.min(prof_mjd[bin_inds]) + np.max(prof_mjd[bin_inds]))
        # Calculate ref phase and period for this MJD:
        prof_ref_phase = pu.calc_phs(prof_mjd_out, params['pepoch'], params['f'][0], params['f'][1], params['f'][2], params['f'][3])
            # Cast to be between 0 and 1
        while(prof_ref_phase <= 0.):
            prof_ref_phase += 1.0
        while(prof_ref_phase > 1.0):
            prof_ref_phase -= 1.0
       # prof_phase_out = ref_phase
        prof_ref_freq  = pu.calc_freq(prof_mjd_out, params['pepoch'], params['f'][0], params['f'][1], params['f'][2], params['f'][3])

        # Create file name
        profile_out.append({'i':prof_out, 'i_err':prof_err_out, 'phase':prof_phase_out, 'mjd':prof_mjd_out, 'psrname':params['psr'], 'ref_phase':prof_ref_phase, 'ref_freq':prof_ref_freq})
        # Now increase i_first to point to the beginning of next set
        i_first = np.where(bin_inds)[0][-1] + 1  # = last index of previous set of indices, plus 1

    # If there is only one output file, then just make profile_out a single profile (i.e. not an array of profiles)
    if(add_all):
       profile_out = profile_out[0]
       pass

#    print 'OUT PROFS = ', profile_out
#    print 'LEN OUT PROFS = ', len(profile_out)
    return profile_out