示例#1
0
文件: cycspec.py 项目: gitj/dynISM
def fixCyclicDedisp(fname, nchan=32, overwrite=False, ext='fix'):
    # copied from paul's fix_cyclic_dedisp script
    import psrchive
    import os
    import psr_utils
    arch = psrchive.Archive_load(fname)
    cf = arch.get_centre_frequency()
    bw = arch.get_bandwidth()
    f_lo = cf - bw/2.0
    nch = arch.get_nchan()
    pfb_nch = nchan
    pfb_bw = bw / pfb_nch
    chan_per_pfb = nch / pfb_nch
    dm = arch.get_dispersion_measure()
    for isub in range(arch.get_nsubint()):
        sub = arch[isub]
        per = sub.get_folding_period()
        for ichan in range(nch):
            pfb_cf = f_lo + ((ichan/chan_per_pfb)+0.5)*pfb_bw
            dt = psr_utils.delay_from_DM(dm,pfb_cf) - psr_utils.delay_from_DM(dm,cf)
            for ipol in range(sub.get_npol()):
                prof = sub.get_Profile(ipol,ichan)
                prof.rotate_phase(dt/per)
    #arch.set_dedispersed(True) # doesn't work, lame...
    if (overwrite):
        outf = fname
    else:
        outf = fname + '.' + ext
    arch.unload(outf)
    os.system("psredit -m -c dmc=1 %s" % outf)
示例#2
0
    def dedisperse(self, dm=0, padval=0, ref_freq=None):
        """Shift channels according to the delays predicted by
            the given DM.
            
            Inputs:
                dm: The DM (in pc/cm^3) to use.
                padval: The padding value to use when shifting
                    channels during dedispersion. See documentation
                    of Spectra.shift_channels. (Default: 0)

            Outputs:
                None

            *** Dedispersion happens in place ***
        """
        if ref_freq is None:
            ref_freq = 0.5*(self.freqs[0]+self.freqs[-1])
        assert dm >= 0
        ref_delay = psr_utils.delay_from_DM(dm-self.dm, ref_freq)
        delays = psr_utils.delay_from_DM(dm-self.dm, self.freqs)
        rel_delays = delays-ref_delay # Relative delay
        rel_bindelays = np.round(rel_delays/self.dt).astype('int')
        # Shift channels
        self.shift_channels(rel_bindelays, padval)

        self.dm=dm
示例#3
0
    def dedisperse(self, dm=0, padval=0, trim=False):
        """Shift channels according to the delays predicted by
            the given DM.
            
            Inputs:
                dm: The DM (in pc/cm^3) to use.
                padval: The padding value to use when shifting
                    channels during dedispersion. See documentation
                    of Spectra.shift_channels. (Default: 0)
                trim: Remove spectra that are no longer complete
                    (Default: False)
            Outputs:
                None
            *** Dedispersion happens in place ***
        """
        assert dm >= 0
        ref_delay = psr_utils.delay_from_DM(dm - self.dm, np.max(self.freqs))
        delays = psr_utils.delay_from_DM(dm - self.dm, self.freqs)
        rel_delays = delays - ref_delay  # Relative delay
        rel_bindelays = np.round(rel_delays / self.dt).astype('int')
        # Shift channels
        self.shift_channels(rel_bindelays, padval)

        self.dm = dm

        if trim:
            ntrim = max(rel_bindelays)
            if ntrim > 0:
                self.data = self.data[:, :-ntrim]
                self.numspectra -= ntrim
示例#4
0
    def shift(self,p=None,pdot=None,dm=None):
        if dm is None:
            dm = self.current_dm
        if p is None:
            p = self.current_p
        if pdot is None:
            pdot = self.current_pdot

        f = rfft(self.profs,axis=-1)

        # the sample at phase p is moved to phase p-dmdelays
        dmdelays = (psr_utils.delay_from_DM(dm,self.pfd.subfreqs) -
                    psr_utils.delay_from_DM(self.current_dm,self.pfd.subfreqs))/self.original_fold_p
        
        start_times = self.pfd.start_secs
        pdelays = (start_times/self.original_fold_p) * (p-self.current_p)/self.original_fold_p
        pdotdelays = ((pdot-self.current_pdot)*start_times**2/2.)/self.original_fold_p
        
        f *= np.exp((2.j*np.pi)*
            dmdelays[np.newaxis,:,np.newaxis]*
            np.arange(f.shape[-1])[np.newaxis,np.newaxis,:])
        f *= np.exp((2.j*np.pi)*
            (pdelays+pdotdelays)[:,np.newaxis,np.newaxis]*
            np.arange(f.shape[-1])[np.newaxis,np.newaxis,:])

        self.profs = irfft(f)
        self.current_p = p
        self.current_dm = dm
        self.current_pdt = pdot
示例#5
0
    def dedisperse(self, dm=0, padval=0, trim=False):
        """Shift channels according to the delays predicted by
            the given DM.
            
            Inputs:
                dm: The DM (in pc/cm^3) to use.
                padval: The padding value to use when shifting
                    channels during dedispersion. See documentation
                    of Spectra.shift_channels. (Default: 0)
                trim: Remove spectra that are no longer complete
                    (Default: False)

            Outputs:
                None

            *** Dedispersion happens in place ***
        """
        assert dm >= 0
        ref_delay = psr_utils.delay_from_DM(dm-self.dm, np.max(self.freqs))
        delays = psr_utils.delay_from_DM(dm-self.dm, self.freqs)
        rel_delays = delays-ref_delay # Relative delay
        rel_bindelays = np.round(rel_delays/self.dt).astype('int')
        # Shift channels
        self.shift_channels(rel_bindelays, padval)

        self.dm=dm

        if trim:
            ntrim = max(rel_bindelays)
            if ntrim > 0:
                self.data = self.data[:,:-ntrim]
                self.numspectra -= ntrim
示例#6
0
文件: cycspec.py 项目: gitj/dynISM
def fixCyclicDedisp(fname, nchan=32, overwrite=False, ext='fix'):
    # copied from paul's fix_cyclic_dedisp script
    import psrchive
    import os
    import psr_utils
    arch = psrchive.Archive_load(fname)
    cf = arch.get_centre_frequency()
    bw = arch.get_bandwidth()
    f_lo = cf - bw / 2.0
    nch = arch.get_nchan()
    pfb_nch = nchan
    pfb_bw = bw / pfb_nch
    chan_per_pfb = nch / pfb_nch
    dm = arch.get_dispersion_measure()
    for isub in range(arch.get_nsubint()):
        sub = arch[isub]
        per = sub.get_folding_period()
        for ichan in range(nch):
            pfb_cf = f_lo + ((ichan / chan_per_pfb) + 0.5) * pfb_bw
            dt = psr_utils.delay_from_DM(dm, pfb_cf) - psr_utils.delay_from_DM(
                dm, cf)
            for ipol in range(sub.get_npol()):
                prof = sub.get_Profile(ipol, ichan)
                prof.rotate_phase(dt / per)
    #arch.set_dedispersed(True) # doesn't work, lame...
    if (overwrite):
        outf = fname
    else:
        outf = fname + '.' + ext
    arch.unload(outf)
    os.system("psredit -m -c dmc=1 %s" % outf)
示例#7
0
def write_toa(summed_pulse, template_profile, debug=False):
    """Given a SummedPulse generate a TOA and write it to stdout. 
        'template_profile' is simply a numpy array.
        'debug' is a boolean value that determines if debugging
        info should be displayed.
        Returns shift required to line up template and pulse.
    """
    if template_profile is None:
        raise ValueError("A template profile MUST be provided.")
    # This code is taken from Scott Ransom's PRESTO's get_TOAs.py
    mjdi = int(summed_pulse.mjd) # integer part of MJD
    mjdf = summed_pulse.mjd - mjdi # fractional part of MJD
    # Period is duration of profile in seconds
    period = summed_pulse.dt*len(summed_pulse.profile)
    
    # Caclulate offset due to shifting channels to account for DM
    # Hifreq doesn't have a half-channel offset 
    # (why? because get_TOAs.py doesn't. Why...)
    # Why subtract 1 channel to get hifreq?
    hifreq = summed_pulse.lofreq + summed_pulse.bw - summed_pulse.chan_width
    midfreq = summed_pulse.lofreq - 0.5*summed_pulse.chan_width + 0.5*summed_pulse.bw
    dmdelay = psr_utils.delay_from_DM(summed_pulse.dm, midfreq) - \
              psr_utils.delay_from_DM(summed_pulse.dm, hifreq)
    dmdelay_mjd = dmdelay/float(psr_utils.SECPERDAY)
    if debug:
        colour.cprint("High frequency (MHz): %f" % hifreq, 'debug')
        colour.cprint("Mid frequency (MHz): %f" % midfreq, 'debug')
        colour.cprint("DM delay added to TOAs (s): %g" % dmdelay, 'debug')
        colour.cprint("DM delay added to TOAs (MJD): %g" % dmdelay_mjd, 'debug')

    t0f = mjdf + dmdelay_mjd
    t0i = mjdi
    shift,eshift,snr,esnr,b,errb,ngood,tphs = measure_phase(summed_pulse.profile, \
                            template_profile)
    # tphs is amount template is rotated by. It is originally 
    # measured in radians, convert to rotational phase
    tphs = tphs/(np.pi*2.0) % 1.0
    # tau and tau_err are the predicted phase of the pulse arrival
    tau, tau_err = shift/summed_pulse.N, eshift/summed_pulse.N
    if debug:
        colour.cprint("FFTFIT: Shift (bins): %f, Tau (phase): %f" % (shift, tau), 'debug')
        colour.cprint("FFTFIT: Shift error (bins): %f, Tau error (phase): %f" % \
                            (eshift, tau_err), 'debug')
    # Note: "error" flags are shift = 0.0 and eshift = 999.0
    if (np.fabs(shift) < 1e-7 and np.fabs(eshift-999.0) < 1e-7):
        raise FFTFitError("Error in FFTFIT. Bad return values.")
    # Send the TOA to STDOUT
    toaf = t0f + tau*period/float(psr_utils.SECPERDAY)
    if debug:
        colour.cprint("t0f (MJD): %r" % t0f, 'debug')
        colour.cprint("period (s): %r" % period, 'debug')
        colour.cprint("toaf (MJD): %r" % toaf, 'debug')
    newdays = int(np.floor(toaf))
    obs_code = telescopes.telescope_to_id[summed_pulse.telescope]
    psr_utils.write_princeton_toa(t0i+newdays, toaf-newdays, \
                                tau_err*period*1e6, midfreq, \
                                summed_pulse.dm, obs=obs_code)
    return tau, tphs
示例#8
0
    def subband(self, nsub, subdm=None, padval=0):
        """Reduce the number of channels to 'nsub' by subbanding.
            The channels within a subband are combined using the
            DM 'subdm'. 'padval' is passed to the call to
            'Spectra.shift_channels'.

            Inputs:
                nsub: Number of subbands. Must be a factor of 
                    the number of channels.
                subdm: The DM with which to combine channels within
                    each subband (Default: don't shift channels 
                    within each subband)
                padval: The padding value to use when shifting
                    channels during dedispersion. See documentation
                    of Spectra.shift_channels. (Default: 0)

            Outputs:
                None

            *** Subbanding happens in-place ***
        """
        assert (self.numchans % nsub) == 0
        assert (subdm is None) or (subdm >= 0)
        nchan_per_sub = self.numchans / nsub
        sub_hifreqs = self.freqs[np.arange(nsub) * nchan_per_sub]
        sub_lofreqs = self.freqs[(1 + np.arange(nsub)) * nchan_per_sub - 1]
        sub_ctrfreqs = 0.5 * (sub_hifreqs + sub_lofreqs)

        if subdm is not None:
            # Compute delays
            ref_delays = psr_utils.delay_from_DM(subdm - self.dm, sub_ctrfreqs)
            delays = psr_utils.delay_from_DM(subdm - self.dm, self.freqs)
            rel_delays = delays - ref_delays.repeat(
                nchan_per_sub)  # Relative delay
            rel_bindelays = np.round(rel_delays / self.dt).astype('int')
            # Shift channels
            self.shift_channels(rel_bindelays, padval)

        # Subband
        self.data = np.array([np.sum(sub, axis=0) for sub in \
                                np.vsplit(self.data, nsub)])
        self.freqs = sub_ctrfreqs
        self.numchans = nsub
示例#9
0
    def subband(self, nsub, subdm=None, padval=0):
        """Reduce the number of channels to 'nsub' by subbanding.
            The channels within a subband are combined using the
            DM 'subdm'. 'padval' is passed to the call to
            'Spectra.shift_channels'.

            Inputs:
                nsub: Number of subbands. Must be a factor of 
                    the number of channels.
                subdm: The DM with which to combine channels within
                    each subband (Default: don't shift channels 
                    within each subband)
                padval: The padding value to use when shifting
                    channels during dedispersion. See documentation
                    of Spectra.shift_channels. (Default: 0)

            Outputs:
                None

            *** Subbanding happens in-place ***
        """
        assert (self.numchans % nsub) == 0
        assert (subdm is None) or (subdm >= 0)
        nchan_per_sub = self.numchans/nsub
        sub_hifreqs = self.freqs[np.arange(nsub)*nchan_per_sub]
        sub_lofreqs = self.freqs[(1+np.arange(nsub))*nchan_per_sub-1]
        sub_ctrfreqs = 0.5*(sub_hifreqs+sub_lofreqs)
        
        if subdm is not None:
            # Compute delays
            ref_delays = psr_utils.delay_from_DM(subdm-self.dm, sub_hifreqs)
            delays = psr_utils.delay_from_DM(subdm-self.dm, self.freqs)
            rel_delays = delays-ref_delays.repeat(nchan_per_sub) # Relative delay
            rel_bindelays = np.round(rel_delays/self.dt).astype('int')
            # Shift channels
            self.shift_channels(rel_bindelays, padval)

        # Subband
        self.data = np.array([np.sum(sub, axis=0) for sub in \
                                np.vsplit(self.data, nsub)])
        self.freqs = sub_ctrfreqs
        self.numchans = nsub
示例#10
0
        def getDMcurve(
                M):  # return the normalized DM curve downsampled to M points
            feature = '%s:%s' % ('DMbins', M)
            if M == 0:
                return np.array([])
            if not feature in self.extracted_feature:
                ddm = (self.dms.max() - self.dms.min()) / 2.
                loDM, hiDM = (self.bestdm - ddm, self.bestdm + ddm)
                loDM = max((0, loDM))  #make sure cut off at 0 DM
                hiDM = max((ddm, hiDM))  #make sure cut off at 0 DM
                N = 100
                interp = False
                sumprofs = self.profs.sum(0)
                if not interp:
                    profs = sumprofs
                else:
                    profs = np.zeros(np.shape(sumprofs), dtype='d')
                DMs = psr_utils.span(loDM, hiDM, N)
                chis = np.zeros(N, dtype='f')
                subdelays_bins = self.subdelays_bins.copy()
                for ii, DM in enumerate(DMs):
                    subdelays = psr_utils.delay_from_DM(DM, self.barysubfreqs)
                    hifreqdelay = subdelays[-1]
                    subdelays = subdelays - hifreqdelay
                    delaybins = subdelays * self.binspersec - subdelays_bins
                    if interp:
                        interp_factor = 16
                        for jj in range(self.nsub):
                            profs[jj] = psr_utils.interp_rotate(
                                sumprofs[jj],
                                delaybins[jj],
                                zoomfact=interp_factor)
                        # Note: Since the interpolation process slightly changes the values of the
                        # profs, we need to re-calculate the average profile value
                        avgprof = (profs / self.proflen).sum()
                    else:
                        new_subdelays_bins = np.floor(delaybins + 0.5)
                        for jj in range(self.nsub):
                            #profs[jj] = psr_utils.rotate(profs[jj], new_subdelays_bins[jj])
                            delay_bins = int(new_subdelays_bins[jj] %
                                             len(profs[jj]))
                            if not delay_bins == 0:
                                profs[jj] = np.concatenate(
                                    (profs[jj][delay_bins:],
                                     profs[jj][:delay_bins]))

                        subdelays_bins += new_subdelays_bins
                        avgprof = self.avgprof
                    sumprof = profs.sum(0)
                    chis[ii] = self.calc_redchi2(prof=sumprof, avg=avgprof)
                DMcurve = normalize(downsample(chis, M))
                self.extracted_feature[feature] = DMcurve
            return self.extracted_feature[feature]
示例#11
0
def baseline_search(prof_2d, nchan, off_pulse_size, subfreqs, LOFAR=True):
    nbin = prof_2d.shape[1]
    binspersec = nbin / 0.0016627
    tmp_fp = np.empty(np.shape(prof_2d))
    mask = np.zeros(np.shape(prof_2d), dtype=bool)
    mask[:, :off_pulse_size] = 1
    binstep = 3
    binshift_range = np.arange(0, nbin, binstep)
    if LOFAR is False:
        dmstep = 0.002  ## WSRT
        dm_range = np.arange(0, 0.05, dmstep)  ## WSRT
    else:
        dmstep = 0.0005  ## LOFAR
        dm_range = np.arange(0, 0.01, dmstep)  ## LOFAR
    fit = np.empty((binshift_range.size, dm_range.size))
    for binshift in binshift_range:
        for dm in dm_range:
            subdelays = psr_utils.delay_from_DM(dm, subfreqs)
            hifreqdelay = subdelays[-1]
            subdelays = subdelays - hifreqdelay
            delaybins = subdelays * binspersec + binshift
            for jj in range(nchan):
                tmp_prof = prof_2d[jj, :].copy()
                tmp_fp[jj] = psr_utils.fft_rotate(tmp_prof, delaybins[jj])
            fit[binshift / binstep,
                int(dm / dmstep)] = np.nanmean(tmp_fp[mask])
    best = np.unravel_index(fit.argmin(), fit.shape)
    Bin = best[0] * binstep
    DM = best[1] * dmstep
    subdelays = psr_utils.delay_from_DM(DM, subfreqs)
    hifreqdelay = subdelays[-1]
    subdelays = subdelays - hifreqdelay
    delaybins = subdelays * binspersec + Bin
    for jj in range(nchan):
        tmp_prof = prof_2d[jj, :].copy()
        tmp_fp[jj] = psr_utils.fft_rotate(tmp_prof, delaybins[jj])
    base_perchan = np.nanmean(tmp_fp[:, :off_pulse_size], axis=1)
    return base_perchan, DM
示例#12
0
    def dedisperse(self, dm=0, padval=0):
        """Shift channels according to the delays predicted by
            the given DM.
            
            Inputs:
                dm: The DM (in pc/cm^3) to use.
                padval: The padding value to use when shifting
                    channels during dedispersion. See documentation
                    of Spectra.shift_channels. (Default: 0)

            Outputs:
                None

            *** Dedispersion happens in place ***
        """
        assert dm >= 0
        ref_delay = psr_utils.delay_from_DM(dm-self.dm, np.max(self.freqs))
        delays = psr_utils.delay_from_DM(dm-self.dm, self.freqs)
        rel_delays = delays-ref_delay # Relative delay
        rel_bindelays = np.round(rel_delays/self.dt).astype('int')
        # Shift channels
        self.shift_channels(rel_bindelays, padval)

        self.dm=dm
示例#13
0
def plot(data,
         cmap='gist_yarg',
         show_cb=False,
         sweep_dms=None,
         sweep_posns=None):
    if sweep_dms is None:
        sweep_dms = []
    if sweep_posns is None:
        sweep_posns = []

    ax = plt.axes((0.15, 0.15, 0.8, 0.7))
    plot_spectra(data, cmap=cmap)

    if show_cb:
        cb = plt.colorbar()
        cb.set_label("Scaled signal intensity (arbitrary units)")
    plt.axis('tight')

    # Sweeping it up
    for ii, sweep_dm in enumerate(sweep_dms):
        ddm = sweep_dm - data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()

        if sweep_posns is None:
            sweep_posn = 0.0
        elif len(sweep_posns) == 1:
            sweep_posn = sweep_posns[0]
        else:
            sweep_posn = sweep_posns[ii]
        sweepstart = data.dt * data.numspectra * sweep_posn + data.starttime
        sty = SWEEP_STYLES[ii % len(SWEEP_STYLES)]
        plt.plot(delays + sweepstart, data.freqs, sty, lw=4, alpha=0.5)

    # Dressing it up
    plt.xlabel("Time")
    plt.ylabel("Observing frequency (MHz)")
    #plt.suptitle("Frequency vs. Time")

    sumax = plt.axes((0.15, 0.85, 0.8, 0.1), sharex=ax)
    plot_timeseries(data)
    plt.setp(sumax.get_xticklabels() + sumax.get_yticklabels(), visible=False)
    plt.ylabel("Intensity")
    plt.ticklabel_format(style='plain', useOffset=False)

    plt.axis('tight')

    return sumax, ax
示例#14
0
文件: training.py 项目: zhuww/ubc_AI
        def getDMcurve(M): # return the normalized DM curve downsampled to M points
            feature = '%s:%s' % ('DMbins', M)
            if M == 0:
                return np.array([])
            if not feature in self.extracted_feature:
                ddm = (self.dms.max() - self.dms.min())/2.
                loDM, hiDM = (self.bestdm - ddm , self.bestdm + ddm)
                loDM = max((0, loDM)) #make sure cut off at 0 DM
                hiDM = max((ddm, hiDM)) #make sure cut off at 0 DM
                N = 100
                interp = False
                sumprofs = self.profs.sum(0)
                if not interp:
                    profs = sumprofs
                else:
                    profs = np.zeros(np.shape(sumprofs), dtype='d')
                DMs = psr_utils.span(loDM, hiDM, N)
                chis = np.zeros(N, dtype='f')
                subdelays_bins = self.subdelays_bins.copy()
                for ii, DM in enumerate(DMs):
                    subdelays = psr_utils.delay_from_DM(DM, self.barysubfreqs)
                    hifreqdelay = subdelays[-1]
                    subdelays = subdelays - hifreqdelay
                    delaybins = subdelays*self.binspersec - subdelays_bins
                    if interp:
                        interp_factor = 16
                        for jj in range(self.nsub):
                            profs[jj] = psr_utils.interp_rotate(sumprofs[jj], delaybins[jj],
                                                                zoomfact=interp_factor)
                        # Note: Since the interpolation process slightly changes the values of the
                        # profs, we need to re-calculate the average profile value
                        avgprof = (profs/self.proflen).sum()
                    else:
                        new_subdelays_bins = np.floor(delaybins+0.5)
                        for jj in range(self.nsub):
                            #profs[jj] = psr_utils.rotate(profs[jj], new_subdelays_bins[jj])
                            delay_bins = int(new_subdelays_bins[jj] % len(profs[jj]))
                            if not delay_bins==0:
                                profs[jj] = np.concatenate((profs[jj][delay_bins:], profs[jj][:delay_bins]))

                        subdelays_bins += new_subdelays_bins
                        avgprof = self.avgprof
                    sumprof = profs.sum(0)
                    chis[ii] = self.calc_redchi2(prof=sumprof, avg=avgprof)
                DMcurve = normalize(downsample(chis, M))
                self.extracted_feature[feature] = DMcurve
            return self.extracted_feature[feature]
