Beispiel #1
0
    def get_spectra_raw(self, start, stop):
        '''Wrapper for get_spectra for waterfaller compatibility
        '''
        data = self.get_spectra(start, stop)

        return spectra.Spectra(self.freqs, self.tsamp, data.transpose(), \
            starttime=self.tsamp*start, dm=0)
Beispiel #2
0
    def get_spectra(self, startsamp, N, nsub=None):
        """Return 2D array of data from PSRFITS file.
 
            Inputs:
                startsamp, Starting sample
                N: number of samples to read
 
            Output:
                data: 2D numpy array
        """
        # Calculate starting subint and ending subint
        startsub = int(startsamp / self.nsamp_per_subint)
        skip = startsamp - (startsub * self.nsamp_per_subint)
        endsub = int((startsamp + N) / self.nsamp_per_subint)
        trunc = ((endsub + 1) * self.nsamp_per_subint) - (startsamp + N)
        if nsub:
            print "Will subband the data to : %i channels" % nsub
            nchan_per_sub = self.nchan / 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)
            sub_freqs = sub_ctrfreqs
        # Read data
        data = []
        for isub in xrange(startsub, endsub + 1):
            if nsub:
                sub_data = self.read_subint(isub)
                # Subband
                sub_data = np.transpose(np.array([np.sum(sub, axis=1) for sub in \
                                        np.hsplit(sub_data, nsub)]))
                data.append(sub_data)
            else:
                data.append(self.read_subint(isub))
        if len(data) > 1:
            data = np.concatenate(data)
        else:
            data = np.array(data).squeeze()
        data = np.transpose(data)
        #Truncate data to desired interval
        if trunc > 0:
            data = data[:, skip:-trunc]
        elif trunc == 0:
            data = data[:, skip:]
        else:
            raise ValueError("Number of bins to truncate is negative: %d" %
                             trunc)
        if not self.specinfo.need_flipband:
            # for psrfits module freqs go from low to high.
            # spectra module expects high frequency first.
            data = data[::-1, :]
            freqs = self.freqs[::-1]
        else:
            freqs = self.freqs
        if nsub:
            freqs = sub_freqs
        return spectra.Spectra(freqs, self.tsamp, data, \
                               starttime=self.tsamp*startsamp, dm=0)
Beispiel #3
0
 def get_spectra(self, start, nspec):
     stop = min(start+nspec, self.nspec)
     pos = self.header_size+start*self.bytes_per_spectrum
     # Compute number of elements to read
     nspec = int(stop) - int(start)
     num_to_read = nspec*self.nchans
     num_to_read = max(0, num_to_read)
     self.filfile.seek(pos, os.SEEK_SET)
     spectra_dat = np.fromfile(self.filfile, dtype=self.dtype,
                           count=num_to_read)
     spectra_dat.shape = nspec, self.nchans
     spec = spectra.Spectra(self.freqs, self.tsamp, spectra_dat.T,
             starttime=start*self.tsamp, dm=0.0)
     return spec
    def get_spectra(self, startsamp, N):
        """Return 2D array of data from filterbank file.
 
            Inputs:
                startsamp: Starting sample
                N: number of samples to read
 
            Output:
                data: 2D numpy array
        """
        self.seek_to_sample(startsamp)
        data = self.read_Nsamples(N)
        data.shape = (N, self.header['nchans'])
        return spectra.Spectra(self.frequencies, self.tsamp, data.T, \
                                starttime=self.tsamp*startsamp, dm=0)
Beispiel #5
0
    def random_gaussian_radius_spectra(self, true_spectrum):
        """ Smears the radius of a spectra object by generating a
          number of random points from a Gaussian pdf generated for
          that bin. The number of points generated is equivalent
          to the number of entries in that bin.

        Args:
          true_spectrum (spectra): spectrum to be smeared

        Returns:
          A smeared spectra object.
        """
        raw_events = true_spectrum._raw_events
        energy_step = (true_spectrum._energy_high -
                       true_spectrum._energy_low) / true_spectrum._energy_bins
        time_step = (true_spectrum._time_high -
                     true_spectrum._time_low) / true_spectrum._time_bins
        radial_step = (true_spectrum._radial_high -
                       true_spectrum._radial_low) / true_spectrum._radial_bins
        smeared_spectrum = spectra.Spectra(
            true_spectrum._name + str(self._position_resolution) +
            "_position_resolution", true_spectrum._num_decays)
        for time_bin in range(true_spectrum._time_bins):
            mean_time = time_bin * time_step + 0.5 * time_step
            for energy_bin in range(true_spectrum._energy_bins):
                mean_energy = energy_bin * energy_step + 0.5 * energy_step
                for radial_bin in range(true_spectrum._radial_bins):
                    mean_radius = radial_bin * radial_step + 0.5 * radial_step
                    entries = true_spectrum._data[energy_bin, radial_bin,
                                                  time_bin]
                    for i in range(int(entries)):
                        try:
                            smeared_spectrum.fill(
                                mean_energy,
                                np.fabs(
                                    np.random.normal(
                                        mean_radius,
                                        self._position_resolution)), mean_time)
                        except ValueError:
                            # Occurs when smeared radius is outside bin range
                            print "Warning: Smeared radius out of bounds. Skipping."
                            continue
        smeared_spectrum._raw_events = raw_events
        return smeared_spectrum
