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 = longdouble2string(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. """ psr = lt.tempopulsar(parfile, timfile) residuals = psr.residuals() binary_delay = psr.binarydelay() outfile = parfile + '.tempo2_test' f = open(outfile, 'w') outstr = 'residuals BinaryDelay ' if general2 is not None and general2 != []: if not has_tempo2_utils: raise ImportError("To get tempo2 general2 results, tempo2_utils are" " required. See page" " https://github.com/demorest/tempo_utils") else: 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 = longdouble2string(residuals[ii]) + ' ' +longdouble2string(binary_delay[ii]) +' ' if general2 is not None: for keys in general2: outstr += longdouble2string(tempo2_vals[keys][ii])+' ' outstr += '\n' f.write(outstr) f.close()
def change_parfile(filename, param, value): """A function to change parfile parameter value and fit flag """ strv = ut.longdouble2string(value) fh, abs_path = mkstemp() with open(abs_path, 'w') as new_file: with open(filename) as old_file: for line in old_file: if line.startswith(param + ' '): l = line.split() line = line.replace(l[1], strv) line = line.replace(' 0 ', ' 1 ') new_file.write(line) close(fh) #Remove original file remove(filename) #Move new file move(abs_path, filename)
def change_parfile(filename, param, value): """A function to change parfile parameter value and fit flag """ strv = ut.longdouble2string(value) fh, abs_path = mkstemp() with open(abs_path,'w') as new_file: with open(filename) as old_file: for line in old_file: if line.startswith(param+' '): l = line.split() line = line.replace(l[1], strv) line = line.replace(' 0 ', ' 1 ') new_file.write(line) close(fh) #Remove original file remove(filename) #Move new file move(abs_path, filename)
def tempo_polyco_table_writer(polycoTable, filename = 'polyco.dat'): """ Write tempo style polyco file from an astropy table Parameters --------- polycoTalbe: astropy table Polycos style table filename : str Name of the output poloco file. 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) + ....) Reference: http://tempo.sourceforge.net/ref_man_sections/tz-polyco.txt """ f = open(filename,'w') try: lenTable = len(polycoTable) if lenTable == 0: errorMssg = ("No sufficent polyco data."+ " Plese make sure polycoTable has data.") raise AttributeError(errorMssg) except: errorMssg = "No sufficent polycoTable. " 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) tmidMjd = utils.longdouble2string(entry.tmid.value)+' ' dm = str(polycoTable['dm'][i]).ljust(72-52+1) dshift = str(polycoTable['dopper'][i]).ljust(79-74+1) logrms = str(polycoTable['logrms'][i]).ljust(80-86+1) line1 = psrname+dateDMY+utcHMS+tmidMjd+dm+dshift+logrms+'\n' # Get the reference phase rph = (entry.rphase.int+entry.rphase.frac).data[0] rphase = utils.longdouble2string(rph)[0:19].ljust(20) f0 = ('%.12lf' % entry.f0).ljust(38-21+1) obs = entry.obs.ljust(43-39+1) tspan = (str(entry.mjdspan.to('min')).split())[0].ljust(49-44+1) 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()
def tempo_polyco_table_writer(polycoTable, filename='polyco.dat'): """ Write tempo style polyco file from an astropy table Parameters --------- polycoTalbe: astropy table Polycos style table filename : str Name of the output poloco file. 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) + ....) Reference: http://tempo.sourceforge.net/ref_man_sections/tz-polyco.txt """ f = open(filename, 'w') try: lenTable = len(polycoTable) if lenTable == 0: errorMssg = ("No sufficent polyco data." + " Plese make sure polycoTable has data.") raise AttributeError(errorMssg) except: errorMssg = "No sufficent polycoTable. " 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) tmidMjd = utils.longdouble2string(entry.tmid.value) + ' ' dm = str(polycoTable['dm'][i]).ljust(72 - 52 + 1) dshift = str(polycoTable['dopper'][i]).ljust(79 - 74 + 1) logrms = str(polycoTable['logrms'][i]).ljust(80 - 86 + 1) line1 = psrname + dateDMY + utcHMS + tmidMjd + dm + dshift + logrms + '\n' # Get the reference phase rph = (entry.rphase.int + entry.rphase.frac).data[0] rphase = utils.longdouble2string(rph)[0:19].ljust(20) f0 = ('%.12lf' % entry.f0).ljust(38 - 21 + 1) obs = entry.obs.ljust(43 - 39 + 1) tspan = (str(entry.mjdspan.to('min')).split())[0].ljust(49 - 44 + 1) 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()
print "param1 num unit is ", param1.num_unit # Change num_value print "Change num_values" param1.num_value = 10.0 print "param1 num value is ", param1.num_value print "param1 value is ", param1.value, "With type ", type(param1.value) print "param1 unit is ", param1.units print "param1 num unit is ", param1.num_unit # Test MJDparameter Class paramMJD = p.MJDParameter( name="TZRMJD", description="Reference epoch for phase = 0.0", parse_value=lambda x: time_from_mjd_string(x, scale="tdb"), ) print "Before assigning paramMJD value." print "paramMJD num value is ", longdouble2string(paramMJD.num_value) print "paramMJD value is ", paramMJD.value, "With type ", type(paramMJD.value) print "paramMJD unit is ", paramMJD.units print "paramMJD num unit is ", paramMJD.num_unit paramMJD.set("54001.012345678901234") print "After assigning paramMJD value." print "paramMJD num value is ", longdouble2string(paramMJD.num_value) print "paramMJD value is ", paramMJD.value, "With type ", type(paramMJD.value) print "paramMJD unit is ", paramMJD.units print "paramMJD num unit is ", paramMJD.num_unit # change num_value paramMJD.num_value = data2longdouble("54001.012345678901234") print "After assigning paramMJD num_value." print "paramMJD num value is ", longdouble2string(paramMJD.num_value) print "paramMJD value is ", paramMJD.value, "With type ", type(paramMJD.value) print "paramMJD unit is ", paramMJD.units