示例#15
0
 def dedisperse(self, DM=None, interp=0, doppler=1):
     """
     dedisperse(DM=self.bestdm, interp=0, doppler=1):
         Rotate (internally) the profiles so that they are de-dispersed
             at a dispersion measure of DM.  Use FFT-based interpolation if
             'interp' is non-zero (NOTE: It is off by default!).
             Doppler shift subband frequencies if doppler is non-zero.
             (NOTE: It is *ON* by default. This default behaviour is
                     different with respect to PRESTO's prepfold.py)
     """
     if DM is None:
         DM = self.bestdm
     # Note:  Since TEMPO Doppler corrects observing frequencies, for
     #        TOAs, at least, we need to de-disperse using topocentric
     #        observing frequencies.
     if doppler:
         freqs = psr_utils.doppler(self.subfreqs, self.avgvoverc)
     else:
         freqs = self.subfreqs
     self.subdelays = psr_utils.delay_from_DM(DM, freqs)
     self.hifreqdelay = self.subdelays[-1]
     self.subdelays = self.subdelays - self.hifreqdelay
     delaybins = self.subdelays * self.binspersec - self.subdelays_bins
     if interp:
         new_subdelays_bins = delaybins
         for ii in range(self.npart):
             for jj in range(self.nsub):
                 tmp_prof = self.profs[ii, jj, :]
                 self.profs[ii, jj] = psr_utils.fft_rotate(
                     tmp_prof, delaybins[jj])
         # Note: Since the rotation process slightly changes the values of the
         # profs, we need to re-calculate the average profile value
         self.avgprof = (self.profs / self.proflen).sum()
     else:
         new_subdelays_bins = Num.floor(delaybins + 0.5)
         #print "DEBUG: in myprepfold.py -- DM, new_subdelays_bins:", DM, new_subdelays_bins
         for ii in range(self.nsub):
             rotbins = int(new_subdelays_bins[ii]) % self.proflen
             if rotbins:  # i.e. if not zero
                 subdata = self.profs[:, ii, :]
                 self.profs[:, ii] = Num.concatenate(
                     (subdata[:, rotbins:], subdata[:, :rotbins]), 1)
     self.subdelays_bins += new_subdelays_bins
     self.sumprof = self.profs.sum(0).sum(0)
     if Num.fabs((self.sumprof / self.proflen).sum() - self.avgprof) > 1.0:
         print "self.avgprof is not the correct value!"
     self.currdm = DM
 def plot_chi2_vs_DM(self, loDM, hiDM, N=100, interp=0, device='/xwin'):
     """
     plot_chi2_vs_DM(self, loDM, hiDM, N=100, interp=0, device='/xwin'):
         Plot (and return) an array showing the reduced-chi^2 versus
             DM (N DMs spanning loDM-hiDM).  Use sinc_interpolation
             if 'interp' is non-zero.
     """
     # Sum the profiles in time
     sumprofs = self.profs.sum(0)
     if not interp:
         profs = sumprofs
     else:
         profs = Num.zeros(Num.shape(sumprofs), dtype='d')
     DMs = psr_utils.span(loDM, hiDM, N)
     chis = Num.zeros(N, dtype='f')
     subdelays_bins = self.subdelays_bins.copy()
     for ii, DM in enumerate(DMs):
         subdelays = psr_utils.delay_from_DM(DM, self.barysubfreqs)
         hifreqdelay = subdelays[-1]
         subdelays = subdelays - hifreqdelay
         delaybins = subdelays * self.binspersec - subdelays_bins
         if interp:
             interp_factor = 16
             for jj in range(self.nsub):
                 profs[jj] = psr_utils.interp_rotate(sumprofs[jj],
                                                     delaybins[jj],
                                                     zoomfact=interp_factor)
             # Note: Since the interpolation process slightly changes the values of the
             # profs, we need to re-calculate the average profile value
             avgprof = (profs / self.proflen).sum()
         else:
             new_subdelays_bins = Num.floor(delaybins + 0.5)
             for jj in range(self.nsub):
                 profs[jj] = psr_utils.rotate(profs[jj],
                                              int(new_subdelays_bins[jj]))
             subdelays_bins += new_subdelays_bins
             avgprof = self.avgprof
         sumprof = profs.sum(0)
         chis[ii] = self.calc_redchi2(prof=sumprof, avg=avgprof)
     # Now plot it
     Pgplot.plotxy(chis,
                   DMs,
                   labx="DM",
                   laby="Reduced-\gx\u2\d",
                   device=device)
     return (chis, DMs)
示例#17
0
 def dedisperse(self, DM=None, interp=0, doppler=1):
     """
     dedisperse(DM=self.bestdm, interp=0, doppler=1):
         Rotate (internally) the profiles so that they are de-dispersed
             at a dispersion measure of DM.  Use FFT-based interpolation if
             'interp' is non-zero (NOTE: It is off by default!).
             Doppler shift subband frequencies if doppler is non-zero.
             (NOTE: It is *ON* by default. This default behaviour is
                     different with respect to PRESTO's prepfold.py)
     """
     if DM is None:
         DM = self.bestdm
     # Note:  Since TEMPO Doppler corrects observing frequencies, for
     #        TOAs, at least, we need to de-disperse using topocentric
     #        observing frequencies.
     if doppler:
         freqs = psr_utils.doppler(self.subfreqs, self.avgvoverc)
     else:
         freqs = self.subfreqs
     self.subdelays = psr_utils.delay_from_DM(DM, freqs)
     self.hifreqdelay = self.subdelays[-1]
     self.subdelays = self.subdelays-self.hifreqdelay
     delaybins = self.subdelays*self.binspersec - self.subdelays_bins
     if interp:
         new_subdelays_bins = delaybins
         for ii in range(self.npart):
             for jj in range(self.nsub):
                 tmp_prof = self.profs[ii,jj,:]
                 self.profs[ii,jj] = psr_utils.fft_rotate(tmp_prof, delaybins[jj])
         # Note: Since the rotation process slightly changes the values of the
         # profs, we need to re-calculate the average profile value
         self.avgprof = (self.profs/self.proflen).sum()
     else:
         new_subdelays_bins = Num.floor(delaybins+0.5)
         #print "DEBUG: in myprepfold.py -- DM, new_subdelays_bins:", DM, new_subdelays_bins
         for ii in range(self.nsub):
             rotbins = int(new_subdelays_bins[ii])%self.proflen
             if rotbins:  # i.e. if not zero
                 subdata = self.profs[:,ii,:]
                 self.profs[:,ii] = Num.concatenate((subdata[:,rotbins:],
                                                     subdata[:,:rotbins]), 1)
     self.subdelays_bins += new_subdelays_bins
     self.sumprof = self.profs.sum(0).sum(0)
     if Num.fabs((self.sumprof/self.proflen).sum() - self.avgprof) > 1.0:
         print "self.avgprof is not the correct value!"
     self.currdm = DM
示例#18
0
def plot(data, cmap='gist_yarg', show_cb=False, sweep_dms=None, sweep_posns=None):
    if sweep_dms is None:
        sweep_dms = []
    if sweep_posns is None:
        sweep_posns = []

    ax = plt.axes((0.15, 0.15, 0.8, 0.7))
    plot_spectra(data, cmap=cmap)

    if show_cb:
        cb = plt.colorbar()
        cb.set_label("Scaled signal intensity (arbitrary units)")
    plt.axis('tight')

    # Sweeping it up
    for ii, sweep_dm in enumerate(sweep_dms):
        ddm = sweep_dm-data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        
        if sweep_posns is None:
            sweep_posn = 0.0
        elif len(sweep_posns) == 1:
            sweep_posn = sweep_posns[0]
        else:
            sweep_posn = sweep_posns[ii]
        sweepstart = data.dt*data.numspectra*sweep_posn+data.starttime
        sty = SWEEP_STYLES[ii%len(SWEEP_STYLES)]
        plt.plot(delays+sweepstart, data.freqs, sty, lw=4, alpha=0.5)

    # Dressing it up
    plt.xlabel("Time")
    plt.ylabel("Observing frequency (MHz)")
    #plt.suptitle("Frequency vs. Time")
    
    sumax = plt.axes((0.15, 0.85, 0.8, 0.1), sharex=ax)
    plot_timeseries(data)
    plt.setp(sumax.get_xticklabels()+sumax.get_yticklabels(), visible=False)
    plt.ylabel("Intensity")
    plt.ticklabel_format(style='plain', useOffset=False)

    plt.axis('tight')

    return sumax, ax
示例#19
0
def get_phasedelays(dm, freqs, period):
    """Return phase delays corresponding to a particular DM.

        Inputs:
            dm: DM (in pc cm-3)
            freqs: The list of frequencies (in MHz)
            period: The profiles period (in seconds)

        Outputs:
            phasedelays: The corresponding phase delays.
    """
    # Prepare delays
    timedelays = psr_utils.delay_from_DM(dm, freqs)
    # Reference all delays to highest frequency channel, which remains
    # unchanged
    # TODO: Do we really want to refer to high freq?
    timedelays -= timedelays[np.argmax(freqs)]
    phasedelays = timedelays/period
    return phasedelays
示例#20
0
def get_phasedelays(dm, freqs, period):
    """Return phase delays corresponding to a particular DM.

        Inputs:
            dm: DM (in pc cm-3)
            freqs: The list of frequencies (in MHz)
            period: The profiles period (in seconds)

        Outputs:
            phasedelays: The corresponding phase delays.
    """
    # Prepare delays
    timedelays = psr_utils.delay_from_DM(dm, freqs)
    # Reference all delays to highest frequency channel, which remains
    # unchanged
    # TODO: Do we really want to refer to high freq?
    timedelays -= timedelays[np.argmax(freqs)]
    phasedelays = timedelays/period
    return phasedelays
示例#21
0
def main():
    fn = args[0]
    rawdatafile = open_data_file(fn)

    if options.dm:
        dmtime = psr_utils.delay_from_DM(options.dm, np.min(rawdatafile.freqs))

    data = get_data(rawdatafile, start=options.start, duration=options.duration+dmtime,
                    mask=options.maskfile)

    data = prepare_data(data, options.width_bins, options.downsamp, options.dm,
                        options.nsub, options.subdm, options.scaleindep)

    # Ploting it up
    fig = plt.figure()
    fig.canvas.set_window_title("Frequency vs. Time")
    plot(data, options.cmap, options.sweep_dms, options.sweep_posns) 
    fig.canvas.mpl_connect('key_press_event', \
            lambda ev: (ev.key in ('q','Q') and plt.close(fig)))
    plt.show()
示例#22
0
文件: prepfold.py 项目: zhuww/ubc_AI
 def plot_chi2_vs_DM(self, loDM, hiDM, N=100, interp=0, device='/xwin'):
     """
     plot_chi2_vs_DM(self, loDM, hiDM, N=100, interp=0, device='/xwin'):
         Plot (and return) an array showing the reduced-chi^2 versus
             DM (N DMs spanning loDM-hiDM).  Use sinc_interpolation
             if 'interp' is non-zero.
     """
     # Sum the profiles in time
     sumprofs = self.profs.sum(0)
     if not interp:
         profs = sumprofs
     else:
         profs = Num.zeros(Num.shape(sumprofs), dtype='d')
     DMs = psr_utils.span(loDM, hiDM, N)
     chis = Num.zeros(N, dtype='f')
     subdelays_bins = self.subdelays_bins.copy()
     for ii, DM in enumerate(DMs):
         subdelays = psr_utils.delay_from_DM(DM, self.barysubfreqs)
         hifreqdelay = subdelays[-1]
         subdelays = subdelays - hifreqdelay
         delaybins = subdelays*self.binspersec - subdelays_bins
         if interp:
             interp_factor = 16
             for jj in range(self.nsub):
                 profs[jj] = psr_utils.interp_rotate(sumprofs[jj], delaybins[jj],
                                                     zoomfact=interp_factor)
             # Note: Since the interpolation process slightly changes the values of the
             # profs, we need to re-calculate the average profile value
             avgprof = (profs/self.proflen).sum()
         else:
             new_subdelays_bins = Num.floor(delaybins+0.5)
             for jj in range(self.nsub):
                 profs[jj] = psr_utils.rotate(profs[jj], int(new_subdelays_bins[jj]))
             subdelays_bins += new_subdelays_bins
             avgprof = self.avgprof
         sumprof = profs.sum(0)
         chis[ii] = self.calc_redchi2(prof=sumprof, avg=avgprof)
     # Now plot it
     Pgplot.plotxy(chis, DMs, labx="DM", laby="Reduced-\gx\u2\d", device=device)
     return (chis, DMs)
示例#23
0
def main():
    fn = args[0]
    rawdatafile = open_data_file(fn)

    if options.dm:
        dmtime = psr_utils.delay_from_DM(options.dm, np.min(rawdatafile.freqs))

    data = get_data(rawdatafile,
                    start=options.start,
                    duration=options.duration + dmtime,
                    mask=options.maskfile)

    data = prepare_data(data, options.width_bins, options.downsamp, options.dm,
                        options.nsub, options.subdm, options.scaleindep)

    # Ploting it up
    fig = plt.figure()
    fig.canvas.set_window_title("Frequency vs. Time")
    plot(data, options.cmap, options.sweep_dms, options.sweep_posns)
    fig.canvas.mpl_connect('key_press_event', \
            lambda ev: (ev.key in ('q','Q') and plt.close(fig)))
    plt.show()
示例#24
0
def make_spd_from_man_params(spdcand, rawdatafile, \
                             txtfile, maskfile, \
                             plot, just_waterfall, \
                             subdm, dm, sweep_dm, \
                             sigma, \
                             start_time, duration, \
                             width_bins, nbins, downsamp, \
                             nsub, \
                             scaleindep, \
                             spec_width, loc_pulse, \
                             integrate_ts, integrate_spec, disp_pulse, \
                             basename, \
                             mask, bandpass_corr, barytime, man_params):            
    """
    Makes spd files from output files of rratrap. 
    Inputs:
        spdcand: spcand parameters instance (read in spcand.params)
        rawdatafile: psrfits file instance
        txtfile: rratrap output file (groups.txt file)
        maskfile: rfifind mask file. need this file if you want to remove the bandpass 
                  or use rfifind mask information.
        plot: do you want to produce the plots as well? 
        just_waterfall: Do you just want to make the waterfall plots.
        subdm: DM to use when subbanding.
        dm: DM to use when dedispersing data for plot. 
        sweep_dm: Show the frequency sweep using this DM.
        sigma: signal-to-noise of the pulse
        start_time: start time of the data to be read in for waterfalling.
        duration: duration of data to be waterfalled.
        width_bins: Smooth each channel/subband with a boxcar width_bins wide.
        nbins: Number of time bins to plot. This option overrides
                the duration argument. 
        downsamp: Factor to downsample in time by. Default: Don't downsample.
        nsub: Number of subbands to use. Must be a factor of number of channels.
        scaleindep:Do you want to scale each subband independently?(Type: Boolean)
        spec_width: Twice this number times the pulse_width around the pulse to consider for the spectrum
        loc_pulse: Fraction of the window length where the pulse is located.(eg. 0.25 = 1/4th of the way in.
                                                                             0.5 = middle of the plot)
        integrate_ts: Do you want to display the dedispersed time series in the plot?
        integrate_spec: Do you want to display the pulse spectrum in the plot?
        disp_pulse: Do you want to see the inset dispersed pulse in the plot?
        basename: output basename of the file. Appended with _DM_TIME(s)_RANK.spd 
        mask: Do you want to mask out rfi contaminated channels?
        bandpass_corr: Do you want to remove the bandpass?
        barytime: Is the given time(s) barycentric?
        man_params: Do you want to specify the parameters for waterfalling 
                    manually? If yes, I suggest using the function make_spd_from_man_params().
                    (I suggest giving it the rratrap output file)    
    Outputs:
       Binary npz file containing the necessary arrays and header information to generate the spd plots.
    """
    rank = None
    if not nsub:
        nsub = rawdatafile.nchan

    # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
    spdcand.manual_params(subdm, dm, sweep_dm, sigma, start_time, \
                         width_bins, downsamp, duration, nbins, nsub, rawdatafile.tsamp, \
                         rawdatafile.specinfo.N, \
                         rawdatafile.frequencies[0], rawdatafile.frequencies[-1], rawdatafile, \
                         loc_pulse=loc_pulse, dedisp=True, scaleindep=False, zerodm=False, \
                         mask=mask, barytime=barytime, bandpass_corr=bandpass_corr)
    #make an array to store header information for the spd files
    temp_filename = basename+"_DM%.1f_%.1fs"%(spdcand.subdm, spdcand.topo_start_time)
           
    print_debug("Running waterfaller with Zero-DM OFF...")
    data, Data_dedisp_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    # Add additional information to the header information array
    text_array = np.array([args[0], rawdatafile.specinfo.telescope, \
                           rawdatafile.specinfo.ra_str, rawdatafile.specinfo.dec_str, \
                           rawdatafile.specinfo.start_MJD[0], rank, \
                           spdcand.nsub, spdcand.nbins, \
                           spdcand.subdm, spdcand.sigma, spdcand.sample_number, \
                           spdcand.duration, spdcand.width_bins, spdcand.pulse_width, \
                           rawdatafile.tsamp, rawdatafile.specinfo.T, spdcand.topo_start_time, \
                           data.starttime, data.dt,data.numspectra, data.freqs.min(), \
                           data.freqs.max()])

    #### Array for plotting Dedispersed waterfall plot zerodm - ON
    print_debug("Running Waterfaller with Zero-DM ON...")
    zerodm=True
    data, Data_dedisp_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    ####Sweeped without zerodm
    spdcand.manual_params(subdm, dm, sweep_dm, sigma, start_time, \
                          width_bins, downsamp, duration, nbins, nsub, rawdatafile.tsamp, \
                          rawdatafile.specinfo.N, \
                          rawdatafile.frequencies[0], rawdatafile.frequencies[-1], rawdatafile, \
                          loc_pulse=loc_pulse, dedisp=None, scaleindep=None, zerodm=None, mask=mask, \
                          barytime=barytime, bandpass_corr=bandpass_corr)
    data, Data_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    text_array = np.append(text_array, spdcand.sweep_duration)
    text_array = np.append(text_array, data.starttime)
    text_array = np.append(text_array, spdcand.bary_start_time)
    text_array = np.append(text_array, man_params)
    # Array to Construct the sweep
    if spdcand.sweep_dm is not None:
        ddm = spdcand.sweep_dm-data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        delays_nozerodm = delays
        freqs_nozerodm = data.freqs
    # Sweeped with zerodm-on 
    zerodm = True
    #downsamp_temp = 1
    data, Data_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    with open(temp_filename+".spd", 'wb') as f:
        np.savez_compressed(f, \
                            Data_dedisp_nozerodm = Data_dedisp_nozerodm.astype(np.float16),\
                            Data_dedisp_zerodm = Data_dedisp_zerodm.astype(np.float16),\
                            Data_nozerodm = Data_nozerodm.astype(np.float16),\
                            delays_nozerodm = delays_nozerodm, \
                            freqs_nozerodm = freqs_nozerodm,\
                            Data_zerodm = Data_zerodm.astype(np.float16), \
                            text_array = text_array)
    #### Arrays for Plotting DM vs Time is in plot_spd.plot(...)
    if plot:
        print_debug("Now plotting...")
        plot_spd.plot(temp_filename+".spd", args[1:], \
                      spec_width=spec_width, loc_pulse=loc_pulse, xwin=False, \
                      outfile = basename, just_waterfall=just_waterfall, \
                      integrate_spec=integrate_spec, integrate_ts=integrate_ts, \
                      disp_pulse=disp_pulse, tar = None)
示例#25
0
    
    # Combine the profiles as required
    profs = fold_pfd.combine_profs(numtoas, numsubbands)

    # PRESTO de-disperses at the high frequency channel so determine a
    # correction to the middle of the band
    if not events:
        subpersumsub = fold_pfd.nsub/numsubbands
        # Calculate the center of the summed subband freqs and delays
        sumsubfreqs = (Num.arange(numsubbands)+0.5)*subpersumsub*fold_pfd.subdeltafreq + \
                      (fold_pfd.lofreq-0.5*fold_pfd.chan_wid)
        # Note:  In the following, we cannot use fold_pfd.hifreqdelay since that
        #        is based on the _barycentric_ high frequency (if the barycentric 
        #        conversion was available).  For TOAs, we need a topocentric
        #        delay, which is based on the topocentric frequency fold_pfd.hifreq
        sumsubdelays = (psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) -
                        psr_utils.delay_from_DM(fold_pfd.bestdm, fold_pfd.hifreq))/SECPERDAY
    else:
        fold_pfd.subfreqs = Num.asarray([0.0])
        sumsubfreqs = Num.asarray([0.0])
        sumsubdelays = Num.asarray([0.0])

    # Read the template profile
    if templatefilenm is not None:
        template = psr_utils.read_profile(templatefilenm, normalize=1)
    else:
        if (gaussfitfile):
            template = psr_utils.read_gaussfitfile(gaussfitfile, fold_pfd.proflen)
        else:
            template = psr_utils.gaussian_profile(fold_pfd.proflen, 0.0, gaussianwidth)
        template = template / max(template)
示例#26
0
    
    # Combine the profiles as required
    profs = fold_pfd.combine_profs(numtoas, numsubbands)

    # PRESTO de-disperses at the high frequency channel so determine a
    # correction to the middle of the band
    if not events:
        subpersumsub = fold_pfd.nsub/numsubbands
        # Calculate the center of the summed subband freqs and delays
        sumsubfreqs = (Num.arange(numsubbands)+0.5)*subpersumsub*fold_pfd.subdeltafreq + \
                      (fold_pfd.lofreq-0.5*fold_pfd.chan_wid)
        # Note:  In the following, we cannot use fold_pfd.hifreqdelay since that
        #        is based on the _barycentric_ high frequency (if the barycentric 
        #        conversion was available).  For TOAs, we need a topocentric
        #        delay, which is based on the topocentric frequency fold_pfd.hifreq
        sumsubdelays = (psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) -
                        psr_utils.delay_from_DM(fold_pfd.bestdm, fold_pfd.hifreq))
        sumsubdelays_phs = Num.fmod(sumsubdelays / p_dedisp, 1.0)
        # Save the "higest channel within a subband" freqs/delays for use in
        # later DM/timing correction. PBD 2011/11/03
        sumsubfreqs_hi = sumsubfreqs + \
                fold_pfd.subdeltafreq/2.0 - fold_pfd.chan_wid/2.0
        subdelays2 = psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) - \
                psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs_hi)

    else:
        fold_pfd.subfreqs = Num.asarray([0.0])
        sumsubfreqs = Num.asarray([0.0])
        sumsubdelays = Num.asarray([0.0])

    # Read the template profile
