class PolycoPhasepol(object): """Polyco wrapper that will get phase relative to some reference time0, picking the appropriate polyco chunk.""" 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 __call__(self, dt): """Get phases for time differences dt (float in seconds) relative to self.time0 (filled by initialiser). Chunks are assumed to be sufficiently closely spaced that one can get the index into the polyco table from the first item. """ try: dt0 = dt[0] except IndexError: dt0 = dt time0 = self.time0 + TimeDelta(dt0, format='sec') phasepol = self.polyco.phasepol( time0, rphase=self.rphase, t0=time0, time_unit=self.time_unit, convert=self.convert) return phasepol(dt-dt0)
def find_phase(t_GPs,scan_no): t0 = Time(start_times[scan_no-1]) t_GP = Time(t_GPs) psr_polyco = Polyco('polyco_new.dat') phase_pol = psr_polyco.phasepol(t0) phase = np.remainder(phase_pol(t_GP.mjd), 1) return phase
class PolycoPhasepol(object): """Polyco wrapper that will get phase relative to some reference time0, picking the appropriate polyco chunk.""" 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 __call__(self, dt): """Get phases for time differences dt (float in seconds) relative to self.time0 (filled by initialiser). Chunks are assumed to be sufficiently closely spaced that one can get the index into the polyco table from the first item. """ try: dt0 = dt[0] except IndexError: dt0 = dt time0 = self.time0 + TimeDelta(dt0, format='sec') phasepol = self.polyco.phasepol(time0, rphase=self.rphase, t0=time0, time_unit=self.time_unit, convert=self.convert) return phasepol(dt - dt0)
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
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) nskip = int( round(((Time('2013-07-25T22:15:00', scale='utc') - time0) / (ntint / fwidth)).to(u.dimensionless_unscaled))) if verbose: print("Using start time {0} and phase polynomial {1}".format( time0, phasepol)) print("Skipping {0} records and folding {1} records to cover " "time span {2} to {3}".format( nskip, nt, time0 + nskip * ntint / fwidth, time0 + (nskip + nt) * ntint / fwidth)) else: fbottom = fwidth * (563. + P * 20)
with twofile(timestamp_file, [file1, file2], comm=comm) as fh1: if verbose: print("Start time = {}; gsb start = {}" .format(fh1.timestamps[0], fh1.gsb_start)) if not isinstance(phasepol, Polynomial): from astropy.utils.data import get_pkg_data_filename from pulsar.predictor import Polyco polyco_file = get_pkg_data_filename(phasepol) polyco = Polyco(polyco_file) time0 = fh1.timestamps[0] # GMRT time is off by 1 second time0 -= (2.**24/(100*u.MHz/6.)).to(u.s) # time0 -= 1. * u.s phasepol = polyco.phasepol(time0, rphase='fraction', t0=time0, time_unit=u.second, convert=True) nskip = int(round( ((Time('2013-07-25T22:15:00', scale='utc') - time0) / (recsize / samplerate)).to(u.dimensionless_unscaled))) if verbose: print("Using start time {0} and phase polynomial {1}" .format(time0, phasepol)) print("Skipping {0} records and folding {1} records to cover " "time span {2} to {3}" .format(nskip, nt, time0 + nskip * recsize / samplerate, time0 + (nskip+nt) * recsize / samplerate)) else: nskip = 0 myfoldspec, myicount, mywaterfall = fold(fh1, np.int8, samplerate,
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
from pulsar.predictor import Polyco t0 = fh.tell(unit='time') t_GP = Time('2015-10-18T23:41:53.316550') psr_polyco = Polyco('oct_polycob0531_ef.dat') phase_pol = psr_polyco.phasepol(t0) phase = np.remainder(phase_pol(t_GP.mjd), 1)
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()
# I assume one is working from a directory above pulsar, or one that # includes a symbolic link -- e.g., I work in a directory where I did # ln -s /home/mhvk/packages/pulsar/pulsar import pulsar # from pulsar.pulsar import ELL1Ephemeris from pulsar.predictor import Polyco import os polyco_file = os.path.dirname(pulsar.__file__) + \ '/tests/data/polyco_new_1957_17may13.dat' if __name__ == '__main__': tstart = Time('2013-05-16 23:47', scale='utc') polyco = Polyco(polyco_file) phasepol = polyco.phasepol(polyco.searchclosest(tstart.mjd)) polyco_mjd_mid = (phasepol.domain[0] + phasepol.domain[1])/2 # blunt way, but should be OK; units of phasecol are cycles/day # so need to convert to cycles/sec polyco_freq_and_derivs = [phasepol.deriv(i).coef[0]/(24*3600)**i for i in range(1,len(phasepol))] # get times over range covered by this polyco, at 1 minute intervals mjd_range = np.linspace(phasepol.domain[0], phasepol.domain[1], 1./1440.) times = Time(mjd_range, format='mjd', scale='utc') # now get predicted phases from ELL1Ephemeris, etc. # you probably want to write a function that calculates these, # following test_ephemeris # eph_phases = ... # then fit those to a polynomial # eph_phasepol = Polynomial.fit(mjd_range - polyco_mjd_mid, # eph_phases)
# I assume one is working from a directory above pulsar, or one that # includes a symbolic link -- e.g., I work in a directory where I did # ln -s /home/mhvk/packages/pulsar/pulsar import pulsar # from pulsar.pulsar import ELL1Ephemeris from pulsar.predictor import Polyco import os polyco_file = os.path.dirname(pulsar.__file__) + \ '/tests/data/polyco_new_1957_17may13.dat' if __name__ == '__main__': tstart = Time('2013-05-16 23:47', scale='utc') polyco = Polyco(polyco_file) phasepol = polyco.phasepol(polyco.searchclosest(tstart.mjd)) polyco_mjd_mid = (phasepol.domain[0] + phasepol.domain[1]) / 2 # blunt way, but should be OK; units of phasecol are cycles/day # so need to convert to cycles/sec polyco_freq_and_derivs = [ phasepol.deriv(i).coef[0] / (24 * 3600)**i for i in range(1, len(phasepol)) ] # get times over range covered by this polyco, at 1 minute intervals mjd_range = np.linspace(phasepol.domain[0], phasepol.domain[1], 1. / 1440.) times = Time(mjd_range, format='mjd', scale='utc') # now get predicted phases from ELL1Ephemeris, etc. # you probably want to write a function that calculates these, # following test_ephemeris # eph_phases = ... # then fit those to a polynomial