Beispiel #1
0
def plot_toa(numtoa, pulse, template=None, pulseshift=0, \
                templateshift=0, basefn=""):
    """Plot the profile used for a TOA.
        - 'numtoa' is the TOA's number within an observation.
        - 'pulse' is the SummedPulse object used to generate 
                the TOA.
        - 'template' is the template used for generating the
                TOA. If it is provided it will be overlayed 
                on the plot. 'template' should be a np.array.
        - 'pulseshift' is an amount of phase to shift pulse by.
        - 'templateshift' is an amount of phase template is shifted by.
        - 'basefn' is the base of the output filename to use.
    """
    if basefn:
        outfn = "%s.TOA%d.ps" % (basefn, numtoa)
    else:
        outfn = "TOA%d.ps" % numtoa

    # scale pulse
    copy_of_pulse = pulse.make_copy()
    copy_of_pulse.scale()
    phases = np.linspace(0, 1.0, copy_of_pulse.N)
    tshifted_phases = (phases - templateshift + pulseshift) % (1.0 + 1e-7)
    tsorted_phase_indices = np.argsort(tshifted_phases)
    # Re-order template
    shifted_template = template[tsorted_phase_indices]
    plt.figure()
    plt.plot(phases, copy_of_pulse.profile, 'k-', lw=0.5)
    if template is not None:
        plt.plot(phases, shifted_template, 'k:', lw=0.5)
    plt.xlabel("Phase (%d profile bins)" % copy_of_pulse.N)
    plt.ylabel("SNR")
    plt.title("TOA #%d" % numtoa)
    plt.savefig(outfn, orientation='landscape')
Beispiel #2
0
def plot_toa(numtoa, pulse, template=None, pulseshift=0, \
                templateshift=0, basefn=""):
    """Plot the profile used for a TOA.
        - 'numtoa' is the TOA's number within an observation.
        - 'pulse' is the SummedPulse object used to generate 
                the TOA.
        - 'template' is the template used for generating the
                TOA. If it is provided it will be overlayed 
                on the plot. 'template' should be a np.array.
        - 'pulseshift' is an amount of phase to shift pulse by.
        - 'templateshift' is an amount of phase template is shifted by.
        - 'basefn' is the base of the output filename to use.
    """
    if basefn:
        outfn = "%s.TOA%d.ps" % (basefn, numtoa)
    else:
        outfn = "TOA%d.ps" % numtoa
    
    # scale pulse
    copy_of_pulse = pulse.make_copy()
    copy_of_pulse.scale()
    phases = np.linspace(0, 1.0, copy_of_pulse.N)
    tshifted_phases = (phases-templateshift+pulseshift) % (1.0+1e-7)
    tsorted_phase_indices = np.argsort(tshifted_phases)
    # Re-order template
    shifted_template = template[tsorted_phase_indices]
    plt.figure()
    plt.plot(phases, copy_of_pulse.profile, 'k-', lw=0.5)
    if template is not None:
        plt.plot(phases, shifted_template, 'k:', lw=0.5)
    plt.xlabel("Phase (%d profile bins)" % copy_of_pulse.N)
    plt.ylabel("SNR")
    plt.title("TOA #%d" % numtoa)
    plt.savefig(outfn, orientation='landscape')
Beispiel #3
0
def main():
    pulses = get_pulses(args)
    # Sort pulses by increasing MJD
    cmp_mjd = lambda this, other: cmp(this.mjd, other.mjd)
    pulses.sort(cmp=cmp_mjd)

    print "Number of pulses loaded: %d" % len(pulses)
    if len(pulses):
        numtoas = 0
        print "Generating TOAs. Please wait..."
        print "TOA threshold:", options.toa_threshold
        print "Min number of pulses for a TOA:", options.min_pulses
        print "Profile template used:", options.template
        # Extract profile template file
        # (Note: first column is index, second column is profile values)
        template = np.loadtxt(options.template, usecols=(1, ))
        # Get TOAs and write them to stdout
        summed_pulse = None
        numpulses = 0
        registry = []  # too keep track of what pulses make up what TOA
        for pulse in pulses:
            if summed_pulse is None:
                summed_pulse = pulse.make_copy()
                numpulses = 1
                pulsenums = [pulse.number]
            else:
                summed_pulse = add_profs(summed_pulse, pulse)
                numpulses += 1
                pulsenums.append(pulse.number)
            if numpulses < options.min_pulses:
                continue
            if get_snr(summed_pulse) > options.toa_threshold:
                # Interpolate and downsample summed_pulse so
                # it is same size as template profile
                summed_pulse.interp_and_downsamp(template.size)
                summed_pulse.scale()
                pulseshift, templateshift = write_toa(summed_pulse, \
                            template, options.debug)
                numtoas += 1
                if options.write_toa_files:
                    # TOA profiles are already downfactored
                    # do not downfactor more when creating plot
                    plot_toa(numtoas, summed_pulse, template, \
                            pulseshift, templateshift)
                    summed_pulse.write_to_file("TOA%d" % numtoas)
                registry.append(pulsenums)
                summed_pulse = None
                numpulses = 0
        print "Number of TOAs: %d" % numtoas
        print "Number of pulses thrown out because 'min pulses' requirement " \
                "or SNR threshold not met: %d" % numpulses
        print "Registry of pulses:"
        for i, pulsenums in enumerate(registry):
            print "\tTOA #%d: %d pulses" % (i + 1, len(pulsenums))
            print "\t\t(pulse numbers: %s)" % ', '.join(
                [str(n) for n in pulsenums])