示例#27
0
    def dm_curve_check(self, spec_index=0.):
        # Sum the profiles in time
        profs = self.pfd.profs.sum(0)

        ### Generate simulated profiles ###

        # prof_avg: median profile value per subint per subband
        #  Sum over subint axis to get median per subband
        prof_avg = self.pfd.stats[:, :, 4].sum(0)
        # prof_var: variance of profile per subint per subband
        #  Sum over subint axis to get variance per subband
        prof_var = self.pfd.stats[:, :, 5].sum(0)
        # The standard deviation in each subband is proportional to the median
        # value of that subband.  Here we scale all subbands to equal levels.
        #scaled_vars = prof_var / prof_avg**2
        scaled_vars = np.array(np.abs(prof_var),
                               dtype=np.float64) / prof_avg**2
        scaled_vars[scaled_vars <= 0] = np.random.rand(
            1
        )  ## This is new, to avoir errors when noise calculates <=0 inside the sqrt
        scaled_profs = (profs.T / prof_avg).T - 1.
        # The mean profile (after scaling) will be our "clean" profile--hardly
        # true in most cases, but we pretend it's noiseless for what follows
        scaled_mean_prof = scaled_profs.mean(0)
        # Extend this "clean" profile across the number of subbands
        sim_profs_clean = np.tile(scaled_mean_prof,\
            scaled_profs.shape[0]).reshape(scaled_profs.shape)
        # Scale these subbands according to the input spectral index
        spec_mult = (self.pfd.subfreqs / self.pfd.subfreqs[0])**spec_index
        spec_mult /= spec_mult.mean()
        sim_profs_spec = (sim_profs_clean.T * spec_mult).T
        # For consistency, set a seed for generating noise
        np.random.seed(1967)
        # Add white noise that matches the variance of the real subbands
        # on a subband by subband basis
        noise = np.random.normal(scale=np.sqrt(scaled_vars),
                                 size=scaled_profs.T.shape).T
        # sim_profs_noisy is the simulated equivalent of scaled_profs
        sim_profs_noisy = sim_profs_spec + noise
        # sim_profs_final is the simulated equivalent of profs
        sim_profs = ((sim_profs_noisy + 1.).T * prof_avg).T

        # The rest of this is essentially code from the prepfold.pfd class
        # in which we loop over DM values and see how strong a signal we
        # get by dedispersing at each of these values

        # Go to higher DMs than the original curve to try to better exclude noise
        DMs = np.linspace(self.pfd.dms[0], self.pfd.dms[-1]+4.*\
            (self.pfd.dms[-1]-self.pfd.dms[0]), len(self.pfd.dms))
        chis = np.zeros_like(DMs)
        sim_chis = np.zeros_like(DMs)
        subdelays_bins = self.pfd.subdelays_bins.copy()
        for ii, DM in enumerate(DMs):
            subdelays = psr_utils.delay_from_DM(DM, self.pfd.barysubfreqs)
            hifreqdelay = subdelays[-1]
            subdelays = subdelays - hifreqdelay
            delaybins = subdelays * self.pfd.binspersec - subdelays_bins
            new_subdelays_bins = np.floor(delaybins + 0.5)
            for jj in range(self.pfd.nsub):
                profs[jj] = psr_utils.rotate(profs[jj],
                                             int(new_subdelays_bins[jj]))
                sim_profs[jj] = psr_utils.rotate(sim_profs[jj],\
                    int(new_subdelays_bins[jj]))
            subdelays_bins += new_subdelays_bins
            # The set of reduced chi2s like those in the prepfold plot
            # (should be the same if the same DMs are used)
            chis[ii] = self.pfd.calc_redchi2(prof=profs.sum(0),
                                             avg=self.pfd.avgprof)
            # The same thing but for our "simulated" data
            sim_chis[ii] = self.pfd.calc_redchi2(prof=sim_profs.sum(0),
                                                 avg=self.pfd.avgprof)
        return DMs, chis, sim_chis
示例#28
0
def write_toa(summed_pulse, polycos, template_profile, \
                        timeseries, start_phase=0.0, debug=False):
    """Given a SummedPulse generate a TOA and write it to stdout. 
        A polycos file is required. 'template_profile' is simply 
        a numpy array. 'timeseries' is a Datfile object.
        'start_phase' is the phase at the start of the profile.
        'debug' is a boolean value that determines if debugging
        info should be displayed.
        Returns shift required to line up template and pulse.
    """
    if template_profile is None:
        raise ValueError("A template profile MUST be provided.")
    # This code is taken from Scott Ransom's PRESTO's get_TOAs.py
    mjdi = int(summed_pulse.mjd) # integer part of MJD
    mjdf = summed_pulse.mjd - mjdi # fractional part of MJD
    (phs, freq) = polycos.get_phs_and_freq(mjdi, mjdf)
    phs -= start_phase
    period = 1.0/freq
    
    # Caclulate offset due to shifting channels to account for DM
    # Hifreq doesn't have a half-channel offset 
    # (why? because get_TOAs.py doesn't. Why...)
    # Why subtract 1 channel to get hifreq?
    hifreq = timeseries.infdata.lofreq + timeseries.infdata.BW - \
                timeseries.infdata.chan_width
    midfreq = timeseries.infdata.lofreq - 0.5*timeseries.infdata.chan_width + \
                0.5*timeseries.infdata.BW
    dmdelay = psr_utils.delay_from_DM(timeseries.infdata.DM, midfreq) - \
              psr_utils.delay_from_DM(timeseries.infdata.DM, hifreq)
    dmdelay_mjd = dmdelay/float(psr_utils.SECPERDAY)
    if debug:
        colour.cprint("High frequency (MHz): %f" % hifreq, 'debug')
        colour.cprint("Mid frequency (MHz): %f" % midfreq, 'debug')
        colour.cprint("DM delay added to TOAs (s): %g" % dmdelay, 'debug')
        colour.cprint("DM delay added to TOAs (MJD): %g" % dmdelay_mjd, 'debug')

    t0f = mjdf - phs*period/psr_utils.SECPERDAY + dmdelay_mjd
    t0i = mjdi
    shift,eshift,snr,esnr,b,errb,ngood,tphs = measure_phase(summed_pulse.profile, \
                            template_profile)
    # tphs is amount template is rotated by. It is originally 
    # measured in radians, convert to rotational phase
    tphs = tphs/(np.pi*2.0) % 1.0
    # tau and tau_err are the predicted phase of the pulse arrival
    tau, tau_err = shift/summed_pulse.N, eshift/summed_pulse.N
    if debug:
        colour.cprint("FFTFIT: Shift (bins): %f, Tau (phase): %f" % (shift, tau), 'debug')
        colour.cprint("FFTFIT: Shift error (bins): %f, Tau error (phase): %f" % \
                            (eshift, tau_err), 'debug')
    # Note: "error" flags are shift = 0.0 and eshift = 999.0
    if (np.fabs(shift) < 1e-7 and np.fabs(eshift-999.0) < 1e-7):
        raise FFTFitError("Error in FFTFIT. Bad return values.")
    # Send the TOA to STDOUT
    toaf = t0f + tau*period/float(psr_utils.SECPERDAY)
    if debug:
        colour.cprint("t0f (MJD): %r" % t0f, 'debug')
        colour.cprint("period (s): %r" % period, 'debug')
        colour.cprint("toaf (MJD): %r" % toaf, 'debug')
    newdays = int(np.floor(toaf))
    obs_code = telescopes.telescope_to_id[timeseries.infdata.telescope]
    psr_utils.write_princeton_toa(t0i+newdays, toaf-newdays, \
                                tau_err*period*1e6, midfreq, \
                                timeseries.infdata.DM, obs=obs_code)
    return tau, tphs
def plot_waterfall(data,
                   start,
                   duration,
                   dm,
                   ofile,
                   integrate_ts=False,
                   integrate_spec=False,
                   show_cb=False,
                   cmap_str="gist_yarg",
                   sweep_dms=[],
                   sweep_posns=[],
                   ax_im=None,
                   ax_ts=None,
                   ax_spec=None,
                   interactive=True,
                   downsamp=1,
                   nsub=None,
                   subdm=None):
    """ I want a docstring too!
    """

    # Set up axes
    fig = plt.figure(figsize=(8, 12))
    #fig.canvas.set_window_title("Frequency vs. Time")
    '''
    im_width = 0.6 if integrate_spec else 0.8
    im_height = 0.6 if integrate_ts else 0.8

    if not ax_im:
        ax_im = plt.axes((0.15, 0.15, im_width, im_height))
    if integrate_ts and not ax_ts:
        ax_ts = plt.axes((0.15, 0.75, im_width, 0.2),sharex=ax_im)

    if integrate_spec and not ax_spec:
        ax_spec = plt.axes((0.75, 0.15, 0.2, im_height),sharey=ax_im)
    '''

    ax_ts = plt.axes((0.15, 0.76, 0.8, 0.19))
    ax_im = plt.axes((0.15, 0.455, 0.8, 0.29), sharex=ax_ts)
    ax_dmvstm = plt.axes((0.15, 0.15, 0.8, 0.29))

    # Ploting it up
    nbinlim = np.int(duration / data.dt)

    # DM-vs-time plot
    dmvstm_array = []
    lodm = int(dm - (dm * 0.15))
    if lodm < 0: lodm = 0
    hidm = int(dm + (dm * 0.15))
    dmstep = (hidm - lodm) / 50.0

    datacopy = copy.deepcopy(data)
    #print lodm,hidm
    for ii in np.arange(lodm, hidm, dmstep):
        #for ii in range(400,600,10):
        #Without this, dispersion delay with smaller DM step does not produce delay close to bin width
        data.dedisperse(0, padval='mean')

        data.dedisperse(ii, padval='mean')
        Data = np.array(data.data[..., :nbinlim])
        Dedisp_ts = Data.sum(axis=0)
        dmvstm_array.append(Dedisp_ts)

    dmvstm_array = np.array(dmvstm_array)
    #print np.shape(dmvstm_array)
    #np.save('dmvstm_1step.npz',dmvstm_array)
    ax_dmvstm.set_xlabel("Time")
    ax_dmvstm.set_ylabel("DM")
    ax_dmvstm.imshow(dmvstm_array,
                     aspect='auto',
                     cmap=matplotlib.cm.cmap_d[cmap_str],
                     origin='lower',
                     extent=(data.starttime,
                             data.starttime + nbinlim * data.dt, lodm, hidm))
    #cmap=matplotlib.cm.cmap_d[cmap_str])
    #interpolation='nearest', origin='upper')
    plt.setp(ax_im.get_xticklabels(), visible=False)
    plt.setp(ax_ts.get_xticklabels(), visible=False)
    #extent=(data.starttime, data.starttime+ nbinlim*data.dt, 500, 700)
    #plt.show()
    #fig2 = plt.figure(2)
    #plt.imshow(dmvstm_array,aspect='auto')

    data = copy.deepcopy(datacopy)

    data.downsample(downsamp)
    data.dedisperse(dm)
    nbinlim = np.int(duration / data.dt)

    #Freq-vs-time plot
    img = ax_im.imshow(data.data[..., :nbinlim], aspect='auto', \
                cmap=matplotlib.cm.cmap_d[cmap_str], \
                interpolation='nearest', origin='upper', \
                extent=(data.starttime, data.starttime+ nbinlim*data.dt, \
                        data.freqs.min(), data.freqs.max()))

    if show_cb:
        cb = ax_im.get_figure().colorbar(img)
        cb.set_label("Scaled signal intensity (arbitrary units)")

    #plt.axis('tight')
    # Sweeping it up
    for ii, sweep_dm in enumerate(sweep_dms):
        ddm = sweep_dm - data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()

        if sweep_posns is None:
            sweep_posn = 0.0
        elif len(sweep_posns) == 1:
            sweep_posn = sweep_posns[0]
        else:
            sweep_posn = sweep_posns[ii]
        sweepstart = data.dt * data.numspectra * sweep_posn + data.starttime
        sty = SWEEP_STYLES[ii % len(SWEEP_STYLES)]
        ax_im.plot(delays + sweepstart, data.freqs, sty, lw=4, alpha=0.5)

    # Dressing it up
    ax_im.xaxis.get_major_formatter().set_useOffset(False)
    #ax_im.set_xlabel("Time")
    ax_im.set_ylabel("Frequency (MHz)")

    # Plot Time series
    if integrate_ts:
        #Data = np.array(data.data[..., :nbinlim])
        Data = np.array(data.data[..., :nbinlim])
        Dedisp_ts = Data.sum(axis=0)
        times = (np.arange(data.numspectra) * data.dt + start)[..., :nbinlim]
        ax_ts.plot(times, Dedisp_ts, "k")
        ax_ts.set_xlim([times.min(), times.max()])
        plt.setp(ax_ts.get_xticklabels(), visible=False)
        plt.setp(ax_ts.get_yticklabels(), visible=False)

    # Plot Spectrum
    if integrate_spec:
        spectrum_window = 0.05 * duration
        window_width = int(spectrum_window / data.dt)  # bins
        burst_bin = nbinlim / 2
        on_spec = np.array(data.data[..., burst_bin - window_width:burst_bin +
                                     window_width])
        Dedisp_spec = on_spec.sum(axis=1)[::-1]

        freqs = np.linspace(data.freqs.min(), data.freqs.max(),
                            len(Dedisp_spec))
        ax_spec.plot(Dedisp_spec, freqs, "k")
        plt.setp(ax_spec.get_xticklabels(), visible=False)
        plt.setp(ax_spec.get_yticklabels(), visible=False)
        ax_spec.set_ylim([data.freqs.min(), data.freqs.max()])
        if integrate_ts:
            ax_ts.axvline(times[burst_bin] - spectrum_window,
                          ls="--",
                          c="grey")
            ax_ts.axvline(times[burst_bin] + spectrum_window,
                          ls="--",
                          c="grey")

    #if interactive:
    #    fig.suptitle("Frequency vs. Time")
    #    fig.canvas.mpl_connect('key_press_event', \
    #            lambda ev: (ev.key in ('q','Q') and plt.close(fig)))
    #oname = "%.3f_%s.png" % (start,str(dm))
    if ofile is "unknown_cand":
        ofile = ofile + "_%.3f_%s.pdf" % (start, str(dm))
        plt.savefig(ofile)
    else:
        plt.savefig(ofile)
示例#30
0
def main():
    fn = args[0]
    #if fn.endswith(".fil"):
    #    # Filterbank file
    #    filetype = "filterbank"
    #    rawdatafile = filterbank.filterbank(fn)
    if fn.endswith(".fits"):
        # PSRFITS file
        filetype = "psrfits"
        rawdatafile = psrfits.PsrfitsFile(fn)
    else:
        raise ValueError("Cannot recognize data file type from "
                         "extension. (Only '.fits' and '.fil' "
                         "are supported.)")

    # Read data
    start_bin = np.round(options.start/rawdatafile.tsamp).astype('int')
	#dmfac = 4.15e3 * np.abs(1./rawdatafile.fch1**2 - 1./(rawdatafile.frequencies[-1])**2)
    dmfac = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2 - 1./rawdatafile.frequencies[-1]**2)
    if options.nbins is None:
        nbins = np.round(options.duration/rawdatafile.tsamp).astype('int')
    else:
        nbins = options.nbins
    binratio = 50    
    if options.dm:
	nbinsextra = np.round((options.duration + dmfac * options.dm)/rawdatafile.tsamp).astype('int')
    else:
        nbinsextra = nbins    
    data = rawdatafile.get_spectra(start_bin, nbinsextra)
    data = maskfile(data, start_bin, nbinsextra)
    data, bins = waterfall(start_bin, dmfac, options.duration, nbins, options.zerodm, options.nsub, options.subdm, options.dm, options.integrate_dm, options.downsamp, options.scaleindep, options.width_bins, rawdatafile, binratio, data)
    # Ploting it up
    fig = plt.figure()
    fig.canvas.set_window_title("Frequency vs. Time")
    ax = plt.axes((0.15, 0.15, 0.8, 0.60))
    ragfac = float(nbins)/bins
    dmrange, trange = data.data.shape
    nbinlim = np.int(trange * ragfac)
    print data.dt, rawdatafile.tsamp 
   #np.save('data',data.data[..., :nbinlim])
    plt.imshow(data.data[..., :nbinlim], aspect='auto', \
                cmap=matplotlib.cm.cmap_d[options.cmap], \
                interpolation='nearest', origin='upper', \
                extent=(data.starttime, data.starttime+ nbinlim*data.dt, \
                        data.freqs.min(), data.freqs.max()))
    if options.show_cb:
        cb = plt.colorbar()
        cb.set_label("Scaled signal intensity (arbitrary units)")

    #plt.axis('tight')
    # Sweeping it up
    for ii, sweep_dm in enumerate(options.sweep_dms):
        ddm = sweep_dm-data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        
        if options.sweep_posns is None:
            sweep_posn = 0.0
        elif len(options.sweep_posns) == 1:
            sweep_posn = options.sweep_posns[0]
        else:
            sweep_posn = options.sweep_posns[ii]
        sweepstart = data.dt*data.numspectra*sweep_posn+data.starttime
        sty = SWEEP_STYLES[ii%len(SWEEP_STYLES)]
        plt.plot(delays+sweepstart, data.freqs, sty, lw=4, alpha=0.5)

    # Dressing it up
    plt.xlabel("Time")
    plt.ylabel("Observing frequency (MHz)")
    plt.suptitle("Frequency vs. Time")
    # Plot Time series
    if options.integrate_dm is not None:
        Data = np.array(data.data[..., :nbinlim])
        Dedisp_ts = Data.sum(axis=0)
        times = (np.arange(data.numspectra)*data.dt + options.start)[..., :nbinlim]
        ax = plt.axes((0.15, 0.75, 0.8, 0.2))
        plt.plot(times, Dedisp_ts)
	plt.setp(ax.get_xticklabels(), visible = False)
	plt.setp(ax.get_yticklabels(), visible = False)
    fig.canvas.mpl_connect('key_press_event', \
            lambda ev: (ev.key in ('q','Q') and plt.close(fig)))
    plt.show()
示例#31
0
def calc_features_from_pfd(pfd_filepath):

    pfd_data = prepfold.pfd(str(pfd_filepath))

    if pfd_filepath.parent.name == 'positive':
        label = 1
    elif pfd_filepath.parent.name == 'negative':
        label = 0
    else:
        label = -1
        # raise RuntimeError('unable to decide the label of pfd file: {}'.format(
        #     str(pfd_filepath)))

    pfd_data.dedisperse()

    #### As done in: prepfold.pfd.plot_sumprofs
    profile = pfd_data.sumprof
    profile = normalise_1d(profile)
    ####

    profile_mean = np.mean(profile)
    profile_std_dev = np.std(profile)
    profile_skewness = scipy.stats.skew(profile)
    profile_excess_kurtosis = scipy.stats.kurtosis(profile)

    profiles_sum_axis0 = pfd_data.profs.sum(0)

    #### As done in: prepfold.pfd.plot_chi2_vs_DM
    loDM = 0
    hiDM = pfd_data.numdms
    N = pfd_data.numdms
    profs = profiles_sum_axis0.copy()  # = pfd_data.profs.sum(0)
    DMs = psr_utils.span(loDM, hiDM, N)
    chis = np.zeros(N, dtype='f')
    subdelays_bins = pfd_data.subdelays_bins.copy()
    for ii, DM in enumerate(DMs):
        subdelays = psr_utils.delay_from_DM(DM, pfd_data.barysubfreqs)
        hifreqdelay = subdelays[-1]
        subdelays = subdelays - hifreqdelay
        delaybins = subdelays * pfd_data.binspersec - subdelays_bins
        new_subdelays_bins = np.floor(delaybins + 0.5)
        for jj in range(pfd_data.nsub):
            profs[jj] = psr_utils.rotate(profs[jj],
                                         int(new_subdelays_bins[jj]))
        subdelays_bins += new_subdelays_bins
        sumprof = profs.sum(0)
        chis[ii] = pfd_data.calc_redchi2(prof=sumprof, avg=pfd_data.avgprof)
    ####

    best_dm = pfd_data.bestdm

    # crop_radius = 100
    # best_dm_index = np.searchsorted(DMs, best_dm)  # Not accurate, but close.
    # bloated_chis = np.insert(chis, N, np.full(crop_radius, chis[-1]))
    # bloated_chis = np.insert(bloated_chis, 0, np.full(crop_radius, chis[0]))
    # cropped_chis = bloated_chis[ best_dm_index : best_dm_index+2*crop_radius ]
    # chis = cropped_chis

    chis_mean = np.mean(chis)
    chis_std_dev = np.std(chis)
    chis_skewness = scipy.stats.skew(chis)
    chis_excess_kurtosis = scipy.stats.kurtosis(chis)

    #### As done in: prepfold.pfd.plot_intervals
    intervals = pfd_data.profs.sum(1)
    intervals = normalise_2d_rowwise(intervals)
    ####

    #### As done in: prepfold.pfd.plot_subbands
    subbands = profiles_sum_axis0.copy()  # = pfd_data.profs.sum(0)
    subbands = normalise_2d_rowwise(subbands)
    ####

    return (label, profile_mean, profile_std_dev, profile_skewness,
            profile_excess_kurtosis, chis_mean, chis_std_dev, chis_skewness,
            chis_excess_kurtosis, best_dm, profile, intervals, subbands, chis)
