def __init__(self, polyco_file, time0, rphase, time_unit, convert): self.polyco = Polyco(polyco_file) self.time0 = time0 self.rphase = rphase self.time_unit = time_unit self.convert = convert
def FindPhase(t0, z_size, dt, ngate): print('z_size', z_size) polyco = Polyco( '/mnt/raid-cita/mahajan/Pulsars/B1957Timing/polycob1957+20_gpfit.dat') p = polyco.phasepol(t0, rphase='fraction', t0=t0, time_unit=u.second, convert=True) # print('p',p) ph = p(np.arange(z_size) * dt.to(u.s).value) # print ('ph0',ph) # print ('len(ph0)',len(ph)) ph -= np.floor(ph[0]) # print('ph1', ph) ncycle = int(np.floor(ph[-1])) + 1 ph = np.remainder(ph * ngate, ngate * ncycle).astype(np.float64) # print('ph2', ph) return ph, ncycle
def get_phasepol(self, time0): """ return the phase polynomial at time0 (calculated if necessary) """ phasepol = self['ppol'] if phasepol is None: subs = [self['src'], str(self['date'])] wrn = "{0} is not configured for time {1} \n".format(*subs) wrn += "\tPlease update observations.conf " raise Warning(wrn) elif not isinstance(phasepol, Polynomial): print("Calculating {0} polyco at {1}".format(self['src'], time0)) from pulsar.predictor import Polyco polyco_file = get_pkg_data_filename(phasepol) polyco = Polyco(polyco_file) phasepol = polyco.phasepol(time0, rphase='fraction', t0=time0, time_unit=u.second, convert=True) return phasepol
icounts = [] waterfalls = [] S = [0, 1] for P in range(7, 12): # GMRT overlap: 7--11; full range 0--19 file1 = file_fmt.format(S=S[0], P=P) file2 = file_fmt.format(S=S[1], P=P) if not isinstance(phasepol, Polynomial): from h5py import File as HDF5File from astropy.utils.data import get_pkg_data_filename from astropy.time import Time from pulsar.predictor import Polyco polyco_file = get_pkg_data_filename(phasepol) polyco = Polyco(polyco_file) h0 = HDF5File(file1.replace('.raw', '.h5'), 'r') time0 = Time(h0['SUB_ARRAY_POINTING_000']. attrs['EXPTIME_START_UTC'].replace('Z', ''), scale='utc') fbottom = (h0['SUB_ARRAY_POINTING_000']['BEAM_000']['COORDINATES'] ['COORDINATE_1'].attrs['AXIS_VALUES_WORLD'][0] * u.Hz).to(u.MHz) # time0 = Time('2013-07-25T22:12:00.000000000', # scale='utc') h0.close() phasepol = polyco.phasepol(time0, rphase='fraction', t0=time0, time_unit=u.second, convert=True)
def fold(foldtype, fn, obsfile, tstart, polyco, dtype, Tint, tbin, nchan, ngate, size, dedisperse, pulsedetect): """ Parameters ---------- foldtype : string One of 'fold', 'sp' fn : file handle handle to file holding voltage timeseries tstart : astropy Time beginning time to reduce, defaults to start of file polyco : string path to file containing polyco solution for timing dtype : string one of 'vdif', 'mark4', 'mark5b' nchan : int number of frequency channels for FFT tbin : float size of time bins in seconds ngate : int number of phase bins to use for folded spectrum ntbin should be an integer fraction of nt size: int number of samples to reduce in each step dedisperse: string one of 'coherent', 'incoherent' **obs: parameters read from obs.conf """ fh = RD(fname=fn, obsfile=obsfile, size=size) psr_polyco = Polyco(polyco) # Derived values for folding dt1 = fh.dt1 sample_rate = fh.sample_rate ntbin = int((Tint / tbin).value) npol = len(fh.thread_ids) t0 = fh.fh.tell('time') if not tstart: tstart = t0 else: tstart = Time(tstart) print("File begins at {0}, beginning at {1}".format(t0.isot, tstart.isot)) offset = int(np.floor(((tstart - t0) / dt1).decompose()).value) # Step is reduced by DM losses, rounded to nearest power of 2 step = fh.step Tstep = int(np.ceil((Tint / (step * dt1)).decompose())) if foldtype == 'fold': foldspec = np.zeros((ntbin, nchan, ngate, npol)) ic = np.zeros((ntbin, nchan, ngate)) # Folding loop for i in range(Tstep): print('On step {0} of {1}'.format(i, Tstep)) print('Reading...') fh.seek(offset + step * i) t0 = fh.fh.tell('time') if i == 0: print('starting at {0}'.format(t0.isot)) phase_pol = psr_polyco.phasepol(t0) if dedisperse == 'coherent': data = fh.readCoherent(size) dchan = np.fft.rfft(data.reshape(-1, 2 * nchan, npol), axis=1) del data elif dedisperse == 'incoherent': dchan = fh.readIncoherent(size, nchan) power = (np.abs(dchan)**2) print("Folding") tsamp = (2 * nchan / sample_rate).to(u.s) tsr = t0 + tsamp * np.arange(power.shape[0]) if pulsedetect: PulseDetect(dchan, tsr, phase_pol) if foldtype == 'fold': phase = (np.remainder(phase_pol(tsr.mjd), 1) * ngate).astype('int') ibin = np.floor( (ntbin * tsamp * (i * power.shape[0] + np.arange(power.shape[0])) // Tint).decompose()).astype('int') time_bins = np.unique(ibin) time_bins = time_bins[time_bins < ntbin] # Beautiful triply embeded for loop for folding for bin in time_bins: pfold = power[ibin == bin] phasefold = phase[ibin == bin] for kfreq in range(nchan): for pol in range(npol): foldspec[bin, kfreq, :, pol] += np.bincount(phasefold, pfold[:, kfreq, pol], ngate) ic[bin, kfreq, :] += np.bincount(phasefold, pfold[:, kfreq, 0] != 0., ngate) if foldtype == 'sp': pfold = power.sum(1) phase = phase_pol(tsr.mjd) ncycle = int(np.ceil(phase[-1] - phase[0])) if i == 0: foldspec = np.zeros((ncycle * Tstep, ngate, npol)) ic = np.zeros((ncycle * Tstep, ngate)) phase0 = phase[0] phase -= phase0 iphase = np.remainder(phase * ngate, ncycle * ngate).astype(np.int) for pol in range(npol): foldspec[i * ncycle:(i + 1) * ncycle, :, pol] += np.bincount(iphase, pfold[..., pol], minlength=ngate * ncycle).reshape( ncycle, ngate) ic[i * ncycle:(i + 1) * ncycle] += np.bincount( iphase, pfold[..., 0] != 0, minlength=ngate * ncycle).reshape(ncycle, ngate) ic[ic == 0] = 1 return foldspec, ic
if sn[peak] >= 50: print('\nBright pulse detected!!! t={0} with S/N={1}'.format( tsr.isot, sn[peak])) print('\n{0} Pulses detected'.format(len(peaks))) return power, sn if __name__ == '__main__': w = np.load(sys.argv[1]) t0 = Time(sys.argv[1].split('_')[-1].split('+')[0], format='isot', scale='utc') psr_polyco = Polyco(sys.argv[2]) phase_pol = psr_polyco.phasepol(t0) # Remove edges of waterfall which are lost to de-dispersion w = w[buff:-3 * buff] # Quick workaround to ensure times are still correct t0 += buff * tsamp w, ok = rfi_filter_raw(w) w, sn = rfi_filter_power(w, t0, phase_pol) plt.plot(sn) plt.xlabel('time') plt.ylabel('intensity') plt.show()