def __init__(self, freqbands): """Read TEMPO results (resid2.tmp, tempo.lis, timfile and parfiles) freqbands is a list of frequency pairs to display. """ # Open tempo.lis. Parse it and find input .tim and .par files. Also find output .par file. inputfiles_re = re.compile( r"Input data from (.*\.tim.*), Parameters from (.*\.par.*)") outputfile_re = re.compile(r"Assumed parameters -- PSR (.*)$") tempolisfile = open("tempo.lis") intimfn, inparfn, outparfn = None, None, None for line in tempolisfile: if line[:15] == "Input data from": sline = line.split() intimfn = sline[3][:-1] # strip the comma intimbase = os.path.splitext(intimfn)[0] inparfn = intimbase + ".par" if sline[6] == 'def' else sline[6] if inparfn[-1] == ".": inparfn = inparfn[:-1] elif line[:15] == "Assumed paramet": outparfn = line.split()[-1] + ".par" if (intimfn != None) and (inparfn != None) and (outparfn != None): # Found what we're looking for no need to continue parsing the file break tempolisfile.close() # Record filename self.inparfn = inparfn self.outparfn = outparfn self.intimfn = intimfn # Read parfiles self.inpar = par.psr_par(inparfn) self.outpar = par.psr_par(outparfn) # Read residuals r = residuals.read_residuals() self.max_TOA = r.bary_TOA.max() self.min_TOA = r.bary_TOA.min() if freqbands is None: self.freqbands = find_freq_clusters(r.bary_freq) else: self.freqbands = freqbands self.residuals = {} for lo, hi in self.freqbands: indices = (r.bary_freq >= lo) & (r.bary_freq < hi) self.residuals[get_freq_label(lo, hi)] = \ Resids(r.bary_TOA[indices], r.bary_freq[indices], \ np.arange(r.numTOAs)[indices], r.orbit_phs[indices], \ r.postfit_phs[indices], r.postfit_sec[indices], \ r.prefit_phs[indices], r.prefit_sec[indices], \ r.uncertainty[indices], r.weight[indices], \ self.inpar, self.outpar)
def __init__(self, parfilenm): self.par = parfile.psr_par(parfilenm) if not hasattr(self.par, 'BINARY'): print("'%s' doesn't contain parameters for a binary pulsar!") return None self.PBsec = self.par.PB * SECPERDAY self.T0 = self.par.T0
def create_polycos(parfn, telescope_id, center_freq, start_mjd, end_mjd, \ max_hour_angle=None, span=SPAN_DEFAULT, \ numcoeffs=NUMCOEFFS_DEFAULT, keep_file=False): """Create polycos object from a parfile. Inputs: parfn: parfile's filename, or a parfile object. telescope_id: The TEMPO 1-character telescope identifier. center_freq: The observation's center frequencies in MHz. start_mjd: MJD on which the polycos should start. end_mjd: MJD until the polycos should extend. max_hour_angle: The maximum hour angle as expected by tempo. (Default: Use default value chosen for given telescope). span: Span of each set of polycos in min. (Default: 60 min). numcoeffs: Number of coefficients to use. (Default: 12). keep_file: If true do not delete polyco.dat file. (Default: delete polyco.dat file). Output: new_polycos: a polycos object. """ if type(parfn) == bytes: # assume parfn is a filename par = parfile.psr_par(parfn) else: # assume par is already a parfile.psr_par object par = parfn if max_hour_angle is None: telescope_name = id_to_telescope[telescope_id] max_hour_angle = telescope_to_maxha[telescope_name] tzfile = open("tz.in", "w") # Default parameters for prediction mode tzfile.write("%s %d %d %d %0.5f\n" % (telescope_id, max_hour_angle, \ SPAN_DEFAULT, NUMCOEFFS_DEFAULT, center_freq)) # TEMPO ignores lines 2 and 3 in tz.in file tzfile.write("\n\n") if hasattr(par, "PSR"): psrname = par.PSR else: psrname = par.PSRJ tzfile.write("%s %d %d %d %0.5f\n" % (psrname, SPAN_DEFAULT, \ NUMCOEFFS_DEFAULT, max_hour_angle, center_freq)) tzfile.close() tempo = subprocess.Popen("tempo -z -f %s" % par.FILE, shell=True, \ stdin=subprocess.PIPE, stdout=subprocess.PIPE, \ stderr=subprocess.PIPE) (out, err) = tempo.communicate("%d %d\n" % (start_mjd, end_mjd)) if err: raise TempoError("The following error message was encountered " \ "when running TEMPO to generate polycos from " \ "the input parfile (%s):\n\n%s\n" % (parfn, err)) new_polycos = polycos(psrname, filenm='polyco.dat') # Remove files created by us and by TEMPO os.remove("tz.in") if not keep_file: os.remove("polyco.dat") return new_polycos
def parse_eph(filenm): global period, time suffix = filenm.split(".")[-1] if suffix == "bestprof": x = bestprof.bestprof(filenm) fs = pu.p_to_f(x.p0_bary, x.p1_bary, x.p2_bary) epoch = x.epochi_bary + x.epochf_bary T = x.T elif suffix == "par": x = parfile.psr_par(filenm) # Try to see how many freq derivs we have fs = [x.F0] for ii in range(1, 20): # hopefully 20 is an upper limit! attrib = "F%d" % ii if hasattr(x, attrib): fs.append(getattr(x, attrib)) else: break epoch = x.PEPOCH T = (x.FINISH - x.START) * 86400.0 else: print("I don't recognize the file type for", filenm) sys.exit() newts = epoch + num.arange(int(T / 10.0 + 0.5), dtype=num.float) / 8640.0 time = num.concatenate((time, newts)) newps = 1.0 / pu.calc_freq(newts, epoch, *fs) period = num.concatenate((period, newps)) print("%13.7f (%0.1f sec): " % (epoch, T), fs)
def read_par(pfname, f1errmax=999.0): pf = parfile.psr_par(pfname) # Try to see how many freq derivs we have fs = [pf.F0] for ii in range(1, 20): # hopefully 20 is an upper limit! attrib = "F%d" % ii if hasattr(pf, attrib): fs.append(getattr(pf, attrib)) else: break epoch = pf.PEPOCH Tobs = (pf.FINISH - pf.START) * 86400.0 return epoch, Tobs, fs
def read_par(pfname, f1errmax=999.0): pf = parfile.psr_par(pfname) f0 = pf.F0 p0 = pf.P0 try: f0err = pf.F0_ERR except: f0err = 2.0e-5 if not isfinite(f0err): f0err = 3.0e-5 f1 = pf.F1 try: p1 = pf.P1 except: p1 = 0.0 try: f1err = pf.F1_ERR except: f1err = 10.0e-8 mjd = pf.PEPOCH if (verbose): # print "%6s: %.4f F0 %10.9g +/- %8.03g F1 %10.5g +/- %8.03g" % (pfname,mjd,f0,f0err,f1,f1err) print("%.4f %10.9g %8.3g %10.5g %8.3g" % (mjd, f0, f0err, f1, f1err), end=' ') if (f1err > f1errmax): print(" * Ignored *") else: print() # print " P0 = %g, P1 = %g" % (p0,p1) print("----- ", pfname) print("PEPOCH ", mjd) print("F0 ", f0) print("F1 ", f1) return mjd, f0, f0err, f1, f1err
def parse_cmd_line(args): """ Parse command line argumentss Input ----- args - a list of command line aruments and values Output ------ user-supplied values for the given arguments """ # create a list of valid command line arguments valid_args = ["-p", "-pb", "-x", "-T0", "-e", "-w", "-par", "-nofit", "-o"] if len(args) == 0: print_usage() exit(0) for arg in args: # check to make sure all arguments are valid if (arg.startswith("-")) and (arg not in valid_args) and \ not arg.strip("-").replace(".","").isdigit(): print("ERROR: Unknown arg %s" % arg) print_usage() exit(0) # go through the given arguments and store user-supplied values try: const_params = args.pop(args.index("-nofit") + 1) args.remove("-nofit") except ValueError: const_params = "" pass if "-par" in args: try: par_file_name = args.pop(args.index("-par") + 1) args.remove("-par") par = parfile.psr_par(par_file_name) p = par.P0 pb_days = par.PB x = par.A1 T0 = par.T0 e = par.E w = par.OM except IOError: print("ERROR: %s not found\n" % par_file_name) exit(0) except AttributeError: print("ERROR: %s does not appear to be a valid binary .par file\n" \ %par_file_name) exit(0) else: try: p = float(args.pop(args.index("-p") + 1)) args.remove("-p") except ValueError: print("ERROR: You must specify a spin period\n") exit(0) try: pb_days = float(args.pop(args.index("-pb") + 1)) args.remove("-pb") except ValueError: print("ERROR: You must specify an orbital period\n") exit(0) try: x = float(args.pop(args.index("-x") + 1)) args.remove("-x") except ValueError: print("ERROR: You must specify a projected semi-major axis\n") exit(0) try: T0 = float(args.pop(args.index("-T0") + 1)) args.remove("-T0") except ValueError: print("ERROR: You must specify a time of periastron passage\n") exit(0) try: e = float(args.pop(args.index("-e") + 1)) args.remove("-e") except ValueError: print( "WARNING: Orbital eccentricity not specified, assuming e = 0\n" ) e = 0.0 const_params = const_params + ",e" pass try: w = float(args.pop(args.index("-w") + 1)) args.remove("-w") except ValueError: print( "WARNING: Longitude of periastron not specified, assuming w = 0\n" ) w = 0.0 const_params = const_params + ",w" pass try: out_file_root = args.pop(args.index("-o") + 1) args.remove("-o") except ValueError: out_file_root = "fitorb" pass in_files = args return p, pb_days, x, T0, e, w, const_params, out_file_root, in_files