示例#32
0
def plot_waterfall(data,
                   start,
                   source_name,
                   duration,
                   dm,
                   ofile,
                   integrate_ts=False,
                   integrate_spec=False,
                   show_cb=False,
                   cmap_str="gist_yarg",
                   sweep_dms=[],
                   sweep_posns=[],
                   ax_im=None,
                   ax_ts=None,
                   ax_spec=None,
                   interactive=True,
                   downsamp=1,
                   nsub=None,
                   subdm=None,
                   width=None,
                   snr=None):
    """ I want a docstring too!
    """

    if source_name is None:
        source_name = "Unknown"

    #Output file
    if ofile is "unknown_cand":
        title = "%s_" + ofile + "_%.3f_%s" % (source_name, start, str(dm))
    else:
        title = source_name + "_" + ofile

    # Set up axes
    fig = plt.figure(figsize=(10, 14))
    #fig.canvas.set_window_title("Frequency vs. Time")
    '''
    im_width = 0.6 if integrate_spec else 0.8
    im_height = 0.6 if integrate_ts else 0.8

    if not ax_im:
        ax_im = plt.axes((0.15, 0.15, im_width, im_height))
    if integrate_ts and not ax_ts:
        ax_ts = plt.axes((0.15, 0.75, im_width, 0.2),sharex=ax_im)

    if integrate_spec and not ax_spec:
        ax_spec = plt.axes((0.75, 0.15, 0.2, im_height),sharey=ax_im)
    '''

    ax_ts = plt.axes((0.1, 0.835, 0.71, 0.145))
    ax_im = plt.axes((0.1, 0.59, 0.71, 0.24), sharex=ax_ts)
    ax_dmvstm = plt.axes((0.1, 0.345, 0.71, 0.24), sharex=ax_ts)
    ax_spec = plt.axes((0.815, 0.59, 0.16, 0.24), sharey=ax_im)
    ax_dmsnr = plt.axes((0.815, 0.345, 0.16, 0.24), sharey=ax_dmvstm)
    ax_orig = plt.axes((0.1, 0.1, 0.71, 0.21))

    data.downsample(downsamp)
    nbinlim = np.int(duration / data.dt)

    # DM-vs-time plot
    dmvstm_array = []
    #Old way
    ''' 
    lodm = int(dm-(dm*0.15))
    if lodm < 0: lodm = 0
    hidm = int(dm+(dm*0.15))
    '''
    band = (data.freqs.max() - data.freqs.min())
    centFreq = (data.freqs.min() + band / 2.0) / (10**3)  # To get it in GHz
    print width, centFreq, band
    #This comes from Cordes and McLaughlin (2003) Equation 13.
    FWHM_DM = 506 * float(width) * pow(centFreq, 3) / band
    #The candidate DM might not be exact so using a longer range
    FWHM_DM = 3 * FWHM_DM

    lodm = dm - FWHM_DM
    if lodm < 0:
        lodm = 0
        hidm = 2 * dm  # If low DM is zero then range should be 0 to 2*DM
    else:
        hidm = dm + FWHM_DM
    print FWHM_DM, dm, lodm, hidm
    dmstep = (hidm - lodm) / 48.0
    datacopy = copy.deepcopy(data)
    #print lodm,hidm
    for ii in np.arange(lodm, hidm, dmstep):
        #for ii in range(400,600,10):
        #Without this, dispersion delay with smaller DM step does not produce delay close to bin width
        data.dedisperse(0, padval='rotate')
        data.dedisperse(ii, padval='rotate')
        Data = np.array(data.data[..., :nbinlim])
        Dedisp_ts = Data.sum(axis=0)
        dmvstm_array.append(Dedisp_ts)
    dmvstm_array = np.array(dmvstm_array)
    #print np.shape(dmvstm_array)
    #np.save('dmvstm_1step.npz',dmvstm_array)
    ax_dmvstm.set_xlabel("Time (sec) ")
    ax_dmvstm.set_ylabel("DM")
    #ax_dmvstm.imshow(dmvstm_array, aspect='auto', cmap=matplotlib.cm.cmap_d[cmap_str], origin='lower',extent=(data.starttime, data.starttime+ nbinlim*data.dt, lodm, hidm))
    ax_dmvstm.imshow(dmvstm_array,
                     aspect='auto',
                     origin='lower',
                     extent=(data.starttime,
                             data.starttime + nbinlim * data.dt, lodm, hidm))
    #cmap=matplotlib.cm.cmap_d[cmap_str])
    #interpolation='nearest', origin='upper')
    plt.setp(ax_im.get_xticklabels(), visible=False)
    plt.setp(ax_ts.get_xticklabels(), visible=False)
    ax_dmvstm.set_ylim(lodm, hidm)
    #extent=(data.starttime, data.starttime+ nbinlim*data.dt, 500, 700)
    #plt.show()
    #fig2 = plt.figure(2)
    #plt.imshow(dmvstm_array,aspect='auto')

    #Plot Freq-vs-time
    data = copy.deepcopy(datacopy)
    #data.downsample(downsamp)
    data.dedisperse(dm, padval='rotate')
    nbinlim = np.int(duration / data.dt)
    img = ax_im.imshow(data.data[..., :nbinlim], aspect='auto', \
                cmap=matplotlib.cm.cmap_d[cmap_str], \
                interpolation='nearest', origin='upper', \
                extent=(data.starttime, data.starttime+ nbinlim*data.dt, \
                        data.freqs.min(), data.freqs.max()))
    #ax_im.axvline(x=(data.starttime + nbinlim*data.dt)/2.0,ymin=data.freqs.min(),ymax=data.freqs.max(),lw=3,color='b')
    if show_cb:
        cb = ax_im.get_figure().colorbar(img)
        cb.set_label("Scaled signal intensity (arbitrary units)")
    # Dressing it up
    ax_im.xaxis.get_major_formatter().set_useOffset(False)
    #ax_im.set_xlabel("Time")
    ax_im.set_ylabel("Frequency (MHz)")

    # Plot Time series
    #Data = np.array(data.data[..., :nbinlim])
    Data = np.array(data.data[..., :nbinlim])
    Dedisp_ts = Data.sum(axis=0)
    times = (np.arange(data.numspectra) * data.dt + start)[..., :nbinlim]
    ax_ts.plot(times, Dedisp_ts, "k")
    ax_ts.set_xlim([times.min(), times.max()])
    text1 = "DM: " + "%.2f" % float(data.dm)
    plt.text(1.1,
             0.9,
             text1,
             fontsize=15,
             ha='center',
             va='center',
             transform=ax_ts.transAxes)
    text2 = "Width: " + "%.2f" % float(width)
    plt.text(1.1,
             0.75,
             text2,
             fontsize=15,
             ha='center',
             va='center',
             transform=ax_ts.transAxes)
    text3 = "SNR: " + "%.2f" % float(snr)
    plt.text(1.1,
             0.6,
             text3,
             fontsize=15,
             ha='center',
             va='center',
             transform=ax_ts.transAxes)
    ax_ts.set_title(title, fontsize=14)
    plt.setp(ax_ts.get_xticklabels(), visible=False)
    plt.setp(ax_ts.get_yticklabels(), visible=False)

    #Spectrum and DM-vs-SNR plot
    #Get window
    spectrum_window = 0.02 * duration
    window_width = int(spectrum_window / data.dt)  # bins
    burst_bin = nbinlim / 2
    ax_ts.axvline(times[burst_bin] - spectrum_window, ls="--", c="grey")
    ax_ts.axvline(times[burst_bin] + spectrum_window, ls="--", c="grey")

    #Get spectrum and DM-vs-SNR for the on-pulse window
    on_spec = np.array(data.data[..., burst_bin - window_width:burst_bin +
                                 window_width])
    on_dmsnr = np.array(dmvstm_array[..., burst_bin - window_width:burst_bin +
                                     window_width])
    Dedisp_spec = np.mean(on_spec, axis=1)
    Dedisp_dmsnr = np.mean(on_dmsnr, axis=1)

    #Get off-pulse and DM-vs-SNR for range outside on-pulse window
    off_spec1 = np.array(data.data[..., 0:burst_bin - window_width])
    off_spec = np.mean(off_spec1, axis=1)
    off_dmsnr1 = np.array(dmvstm_array[..., 0:burst_bin - window_width])
    off_dmsnr = np.mean(off_dmsnr1, axis=1)

    #Get Y-axis for both plots
    dms = np.linspace(lodm, hidm, len(Dedisp_dmsnr))
    freqs = np.linspace(data.freqs.max(), data.freqs.min(), len(Dedisp_spec))

    #Spectrum plot
    ax_spec.plot(Dedisp_spec, freqs, color="red", lw=2)
    ax_spec.plot(off_spec, freqs, color="grey", alpha=0.5, lw=1)
    ttest = float(stats.ttest_ind(Dedisp_spec, off_spec)[0].tolist())
    ttestprob = float(stats.ttest_ind(Dedisp_spec, off_spec)[1].tolist())
    text3 = "t-test"
    plt.text(1.1,
             0.45,
             text3,
             fontsize=12,
             ha='center',
             va='center',
             transform=ax_ts.transAxes)
    text4 = "  %.2f" % (ttest) + "(%.2f" % ((1 - ttestprob) * 100) + "%)"
    plt.text(1.1,
             0.3,
             text4,
             fontsize=12,
             ha='center',
             va='center',
             transform=ax_ts.transAxes)

    #DMvsSNR plot
    ax_dmsnr.plot(Dedisp_dmsnr, dms, color="red", lw=2)
    ax_dmsnr.plot(off_dmsnr, dms, color="grey", alpha=0.5, lw=1)
    Dedisp_dmsnr_split = np.array_split(Dedisp_dmsnr, 3)
    #Sub-array could be different sizes that's why
    Dedisp_dmsnr_split[0] = Dedisp_dmsnr_split[0].sum()
    Dedisp_dmsnr_split[1] = Dedisp_dmsnr_split[1].sum()
    Dedisp_dmsnr_split[2] = Dedisp_dmsnr_split[2].sum()

    #Plot settings
    plt.setp(ax_spec.get_xticklabels(), visible=True)
    plt.setp(ax_dmsnr.get_xticklabels(), visible=False)
    plt.setp(ax_spec.get_yticklabels(), visible=False)
    plt.setp(ax_dmsnr.get_yticklabels(), visible=False)
    ax_spec.set_ylim([data.freqs.min(), data.freqs.max()])
    ax_dmsnr.set_ylim(lodm, hidm)

    #Plot original data
    data.dedisperse(0, padval='rotate')
    ax_im.set_ylabel("Frequency (MHz)")
    ax_orig.set_ylabel("Frequency (MHz)")
    ax_orig.set_xlabel("Time (sec)")
    FTdirection = source_name.split("_")[0]

    if FTdirection == 'nT':
        ndata = data.data[..., ::-1]
        print "Will be flipped in Time"
    elif FTdirection == 'nF':
        ndata = data.data[::-1, ...]
        print "Will be flipped in freq"
    elif FTdirection == 'nTnF':
        ndata = data.data[::-1, ::-1]
        print "Will be flipped in time and freq"
    else:
        ndata = data.data
        print "No flip"

    # Sweeping it up
    for ii, sweep_dm in enumerate(sweep_dms):
        ddm = sweep_dm - data.dm

        #delays = psr_utils.delay_from_DM(ddm, data.freqs)
        #delays -= delays.min()

        if sweep_posns is None:
            sweep_posn = 0.0
        elif len(sweep_posns) == 1:
            sweep_posn = sweep_posns[0]
        else:
            sweep_posn = sweep_posns[ii]

        sweepstart = data.dt * data.numspectra * sweep_posn + data.starttime
        #sweepstart = data.dt*data.numspectra + data.starttime

        sty = "b-"

        if FTdirection == 'nT':
            ddm = (-1) * ddm  # Negative DM
            nfreqs = data.freqs
        elif FTdirection == 'nF':
            nfreqs = data.freqs[::-1]
        elif FTdirection == 'nTnF':
            ddm = (-1) * ddm  # Negative DM
            nfreqs = data.freqs[::-1]
        else:
            nfreqs = data.freqs

        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        ndelay = (delays + sweepstart)
        ndelay2 = (delays + sweepstart + duration)

        ax_orig.set_xlim(data.starttime,
                         data.starttime + len(data.data[0]) * data.dt)
        ax_orig.set_ylim(data.freqs.min(), data.freqs.max())
        ax_orig.plot(ndelay, nfreqs, "b-", lw=2, alpha=0.7)
        ax_orig.plot(ndelay2, nfreqs, "b-", lw=2, alpha=0.7)

    ax_orig.imshow(ndata, aspect='auto', \
        cmap=matplotlib.cm.cmap_d[cmap_str], \
        interpolation='nearest', origin='upper', \
        extent=(data.starttime, data.starttime + len(data.data[0])*data.dt, \
        data.freqs.min(), data.freqs.max()))

    #if interactive:
    #    fig.suptitle("Frequency vs. Time")
    #    fig.canvas.mpl_connect('key_press_event', \
    #            lambda ev: (ev.key in ('q','Q') and plt.close(fig)))
    #oname = "%.3f_%s.png" % (start,str(dm))

    if ofile is "unknown_cand":
        ofile = ofile + "_%.3f_%s.png" % (start, str(dm))

    if ttest > 2 and Dedisp_dmsnr_split[1] > Dedisp_dmsnr_split[
            0] and Dedisp_dmsnr_split[1] > Dedisp_dmsnr_split[2]:
        ofile = "A_" + ofile  #If t-test good then put those candidate first
        plt.text(1.1,
                 0.2,
                 "cat: A",
                 fontsize=12,
                 ha='center',
                 va='center',
                 transform=ax_ts.transAxes)
    if ttest <= 2 and ttest > 1 and Dedisp_dmsnr_split[1] > Dedisp_dmsnr_split[
            0] and Dedisp_dmsnr_split[1] > Dedisp_dmsnr_split[2]:
        ofile = "B_" + ofile
        plt.text(1.1,
                 0.2,
                 "cat: B",
                 fontsize=12,
                 ha='center',
                 va='center',
                 transform=ax_ts.transAxes)
    if ttest <= 1 and Dedisp_dmsnr_split[1] > Dedisp_dmsnr_split[
            0] and Dedisp_dmsnr_split[1] > Dedisp_dmsnr_split[2]:
        plt.text(1.1,
                 0.2,
                 "cat: C",
                 fontsize=12,
                 ha='center',
                 va='center',
                 transform=ax_ts.transAxes)
        ofile = "C_" + ofile

    plt.savefig(ofile)
示例#33
0
    def dm_curve_check(self, spec_index=0.0):
        # Sum the profiles in time
        profs = self.pfd.profs.sum(0)

        ### Generate simulated profiles ###

        # prof_avg: median profile value per subint per subband
        #  Sum over subint axis to get median per subband
        prof_avg = self.pfd.stats[:, :, 4].sum(0)
        # prof_var: variance of profile per subint per subband
        #  Sum over subint axis to get variance per subband
        prof_var = self.pfd.stats[:, :, 5].sum(0)
        # The standard deviation in each subband is proportional to the median
        # value of that subband.  Here we scale all subbands to equal levels.
        scaled_vars = prof_var / prof_avg ** 2
        scaled_profs = (profs.T / prof_avg).T - 1.0
        # The mean profile (after scaling) will be our "clean" profile--hardly
        # true in most cases, but we pretend it's noiseless for what follows
        scaled_mean_prof = scaled_profs.mean(0)
        # Extend this "clean" profile across the number of subbands
        sim_profs_clean = np.tile(scaled_mean_prof, scaled_profs.shape[0]).reshape(scaled_profs.shape)
        # Scale these subbands according to the input spectral index
        spec_mult = (self.pfd.subfreqs / self.pfd.subfreqs[0]) ** spec_index
        spec_mult /= spec_mult.mean()
        sim_profs_spec = (sim_profs_clean.T * spec_mult).T
        # For consistency, set a seed for generating noise
        np.random.seed(1967)
        # Add white noise that matches the variance of the real subbands
        # on a subband by subband basis
        noise = np.random.normal(scale=np.sqrt(scaled_vars), size=scaled_profs.T.shape).T
        # sim_profs_noisy is the simulated equivalent of scaled_profs
        sim_profs_noisy = sim_profs_spec + noise
        # sim_profs_final is the simulated equivalent of profs
        sim_profs = ((sim_profs_noisy + 1.0).T * prof_avg).T

        # The rest of this is essentially code from the prepfold.pfd class
        # in which we loop over DM values and see how strong a signal we
        # get by dedispersing at each of these values

        # Go to higher DMs than the original curve to try to better exclude noise
        DMs = np.linspace(
            self.pfd.dms[0], self.pfd.dms[-1] + 4.0 * (self.pfd.dms[-1] - self.pfd.dms[0]), len(self.pfd.dms)
        )
        chis = np.zeros_like(DMs)
        sim_chis = np.zeros_like(DMs)
        subdelays_bins = self.pfd.subdelays_bins.copy()
        for ii, DM in enumerate(DMs):
            subdelays = psr_utils.delay_from_DM(DM, self.pfd.barysubfreqs)
            hifreqdelay = subdelays[-1]
            subdelays = subdelays - hifreqdelay
            delaybins = subdelays * self.pfd.binspersec - subdelays_bins
            new_subdelays_bins = np.floor(delaybins + 0.5)
            for jj in range(self.pfd.nsub):
                profs[jj] = psr_utils.rotate(profs[jj], int(new_subdelays_bins[jj]))
                sim_profs[jj] = psr_utils.rotate(sim_profs[jj], int(new_subdelays_bins[jj]))
            subdelays_bins += new_subdelays_bins
            # The set of reduced chi2s like those in the prepfold plot
            # (should be the same if the same DMs are used)
            chis[ii] = self.pfd.calc_redchi2(prof=profs.sum(0), avg=self.pfd.avgprof)
            # The same thing but for our "simulated" data
            sim_chis[ii] = self.pfd.calc_redchi2(prof=sim_profs.sum(0), avg=self.pfd.avgprof)
        return DMs, chis, sim_chis
示例#34
0
def main():
    filfns = args # addtional argument is fileterbank files
    if options.debug:
        print "Input filterbank files:", filfns

    obs = fbobs.fbobs(filfns)
    # filfile.print_header()
    obslen = obs.obslen 

    # Determine start and end of interval (in seconds and samples)
    if options.start < 0:
        options.start = 0
    if (options.end is None) or (options.end > obslen):
        # set to end of filterbank file
        options.end = obslen

    reqstartsamp = int(options.start / obs.tsamp) # requested
    # Round down to a multiple of downsamp bins
    reqstartsamp = reqstartsamp - (reqstartsamp % options.downsamp)
    # Get extra bins for smoothing
    startsamp = reqstartsamp - options.width*options.downsamp

    reqendsamp = int(options.end / obs.tsamp) # requested
    # Round up to a multiple of downsamp bins
    reqendsamp = reqendsamp - (reqendsamp % options.downsamp) + options.downsamp
    # Get extra bins for smoothing
    endsamp = reqendsamp + options.width*options.downsamp
    if options.dm:
        # Get extra bins for dedispersion
        # Compute DM delays
        delay_seconds = psr_utils.delay_from_DM(options.dm, obs.frequencies)
        delay_seconds -= np.min(delay_seconds)
        delay_samples = delay_seconds/(options.downsamp*obs.tsamp)
        maxsamps = np.max(delay_samples*options.downsamp)
        maxsamps = int(np.round(float(maxsamps)/options.downsamp))*options.downsamp
        endsamp += maxsamps

    reqnumsaps = reqendsamp - reqstartsamp
    numsamps = endsamp - startsamp

    if options.debug:
        print "Requested start time: %s s (%d samples)" % \
                    (options.start, reqstartsamp)
        print "Actual start time: %s s (%d samples)" % \
                    (startsamp*obs.tsamp, startsamp)
        print "Requested end time: %s s (%d samples)" % \
                    (options.end, reqendsamp)
        print "Actual end time: %s s (%d samples)" % \
                    (endsamp*obs.tsamp, endsamp)

    # read data
    data = obs.get_sample_interval(startsamp, endsamp).astype('float32')
    obs.close_all()
    data.shape = (numsamps, obs.nchans)
   
    if options.mask is not None:
        if options.debug:
            print "Masking channels using %s" % options.mask
        # Mask channels
        mask = rfifind.rfifind(options.mask)
        maskchans = mask.mask_zap_chans
        maskchans = obs.nchans - 1 - np.array(list(maskchans))
        data = mask_channels(data, maskchans)

    # Modify data
    if options.downsamp > 1:
        if options.debug:
            print "Downsampling by %d bins" % options.downsamp
        data = downsample(data, factor=options.downsamp)
    if options.width > 1:
        if options.debug:
            print "Smoothing with boxcar %d bins wide" % options.width
        data = smooth(data, factor=options.width)[options.width:-options.width,:]
        startsamp += options.width*options.downsamp
        endsamp -= options.width*options.downsamp

    # plot data as an image
    fig = plt.figure()
    fig.canvas.set_window_title("Frequency vs. Time")
    ax = plt.axes((0.15, 0.15, 0.8, 0.7))
    data_scaled = scale(data, indep=options.scaleindep)
    data_scaled = data_scaled[:-maxsamps/options.downsamp]
    endsamp -= maxsamps
    plt.imshow(data_scaled.transpose(), \
                    aspect='auto', cmap=matplotlib.cm.binary, interpolation='nearest', \
                    extent=(startsamp/options.downsamp, endsamp/options.downsamp, \
                                obs.frequencies[-1], obs.frequencies[0]))
    plt.xlabel("Sample")
    plt.ylabel("Observing frequency (MHz)")
    plt.suptitle("Frequency vs. Time")
    fig.text(0.05, 0.02, \
                r"Start time: $\sim$ %s s, End time: $\sim$ %s s; " \
                "Downsampled: %d bins, Smoothed: %d bins; " \
                "DM trace: %g $cm^{-3}pc$" % \
                (options.start, options.end, options.downsamp, \
                    options.width, options.dm), \
                ha="left", va="center", size="x-small")
    if options.dm is not None:
        xlim = plt.xlim()
        ylim = plt.ylim()
        
        # Plot dispersion delay trace
        plt.plot(startsamp/options.downsamp+delay_samples, obs.frequencies, \
                    'r-', lw=5, alpha=0.25)
        plt.xlim(xlim)
        plt.ylim(ylim)

        profax = plt.axes((0.15, 0.85, 0.8, 0.1), sharex=ax)
        dedisp_prof = dedisperse(data, delay_samples)[:-maxsamps/options.downsamp]
        plt.plot(np.linspace(xlim[0],xlim[1],dedisp_prof.size), dedisp_prof, 'k-')
        plt.setp(profax.xaxis.get_ticklabels(), visible=False)
        plt.setp(profax.yaxis.get_ticklabels(), visible=False)
        plt.xlim(xlim)
    fig.canvas.mpl_connect('key_press_event', keypress)
    plt.show()
示例#35
0
    # Combine the profiles as required
    profs = fold_pfd.combine_profs(numtoas, numsubbands)

    # PRESTO de-disperses at the high frequency channel so determine a
    # correction to the middle of the band
    if not events:
        subpersumsub = fold_pfd.nsub / numsubbands
        # Calculate the center of the summed subband freqs and delays
        sumsubfreqs = (Num.arange(numsubbands)+0.5)*subpersumsub*fold_pfd.subdeltafreq + \
                      (fold_pfd.lofreq-0.5*fold_pfd.chan_wid)
        # Note:  In the following, we cannot use fold_pfd.hifreqdelay since that
        #        is based on the _barycentric_ high frequency (if the barycentric
        #        conversion was available).  For TOAs, we need a topocentric
        #        delay, which is based on the topocentric frequency fold_pfd.hifreq
        sumsubdelays = (psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) -
                        psr_utils.delay_from_DM(fold_pfd.bestdm,
                                                fold_pfd.hifreq)) / SECPERDAY
    else:
        fold_pfd.subfreqs = Num.asarray([0.0])
        sumsubfreqs = Num.asarray([0.0])
        sumsubdelays = Num.asarray([0.0])

    # Read the template profile
    if templatefilenm is not None:
        template = psr_utils.read_profile(templatefilenm, normalize=1)
    else:
        if (gaussfitfile):
            template = psr_utils.read_gaussfitfile(gaussfitfile,
                                                   fold_pfd.proflen)
        else:
