def get_tempo_result(parfile, timfile): """This function is to get the results from tempo and write them to a file. Parameter --------- parfile : str The file to read parameters. timfile : str The file to read toas. Return ---------- A file named as parfile name ends with '.tempo_test' stored residuals in the first column """ t1_toas = t1u.read_toa_file(timfile) t1u.run_tempo(t1_toas, parfile) t1_resids = t1_toas.get_resids(units="phase") outfile = parfile + ".tempo_test" f = open(outfile, "w") outstr = "residuals_phase " outstr += "\n" f.write(outstr) for res in t1_resids: outstr = longdouble2str(res) + "\n" f.write(outstr) f.close()
def get_tempo2_result(parfile, timfile, general2=None): """This function is to get the results from tempo and write them to a file. Parameter --------- parfile : str The file to read parameters. timfile : str The file to read toas. general2 : list/ None The values required from tempo2 general2 plugin. Return ---------- A file named as parfile name ends with '.tempo2_test' stored residuals in the first column, binary delay in the second column and general2 results if general2 are provided. """ if not has_tempo2_utils: raise ImportError( "To get tempo2 general2 results, tempo2_utils are" " required. See page" " https://github.com/demorest/tempo_utils" ) residuals = t2u.general2(parfile, timfile, ["pre"])["pre"] outfile = parfile + ".tempo2_test" f = open(outfile, "w") outstr = "residuals " if general2 is not None and general2 != []: tempo2_vals = t2u.general2(parfile, timfile, general2) for keys in general2: outstr += keys + " " outstr += "\n" f.write(outstr) for ii in range(len(residuals)): outstr = longdouble2str(residuals[ii]) + " " if general2 is not None: for keys in general2: outstr += longdouble2str(tempo2_vals[keys][ii]) + " " outstr += "\n" f.write(outstr) f.close()
def test_longdouble_str_roundtrip_is_exact(i, f): ld = np.longdouble(i) + np.longdouble(f) assert ld == str2longdouble(longdouble2str(ld))
def test_longdouble2str_same_as_str_and_repr(i, f): ld = np.longdouble(i) + np.longdouble(f) assert longdouble2str(ld) == str(ld) assert longdouble2str(ld) == repr(ld)
def tempo_polyco_table_writer(polycoTable, filename='polyco.dat'): """ Write tempo style polyco file from an astropy table Tempo style polyco file: The polynomial ephemerides are written to file 'polyco.dat'. Entries are listed sequentially within the file. The file format is:: Line Columns Item ---- ------- ----------------------------------- 1 1-10 Pulsar Name 11-19 Date (dd-mmm-yy) 20-31 UTC (hhmmss.ss) 32-51 TMID (MJD) 52-72 DM 74-79 Doppler shift due to earth motion (10^-4) 80-86 Log_10 of fit rms residual in periods 2 1-20 Reference Phase (RPHASE) 21-38 Reference rotation frequency (F0) 39-43 Observatory number 44-49 Data span (minutes) 50-54 Number of coefficients 55-75 Observing frequency (MHz) 76-80 Binary phase 3* 1-25 Coefficient 1 (COEFF(1)) 26-50 Coefficient 2 (COEFF(2)) 51-75 Coefficient 3 (COEFF(3)) * Subsequent lines have three coefficients each, up to NCOEFF One polyco file could include more then one entrie The pulse phase and frequency at time T are then calculated as:: DT = (T-TMID)*1440 PHASE = RPHASE + DT*60*F0 + COEFF(1) + DT*COEFF(2) + DT^2*COEFF(3) + .... FREQ(Hz) = F0 + (1/60)*(COEFF(2) + 2*DT*COEFF(3) + 3*DT^2*COEFF(4) + ....) Parameters --------- polycoTable: astropy table Polycos style table filename : str Name of the output poloco file. References ---------- http://tempo.sourceforge.net/ref_man_sections/tz-polyco.txt """ f = open(filename, 'w') lenTable = len(polycoTable) if lenTable == 0: errorMssg = ("Insufficent polyco data." + " Please make sure polycoTable has data.") raise AttributeError(errorMssg) for i in range(lenTable): entry = polycoTable['entry'][i] psrname = polycoTable['psr'][i].ljust(10) dateDMY = polycoTable['date'][i].ljust(10) utcHMS = polycoTable['utc'][i][0:9].ljust(10) tmid_mjd = utils.longdouble2str(entry.tmid.value) + ' ' dm = str(polycoTable['dm'][i]).ljust(72 - 52 + 1) dshift = str(polycoTable['doppler'][i]).ljust(79 - 74 + 1) logrms = str(polycoTable['logrms'][i]).ljust(80 - 86 + 1) line1 = psrname + dateDMY + utcHMS + tmid_mjd + dm + dshift + logrms + '\n' # Get the reference phase rph = (entry.rphase.int + entry.rphase.frac).value[0] # FIXME: sometimes raises error, sometimes loses precision! rphase = utils.longdouble2str(rph)[0:19].ljust(20) f0 = ('%.12lf' % entry.f0).ljust(38 - 21 + 1) obs = entry.obs.ljust(43 - 39 + 1) tspan = str(round(entry.mjdspan.to('min').value, 4))[0].ljust(49 - 44 + 1) if len(tspan) >= (49 - 44 + 1): # Hack to fix read errors in python tspan = tspan + ' ' ncoeff = str(entry.ncoeff).ljust(54 - 50 + 1) obsfreq = str(polycoTable['obsfreq'][i]).ljust(75 - 55 + 1) binPhase = str(polycoTable['binary_phase'][i]).ljust(80 - 76 + 1) line2 = rphase + f0 + obs + tspan + ncoeff + obsfreq + binPhase + '\n' coeffBlock = "" for j, coeff in enumerate(entry.coeffs): coeffBlock += ('%.17e' % coeff).ljust(25) if (j + 1) % 3 == 0: coeffBlock += '\n' f.write(line1 + line2 + coeffBlock) f.close()