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 range(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)
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