示例#36
0
def make_spd_from_file(spdcand, rawdatafile, \
                       txtfile, maskfile, \
                       min_rank, group_rank, \
                       plot, just_waterfall, \
                       integrate_ts, integrate_spec, disp_pulse, \
                       loc_pulse, nsub, \
                       maxnumcands, \
                       basename, \
                       mask=False, bandpass_corr=True, barytime=True, \
                       man_params=None):
    """
    Makes spd files from output files of rratrap. 
    Inputs:
        spdcand: spcand parameters instance (read in spcand.params)
        rawdatafile: psrfits file instance
        txtfile: rratrap output file (groups.txt file)
        maskfile: rfifind mask file. need this file if you want to remove the bandpass 
                  or use rfifind mask information.
        min_rank: plot all groups with rank more than this. min 1, max 6
        group_rank: plot groups ranked whatever you specify
        plot: do you want to produce the plots as well? 
        just_waterfall: Do you just want to make the waterfall plots.
        integrate_ts: Do you want to display the dedispersed time series in the plot?
        integrate_spec: Do you want to display the pulse spectrum in the plot?
        disp_pulse: Do you want to see the inset dispersed pulse in the plot?
        loc_pulse: Fraction of the window length where the pulse is located.(eg. 0.25 = 1/4th of the way in.
                                                                             0.5 = middle of the plot)
        maxnumcands: What is the maximum number of candidates you would like to generate?
        basename: output basename of the file. Appended with _DM_TIME(s)_RANK.spd 
    Optional arguments:
        mask: Do you want to mask out rfi contaminated channels?
        bandpass_corr: Do you want to remove the bandpass?
        barytime: Is the given time(s) barycentric?
        man_params: Do you want to specify the parameters for waterfalling 
                    manually? If yes, I suggest using the function make_spd_from_man_params().
                    (I suggest giving it the rratrap output file)    
    Outputs:
       Binary npz file containing the necessary arrays and header information to generate the spd plots.
    """
    numcands = 0  # counter for max number of candidates
    loop_must_break = False  # dont break the loop unless num of cands >100.
    files = spio.get_textfile(options.txtfile)
    if group_rank:
        groups = [group_rank - 1]
    else:
        groups = [i for i in range(6) if (i >= min_rank)][::-1]

    for group in groups:
        rank = group + 1
        if files[group] != "Number of rank %i groups: 0 " % rank:
            values = spio.split_parameters(rank, txtfile)
            lis = np.where(files == '\tRank:             %i.000000' % rank)[0]
            for ii in range(len(values)):
                #### Arrays for Plotting DM vs SNR
                dm_list, time_list, dm_arr, sigma_arr, width_arr = spio.read_RRATrap_info(
                    txtfile, lis[ii], rank)

                # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
                spdcand.read_from_file(values[ii], rawdatafile.tsamp, rawdatafile.specinfo.N, \
                                       rawdatafile.frequencies[0], rawdatafile.frequencies[-1], \
                                       rawdatafile, loc_pulse=loc_pulse, dedisp = True, \
                                       scaleindep = None, zerodm = None, mask = mask, \
                                       barytime=barytime, \
                                       nsub = nsub, bandpass_corr = bandpass_corr)

                #make an array to store header information for the spd files
                temp_filename = basename+"_DM%.1f_%.1fs_rank_%i"%(spdcand.subdm, \
                                                   spdcand.topo_start_time, rank)

                print_debug("Running waterfaller with Zero-DM OFF...")

                # Add additional information to the header information array
                data, Data_dedisp_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                             spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                             spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                             spdcand.scaleindep, spdcand.width_bins, \
                                             spdcand.mask, maskfile, spdcand.bandpass_corr)

                text_array = np.array([args[0], rawdatafile.specinfo.telescope, \
                                       rawdatafile.specinfo.ra_str, rawdatafile.specinfo.dec_str, \
                                       rawdatafile.specinfo.start_MJD[0], \
                                       rank, spdcand.nsub, spdcand.nbins, spdcand.subdm, \
                                       spdcand.sigma, spdcand.sample_number, spdcand.duration, \
                                       spdcand.width_bins, spdcand.pulse_width, rawdatafile.tsamp,\
                                       rawdatafile.specinfo.T, spdcand.topo_start_time, data.starttime, \
                                       data.dt,data.numspectra, data.freqs.min(), data.freqs.max()])

                #### Array for plotting Dedispersed waterfall plot zerodm - ON
                print_debug("Running Waterfaller with Zero-DM ON...")
                zerodm = True
                data, Data_dedisp_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                           spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                           spdcand.subdm, zerodm, spdcand.downsamp, \
                                           spdcand.scaleindep, spdcand.width_bins, \
                                           spdcand.mask, maskfile, spdcand.bandpass_corr)
                ####Sweeped without zerodm
                spdcand.read_from_file(values[ii], rawdatafile.tsamp, rawdatafile.specinfo.N, \
                                      rawdatafile.frequencies[0], rawdatafile.frequencies[-1], \
                                      rawdatafile, loc_pulse=loc_pulse, dedisp = None, \
                                      scaleindep = None, zerodm = None, mask = mask, \
                                      barytime=barytime, \
                                      nsub = nsub, bandpass_corr = bandpass_corr)
                data, Data_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                           spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                           spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                           spdcand.scaleindep, spdcand.width_bins, \
                                           spdcand.mask, maskfile, spdcand.bandpass_corr)
                text_array = np.append(text_array, spdcand.sweep_duration)
                text_array = np.append(text_array, data.starttime)
                text_array = np.append(text_array, spdcand.bary_start_time)
                text_array = np.append(text_array, man_params)
                # Array to Construct the sweep
                if spdcand.sweep_dm is not None:
                    ddm = spdcand.sweep_dm - data.dm
                    delays = psr_utils.delay_from_DM(ddm, data.freqs)
                    delays -= delays.min()
                    delays_nozerodm = delays
                    freqs_nozerodm = data.freqs
                # Sweeped with zerodm-on
                zerodm = True
                #downsamp_temp = 1
                data, Data_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                           spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                           spdcand.subdm, zerodm, spdcand.downsamp, \
                                           spdcand.scaleindep, spdcand.width_bins, \
                                           spdcand.mask, maskfile, spdcand.bandpass_corr)
                # Saving the arrays into the .spd file.
                with open(temp_filename + ".spd", 'wb') as f:
                    np.savez_compressed(f, \
                                        Data_dedisp_nozerodm = Data_dedisp_nozerodm.astype(np.float16),\
                                        Data_dedisp_zerodm = Data_dedisp_zerodm.astype(np.float16),\
                                        Data_nozerodm = Data_nozerodm.astype(np.float16),\
                                        delays_nozerodm = delays_nozerodm, \
                                        freqs_nozerodm = freqs_nozerodm,\
                                        Data_zerodm = Data_zerodm.astype(np.float16), \
                                        dm_arr= list(map(np.float16, dm_arr)),\
                                        sigma_arr = list(map(np.float16, sigma_arr)), \
                                        width_arr =list(map(np.uint8, width_arr)),\
                                        dm_list= list(map(np.float16, dm_list)), \
                                        time_list = list(map(np.float16, time_list)), \
                                        text_array = text_array)
                #### Arrays for Plotting DM vs Time is in plot_spd.plot(...)
                if plot:
                    print_debug("Now plotting...")
                    plot_spd.plot(temp_filename+".spd", args[1:], \
                                  spec_width=1.5, loc_pulse=loc_pulse, \
                                  xwin=False, outfile=basename, \
                                  just_waterfall=just_waterfall, \
                                  integrate_spec=integrate_spec, \
                                  integrate_ts=integrate_ts, \
                                  disp_pulse=disp_pulse, tar = None)
                    print_debug("Finished plot %i " % ii +
                                strftime("%Y-%m-%d %H:%M:%S"))
                numcands += 1
                print_debug('Finished sp_candidate : %i' % numcands)
                if numcands >= maxnumcands:  # Max number of candidates to plot 100.
                    loop_must_break = True
                    break
            if loop_must_break:
                break

        print_debug("Finished group %i... " % rank +
                    strftime("%Y-%m-%d %H:%M:%S"))
    print_debug("Finished running waterfaller... " +
                strftime("%Y-%m-%d %H:%M:%S"))
示例#37
0
def inject(infile, outfn, prof, period, dm, nbitsout=None, 
           block_size=BLOCKSIZE, pulsar_only=False, inplace=False):
    if isinstance(infile, filterbank.FilterbankFile):
        fil = infile
    elif inplace:
        fil = filterbank.FilterbankFile(infile, 'readwrite')
    else:
        fil = filterbank.FilterbankFile(infile, 'read')
    print "Injecting pulsar signal into: %s" % fil.filename
    if False:
        delays = psr_utils.delay_from_DM(dm, fil.frequencies)
        delays -= delays[np.argmax(fil.frequencies)]
        get_phases = lambda times: (times-delays)/period % 1
    else:
        get_phases = lambda times: times/period % 1

    # Create the output filterbank file
    if nbitsout is None:
        nbitsout = fil.nbits
    if inplace:
        warnings.warn("Injecting pulsar signal *in-place*")
        outfil = fil
    else:
        # Start an output file
        print "Creating out file: %s" % outfn
        outfil = filterbank.create_filterbank_file(outfn, fil.header, \
                                            nbits=nbitsout, mode='append')

    if outfil.nbits == 8:
        raise NotImplementedError("This code is out of date. 'delays' is not " \
                                    "done in this way anymore..")
        # Read the first second of data to get the global scaling to use
        onesec = fil.get_timeslice(0, 1).copy()
        onesec_nspec = onesec.shape[0]
        times = np.atleast_2d(np.arange(onesec_nspec)*fil.tsamp).T+delays
        phases = times/period % 1
        onesec += prof(phases)
        minimum = np.min(onesec)
        median = np.median(onesec)
        # Set median to 1/3 of dynamic range
        global_scale = (256.0/3.0) / median
        del onesec
    else:
        # No scaling to be performed
        # These values will cause scaling to keep data unchanged
        minimum = 0
        global_scale = 1

    sys.stdout.write(" %3.0f %%\r" % 0)
    sys.stdout.flush()
    oldprogress = -1
    
    # Loop over data
    lobin = 0
    spectra = fil.get_spectra(0, block_size)
    numread = spectra.shape[0]
    while numread:
        if pulsar_only:
            # Do not write out data from input file
            # zero it out
            spectra *= 0
        hibin = lobin+numread
        # Sample at middle of time bin
        times = (np.arange(lobin, hibin, 1.0/NINTEG_PER_BIN)+0.5/NINTEG_PER_BIN)*fil.dt
        #times = (np.arange(lobin, hibin)+0.5)*fil.dt
        phases = get_phases(times)
        profvals = prof(phases)
        shape = list(profvals.shape)
        shape[1:1] = [NINTEG_PER_BIN]
        shape[0] /= NINTEG_PER_BIN
        profvals.shape = shape
        toinject = profvals.mean(axis=1)
        #toinject = profvals
        if np.ndim(toinject) > 1:
            injected = spectra+toinject
        else:
            injected = spectra+toinject[:,np.newaxis]
        scaled = (injected-minimum)*global_scale
        if inplace:
            outfil.write_spectra(scaled, lobin)
        else:
            outfil.append_spectra(scaled)
        
        # Print progress to screen
        progress = int(100.0*hibin/fil.nspec)
        if progress > oldprogress: 
            sys.stdout.write(" %3.0f %%\r" % progress)
            sys.stdout.flush()
            oldprogress = progress
        
        # Prepare for next iteration
        lobin = hibin 
        spectra = fil.get_spectra(lobin, block_size)
        numread = spectra.shape[0]

    sys.stdout.write("Done   \n")
    sys.stdout.flush()
def main():
    parser = optparse.OptionParser(prog="sp_pipeline..py", \
                        version=" Chitrang Patel (May. 12, 2015)", \
                        usage="%prog INFILE(PsrFits FILE, SINGLEPULSE FILES)", \
                        description="Create single pulse plots to show the " \
                                    "frequency sweeps of a single pulse,  " \
                    "DM vs time, and SNR vs DM,"\
                                    "in psrFits data.")
    parser.add_option('--infile', dest='infile', type='string', \
                        help="Give a .inf file to read the appropriate header information.")
    parser.add_option('--groupsfile', dest='txtfile', type='string', \
                        help="Give the groups.txt file to read in the groups information.")
    parser.add_option('--mask', dest='maskfile', type='string', \
                        help="Mask file produced by rfifind. (Default: No Mask).", \
                        default=None)
    options, args = parser.parse_args()
    if not hasattr(options, 'infile'):
        raise ValueError("A .inf file must be given on the command line! ")
    if not hasattr(options, 'txtfile'):
        raise ValueError(
            "The groups.txt file must be given on the command line! ")

    files = get_textfile(options.txtfile)
    print_debug("Begining waterfaller... " + strftime("%Y-%m-%d %H:%M:%S"))
    Detrendlen = 50
    if not args[0].endswith("fits"):
        raise ValueError("The first file must be a psrFits file! ")
    basename = args[0][:-5]
    filetype = "psrfits"
    inffile = options.infile
    topo, bary = bary_and_topo.bary_to_topo(inffile)
    time_shift = bary - topo
    inf = infodata.infodata(inffile)
    RA = inf.RA
    dec = inf.DEC
    MJD = inf.epoch
    mjd = Popen(["mjd2cal", "%f" % MJD], stdout=PIPE, stderr=PIPE)
    date, err = mjd.communicate()
    date = date.split()[2:5]
    telescope = inf.telescope
    N = inf.N
    Total_observed_time = inf.dt * N
    print_debug('getting file..')
    rawdatafile = psrfits.PsrfitsFile(args[0])
    print "rawdatafile", memory.resident() / (1024.0**3)
    bin_shift = np.round(time_shift / rawdatafile.tsamp).astype('int')
    for group in [6, 5, 4, 3, 2]:
        rank = group + 1
        if files[group] != "Number of rank %i groups: 0 " % rank:
            print_debug(files[group])
            values = split_parameters(rank, options.txtfile)
            lis = np.where(files == '\tRank:             %i.000000' % rank)[0]
            for ii in range(len(values)):
                #### Array for Plotting DM vs SNR
                print "DM, S/N", memory.resident() / (1024.0**3)
                print_debug("Making arrays for DM vs Signal to Noise...")
                temp_list = files[lis[ii] - 6].split()
                npulses = int(temp_list[2])
                temp_lines = files[(lis[ii] + 3):(lis[ii] + npulses + 1)]
                arr = np.split(temp_lines, len(temp_lines))
                dm_list = []
                time_list = []
                for i in range(len(arr)):
                    dm_val = float(arr[i][0].split()[0])
                    time_val = float(arr[i][0].split()[2])
                    dm_list.append(dm_val)
                    time_list.append(time_val)
                arr_2 = np.array([arr[i][0].split() for i in range(len(arr))],
                                 dtype=np.float32)
                dm_arr = np.array([arr_2[i][0] for i in range(len(arr))],
                                  dtype=np.float32)
                sigma_arr = np.array([arr_2[i][1] for i in range(len(arr))],
                                     dtype=np.float32)
                print "After DM, S/N", memory.resident() / (1024.0**3)

                #### Array for Plotting DM vs Time is in show_spplots.plot(...)

                #### Setting variables up for the waterfall arrays.
                j = ii + 1
                subdm = dm = sweep_dm = values[ii][0]
                integrate_dm = None
                sigma = values[ii][1]
                sweep_posn = 0.0
                topo_start_time = values[ii][2] - topo_timeshift(
                    values[ii][2], time_shift, topo)[0]
                sample_number = values[ii][3]
                width_bins = values[ii][4]
                binratio = 50
                scaleindep = False
                zerodm = None
                downsamp = np.round((values[ii][2] / sample_number /
                                     6.54761904761905e-05)).astype('int')
                duration = binratio * width_bins * rawdatafile.tsamp * downsamp
                start = topo_start_time - (0.25 * duration)
                if (start < 0.0):
                    start = 0.0
                pulse_width = width_bins * downsamp * 6.54761904761905e-05
                if sigma <= 10:
                    nsub = 32
                elif sigma >= 10 and sigma < 15:
                    nsub = 64
                else:
                    nsub = 96
                nbins = np.round(duration / rawdatafile.tsamp).astype('int')
                start_bin = np.round(start / rawdatafile.tsamp).astype('int')
                dmfac = 4.15e3 * np.abs(1. / rawdatafile.frequencies[0]**2 -
                                        1. / rawdatafile.frequencies[-1]**2)
                nbinsextra = np.round(
                    (duration + dmfac * dm) / rawdatafile.tsamp).astype('int')
                if (start_bin + nbinsextra) > N - 1:
                    nbinsextra = N - 1 - start_bin
                data = rawdatafile.get_spectra(start_bin, nbinsextra)
                print "After rawdata", memory.resident() / (1024.0**3)
                data = maskdata(data, start_bin, nbinsextra, options.maskfile)
                #make an array to store header information for the .npz files
                temp_filename = basename + "_DM%.1f_%.1fs_rank_%i" % (
                    subdm, topo_start_time, rank)
                # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
                print_debug("Running waterfaller with Zero-DM OFF...")
                data, Data_dedisp_nozerodm = waterfall_array(
                    start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm,
                    integrate_dm, downsamp, scaleindep, width_bins,
                    rawdatafile, binratio, data)
                print "waterfall", memory.resident() / (1024.0**3)
                # Add additional information to the header information array
                text_array = np.array([
                    args[0], 'Arecibo', RA, dec, MJD, rank, nsub, nbins, subdm,
                    sigma, sample_number, duration, width_bins, pulse_width,
                    rawdatafile.tsamp, Total_observed_time, topo_start_time,
                    data.starttime, data.dt, data.numspectra,
                    data.freqs.min(),
                    data.freqs.max()
                ])

                #### Array for plotting Dedispersed waterfall plot zerodm - ON
                print_debug("Running Waterfaller with Zero-DM ON...")
                data = rawdatafile.get_spectra(start_bin, nbinsextra)
                data = maskdata(data, start_bin, nbinsextra, options.maskfile)
                zerodm = True
                data, Data_dedisp_zerodm = waterfall_array(
                    start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm,
                    integrate_dm, downsamp, scaleindep, width_bins,
                    rawdatafile, binratio, data)
                print "waterfall", memory.resident() / (1024.0**3)
                ####Sweeped without zerodm
                print_debug("Running waterfaller for sweeped arrays.")
                start = start + (0.25 * duration)
                start_bin = np.round(start / rawdatafile.tsamp).astype('int')
                sweep_duration = 4.15e3 * np.abs(
                    1. / rawdatafile.frequencies[0]**2 -
                    1. / rawdatafile.frequencies[-1]**2) * sweep_dm
                nbins = np.round(sweep_duration /
                                 (rawdatafile.tsamp)).astype('int')
                if ((nbins + start_bin) > (N - 1)):
                    nbins = N - 1 - start_bin
                data = rawdatafile.get_spectra(start_bin, nbins)
                data = maskdata(data, start_bin, nbins, options.maskfile)
                zerodm = None
                dm = None
                data, Data_nozerodm = waterfall_array(
                    start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm,
                    integrate_dm, downsamp, scaleindep, width_bins,
                    rawdatafile, binratio, data)
                print "waterfall", memory.resident() / (1024.0**3)
                text_array = np.append(text_array, sweep_duration)
                text_array = np.append(text_array, data.starttime)
                # Array to Construct the sweep
                if sweep_dm is not None:
                    ddm = sweep_dm - data.dm
                    delays = psr_utils.delay_from_DM(ddm, data.freqs)
                    delays -= delays.min()
                    delays_nozerodm = delays
                    freqs_nozerodm = data.freqs
                # Sweeped with zerodm-on
                zerodm = True
                downsamp_temp = 1
                data, Data_zerodm = waterfall_array(start_bin, dmfac, duration,
                                                    nbins, zerodm, nsub, subdm,
                                                    dm, integrate_dm,
                                                    downsamp_temp, scaleindep,
                                                    width_bins, rawdatafile,
                                                    binratio, data)
                print "waterfall", memory.resident() / (1024.0**3)
                # Saving the arrays into the .spd file.
                with open(temp_filename + ".spd", 'wb') as f:
                    np.savez_compressed(
                        f,
                        Data_dedisp_nozerodm=Data_dedisp_nozerodm.astype(
                            np.float16),
                        Data_dedisp_zerodm=Data_dedisp_zerodm.astype(
                            np.float16),
                        Data_nozerodm=Data_nozerodm.astype(np.float16),
                        delays_nozerodm=delays_nozerodm,
                        freqs_nozerodm=freqs_nozerodm,
                        Data_zerodm=Data_zerodm.astype(np.float16),
                        dm_arr=map(np.float16, dm_arr),
                        sigma_arr=map(np.float16, sigma_arr),
                        dm_list=map(np.float16, dm_list),
                        time_list=map(np.float16, time_list),
                        text_array=text_array)
                print_debug("Now plotting...")
                print "Before plot..", memory.resident() / (1024.0**3)
                show_spplots.plot(temp_filename + ".spd",
                                  args[1:],
                                  xwin=False,
                                  outfile=basename,
                                  tar=None)
                print "After plot..", memory.resident() / (1024.0**3)
                print_debug("Finished plot %i " % j +
                            strftime("%Y-%m-%d %H:%M:%S"))
        print_debug("Finished group %i... " % rank +
                    strftime("%Y-%m-%d %H:%M:%S"))
    print_debug("Finished running waterfaller... " +
                strftime("%Y-%m-%d %H:%M:%S"))
示例#39
0
def main():
    fn = args[0]
    if fn.endswith(".fil"):
        # Filterbank file
        filetype = "filterbank"
        rawdatafile = filterbank.filterbank(fn)
    elif fn.endswith(".fits"):
        # PSRFITS file
        filetype = "psrfits"
        rawdatafile = psrfits.PsrfitsFile(fn)
    else:
        raise ValueError("Cannot recognize data file type from "
                         "extension. (Only '.fits' and '.fil' "
                         "are supported.)")

    # Read data
    start_bin = np.round(options.start/rawdatafile.tsamp).astype('int')
	#dmfac = 4.15e3 * np.abs(1./rawdatafile.fch1**2 - 1./(rawdatafile.frequencies[-1])**2)
    dmfac = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2 - 1./rawdatafile.frequencies[-1]**2)
    if options.nbins is None:
        nbins = np.round(options.duration/rawdatafile.tsamp).astype('int')
    else:
        nbins = options.nbins
    binratio = 50    
    if options.dm:
	nbinsextra = np.round((options.duration + dmfac * options.dm)/rawdatafile.tsamp).astype('int')
    else:
        nbinsextra = nbins    
    data = rawdatafile.get_spectra(start_bin, nbinsextra)
    data = maskfile(data, start_bin, nbinsextra)
    data, bins = waterfall(start_bin, dmfac, options.duration, nbins, options.zerodm, options.nsub, options.subdm, options.dm, options.integrate_dm, options.downsamp, options.scaleindep, options.width_bins, rawdatafile, binratio, data)
    # Ploting it up
    fig = plt.figure()
    fig.canvas.set_window_title("Frequency vs. Time")
    ax = plt.axes((0.15, 0.15, 0.8, 0.60))
    ragfac = float(nbins)/bins
    dmrange, trange = data.data.shape
    nbinlim = np.int(trange * ragfac)
    print data.dt, rawdatafile.tsamp 
   #np.save('data',data.data[..., :nbinlim])
    plt.imshow(data.data[..., :nbinlim], aspect='auto', \
                cmap=matplotlib.cm.cmap_d[options.cmap], \
                interpolation='nearest', origin='upper', \
                extent=(data.starttime, data.starttime+ nbinlim*data.dt, \
                        data.freqs.min(), data.freqs.max()))
    if options.show_cb:
        cb = plt.colorbar()
        cb.set_label("Scaled signal intensity (arbitrary units)")

    #plt.axis('tight')
    # Sweeping it up
    for ii, sweep_dm in enumerate(options.sweep_dms):
        ddm = sweep_dm-data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        
        if options.sweep_posns is None:
            sweep_posn = 0.0
        elif len(options.sweep_posns) == 1:
            sweep_posn = options.sweep_posns[0]
        else:
            sweep_posn = options.sweep_posns[ii]
        sweepstart = data.dt*data.numspectra*sweep_posn+data.starttime
        sty = SWEEP_STYLES[ii%len(SWEEP_STYLES)]
        plt.plot(delays+sweepstart, data.freqs, sty, lw=4, alpha=0.5)

    # Dressing it up
    plt.xlabel("Time")
    plt.ylabel("Observing frequency (MHz)")
    plt.suptitle("Frequency vs. Time")
    # Plot Time series
    if options.integrate_dm is not None:
        Data = np.array(data.data[..., :nbinlim])
        Dedisp_ts = Data.sum(axis=0)
        times = (np.arange(data.numspectra)*data.dt + options.start)[..., :nbinlim]
        ax = plt.axes((0.15, 0.75, 0.8, 0.2))
        plt.plot(times, Dedisp_ts)
	plt.setp(ax.get_xticklabels(), visible = False)
	plt.setp(ax.get_yticklabels(), visible = False)
    fig.canvas.mpl_connect('key_press_event', \
            lambda ev: (ev.key in ('q','Q') and plt.close(fig)))
    plt.show()
示例#40
0
    if counterpart == False:
      print "Didn't find counterpart frequency for %f" % f0
      sub.set_weight(i,0.0)
      continue
#    else:
#      print counterpart,f1
#    print f0, f1
#    sys.exit()
#    print f0,f1

    p = sub.get_folding_period()

    smear = (psr_utils.dm_smear(dm,chbw,f0) 
            + psr_utils.dm_smear(dm,chbw,f1)) / p

    delay = (psr_utils.delay_from_DM(dm,f0) 
            - psr_utils.delay_from_DM(dm,f1)) / p

    delay -= int(delay)
    if delay<-0.5: delay += 1.0
    if delay>0.5: delay -= 1.0

    #print i, smear, delay

    for pol in range(2):
        if pol == 0:
          correction = pol0correction/2.0
        else:
          correction = pol1correction/2.0