Beispiel #6
0
    def get_spectra(self, startsamp, N):
        """Return 2D array of data from PSRFITS file.
 
            Inputs:
                startsamp, Starting sample
                N: number of samples to read
 
            Output:
                data: 2D numpy array
        """
        # Calculate starting subint and ending subint
        startsub = int(startsamp / self.nsamp_per_subint)
        skip = startsamp - (startsub * self.nsamp_per_subint)
        endsub = int((startsamp + N) / self.nsamp_per_subint)
        trunc = ((endsub + 1) * self.nsamp_per_subint) - (startsamp + N)

        # Read data
        data = []
        for isub in xrange(startsub, endsub + 1):
            data.append(self.read_subint(isub))
        if len(data) > 1:
            data = np.concatenate(data)
        else:
            data = np.array(data).squeeze()
        data = np.transpose(data)
        # Truncate data to desired interval
        if trunc > 0:
            data = data[:, skip:-trunc]
        elif trunc == 0:
            data = data[:, skip:]
        else:
            raise ValueError("Number of bins to truncate is negative: %d" %
                             trunc)
        if not self.specinfo.need_flipband:
            # for psrfits module freqs go from low to high.
            # spectra module expects high frequency first.
            data = data[::-1, :]
            freqs = self.freqs[::-1]
        else:
            freqs = self.freqs

        return spectra.Spectra(freqs, self.tsamp, data, \
                                      starttime=self.tsamp*startsamp, dm=0)
Beispiel #7
0
def dm_transform_filobj(data,
                        dt,
                        freqs,
                        ref_freq=np.inf,
                        dm_min=-10,
                        dm_max=10,
                        ndm=50):
    """ Transform freq/time data to dm/time data.
    """
    import spectra
    nt = data.shape[1]
    ref_freq = 0.5 * (freqs[0] + freqs[-1])

    datafilobj = spectra.Spectra(freqs, dt, data, starttime=0, dm=0)

    dms = np.linspace(dm_min, dm_max, 250)
    dmtarr = np.empty([250, nt])

    for ii, dm in enumerate(dms):
        datafilobj.dedisperse(dm, ref_freq=ref_freq)
        dmtarr[ii] = datafilobj.data.mean(0)

    return dmtarr
Beispiel #8
0
    def weight_gaussian_radius_spectra(self, true_spectrum, num_sigma=5.):
        """ Smears the radius of a spectra object by calculating a Gaussian pdf
          for each bin and applies a weight to the bin and corresponding bins a
          default 5 sigma apart.

        Args:
          true_spectrum (spectra): spectrum to be smeared
          num_sigma (float): Width of window to apply the weight method.
            Default is 5.

        Returns:
          A smeared spectra object.
        """
        raw_events = true_spectrum._raw_events
        energy_step = (true_spectrum._energy_high -
                       true_spectrum._energy_low) / true_spectrum._energy_bins
        time_step = (true_spectrum._time_high -
                     true_spectrum._time_low) / true_spectrum._time_bins
        radial_step = (true_spectrum._radial_high -
                       true_spectrum._radial_low) / true_spectrum._radial_bins
        smeared_spectrum = spectra.Spectra(
            true_spectrum._name + str(self._position_resolution) +
            "_position_resolution", true_spectrum._num_decays)
        for time_bin in range(true_spectrum._time_bins):
            mean_time = time_bin * time_step + 0.5 * time_step
            for energy_bin in range(true_spectrum._energy_bins):
                mean_energy = energy_bin * energy_step + 0.5 * energy_step
                for radial_bin in range(true_spectrum._radial_bins):
                    mean_radius = radial_bin * radial_step + 0.5 * radial_step
                    entries = float(true_spectrum._data[energy_bin, radial_bin,
                                                        time_bin])
                    if entries == 0:
                        continue  # Bin Empty
                    lower_bin = self.floor_to_bin(
                        mean_radius - num_sigma * self._position_resolution,
                        radial_step) + 0.5 * radial_step
                    upper_bin = self.ceil_to_bin(
                        mean_radius + num_sigma * self._position_resolution,
                        radial_step) - 0.5 * radial_step
                    if upper_bin > true_spectrum._radial_high:
                        upper_bin = true_spectrum._radial_high - 0.5 * energy_step
                    if lower_bin < true_spectrum._radial_low:
                        lower_bin = true_spectrum._radial_low + 0.5 * energy_step
                    weights = []
                    for radius in np.arange(lower_bin, upper_bin, radial_step):
                        weights.append(
                            self.calc_gaussian(radius, mean_radius,
                                               self._position_resolution))
                    weight_tot = np.array(weights).sum()
                    i = 0
                    for radius in np.arange(lower_bin, upper_bin, radial_step):
                        try:
                            smeared_spectrum.fill(
                                mean_energy, radius, mean_time,
                                entries * weights[i] / weight_tot)
                        except ValueError:
                            # Occurs when smeared radius is outside bin range
                            print "Warning: Smeared radius out of bounds. Skipping."
                            continue
                        i += 1
        smeared_spectrum._raw_events = raw_events
        return smeared_spectrum