Beispiel #4
0
def main():
    pulses = get_pulses(args)
    # Sort pulses by increasing MJD
    cmp_mjd = lambda this, other: cmp(this.mjd, other.mjd)
    pulses.sort(cmp=cmp_mjd)

    print "Number of pulses loaded: %d" % len(pulses)
    if len(pulses):
        numtoas = 0
        print "Generating TOAs. Please wait..."
        print "TOA threshold:", options.toa_threshold
        print "Min number of pulses for a TOA:", options.min_pulses
        print "Profile template used:", options.template
        # Extract profile template file
        # (Note: first column is index, second column is profile values)
        template = np.loadtxt(options.template, usecols=(1,))
        # Get TOAs and write them to stdout
        summed_pulse = None
        numpulses = 0
        registry = [] # too keep track of what pulses make up what TOA
        for pulse in pulses:
            if summed_pulse is None:
                summed_pulse = pulse.make_copy()
                numpulses = 1
                pulsenums = [pulse.number]
            else:
                summed_pulse= add_profs(summed_pulse, pulse)
                numpulses += 1
                pulsenums.append(pulse.number)
            if numpulses < options.min_pulses:
                continue
            if get_snr(summed_pulse) > options.toa_threshold:
                # Interpolate and downsample summed_pulse so
                # it is same size as template profile
                summed_pulse.interp_and_downsamp(template.size)
                summed_pulse.scale()
                pulseshift, templateshift = write_toa(summed_pulse, \
                            template, options.debug)
                numtoas += 1
                if options.write_toa_files:
                    # TOA profiles are already downfactored
                    # do not downfactor more when creating plot
                    plot_toa(numtoas, summed_pulse, template, \
                            pulseshift, templateshift)
                    summed_pulse.write_to_file("TOA%d" % numtoas)
                registry.append(pulsenums)
                summed_pulse = None
                numpulses = 0
        print "Number of TOAs: %d" % numtoas
        print "Number of pulses thrown out because 'min pulses' requirement " \
                "or SNR threshold not met: %d" % numpulses
        print "Registry of pulses:"
        for i, pulsenums in enumerate(registry):
            print "\tTOA #%d: %d pulses" % (i+1, len(pulsenums))
            print "\t\t(pulse numbers: %s)" % ', '.join([str(n) for n in pulsenums])
Beispiel #5
0
def get_snr(pulse, uncertainty=1):
    """Compute and return statistics about the given pulse.
        Also accepts uncertainty on profile bins. 'uncertainty'
        can be a scalar, or numpy array.
    """
    # Scale profile
    copy_of_pulse = pulse.make_copy()
    copy_of_pulse.scale()
    snr = np.max(copy_of_pulse.get_on_pulse())
    # snr = np.sum(copy_of_pulse.get_on_pulse())
    warnings.warn("Only checking on-pulse region for pulses.")
    return snr
Beispiel #6
0
def get_snr(pulse, uncertainty=1):
    """Compute and return statistics about the given pulse.
        Also accepts uncertainty on profile bins. 'uncertainty'
        can be a scalar, or numpy array.
    """
    # Scale profile
    copy_of_pulse = pulse.make_copy()
    copy_of_pulse.scale()
    snr = np.max(copy_of_pulse.get_on_pulse())
    # snr = np.sum(copy_of_pulse.get_on_pulse())
    warnings.warn("Only checking on-pulse region for pulses.")
    return snr
Beispiel #7
0
def add_profs(pulse, other):
    """Given two Pulse objects add the profile of 'other'
        to the profile of 'pulse'.

        Return a copy of 'pulse' with the summed profile.

        NOTE: The summed profile (of 'pulse') will have the
                length of the shorter of the two input profiles.
              No information of 'pulse' is changed.
    """
    copy = pulse.make_copy()
    # Truncate to size of smaller profile
    N = np.min([len(pulse.profile), len(other.profile)])
    copy.profile = pulse.profile[:N] + other.profile[:N]
    copy.N = N
    copy.duration = N * copy.dt
    return copy
Beispiel #8
0
def add_profs(pulse, other):
    """Given two Pulse objects add the profile of 'other'
        to the profile of 'pulse'.

        Return a copy of 'pulse' with the summed profile.

        NOTE: The summed profile (of 'pulse') will have the
                length of the shorter of the two input profiles.
              No information of 'pulse' is changed.
    """
    copy = pulse.make_copy()
    # Truncate to size of smaller profile
    N = np.min([len(pulse.profile), len(other.profile)])
    copy.profile = pulse.profile[:N] + other.profile[:N]
    copy.N = N
    copy.duration = N*copy.dt
    return copy