#        correction = correction * 16.0
示例#41
0
def main():
    parser = optparse.OptionParser(prog="sp_pipeline..py", \
                        version=" Chitrang Patel (May. 12, 2015)", \
                        usage="%prog INFILE(PsrFits FILE, SINGLEPULSE FILES)", \
                        description="Create single pulse plots to show the " \
                                    "frequency sweeps of a single pulse,  " \
                    "DM vs time, and SNR vs DM,"\
                                    "in psrFits data.")
    parser.add_option('--infile', dest='infile', type='string', \
                        help="Give a .inf file to read the appropriate header information.")
    parser.add_option('--groupsfile', dest='txtfile', type='string', \
                        help="Give the groups.txt file to read in the groups information.")
    parser.add_option('--mask', dest='maskfile', type='string', \
                        help="Mask file produced by rfifind. (Default: No Mask).", \
                        default=None)
    parser.add_option('-n', dest='maxnumcands', type='int', \
                        help="Maximum number of candidates to plot. (Default: 100).", \
                        default=100)
    options, args = parser.parse_args()
    if not hasattr(options, 'infile'):
        raise ValueError("A .inf file must be given on the command line! ")
    if not hasattr(options, 'txtfile'):
        raise ValueError(
            "The groups.txt file must be given on the command line! ")

    files = get_textfile(options.txtfile)
    print_debug("Begining waterfaller... " + strftime("%Y-%m-%d %H:%M:%S"))
    if not args[0].endswith("fits"):
        raise ValueError("The first file must be a psrFits file! ")
    print_debug('Maximum number of candidates to plot: %i' %
                options.maxnumcands)
    basename = args[0][:-5]
    filetype = "psrfits"
    inffile = options.infile
    topo, bary = bary_and_topo.bary_to_topo(inffile)
    time_shift = bary - topo
    inf = infodata.infodata(inffile)
    RA = inf.RA
    dec = inf.DEC
    MJD = inf.epoch
    mjd = Popen(["mjd2cal", "%f" % MJD], stdout=PIPE, stderr=PIPE)
    date, err = mjd.communicate()
    date = date.split()[2:5]
    telescope = inf.telescope
    N = inf.N
    numcands = 0
    Total_observed_time = inf.dt * N
    print_debug('getting file..')
    values = split_parameters(options.txtfile)
    if len(values) > options.maxnumcands:
        values = sorted(values, key=itemgetter(
            5, 1))  #sorting candidates based on ranks and snr
        values = values[-options.maxnumcands:]
        print "More than", options.maxnumcands, "candidates, making plots for", options.maxnumcands, "candidates"
    values = sorted(values, key=itemgetter(0))
    for ii in range(len(values)):
        #### Array for Plotting DM vs SNR
        print_debug("Making arrays for DM vs Signal to Noise...")
        temp_list = files[values[ii][6] - 6].split()
        npulses = int(temp_list[2])
        temp_lines = files[(values[ii][6] + 3):(values[ii][6] + npulses + 1)]
        arr = np.split(temp_lines, len(temp_lines))
        dm_list = []
        time_list = []
        for i in range(len(arr)):
            dm_val = float(arr[i][0].split()[0])
            time_val = float(arr[i][0].split()[2])
            dm_list.append(dm_val)
            time_list.append(time_val)
        arr_2 = np.array([arr[i][0].split() for i in range(len(arr))],
                         dtype=np.float32)
        dm_arr = np.array([arr_2[i][0] for i in range(len(arr))],
                          dtype=np.float32)
        sigma_arr = np.array([arr_2[i][1] for i in range(len(arr))],
                             dtype=np.float32)
        #### Array for Plotting DM vs Time is in show_spplots.plot(...)

        #### Setting variables up for the waterfall arrays.
        j = ii + 1
        subdm = dm = sweep_dm = values[ii][0]
        sample_number = values[ii][3]
        rank = values[ii][5]
        width_bins = values[ii][4]
        #print "dm", dm
        #print "width_bins", width_bins
        downsamp = np.round(
            (values[ii][2] / sample_number / inf.dt)).astype('int')
        #print "downsamp", downsamp
        pulse_width = width_bins * downsamp * inf.dt
        #print "pulse_width", pulse_width
        if ii == 0:
            mask_subband = rfifind.rfifind("%s_rfifind.mask" % (basename))
            mask_subband.set_zap_chans(power=1000, plot=False)
            mask_subband.set_weights_and_offsets()
            mask_subband.write_weights(filename="%s_weights.txt" % (basename))
            cmd = "psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s" % (
                dm, basename, dm, basename, args[0])
            call(cmd, shell=True)
            #subband args[0] at dm and then generate a file that will be set equal to rawdatafile
            subband_file = "%s_subband_%.2f_0001.fits" % (basename, dm)
            dm_prev = dm
            subband_prev = subband_file
        else:
            dm_diff = dm - dm_prev
            t_smear = 8.3e3 * dm_diff * (350**-3) * (
                np.abs(rawdatafile.frequencies[0] -
                       rawdatafile.frequencies[-1]) / 128.)
            if (5 * t_smear) > pulse_width:
                cmd = "psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s" % (
                    dm, basename, dm, basename, args[0])
                call(cmd, shell=True)
                #subband args[0] at dm and then generate a file that will be set equal to rawdatafile
                subband_file = "%s_subband_%.2f_0001.fits" % (basename, dm)
                dm_prev = dm
                subband_prev = subband_file

        rawdatafile = psrfits.PsrfitsFile(subband_file)
        bin_shift = np.round(time_shift / rawdatafile.tsamp).astype('int')
        integrate_dm = None
        sigma = values[ii][1]
        sweep_posn = 0.0
        bary_start_time = values[ii][2]
        topo_start_time = bary_start_time - topo_timeshift(
            bary_start_time, time_shift, topo)[0]
        binratio = 50
        scaleindep = False
        zerodm = None
        duration = binratio * width_bins * rawdatafile.tsamp * downsamp
        start = topo_start_time - (0.25 * duration)
        if (start < 0.0):
            start = 0.0
        if sigma <= 7:
            nsub = 32
        elif sigma >= 7 and sigma < 10:
            nsub = 64
        else:
            nsub = 128
        nbins = np.round(duration / rawdatafile.tsamp).astype('int')
        start_bin = np.round(start / rawdatafile.tsamp).astype('int')
        dmfac = 4.15e3 * np.abs(1. / rawdatafile.frequencies[0]**2 -
                                1. / rawdatafile.frequencies[-1]**2)
        nbinsextra = np.round(
            (duration + dmfac * dm) / rawdatafile.tsamp).astype('int')
        if (start_bin + nbinsextra) > N - 1:
            nbinsextra = N - 1 - start_bin
        data = rawdatafile.get_spectra(start_bin, nbinsextra)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
        #make an array to store header information for the .npz files
        temp_filename = basename + "_DM%.1f_%.1fs_rank_%i" % (
            subdm, topo_start_time, rank)
        # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
        print_debug("Running waterfaller with Zero-DM OFF...")
        data, Data_dedisp_nozerodm = waterfall_array(
            start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm,
            integrate_dm, downsamp, scaleindep, width_bins, rawdatafile,
            binratio, data)
        #Add additional information to the header information array
        text_array = np.array([
            subband_file, 'GBT', RA, dec, MJD, rank, nsub, nbins, subdm, sigma,
            sample_number, duration, width_bins, pulse_width,
            rawdatafile.tsamp, Total_observed_time, topo_start_time,
            data.starttime, data.dt, data.numspectra,
            data.freqs.min(),
            data.freqs.max()
        ])
        #### Array for plotting Dedispersed waterfall plot zerodm - ON
        print_debug("Running Waterfaller with Zero-DM ON...")
        #print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbinsextra)
        #print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
        zerodm = True
        data, Data_dedisp_zerodm = waterfall_array(start_bin, dmfac, duration,
                                                   nbins, zerodm, nsub, subdm,
                                                   dm, integrate_dm, downsamp,
                                                   scaleindep, width_bins,
                                                   rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
        ####Sweeped without zerodm
        start = start + (0.25 * duration)
        start_bin = np.round(start / rawdatafile.tsamp).astype('int')
        sweep_duration = 4.15e3 * np.abs(
            1. / rawdatafile.frequencies[0]**2 -
            1. / rawdatafile.frequencies[-1]**2) * sweep_dm
        nbins = np.round(sweep_duration / (rawdatafile.tsamp)).astype('int')
        if ((nbins + start_bin) > (N - 1)):
            nbins = N - 1 - start_bin

#print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbins)
        #print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbins, options.maskfile)
        zerodm = None
        dm = None
        data, Data_nozerodm = waterfall_array(start_bin, dmfac, duration,
                                              nbins, zerodm, nsub, subdm, dm,
                                              integrate_dm, downsamp,
                                              scaleindep, width_bins,
                                              rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
        text_array = np.append(text_array, sweep_duration)
        text_array = np.append(text_array, data.starttime)
        text_array = np.append(text_array, bary_start_time)
        # Array to Construct the sweep
        if sweep_dm is not None:
            ddm = sweep_dm - data.dm
            delays = psr_utils.delay_from_DM(ddm, data.freqs)
            delays -= delays.min()
            delays_nozerodm = delays
            freqs_nozerodm = data.freqs
        # Sweeped with zerodm-on
        zerodm = True
        downsamp_temp = 1
        data, Data_zerodm = waterfall_array(start_bin, dmfac, duration, nbins,
                                            zerodm, nsub, subdm, dm,
                                            integrate_dm, downsamp_temp,
                                            scaleindep, width_bins,
                                            rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
        # Saving the arrays into the .spd file.
        with open(temp_filename + ".spd", 'wb') as f:
            np.savez_compressed(
                f,
                Data_dedisp_nozerodm=Data_dedisp_nozerodm.astype(np.float16),
                Data_dedisp_zerodm=Data_dedisp_zerodm.astype(np.float16),
                Data_nozerodm=Data_nozerodm.astype(np.float16),
                delays_nozerodm=delays_nozerodm,
                freqs_nozerodm=freqs_nozerodm,
                Data_zerodm=Data_zerodm.astype(np.float16),
                dm_arr=map(np.float16, dm_arr),
                sigma_arr=map(np.float16, sigma_arr),
                dm_list=map(np.float16, dm_list),
                time_list=map(np.float16, time_list),
                text_array=text_array)
        print_debug("Now plotting...")
        #print "Before plot..",memory.resident()/(1024.0**3)
        show_spplots.plot(temp_filename + ".spd",
                          args[1:],
                          xwin=False,
                          outfile=basename,
                          tar=None)
        print_debug("Finished plot %i " % j + strftime("%Y-%m-%d %H:%M:%S"))
        #print "After plot..",memory.resident()/(1024.0**3)
        numcands += 1
        print_debug('Finished sp_candidate : %i' % numcands)
    print_debug("Finished running waterfaller... " +
                strftime("%Y-%m-%d %H:%M:%S"))
示例#42
0
 def get_delaybins(self, dm):
     subdelays = psr_utils.delay_from_DM(dm, self.subfreqs)
     hifreqdelay = subdelays[-1]
     subdelays = subdelays-hifreqdelay
     delaybins = subdelays*self.binspersec
     return np.floor(delaybins+0.5)
示例#43
0
 def get_delaybins(self, dm):
     subdelays = psr_utils.delay_from_DM(dm, self.subfreqs)
     hifreqdelay = subdelays[-1]
     subdelays = subdelays - hifreqdelay
     delaybins = subdelays * self.binspersec
     return np.floor(delaybins + 0.5)
示例#44
0
        if counterpart == False:
            print "Didn't find counterpart frequency for %f" % f0
            sub.set_weight(i, 0.0)
            continue
#    else:
#      print counterpart,f1
#    print f0, f1
#    sys.exit()
#    print f0,f1

        p = sub.get_folding_period()

        smear = (psr_utils.dm_smear(dm, chbw, f0) +
                 psr_utils.dm_smear(dm, chbw, f1)) / p

        delay = (psr_utils.delay_from_DM(dm, f0) -
                 psr_utils.delay_from_DM(dm, f1)) / p

        delay -= int(delay)
        if delay < -0.5: delay += 1.0
        if delay > 0.5: delay -= 1.0

        #print i, smear, delay

        for pol in range(2):
            if pol == 0:
                correction = pol0correction / 2.0
            else:
                correction = pol1correction / 2.0
#        correction = correction * 16.0
示例#45
0
    # Combine the profiles as required
    profs = fold_pfd.combine_profs(numtoas, numsubbands)

    # PRESTO de-disperses at the high frequency channel so determine a
    # correction to the middle of the band
    if not events:
        subpersumsub = fold_pfd.nsub / numsubbands
        # Calculate the center of the summed subband freqs and delays
        sumsubfreqs = (Num.arange(numsubbands)+0.5)*subpersumsub*fold_pfd.subdeltafreq + \
                      (fold_pfd.lofreq-0.5*fold_pfd.chan_wid)
        # Note:  In the following, we cannot use fold_pfd.hifreqdelay since that
        #        is based on the _barycentric_ high frequency (if the barycentric
        #        conversion was available).  For TOAs, we need a topocentric
        #        delay, which is based on the topocentric frequency fold_pfd.hifreq
        sumsubdelays = (
            psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) -
            psr_utils.delay_from_DM(fold_pfd.bestdm, fold_pfd.hifreq))
        sumsubdelays_phs = Num.fmod(sumsubdelays / p_dedisp, 1.0)
        # Save the "higest channel within a subband" freqs/delays for use in
        # later DM/timing correction. PBD 2011/11/03
        sumsubfreqs_hi = sumsubfreqs + \
                fold_pfd.subdeltafreq/2.0 - fold_pfd.chan_wid/2.0
        subdelays2 = psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs) - \
                psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs_hi)

    else:
        fold_pfd.subfreqs = Num.asarray([0.0])
        sumsubfreqs = Num.asarray([0.0])
        sumsubdelays = Num.asarray([0.0])
        subdelays2 = Num.asarray([0.0])
        sumsubdelays_phs = Num.asarray([0.0])
示例#46
0
    profs = fold_pfd.combine_profs(numtoas, numsubbands)

    # PRESTO de-disperses at the high frequency channel so determine a
    # correction to the middle of the band
    if not events:
        subpersumsub = fold_pfd.nsub / numsubbands
        # Calculate the center of the summed subband freqs and delays
        sumsubfreqs = (Num.arange(numsubbands) + 0.5) * subpersumsub * fold_pfd.subdeltafreq + (
            fold_pfd.lofreq - 0.5 * fold_pfd.chan_wid
        )
        # Note:  In the following, we cannot use fold_pfd.hifreqdelay since that
        #        is based on the _barycentric_ high frequency (if the barycentric
        #        conversion was available).  For TOAs, we need a topocentric
        #        delay, which is based on the topocentric frequency fold_pfd.hifreq
        sumsubdelays = (
            psr_utils.delay_from_DM(fold_pfd.bestdm, sumsubfreqs)
            - psr_utils.delay_from_DM(fold_pfd.bestdm, fold_pfd.hifreq)
        ) / SECPERDAY
    else:
        fold_pfd.subfreqs = Num.asarray([0.0])
        sumsubfreqs = Num.asarray([0.0])
        sumsubdelays = Num.asarray([0.0])

    # Read the template profile
    if templatefilenm is not None:
        template = psr_utils.read_profile(templatefilenm, normalize=1)
    else:
        if gaussfitfile:
            template = psr_utils.read_gaussfitfile(gaussfitfile, fold_pfd.proflen)
        else:
            template = psr_utils.gaussian_profile(fold_pfd.proflen, 0.0, gaussianwidth)
示例#47
0
def plot_waterfall(data, start, duration, 
                   integrate_ts=False, integrate_spec=False, show_cb=False, 
                   cmap_str="gist_yarg", sweep_dms=[], sweep_posns=[],
                   ax_im=None, ax_ts=None, ax_spec=None, interactive=True):
    """ I want a docstring too!
    """

    # Set up axes
    if interactive:
        fig = plt.figure()
        fig.canvas.set_window_title("Frequency vs. Time")

    im_width = 0.6 if integrate_spec else 0.8
    im_height = 0.6 if integrate_ts else 0.8

    if not ax_im:
        ax_im = plt.axes((0.15, 0.15, im_width, im_height))
    if integrate_ts and not ax_ts:
        ax_ts = plt.axes((0.15, 0.75, im_width, 0.2),sharex=ax_im)

    if integrate_spec and not ax_spec:
        ax_spec = plt.axes((0.75, 0.15, 0.2, im_height),sharey=ax_im)

    # Ploting it up
    nbinlim = np.int(duration/data.dt)

    img = ax_im.imshow(data.data[..., :nbinlim], aspect='auto', \
                cmap=matplotlib.cm.cmap_d[cmap_str], \
                interpolation='nearest', origin='upper', \
                extent=(data.starttime, data.starttime+ nbinlim*data.dt, \
                        data.freqs.min(), data.freqs.max()))
    if show_cb:
        cb = ax_im.get_figure().colorbar(img)
        cb.set_label("Scaled signal intensity (arbitrary units)")

    #plt.axis('tight')
    # Sweeping it up
    for ii, sweep_dm in enumerate(sweep_dms):
        ddm = sweep_dm-data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        
        if sweep_posns is None:
            sweep_posn = 0.0
        elif len(sweep_posns) == 1:
            sweep_posn = sweep_posns[0]
        else:
            sweep_posn = sweep_posns[ii]
        sweepstart = data.dt*data.numspectra*sweep_posn+data.starttime
        sty = SWEEP_STYLES[ii%len(SWEEP_STYLES)]
        ax_im.plot(delays+sweepstart, data.freqs, sty, lw=4, alpha=0.5)

    # Dressing it up
    ax_im.xaxis.get_major_formatter().set_useOffset(False)
    ax_im.set_xlabel("Time")
    ax_im.set_ylabel("Observing frequency (MHz)")

    # Plot Time series
    if integrate_ts:
        Data = np.array(data.data[..., :nbinlim])
        Dedisp_ts = Data.sum(axis=0)
        times = (np.arange(data.numspectra)*data.dt + start)[..., :nbinlim]
        ax_ts.plot(times, Dedisp_ts,"k")
        ax_ts.set_xlim([times.min(),times.max()])
	plt.setp(ax_ts.get_xticklabels(), visible = False)
	plt.setp(ax_ts.get_yticklabels(), visible = False)

    # Plot Spectrum                                                             
    if integrate_spec:                                                         
        spectrum_window = 0.05*duration
        window_width = int(spectrum_window/data.dt) # bins
        burst_bin = nbinlim/2
        on_spec = np.array(data.data[..., burst_bin-window_width:burst_bin+window_width])
        Dedisp_spec = on_spec.sum(axis=1)[::-1]                                 
                                                                                
        freqs = np.linspace(data.freqs.min(), data.freqs.max(), len(Dedisp_spec))           
        ax_spec.plot(Dedisp_spec,freqs,"k")                                       
        plt.setp(ax_spec.get_xticklabels(), visible = False)                   
        plt.setp(ax_spec.get_yticklabels(), visible = False)                    
        ax_spec.set_ylim([data.freqs.min(),data.freqs.max()])                   
        if integrate_ts:
            ax_ts.axvline(times[burst_bin]-spectrum_window,ls="--",c="grey")                  
            ax_ts.axvline(times[burst_bin]+spectrum_window,ls="--",c="grey")                  

    if interactive:
        fig.suptitle("Frequency vs. Time")
        fig.canvas.mpl_connect('key_press_event', \
                lambda ev: (ev.key in ('q','Q') and plt.close(fig)))

        plt.show()
示例#48
0
def make_spd_from_man_params(spdcand, rawdatafile, \
                             txtfile, maskfile, \
                             plot, just_waterfall, \
                             subdm, dm, sweep_dm, \
                             sigma, \
                             start_time, duration, \
                             width_bins, nbins, downsamp, \
                             nsub, \
                             scaleindep, \
                             spec_width, loc_pulse, \
                             integrate_ts, integrate_spec, disp_pulse, \
                             basename, \
                             mask, bandpass_corr, barytime, man_params):
    """
    Makes spd files from output files of rratrap. 
    Inputs:
        spdcand: spcand parameters instance (read in spcand.params)
        rawdatafile: psrfits file instance
        txtfile: rratrap output file (groups.txt file)
        maskfile: rfifind mask file. need this file if you want to remove the bandpass 
                  or use rfifind mask information.
        plot: do you want to produce the plots as well? 
        just_waterfall: Do you just want to make the waterfall plots.
        subdm: DM to use when subbanding.
        dm: DM to use when dedispersing data for plot. 
        sweep_dm: Show the frequency sweep using this DM.
        sigma: signal-to-noise of the pulse
        start_time: start time of the data to be read in for waterfalling.
        duration: duration of data to be waterfalled.
        width_bins: Smooth each channel/subband with a boxcar width_bins wide.
        nbins: Number of time bins to plot. This option overrides
                the duration argument. 
        downsamp: Factor to downsample in time by. Default: Don't downsample.
        nsub: Number of subbands to use. Must be a factor of number of channels.
        scaleindep:Do you want to scale each subband independently?(Type: Boolean)
        spec_width: Twice this number times the pulse_width around the pulse to consider for the spectrum
        loc_pulse: Fraction of the window length where the pulse is located.(eg. 0.25 = 1/4th of the way in.
                                                                             0.5 = middle of the plot)
        integrate_ts: Do you want to display the dedispersed time series in the plot?
        integrate_spec: Do you want to display the pulse spectrum in the plot?
        disp_pulse: Do you want to see the inset dispersed pulse in the plot?
        basename: output basename of the file. Appended with _DM_TIME(s)_RANK.spd 
        mask: Do you want to mask out rfi contaminated channels?
        bandpass_corr: Do you want to remove the bandpass?
        barytime: Is the given time(s) barycentric?
        man_params: Do you want to specify the parameters for waterfalling 
                    manually? If yes, I suggest using the function make_spd_from_man_params().
                    (I suggest giving it the rratrap output file)    
    Outputs:
       Binary npz file containing the necessary arrays and header information to generate the spd plots.
    """
    rank = None
    if not nsub:
        nsub = rawdatafile.nchan

    # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
    spdcand.manual_params(subdm, dm, sweep_dm, sigma, start_time, \
                         width_bins, downsamp, duration, nbins, nsub, rawdatafile.tsamp, \
                         rawdatafile.specinfo.N, \
                         rawdatafile.frequencies[0], rawdatafile.frequencies[-1], rawdatafile, \
                         loc_pulse=loc_pulse, dedisp=True, scaleindep=False, zerodm=False, \
                         mask=mask, barytime=barytime, bandpass_corr=bandpass_corr)
    #make an array to store header information for the spd files
    temp_filename = basename + "_DM%.1f_%.1fs" % (spdcand.subdm,
                                                  spdcand.topo_start_time)

    print_debug("Running waterfaller with Zero-DM OFF...")
    data, Data_dedisp_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    # Add additional information to the header information array
    text_array = np.array([args[0], rawdatafile.specinfo.telescope, \
                           rawdatafile.specinfo.ra_str, rawdatafile.specinfo.dec_str, \
                           rawdatafile.specinfo.start_MJD[0], rank, \
                           spdcand.nsub, spdcand.nbins, \
                           spdcand.subdm, spdcand.sigma, spdcand.sample_number, \
                           spdcand.duration, spdcand.width_bins, spdcand.pulse_width, \
                           rawdatafile.tsamp, rawdatafile.specinfo.T, spdcand.topo_start_time, \
                           data.starttime, data.dt,data.numspectra, data.freqs.min(), \
                           data.freqs.max()])

    #### Array for plotting Dedispersed waterfall plot zerodm - ON
    print_debug("Running Waterfaller with Zero-DM ON...")
    zerodm = True
    data, Data_dedisp_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    ####Sweeped without zerodm
    spdcand.manual_params(subdm, dm, sweep_dm, sigma, start_time, \
                          width_bins, downsamp, duration, nbins, nsub, rawdatafile.tsamp, \
                          rawdatafile.specinfo.N, \
                          rawdatafile.frequencies[0], rawdatafile.frequencies[-1], rawdatafile, \
                          loc_pulse=loc_pulse, dedisp=None, scaleindep=None, zerodm=None, mask=mask, \
                          barytime=barytime, bandpass_corr=bandpass_corr)
    data, Data_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    text_array = np.append(text_array, spdcand.sweep_duration)
    text_array = np.append(text_array, data.starttime)
    text_array = np.append(text_array, spdcand.bary_start_time)
    text_array = np.append(text_array, man_params)
    # Array to Construct the sweep
    if spdcand.sweep_dm is not None:
        ddm = spdcand.sweep_dm - data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        delays_nozerodm = delays
        freqs_nozerodm = data.freqs
    # Sweeped with zerodm-on
    zerodm = True
    #downsamp_temp = 1
    data, Data_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                 spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                 spdcand.subdm, zerodm, spdcand.downsamp, \
                                 spdcand.scaleindep, spdcand.width_bins, \
                                 spdcand.mask, maskfile, spdcand.bandpass_corr)
    with open(temp_filename + ".spd", 'wb') as f:
        np.savez_compressed(f, \
                            Data_dedisp_nozerodm = Data_dedisp_nozerodm.astype(np.float16),\
                            Data_dedisp_zerodm = Data_dedisp_zerodm.astype(np.float16),\
                            Data_nozerodm = Data_nozerodm.astype(np.float16),\
                            delays_nozerodm = delays_nozerodm, \
                            freqs_nozerodm = freqs_nozerodm,\
                            Data_zerodm = Data_zerodm.astype(np.float16), \
                            text_array = text_array)
    #### Arrays for Plotting DM vs Time is in plot_spd.plot(...)
    if plot:
        print_debug("Now plotting...")
        plot_spd.plot(temp_filename+".spd", args[1:], \
                      spec_width=spec_width, loc_pulse=loc_pulse, xwin=False, \
                      outfile = basename, just_waterfall=just_waterfall, \
                      integrate_spec=integrate_spec, integrate_ts=integrate_ts, \
                      disp_pulse=disp_pulse, tar = None)
示例#49
0
def plot_waterfall(data,data_noscale,start,duration,centre_MJD,dm,sigma,width,tsamp,\
                   fres,tavg=1,favg=1,cmap_str="gist_yarg",integrate_spec=False,sweep_dms=[], sweep_posns=[],save_plot=False,interactive=True):

    # Set up axes
    if interactive:
        fig = plt.figure(figsize=[12, 7])

    fig.text(0.1, 0.9, "DM = %s pc/cc" % dm)
    fig.text(0.1, 0.85, "Centre MJD = %s" % centre_MJD)
    fig.text(0.1, 0.8, "Significance = %s sigma" % sigma)
    fig.text(0.1, 0.75, "Boxcar width = %s seconds" % (tsamp * width))
    fig.text(0.1, 0.25, "Plotting resolution")
    fig.text(0.1, 0.2,
             "Time resolution %s milliseconds" % (tsamp * tavg * 1e3))
    fig.text(0.1, 0.15, "Frequency resolution %s MHz" % (fres * favg))

    im_width = 0.3 if integrate_spec else 0.4
    im_height = 0.8
    ax_im = plt.axes((0.45, 0.15, im_width, im_height - 0.2))
    ax_ts = plt.axes((0.45, 0.75, im_width, 0.2), sharex=ax_im)
    if integrate_spec:
        ax_spec = plt.axes((0.75, 0.15, 0.2, im_height), sharey=ax_im)

    # Ploting it up
    nbinlim = np.int(duration / data.dt)

    #downsample in frequency
    if favg != None and favg > 1:
        nchan_tot = data.data.shape[0]
        favg = float(favg)
        if (nchan_tot / favg) - int(nchan_tot / favg) != 0:
            print(
                "The total number of channels is %s, please choose an fscrunch val\
ue that divides the total number of channels." % nchan_tot)
            sys.exit()
        else:
            newnchan = nchan_tot / favg
            data.data = np.array(
                np.row_stack([
                    np.mean(subint, axis=0)
                    for subint in np.vsplit(data.data, newnchan)
                ]))

    img = ax_im.imshow(data.data[..., :nbinlim], aspect='auto', \
                cmap=matplotlib.cm.cmap_d[cmap_str], \
                interpolation='nearest', origin='upper', \
                extent=(data.starttime, data.starttime+ nbinlim*data.dt, \
                        data.freqs.min(), data.freqs.max()))

    # Sweeping it up
    for ii, sweep_dm in enumerate(sweep_dms):
        ddm = sweep_dm - data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()

        if sweep_posns is None:
            sweep_posn = 0.0
        elif len(sweep_posns) == 1:
            sweep_posn = sweep_posns[0]
        else:
            sweep_posn = sweep_posns[ii]
        sweepstart = data.dt * data.numspectra * sweep_posn + data.starttime
        #sty = SWEEP_STYLES[ii%len(SWEEP_STYLES)]
        ax_im.plot(delays + sweepstart, data.freqs, lw=4, alpha=0.5)

    # Dressing it up
    ax_im.xaxis.get_major_formatter().set_useOffset(False)
    ax_im.set_xlabel("Time")
    ax_im.set_ylabel("Frequency (MHz)")
    ax_im.set_ylim((data.freqs.min(), data.freqs.max()))

    axsec = ax_im.twinx()

    axsec.set_yticks(
        np.round(np.linspace(0, data.data[..., :nbinlim].shape[0], 10)) * favg)
    axsec.set_ylabel('Bins')

    # Plot Time series

    Data = np.array(data_noscale.data[..., :nbinlim])
    Dedisp_ts = Data.sum(axis=0)
    times = (np.arange(data_noscale.numspectra) * data_noscale.dt +
             start)[..., :nbinlim]

    ax_ts.plot(times, Dedisp_ts, "k")
    ax_ts.set_xlim([times.min(), times.max()])
    plt.setp(ax_ts.get_xticklabels(), visible=False)
    plt.setp(ax_ts.get_yticklabels(), visible=False)

    # Plot Spectrum
    if integrate_spec:
        spectrum_window = 0.05 * duration
        window_width = int(spectrum_window / data.dt)  # bins
        burst_bin = nbinlim / 2
        on_spec = np.array(data.data[..., burst_bin - window_width:burst_bin +
                                     window_width])
        Dedisp_spec = on_spec.sum(axis=1)[::-1]

        freqs = np.linspace(data.freqs.min(), data.freqs.max(),
                            len(Dedisp_spec))
        ax_spec.plot(Dedisp_spec, freqs, "k")
        plt.setp(ax_spec.get_xticklabels(), visible=False)
        plt.setp(ax_spec.get_yticklabels(), visible=False)
        ax_spec.set_ylim([data.freqs.min(), data.freqs.max()])

        ax_ts.axvline(times[burst_bin] - spectrum_window, ls="--", c="grey")
        ax_ts.axvline(times[burst_bin] + spectrum_window, ls="--", c="grey")

    if interactive:
        fig.canvas.mpl_connect('key_press_event', \
                lambda ev: (ev.key in ('q','Q') and plt.close(fig)))

    if save_plot == True:
        fig.savefig('waterfall_%sMJD_DM%s_sigma%s.png' %
                    (centre_MJD, dm, sigma),
                    dpi=300,
                    facecolor='w',
                    edgecolor='w',
                    format='png')
        plt.close()
    else:
        plt.show()
示例#50
0
def make_spd_from_file(spdcand, rawdatafile, \
                       txtfile, maskfile, \
                       min_rank, group_rank, \
                       plot, just_waterfall, \
                       integrate_ts, integrate_spec, disp_pulse, \
                       loc_pulse, nsub, \
                       maxnumcands, \
                       basename, \
                       mask=False, bandpass_corr=True, barytime=True, \
                       man_params=None):
    
    """
    Makes spd files from output files of rratrap. 
    Inputs:
        spdcand: spcand parameters instance (read in spcand.params)
        rawdatafile: psrfits file instance
        txtfile: rratrap output file (groups.txt file)
        maskfile: rfifind mask file. need this file if you want to remove the bandpass 
                  or use rfifind mask information.
        min_rank: plot all groups with rank more than this. min 1, max 6
        group_rank: plot groups ranked whatever you specify
        plot: do you want to produce the plots as well? 
        just_waterfall: Do you just want to make the waterfall plots.
        integrate_ts: Do you want to display the dedispersed time series in the plot?
        integrate_spec: Do you want to display the pulse spectrum in the plot?
        disp_pulse: Do you want to see the inset dispersed pulse in the plot?
        loc_pulse: Fraction of the window length where the pulse is located.(eg. 0.25 = 1/4th of the way in.
                                                                             0.5 = middle of the plot)
        maxnumcands: What is the maximum number of candidates you would like to generate?
        basename: output basename of the file. Appended with _DM_TIME(s)_RANK.spd 
    Optional arguments:
        mask: Do you want to mask out rfi contaminated channels?
        bandpass_corr: Do you want to remove the bandpass?
        barytime: Is the given time(s) barycentric?
        man_params: Do you want to specify the parameters for waterfalling 
                    manually? If yes, I suggest using the function make_spd_from_man_params().
                    (I suggest giving it the rratrap output file)    
    Outputs:
       Binary npz file containing the necessary arrays and header information to generate the spd plots.
    """
    numcands=0 # counter for max number of candidates
    loop_must_break = False # dont break the loop unless num of cands >100.
    files = spio.get_textfile(options.txtfile)
    if group_rank:
        groups=[group_rank-1]
    else:
        groups = [i for i in range(6) if(i>=min_rank)][::-1]
     
    for group in groups:
        rank = group+1
        if files[group] != "Number of rank %i groups: 0 "%rank:
            values = spio.split_parameters(rank, txtfile)
            lis = np.where(files == '\tRank:             %i.000000'%rank)[0]
            for ii in range(len(values)):
                #### Arrays for Plotting DM vs SNR
                dm_list, time_list, dm_arr, sigma_arr, width_arr = spio.read_RRATrap_info(txtfile, lis[ii], rank)


                # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
                spdcand.read_from_file(values[ii], rawdatafile.tsamp, rawdatafile.specinfo.N, \
                                       rawdatafile.frequencies[0], rawdatafile.frequencies[-1], \
                                       rawdatafile, loc_pulse=loc_pulse, dedisp = True, \
                                       scaleindep = None, zerodm = None, mask = mask, \
                                       barytime=barytime, \
                                       nsub = nsub, bandpass_corr = bandpass_corr)

                #make an array to store header information for the spd files
                temp_filename = basename+"_DM%.1f_%.1fs_rank_%i"%(spdcand.subdm, \
                                                   spdcand.topo_start_time, rank)
                
                print_debug("Running waterfaller with Zero-DM OFF...")
                
                # Add additional information to the header information array
                data, Data_dedisp_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                             spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                             spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                             spdcand.scaleindep, spdcand.width_bins, \
                                             spdcand.mask, maskfile, spdcand.bandpass_corr)

                text_array = np.array([args[0], rawdatafile.specinfo.telescope, \
                                       rawdatafile.specinfo.ra_str, rawdatafile.specinfo.dec_str, \
                                       rawdatafile.specinfo.start_MJD[0], \
                                       rank, spdcand.nsub, spdcand.nbins, spdcand.subdm, \
                                       spdcand.sigma, spdcand.sample_number, spdcand.duration, \
                                       spdcand.width_bins, spdcand.pulse_width, rawdatafile.tsamp,\
                                       rawdatafile.specinfo.T, spdcand.topo_start_time, data.starttime, \
                                       data.dt,data.numspectra, data.freqs.min(), data.freqs.max()])

                #### Array for plotting Dedispersed waterfall plot zerodm - ON
                print_debug("Running Waterfaller with Zero-DM ON...")
                zerodm=True
                data, Data_dedisp_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                           spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                           spdcand.subdm, zerodm, spdcand.downsamp, \
                                           spdcand.scaleindep, spdcand.width_bins, \
                                           spdcand.mask, maskfile, spdcand.bandpass_corr)
                ####Sweeped without zerodm
                spdcand.read_from_file(values[ii], rawdatafile.tsamp, rawdatafile.specinfo.N, \
                                      rawdatafile.frequencies[0], rawdatafile.frequencies[-1], \
                                      rawdatafile, loc_pulse=loc_pulse, dedisp = None, \
                                      scaleindep = None, zerodm = None, mask = mask, \
                                      barytime=barytime, \
                                      nsub = nsub, bandpass_corr = bandpass_corr)
                data, Data_nozerodm = waterfall_array(rawdatafile, spdcand.start, \
                                           spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                           spdcand.subdm, spdcand.zerodm, spdcand.downsamp, \
                                           spdcand.scaleindep, spdcand.width_bins, \
                                           spdcand.mask, maskfile, spdcand.bandpass_corr)
                text_array = np.append(text_array, spdcand.sweep_duration)
                text_array = np.append(text_array, data.starttime)
                text_array = np.append(text_array, spdcand.bary_start_time)
                text_array = np.append(text_array, man_params)
                # Array to Construct the sweep
                if spdcand.sweep_dm is not None:
                    ddm = spdcand.sweep_dm-data.dm
                    delays = psr_utils.delay_from_DM(ddm, data.freqs)
                    delays -= delays.min()
                    delays_nozerodm = delays
                    freqs_nozerodm = data.freqs
                # Sweeped with zerodm-on 
                zerodm = True
                #downsamp_temp = 1
                data, Data_zerodm = waterfall_array(rawdatafile, spdcand.start, \
                                           spdcand.duration, spdcand.dm, spdcand.nbins, spdcand.nsub, \
                                           spdcand.subdm, zerodm, spdcand.downsamp, \
                                           spdcand.scaleindep, spdcand.width_bins, \
                                           spdcand.mask, maskfile, spdcand.bandpass_corr)
                # Saving the arrays into the .spd file.
                with open(temp_filename+".spd", 'wb') as f:
                    np.savez_compressed(f, \
                                        Data_dedisp_nozerodm = Data_dedisp_nozerodm.astype(np.float16),\
                                        Data_dedisp_zerodm = Data_dedisp_zerodm.astype(np.float16),\
                                        Data_nozerodm = Data_nozerodm.astype(np.float16),\
                                        delays_nozerodm = delays_nozerodm, \
                                        freqs_nozerodm = freqs_nozerodm,\
                                        Data_zerodm = Data_zerodm.astype(np.float16), \
                                        dm_arr= list(map(np.float16, dm_arr)),\
                                        sigma_arr = list(map(np.float16, sigma_arr)), \
                                        width_arr =list(map(np.uint8, width_arr)),\
                                        dm_list= list(map(np.float16, dm_list)), \
                                        time_list = list(map(np.float16, time_list)), \
                                        text_array = text_array)
                #### Arrays for Plotting DM vs Time is in plot_spd.plot(...)
                if plot:
                    print_debug("Now plotting...")
                    plot_spd.plot(temp_filename+".spd", args[1:], \
                                  spec_width=1.5, loc_pulse=loc_pulse, \
                                  xwin=False, outfile=basename, \
                                  just_waterfall=just_waterfall, \
                                  integrate_spec=integrate_spec, \
                                  integrate_ts=integrate_ts, \
                                  disp_pulse=disp_pulse, tar = None)
                    print_debug("Finished plot %i " %ii+strftime("%Y-%m-%d %H:%M:%S"))
                numcands+= 1
                print_debug('Finished sp_candidate : %i'%numcands)
                if numcands >= maxnumcands:    # Max number of candidates to plot 100.
                    loop_must_break = True
                    break
            if loop_must_break:
                break

        print_debug("Finished group %i... "%rank+strftime("%Y-%m-%d %H:%M:%S"))
    print_debug("Finished running waterfaller... "+strftime("%Y-%m-%d %H:%M:%S"))
示例#51
0
def inject(infile, outfn, prof, period, dm, nbitsout=None, 
           block_size=BLOCKSIZE, pulsar_only=False, inplace=False):
    if isinstance(infile, filterbank.FilterbankFile):
        fil = infile
    elif inplace:
        fil = filterbank.FilterbankFile(infile, 'readwrite')
    else:
        fil = filterbank.FilterbankFile(infile, 'read')
    print("Injecting pulsar signal into: %s" % fil.filename)
    if False:
        delays = psr_utils.delay_from_DM(dm, fil.frequencies)
        delays -= delays[np.argmax(fil.frequencies)]
        get_phases = lambda times: (times-delays)/period % 1
    else:
        get_phases = lambda times: times/period % 1

    # Create the output filterbank file
    if nbitsout is None:
        nbitsout = fil.nbits
    if inplace:
        warnings.warn("Injecting pulsar signal *in-place*")
        outfil = fil
    else:
        # Start an output file
        print("Creating out file: %s" % outfn)
        outfil = filterbank.create_filterbank_file(outfn, fil.header, \
                                            nbits=nbitsout, mode='append')

    if outfil.nbits == 8:
        raise NotImplementedError("This code is out of date. 'delays' is not " \
                                    "done in this way anymore..")
        # Read the first second of data to get the global scaling to use
        onesec = fil.get_timeslice(0, 1).copy()
        onesec_nspec = onesec.shape[0]
        times = np.atleast_2d(np.arange(onesec_nspec)*fil.tsamp).T+delays
        phases = times/period % 1
        onesec += prof(phases)
        minimum = np.min(onesec)
        median = np.median(onesec)
        # Set median to 1/3 of dynamic range
        global_scale = (256.0/3.0) / median
        del onesec
    else:
        # No scaling to be performed
        # These values will cause scaling to keep data unchanged
        minimum = 0
        global_scale = 1

    sys.stdout.write(" %3.0f %%\r" % 0)
    sys.stdout.flush()
    oldprogress = -1
    
    # Loop over data
    lobin = 0
    spectra = fil.get_spectra(0, block_size)
    numread = spectra.shape[0]
    while numread:
        if pulsar_only:
            # Do not write out data from input file
            # zero it out
            spectra *= 0
        hibin = lobin+numread
        # Sample at middle of time bin
        times = (np.arange(lobin, hibin, 1.0/NINTEG_PER_BIN)+0.5/NINTEG_PER_BIN)*fil.dt
        #times = (np.arange(lobin, hibin)+0.5)*fil.dt
        phases = get_phases(times)
        profvals = prof(phases)
        shape = list(profvals.shape)
        shape[1:1] = [NINTEG_PER_BIN]
        shape[0] /= NINTEG_PER_BIN
        profvals.shape = shape
        toinject = profvals.mean(axis=1)
        #toinject = profvals
        if np.ndim(toinject) > 1:
            injected = spectra+toinject
        else:
            injected = spectra+toinject[:,np.newaxis]
        scaled = (injected-minimum)*global_scale
        if inplace:
            outfil.write_spectra(scaled, lobin)
        else:
            outfil.append_spectra(scaled)
        
        # Print progress to screen
        progress = int(100.0*hibin/fil.nspec)
        if progress > oldprogress: 
            sys.stdout.write(" %3.0f %%\r" % progress)
            sys.stdout.flush()
            oldprogress = progress
        
        # Prepare for next iteration
        lobin = hibin 
        spectra = fil.get_spectra(lobin, lobin+block_size)
        numread = spectra.shape[0]

    sys.stdout.write("Done   \n")
    sys.stdout.flush()
示例#52
0
def main():
    parser = optparse.OptionParser(prog="sp_pipeline..py", \
                        version=" Chitrang Patel (May. 12, 2015)", \
                        usage="%prog INFILE(PsrFits FILE, SINGLEPULSE FILES)", \
                        description="Create single pulse plots to show the " \
                                    "frequency sweeps of a single pulse,  " \
                    "DM vs time, and SNR vs DM,"\
                                    "in psrFits data.")
    parser.add_option('--infile', dest='infile', type='string', \
                        help="Give a .inf file to read the appropriate header information.")
    parser.add_option('--groupsfile', dest='txtfile', type='string', \
                        help="Give the groups.txt file to read in the groups information.") 
    parser.add_option('--mask', dest='maskfile', type='string', \
                        help="Mask file produced by rfifind. (Default: No Mask).", \
                        default=None)
    parser.add_option('-n', dest='maxnumcands', type='int', \
                        help="Maximum number of candidates to plot. (Default: 100).", \
                        default=100)
    options, args = parser.parse_args()
    if not hasattr(options, 'infile'):
        raise ValueError("A .inf file must be given on the command line! ") 
    if not hasattr(options, 'txtfile'):
        raise ValueError("The groups.txt file must be given on the command line! ") 
    
    files = get_textfile(options.txtfile)
    print_debug("Begining waterfaller... "+strftime("%Y-%m-%d %H:%M:%S"))
    if not args[0].endswith("fits"):
        raise ValueError("The first file must be a psrFits file! ") 
    print_debug('Maximum number of candidates to plot: %i'%options.maxnumcands)
    basename = args[0][:-5]
    filetype = "psrfits"
    inffile = options.infile
    topo, bary = bary_and_topo.bary_to_topo(inffile)
    time_shift = bary-topo
    inf = infodata.infodata(inffile)
    RA = inf.RA
    dec = inf.DEC
    MJD = inf.epoch
    mjd = Popen(["mjd2cal", "%f"%MJD], stdout=PIPE, stderr=PIPE)
    date, err = mjd.communicate()
    date = date.split()[2:5]
    telescope = inf.telescope
    N = inf.N
    numcands=0
    Total_observed_time = inf.dt *N
    print_debug('getting file..')
    values = split_parameters(options.txtfile)
    if len(values)> options.maxnumcands:
	values=sorted(values, key=itemgetter(5,1)) #sorting candidates based on ranks and snr
	values=values[-options.maxnumcands:] 
	print "More than", options.maxnumcands, "candidates, making plots for", options.maxnumcands, "candidates" 
    values = sorted(values, key=itemgetter(0))
    for ii in range(len(values)):
        #### Array for Plotting DM vs SNR
        print_debug("Making arrays for DM vs Signal to Noise...")
        temp_list = files[values[ii][6]-6].split()
        npulses = int(temp_list[2])
        temp_lines = files[(values[ii][6]+3):(values[ii][6]+npulses+1)]
        arr = np.split(temp_lines, len(temp_lines))
        dm_list = []
        time_list = []
        for i in range(len(arr)):
            dm_val= float(arr[i][0].split()[0])
            time_val = float(arr[i][0].split()[2])
            dm_list.append(dm_val)
            time_list.append(time_val)
        arr_2 = np.array([arr[i][0].split() for i in range(len(arr))], dtype = np.float32)
        dm_arr = np.array([arr_2[i][0] for i in range(len(arr))], dtype = np.float32)
        sigma_arr = np.array([arr_2[i][1] for i in range(len(arr))], dtype = np.float32)
	#### Array for Plotting DM vs Time is in show_spplots.plot(...)

                
        #### Setting variables up for the waterfall arrays.
        j = ii+1
        subdm = dm = sweep_dm= values[ii][0]
	sample_number = values[ii][3]
	rank=values[ii][5]
	width_bins = values[ii][4]
	#print "dm", dm 	
        #print "width_bins", width_bins 
	downsamp = np.round((values[ii][2]/sample_number/inf.dt)).astype('int')
	#print "downsamp", downsamp 
	pulse_width = width_bins*downsamp*inf.dt
	#print "pulse_width", pulse_width 
	if ii == 0:
	    mask_subband=rfifind.rfifind("%s_rfifind.mask"%(basename))
	    mask_subband.set_zap_chans(power=1000,plot=False)
	    mask_subband.set_weights_and_offsets()
	    mask_subband.write_weights(filename="%s_weights.txt"%(basename))
	    cmd="psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s"%(dm,basename,dm,basename,args[0])
	    call(cmd, shell=True)
	    #subband args[0] at dm and then generate a file that will be set equal to rawdatafile
	    subband_file="%s_subband_%.2f_0001.fits" %(basename,dm)
	    dm_prev=dm
	    subband_prev= subband_file
	else:	
	    dm_diff=dm-dm_prev
	    t_smear=8.3e3*dm_diff*(350**-3)*(np.abs(rawdatafile.frequencies[0]-rawdatafile.frequencies[-1])/128.)
	    if (5*t_smear) > pulse_width:
		cmd="psrfits_subband -dm %.2f -nsub 128 -o %s_subband_%.2f -weights %s_weights.txt %s"%(dm,basename,dm,basename,args[0])
		call(cmd, shell=True)
		#subband args[0] at dm and then generate a file that will be set equal to rawdatafile
		subband_file="%s_subband_%.2f_0001.fits" %(basename,dm)
		dm_prev=dm
		subband_prev=subband_file

	rawdatafile = psrfits.PsrfitsFile(subband_file)
	bin_shift = np.round(time_shift/rawdatafile.tsamp).astype('int')
	integrate_dm = None
	sigma = values[ii][1]
        sweep_posn = 0.0
        bary_start_time = values[ii][2]
        topo_start_time = bary_start_time - topo_timeshift(bary_start_time, time_shift, topo)[0]
	binratio = 50
	scaleindep = False
        zerodm = None
        duration = binratio * width_bins * rawdatafile.tsamp * downsamp
	start = topo_start_time - (0.25 * duration)
	if (start<0.0):
            start = 0.0        
	if sigma <= 7:
	    nsub = 32
	elif sigma >= 7 and sigma < 10:
            nsub = 64
        else:
            nsub = 128
        nbins = np.round(duration/rawdatafile.tsamp).astype('int')
	start_bin = np.round(start/rawdatafile.tsamp).astype('int')
        dmfac = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2 - 1./rawdatafile.frequencies[-1]**2)
	nbinsextra = np.round((duration + dmfac * dm)/rawdatafile.tsamp).astype('int')
	if (start_bin+nbinsextra) > N-1:
            nbinsextra = N-1-start_bin
	data = rawdatafile.get_spectra(start_bin, nbinsextra)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
	#make an array to store header information for the .npz files
        temp_filename = basename+"_DM%.1f_%.1fs_rank_%i"%(subdm, topo_start_time, rank)
	# Array for Plotting Dedispersed waterfall plot - zerodm - OFF
        print_debug("Running waterfaller with Zero-DM OFF...")
        data, Data_dedisp_nozerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
        #Add additional information to the header information array
        text_array = np.array([subband_file, 'GBT', RA, dec, MJD, rank, nsub, nbins, subdm, sigma, sample_number, duration, width_bins, pulse_width, rawdatafile.tsamp, Total_observed_time, topo_start_time, data.starttime, data.dt, data.numspectra, data.freqs.min(), data.freqs.max()])
        #### Array for plotting Dedispersed waterfall plot zerodm - ON
        print_debug("Running Waterfaller with Zero-DM ON...")
	#print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbinsextra)
	#print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbinsextra, options.maskfile)
        zerodm = True
        data, Data_dedisp_zerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
	####Sweeped without zerodm
        start = start + (0.25*duration)
        start_bin = np.round(start/rawdatafile.tsamp).astype('int')
        sweep_duration = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2-1./rawdatafile.frequencies[-1]**2)*sweep_dm
        nbins = np.round(sweep_duration/(rawdatafile.tsamp)).astype('int')
        if ((nbins+start_bin)> (N-1)):
            nbins = N-1-start_bin
	#print "before get_spectra",memory.resident()/(1024.0**3)
        data = rawdatafile.get_spectra(start_bin, nbins)
	#print "after get_spectra",memory.resident()/(1024.0**3)
        data = maskdata(data, start_bin, nbins, options.maskfile)
        zerodm = None
        dm = None
        data, Data_nozerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
	text_array = np.append(text_array, sweep_duration)
        text_array = np.append(text_array, data.starttime)
        text_array = np.append(text_array, bary_start_time)
        # Array to Construct the sweep
        if sweep_dm is not None:
            ddm = sweep_dm-data.dm
            delays = psr_utils.delay_from_DM(ddm, data.freqs)
            delays -= delays.min()
            delays_nozerodm = delays
            freqs_nozerodm = data.freqs
        # Sweeped with zerodm-on 
        zerodm = True
        downsamp_temp = 1
        data, Data_zerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp_temp, scaleindep, width_bins, rawdatafile, binratio, data)
        #print "waterfall",memory.resident()/(1024.0**3)
	# Saving the arrays into the .spd file.
        with open(temp_filename+".spd", 'wb') as f:
            np.savez_compressed(f, Data_dedisp_nozerodm = Data_dedisp_nozerodm.astype(np.float16), Data_dedisp_zerodm = Data_dedisp_zerodm.astype(np.float16), Data_nozerodm = Data_nozerodm.astype(np.float16), delays_nozerodm = delays_nozerodm, freqs_nozerodm = freqs_nozerodm, Data_zerodm = Data_zerodm.astype(np.float16), dm_arr= map(np.float16, dm_arr), sigma_arr = map(np.float16, sigma_arr), dm_list= map(np.float16, dm_list), time_list = map(np.float16, time_list), text_array = text_array)
        print_debug("Now plotting...")
        #print "Before plot..",memory.resident()/(1024.0**3)
	show_spplots.plot(temp_filename+".spd", args[1:], xwin=False, outfile = basename, tar = None)
        print_debug("Finished plot %i " %j+strftime("%Y-%m-%d %H:%M:%S"))
	#print "After plot..",memory.resident()/(1024.0**3)
	numcands+=1
        print_debug('Finished sp_candidate : %i'%numcands)
    print_debug("Finished running waterfaller... "+strftime("%Y-%m-%d %H:%M:%S"))
示例#53
0
def main():
    parser = optparse.OptionParser(prog="sp_pipeline..py", \
                        version=" Chitrang Patel (May. 12, 2015)", \
                        usage="%prog INFILE(PsrFits FILE, SINGLEPULSE FILES)", \
                        description="Create single pulse plots to show the " \
                                    "frequency sweeps of a single pulse,  " \
                                    "DM vs time, and SNR vs DM,"\
                                    "in psrFits data.")
    parser.add_option('--infile', dest='infile', type='string', \
                        help="Give a .inf file to read the appropriate header information.")
    parser.add_option('--groupsfile', dest='txtfile', type='string', \
                        help="Give the groups.txt file to read in the groups information.") 
    parser.add_option('--mask', dest='maskfile', type='string', \
                        help="Mask file produced by rfifind. (Default: No Mask).", \
                        default=None)
    parser.add_option('-n', dest='maxnumcands', type='int', \
                        help="Maximum number of candidates to plot. (Default: 100).", \
                        default=100)
    options, args = parser.parse_args()
    if not hasattr(options, 'infile'):
        raise ValueError("A .inf file must be given on the command line! ") 
    if not hasattr(options, 'txtfile'):
        raise ValueError("The groups.txt file must be given on the command line! ") 
    
    files = sp_utils.spio.get_textfile(options.txtfile)
    print_debug("Begining waterfaller... "+strftime("%Y-%m-%d %H:%M:%S"))
    if not args[0].endswith("fits"):
        raise ValueError("The first file must be a psrFits file! ") 
    print_debug('Maximum number of candidates to plot: %i'%options.maxnumcands)
    basename = args[0][:-5]
    filetype = "psrfits"
    inffile = options.infile
    topo, bary = bary_and_topo.bary_to_topo(inffile)
    time_shift = bary-topo
    inf = infodata.infodata(inffile)
    RA = inf.RA
    dec = inf.DEC
    MJD = inf.epoch
    mjd = Popen(["mjd2cal", "%f"%MJD], stdout=PIPE, stderr=PIPE)
    date, err = mjd.communicate()
    date = date.split()[2:5]
    telescope = inf.telescope
    N = inf.N
    Total_observed_time = inf.dt *N
    print_debug('getting file..')
    rawdatafile = psrfits.PsrfitsFile(args[0])
    bin_shift = np.round(time_shift/rawdatafile.tsamp).astype('int')
    numcands = 0 # candidate counter. Use this to decide the maximum bumber of candidates to plot.
    loop_must_break = False # dont break the loop unless num of cands >100.
    for group in [6, 5, 4, 3, 2]:
        rank = group+1
        if files[group] != "Number of rank %i groups: 0 "%rank:
            print_debug(files[group])
            values = sp_utils.spio.split_parameters(rank, options.txtfile)
            lis = np.where(files == '\tRank:             %i.000000'%rank)[0]
            for ii in range(len(values)):
                #### Array for Plotting DM vs SNR
                print_debug("Making arrays for DM vs Signal to Noise...")
                temp_list = files[lis[ii]-6].split()
                npulses = int(temp_list[2])
                temp_lines = files[(lis[ii]+3):(lis[ii]+npulses+1)]
                arr = np.split(temp_lines, len(temp_lines))
                dm_list = []
                time_list = []
                for i in range(len(arr)):
                    dm_val= float(arr[i][0].split()[0])
                    time_val = float(arr[i][0].split()[2])
                    dm_list.append(dm_val)
                    time_list.append(time_val)
                arr_2 = np.array([arr[i][0].split() for i in range(len(arr))], dtype = np.float32)
                dm_arr = np.array([arr_2[i][0] for i in range(len(arr))], dtype = np.float32)
                sigma_arr = np.array([arr_2[i][1] for i in range(len(arr))], dtype = np.float32)

                #### Array for Plotting DM vs Time is in show_spplots.plot(...)

                
                #### Setting variables up for the waterfall arrays.
                j = ii+1
                subdm = dm = sweep_dm= values[ii][0]
                integrate_dm = None
                sigma = values[ii][1]
                sweep_posn = 0.0
                bary_start_time = values[ii][2]
                topo_start_time = bary_start_time - topo_timeshift(bary_start_time, time_shift, topo)[0]
                sample_number = values[ii][3]
                width_bins = values[ii][4]
                binratio = 50
                scaleindep = False
                zerodm = None
                downsamp = np.round((values[ii][2]/sample_number/rawdatafile.tsamp)).astype('int')
                duration = binratio * width_bins * rawdatafile.tsamp * downsamp
                start = topo_start_time - (0.25 * duration)
                if (start<0.0):
                    start = 0.0
                pulse_width = width_bins*downsamp*rawdatafile.tsamp
                if sigma < 10:
                    nsub = 32
                elif sigma >= 10 and sigma < 15:
                    nsub = 64
                else:
                    nsub = 96
                
                if nsub > inf.numchan:
                    nsub = inf.numchan

                nbins = np.round(duration/rawdatafile.tsamp).astype('int')
                start_bin = np.round(start/rawdatafile.tsamp).astype('int')
                dmfac = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2 - 1./rawdatafile.frequencies[-1]**2)
                nbinsextra = np.round((duration + dmfac * dm)/rawdatafile.tsamp).astype('int')
                if (start_bin+nbinsextra) > N-1:
                    nbinsextra = N-1-start_bin
                data = rawdatafile.get_spectra(start_bin, nbinsextra)
                data = maskdata(data, start_bin, nbinsextra, options.maskfile)

                #make an array to store header information for the .npz files
                temp_filename = basename+"_DM%.1f_%.1fs_rank_%i"%(subdm, topo_start_time, rank)
                # Array for Plotting Dedispersed waterfall plot - zerodm - OFF
                print_debug("Running waterfaller with Zero-DM OFF...")
                data, Data_dedisp_nozerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
                # Add additional information to the header information array
                text_array = np.array([args[0], 'Arecibo', RA, dec, MJD, rank, nsub, nbins, subdm, sigma, sample_number, duration, width_bins, pulse_width, rawdatafile.tsamp, Total_observed_time, topo_start_time, data.starttime, data.dt, data.numspectra, data.freqs.min(), data.freqs.max()])

                #### Array for plotting Dedispersed waterfall plot zerodm - ON
                print_debug("Running Waterfaller with Zero-DM ON...")
                data = rawdatafile.get_spectra(start_bin, nbinsextra)
                data = maskdata(data, start_bin, nbinsextra, options.maskfile)
                zerodm = True
                data, Data_dedisp_zerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
                ####Sweeped without zerodm
                start = start + (0.25*duration)
                start_bin = np.round(start/rawdatafile.tsamp).astype('int')
                sweep_duration = 4.15e3 * np.abs(1./rawdatafile.frequencies[0]**2-1./rawdatafile.frequencies[-1]**2)*sweep_dm
                nbins = np.round(sweep_duration/(rawdatafile.tsamp)).astype('int')
                if ((nbins+start_bin)> (N-1)):
                    nbins = N-1-start_bin
                data = rawdatafile.get_spectra(start_bin, nbins)
                data = maskdata(data, start_bin, nbins, options.maskfile)
                zerodm = None
                dm = None
                data, Data_nozerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp, scaleindep, width_bins, rawdatafile, binratio, data)
                text_array = np.append(text_array, sweep_duration)
                text_array = np.append(text_array, data.starttime)
                text_array = np.append(text_array, bary_start_time)
                # Array to Construct the sweep
                if sweep_dm is not None:
                    ddm = sweep_dm-data.dm
                    delays = psr_utils.delay_from_DM(ddm, data.freqs)
                    delays -= delays.min()
                    delays_nozerodm = delays
                    freqs_nozerodm = data.freqs
                # Sweeped with zerodm-on 
                zerodm = True
                downsamp_temp = 1
                data, Data_zerodm = waterfall_array(start_bin, dmfac, duration, nbins, zerodm, nsub, subdm, dm, integrate_dm, downsamp_temp, scaleindep, width_bins, rawdatafile, binratio, data)
                # Saving the arrays into the .spd file.
                with open(temp_filename+".spd", 'wb') as f:
                    np.savez_compressed(f, Data_dedisp_nozerodm = Data_dedisp_nozerodm.astype(np.float16), Data_dedisp_zerodm = Data_dedisp_zerodm.astype(np.float16), Data_nozerodm = Data_nozerodm.astype(np.float16), delays_nozerodm = delays_nozerodm, freqs_nozerodm = freqs_nozerodm, Data_zerodm = Data_zerodm.astype(np.float16), dm_arr= map(np.float16, dm_arr), sigma_arr = map(np.float16, sigma_arr), dm_list= map(np.float16, dm_list), time_list = map(np.float16, time_list), text_array = text_array)
                print_debug("Now plotting...")
                show_spplots.plot(temp_filename+".spd", args[1:], xwin=False, outfile = basename, tar = None)
                print_debug("Finished plot %i " %j+strftime("%Y-%m-%d %H:%M:%S"))
                numcands+= 1
                print_debug('Finished sp_candidate : %i'%numcands)
                if numcands >= options.maxnumcands:    # Max number of candidates to plot 100.
                    loop_must_break = True
                    break
            if loop_must_break:
                break
        print_debug("Finished group %i... "%rank+strftime("%Y-%m-%d %H:%M:%S"))
    print_debug("Finished running waterfaller... "+strftime("%Y-%m-%d %H:%M:%S"))
示例#54
0
def plot_waterfall(data, start, source_name, duration, dm,ofile,
                   integrate_ts=False, integrate_spec=False, show_cb=False, 
                   cmap_str="gist_yarg", sweep_dms=[], sweep_posns=[], 
                   ax_im=None, ax_ts=None, ax_spec=None, interactive=True, downsamp=1,nsub=None,subdm=None,width=None, snr=None):
    """ I want a docstring too!
    """
    
    if source_name is None:
	source_name="Unknown"

    #Output file 	
    if ofile is "unknown_cand":
    	title = "%s_" +  ofile + "_%.3f_%s" % (source_name,start,str(dm))
    else: title=source_name + "_" + ofile

    # Set up axes
    fig = plt.figure(figsize=(10,14))
    #fig.canvas.set_window_title("Frequency vs. Time")

    '''
    im_width = 0.6 if integrate_spec else 0.8
    im_height = 0.6 if integrate_ts else 0.8

    if not ax_im:
        ax_im = plt.axes((0.15, 0.15, im_width, im_height))
    if integrate_ts and not ax_ts:
        ax_ts = plt.axes((0.15, 0.75, im_width, 0.2),sharex=ax_im)

    if integrate_spec and not ax_spec:
        ax_spec = plt.axes((0.75, 0.15, 0.2, im_height),sharey=ax_im)
    '''
    
    ax_ts = plt.axes((0.15, 0.835, 0.8, 0.145))
    ax_im = plt.axes((0.15, 0.59, 0.8, 0.24),  sharex=ax_ts)
    ax_dmvstm = plt.axes((0.15, 0.345, 0.8, 0.24), sharex=ax_ts) 
    ax_orig = plt.axes((0.15, 0.1,0.8, 0.21))	

    data.downsample(downsamp)	
    nbinlim = np.int(duration/data.dt)

    # DM-vs-time plot	
    dmvstm_array = []
    lodm = int(dm-(dm*0.15))
    if lodm < 0: lodm = 0
    hidm = int(dm+(dm*0.15))
    dmstep = (hidm-lodm)/50.0
    datacopy = copy.deepcopy(data)
    #print lodm,hidm
    for ii in np.arange(lodm,hidm,dmstep):
    #for ii in range(400,600,10):
	#Without this, dispersion delay with smaller DM step does not produce delay close to bin width
	data.dedisperse(0,padval='rotate')
	data.dedisperse(ii,padval='rotate')		
	Data = np.array(data.data[..., :nbinlim])			
	Dedisp_ts = Data.sum(axis=0)
	dmvstm_array.append(Dedisp_ts)
    dmvstm_array=np.array(dmvstm_array)		
    #print np.shape(dmvstm_array)
    #np.save('dmvstm_1step.npz',dmvstm_array)
    ax_dmvstm.set_xlabel("Time (sec) ")
    ax_dmvstm.set_ylabel("DM")	
    #ax_dmvstm.imshow(dmvstm_array, aspect='auto', cmap=matplotlib.cm.cmap_d[cmap_str], origin='lower',extent=(data.starttime, data.starttime+ nbinlim*data.dt, lodm, hidm))	
    ax_dmvstm.imshow(dmvstm_array, aspect='auto', origin='lower',extent=(data.starttime, data.starttime+ nbinlim*data.dt, lodm, hidm))
    #cmap=matplotlib.cm.cmap_d[cmap_str])
    #interpolation='nearest', origin='upper')
    plt.setp(ax_im.get_xticklabels(), visible = False)
    plt.setp(ax_ts.get_xticklabels(), visible = False)
    #extent=(data.starttime, data.starttime+ nbinlim*data.dt, 500, 700)	
    #plt.show()
    #fig2 = plt.figure(2)	
    #plt.imshow(dmvstm_array,aspect='auto')

    #Plot Freq-vs-time 
    data = copy.deepcopy(datacopy)
    #data.downsample(downsamp)
    data.dedisperse(dm,padval='rotate')	
    nbinlim = np.int(duration/data.dt)
     	
    img = ax_im.imshow(data.data[..., :nbinlim], aspect='auto', \
                cmap=matplotlib.cm.cmap_d[cmap_str], \
                interpolation='nearest', origin='upper', \
                extent=(data.starttime, data.starttime+ nbinlim*data.dt, \
                        data.freqs.min(), data.freqs.max()))
    if show_cb:
        cb = ax_im.get_figure().colorbar(img)
        cb.set_label("Scaled signal intensity (arbitrary units)")
    # Dressing it up
    ax_im.xaxis.get_major_formatter().set_useOffset(False)
    #ax_im.set_xlabel("Time")
    ax_im.set_ylabel("Frequency (MHz)")


    # Plot Time series
    #Data = np.array(data.data[..., :nbinlim])
    Data = np.array(data.data[..., :nbinlim])
    Dedisp_ts = Data.sum(axis=0)
    times = (np.arange(data.numspectra)*data.dt + start)[..., :nbinlim]
    ax_ts.plot(times, Dedisp_ts,"k")
    ax_ts.set_xlim([times.min(),times.max()])
    text1 = "DM: " + "%.2f" % float(data.dm)
    plt.text(0.1,0.9,text1,fontsize=12,ha='center', va='center', transform=ax_ts.transAxes)	
    text2 = "Width: " + "%.2f" % float(width)	
    plt.text(0.1,0.8,text2,fontsize=12,ha='center', va='center', transform=ax_ts.transAxes)
    text3 = "SNR: " + "%.2f" % float(snr)
    plt.text(0.1,0.7,text3,fontsize=12,ha='center', va='center', transform=ax_ts.transAxes)	 
    ax_ts.set_title(title,fontsize=12)	
    plt.setp(ax_ts.get_xticklabels(), visible = False)
    plt.setp(ax_ts.get_yticklabels(), visible = False)


    #Plot original data
    data.dedisperse(0,padval='rotate')
    ax_im.set_ylabel("Frequency (MHz)")
    ax_orig.set_ylabel("Frequency (MHz)")
    ax_orig.set_xlabel("Time (sec)")
    # Sweeping it up
    for ii, sweep_dm in enumerate(sweep_dms):
        ddm = sweep_dm-data.dm
        delays = psr_utils.delay_from_DM(ddm, data.freqs)
        delays -= delays.min()
        
        if sweep_posns is None:
            sweep_posn = 0.0
        elif len(sweep_posns) == 1:
            sweep_posn = sweep_posns[0]
        else:
            sweep_posn = sweep_posns[ii]
        sweepstart = data.dt*data.numspectra*sweep_posn+data.starttime
        #sty = SWEEP_STYLES[ii%len(SWEEP_STYLES)]
	sty="b-"
	ax_orig.plot(delays+sweepstart,  data.freqs, "b-", lw=2, alpha=0.7)
	ax_orig.plot(delays+sweepstart+duration,  data.freqs, "b-", lw=2, alpha=0.7)	
    
    ax_orig.imshow(data.data, aspect='auto', \
	cmap=matplotlib.cm.cmap_d[cmap_str], \
        interpolation='nearest', origin='upper', \
        extent=(data.starttime, data.starttime + len(data.data[0])*data.dt, \
        data.freqs.min(), data.freqs.max()))

    #if interactive:
    #    fig.suptitle("Frequency vs. Time")
    #    fig.canvas.mpl_connect('key_press_event', \
    #            lambda ev: (ev.key in ('q','Q') and plt.close(fig)))
    #oname = "%.3f_%s.png" % (start,str(dm))

    if ofile is "unknown_cand":    	    	
		plt.savefig(ofile)
		ofile = ofile + "_%.3f_%s.png" % (start,str(dm))
    else: plt.savefig(ofile)