PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/python/catalog_oldstyle.py

https://github.com/joerivanleeuwen/presto
Python | 345 lines | 303 code | 7 blank | 35 comment | 0 complexity | 33f7a21a2097c88da58b7ce89ae91b2c MD5 | raw file
  1. ## Automatically adapted for numpy Apr 14, 2006 by convertcode.py
  2. from math import *
  3. import struct, os, os.path
  4. ## The most recent catalogs are available here:
  5. ##
  6. ## http://www.atnf.csiro.au/research/pulsar/catalogue/
  7. ## For the radio pulsars, listed parameters in order are: pulsar J2000
  8. ## name, J2000 right ascension (h m s) and its error, J2000 declination
  9. ## (deg. ' ") and its error, Galactic longitude and latitude (deg.),
  10. ## pulse period (s) and its error, pulse period derivative (10^-15) and
  11. ## its error, epoch of the period (MJD), dispersion measure (cm^-3 pc)
  12. ## and its error, rotation measure (rad m^-2) and its error, pulsar
  13. ## distance (kpc), in most cases estimated from the dispersion measure
  14. ## using the Taylor & Cordes (1993) model for the Galactic free electron
  15. ## density, 400 MHz mean flux density (mJy) and its error, 1400 MHz mean
  16. ## flux density (mJy) and its error, a binary coded integer representing
  17. ## surveys in which the pulsar has been detected, and codes for surveys
  18. ## in which the pulsar has been detected. Errors are in the last quoted
  19. ## digit of the value.
  20. ## Binary parameters in order are: J2000 pulsar name, binary orbital
  21. ## period (d) and its error, projected semi-major axis (s) and its error,
  22. ## orbit eccentricity and its error, longitude of periastron (deg.) and
  23. ## its error, epoch of periastron (MJD) and its error. For some
  24. ## near-circular orbit pulsars the parameters eps1 = ecc * sin(omega),
  25. ## eps2 = ecc * cos(omega) and the epoch of ascending node (MJD) are also
  26. ## given.
  27. ## For the high-energy-only pulsars, the parameters are pulsar J2000
  28. ## name, J2000 right ascension (h m s) and its error, J2000 declination
  29. ## (deg. ' ") and its error, Galactic longitude and latitude (deg.),
  30. ## pulse period (s) and its error, pulse period derivative (10^-15) and
  31. ## its error, epoch of the period (MJD), estimated pulsar distance (kpc),
  32. ## a binary coded integer representing surveys in which the pulsar has
  33. ## been detected, and codes for surveys in which the pulsar has been
  34. ## detected.
  35. ARCSECTORAD = float('4.8481368110953599358991410235794797595635330237270e-6')
  36. RADTOARCSEC = float('206264.80624709635515647335733077861319665970087963')
  37. SECTORAD = float('7.2722052166430399038487115353692196393452995355905e-5')
  38. RADTOSEC = float('13750.987083139757010431557155385240879777313391975')
  39. RADTODEG = float('57.295779513082320876798154814105170332405472466564')
  40. DEGTORAD = float('1.7453292519943295769236907684886127134428718885417e-2')
  41. RADTOHRS = float('3.8197186342054880584532103209403446888270314977710')
  42. HRSTORAD = float('2.6179938779914943653855361527329190701643078328126e-1')
  43. PI = float('3.1415926535897932384626433832795028841971693993751')
  44. TWOPI = float('6.2831853071795864769252867665590057683943387987502')
  45. def coord_to_string(h_or_d, m, s):
  46. """
  47. coord_to_string(h_or_d, m, s):
  48. Return a formatted string of RA or DEC values as
  49. 'hh:mm:ss.ssss' if RA, or 'dd:mm:ss.ssss' if DEC.
  50. """
  51. if (s >= 10.0):
  52. return "%.2d:%.2d:%.4f" % (h_or_d, m, s)
  53. else:
  54. return "%.2d:%.2d:0%.4f" % (h_or_d, m, s)
  55. def rad_to_dms(rad):
  56. """
  57. rad_to_dms(rad):
  58. Convert radians to degrees, minutes, and seconds of arc.
  59. """
  60. if (rad < 0.0): sign = -1
  61. else: sign = 1
  62. arc = RADTODEG * fmod(fabs(rad), PI)
  63. d = int(arc)
  64. arc = (arc - d) * 60.0
  65. m = int(arc)
  66. s = (arc - m) * 60.0
  67. return (sign * d, m, s)
  68. def rad_to_hms(rad):
  69. """
  70. rad_to_hms(rad):
  71. Convert radians to hours, minutes, and seconds of arc.
  72. """
  73. rad = fmod(rad, TWOPI)
  74. if (rad < 0.0): rad = rad + TWOPI
  75. arc = RADTOHRS * rad
  76. h = int(arc)
  77. arc = (arc - h) * 60.0
  78. m = int(arc)
  79. s = (arc - m) * 60.0
  80. return (h, m, s)
  81. def hms_to_rad(hmslist):
  82. hour = float(hmslist[0])
  83. min = float(hmslist[1])
  84. sec = 0.0
  85. if (len(hmslist)==3):
  86. sec = float(hmslist[2])
  87. if (hour < 0.0): sign = -1
  88. else: sign = 1
  89. return sign*SECTORAD*(60.0*(60.0*fabs(hour)+fabs(min))+fabs(sec))
  90. def val_and_err(valstr, errstr):
  91. exponent = 0
  92. coord = 0
  93. if (valstr=='*'):
  94. return (None, None)
  95. elif valstr.count(":"): # This is a coordinate
  96. coord = 1
  97. val = hms_to_rad(valstr.split(':'))
  98. else:
  99. val = float(valstr)
  100. eplace = valstr.find('E')
  101. if (eplace > 0):
  102. exponent = int(valstr[eplace+1:])
  103. valstr = valstr[:eplace]
  104. if (errstr=='*'):
  105. err = 0.0
  106. else:
  107. err = float(errstr)
  108. decimal = valstr.find('.')
  109. if (decimal > 0): # Decimal was found
  110. err *= 10.0**-(len(valstr)-decimal-1-exponent)
  111. if coord:
  112. err *= SECTORAD
  113. return (val, err)
  114. class psr:
  115. def __init__(self, line):
  116. parts = line.split()
  117. self.jname = parts[0][1:]
  118. (self.ra, self.raerr) = val_and_err(parts[1], parts[2])
  119. (self.dec, self.decerr) = val_and_err(parts[3], parts[4])
  120. self.dec /= 15.0
  121. self.decerr /= 15.0
  122. self.l = float(parts[5])
  123. self.b = float(parts[6])
  124. (self.p, self.perr) = val_and_err(parts[7], parts[8])
  125. (self.pd, self.pderr) = val_and_err(parts[9], parts[10])
  126. if (self.pd):
  127. self.pd *= 1.0e-15
  128. self.pderr *= 1.0e-15
  129. else:
  130. self.pd = 0.0
  131. self.pderr = 0.0
  132. if (parts[11]=='*'):
  133. self.pepoch = 51000.0 # Just to pick a reasonable value
  134. else:
  135. self.pepoch = float(parts[11])
  136. if (len(parts)==15): # The high-energy/AXP tables
  137. if (parts[12]=='*'):
  138. self.dist = None
  139. else:
  140. self.dist = float(parts[12])
  141. self.dm = 0.0
  142. self.dmerr = 0.0
  143. self.s400 = 0.0
  144. self.s400err = 0.0
  145. self.s1400 = 0.0
  146. self.s1400err = 0.0
  147. else: # The radio pulsar table
  148. (self.dm, self.dmerr) = val_and_err(parts[12], parts[13])
  149. if (self.dm is None):
  150. self.dm = 0.0
  151. self.dmerr = 0.0
  152. (self.rm, self.rmerr) = val_and_err(parts[14], parts[15])
  153. if (parts[16]=='*'):
  154. self.dist = None
  155. else:
  156. self.dist = float(parts[16])
  157. (self.s400, self.s400err) = val_and_err(parts[17], parts[18])
  158. (self.s1400, self.s1400err) = val_and_err(parts[19], parts[20])
  159. self.bname = ""
  160. self.alias = ""
  161. self.binary = 0
  162. def __cmp__(self, other):
  163. return cmp(self.jname, other.jname)
  164. def __str__(self):
  165. out = ''
  166. if (self.bname):
  167. out = out + "\nPulsar B%s (J%s)\n" % \
  168. (self.bname, self.jname)
  169. else:
  170. out = out + "\nPulsar J%s\n" % (self.jname)
  171. if (self.alias):
  172. out = out + " Alias = %s\n" % self.alias
  173. (h, m, s) = rad_to_hms(self.ra)
  174. serr = RADTOSEC * self.raerr
  175. out = out + " RA (J2000) = %s +/- %.4fs\n" % \
  176. (coord_to_string(h, m, s), serr)
  177. (d, m, s) = rad_to_dms(self.dec)
  178. serr = RADTOARCSEC * self.decerr
  179. out = out + " DEC (J2000) = %s +/- %.4f\"\n" % \
  180. (coord_to_string(d, m, s), serr)
  181. out = out + " (l, b) = (%.2f, %.2f)\n" % \
  182. (self.l, self.b)
  183. out = out + " DM (cm-3 pc) = %.5g +/- %.5g\n" % \
  184. (self.dm, self.dmerr)
  185. if (self.s400 is not None):
  186. out = out + " S_400MHz (mJy) = %.2g +/- %.2g\n" % \
  187. (self.s400, self.s400err)
  188. if (self.s1400 is not None):
  189. out = out + " S_1400MHz (mJy) = %.2g +/- %.2g\n" % \
  190. (self.s1400, self.s1400err)
  191. if (self.dist is not None):
  192. out = out + " Distance (kpc) = %.3g\n" % self.dist
  193. out = out + " Period (s) = %.15g +/- %.15g\n" % \
  194. (self.p, self.perr)
  195. out = out + " P-dot (s/s) = %.8g +/- %.8g\n" % \
  196. (self.pd, self.pderr)
  197. out = out + " Epoch (MJD) = %.10g\n" % self.pepoch
  198. if (self.binary):
  199. out = out + " P_binary (s) = %.10g +/- %.10g\n" % \
  200. (self.pb*86400.0, self.pberr*86400.0)
  201. out = out + " P_binary (d) = %.10g +/- %.10g\n" % \
  202. (self.pb, self.pberr)
  203. out = out + " a*sin(i)/c (s) = %.8g +/- %.8g\n" % \
  204. (self.x, self.xerr)
  205. out = out + " Eccentricity = %.8g +/- %.8g\n" % \
  206. (self.e, self.eerr)
  207. if (self.e > 0.0):
  208. out = out + " Long of Peri (deg) = %.10g +/- %.10g\n" % \
  209. (self.w, self.werr)
  210. out = out + " Time of Peri (MJD) = %.12g +/- %.12g\n" % \
  211. (self.To, self.Toerr)
  212. else:
  213. out = out + " T of Ascd Node (MJD) = %.12g +/- %.12g\n" % \
  214. (self.To, self.Toerr)
  215. return out
  216. def add_bin_params(self, parts):
  217. self.binary = 1
  218. (self.pb, self.pberr) = val_and_err(parts[1], parts[2])
  219. (self.x, self.xerr) = val_and_err(parts[3], parts[4])
  220. if (self.x is None):
  221. self.x = 0.0
  222. self.xerr = 0.0
  223. (self.e, self.eerr) = val_and_err(parts[5], parts[6])
  224. if (self.e is None):
  225. self.e = 0.0
  226. self.eerr = 0.0
  227. (self.w, self.werr) = val_and_err(parts[7], parts[8])
  228. if (self.w is None):
  229. self.w = 0.0
  230. self.werr = 0.0
  231. (self.To, self.Toerr) = val_and_err(parts[9], parts[10])
  232. if (self.To is None):
  233. (self.To, self.Toerr) = val_and_err(parts[15], parts[16])
  234. if (self.To is None):
  235. self.To = 0.0
  236. self.Toerr = 0.0
  237. if (parts[11]!='*'): # Use the
  238. (eps1, eps2) = (float(parts[11]), float(parts[13]))
  239. self.e = sqrt(eps1*eps1 + eps2*eps2)
  240. self.eerr = 0.0001 # This needs fixing...
  241. self.w = RADTODEG*atan2(eps1, eps2)
  242. if (self.w < 0.0):
  243. self.w += 360.0
  244. self.werr = 1.0 # This needs fixing...
  245. (self.To, self.Toerr) = val_and_err(parts[15], parts[16])
  246. def pack_structs(self):
  247. out = struct.pack("13s9s10s12d", \
  248. self.jname, self.bname, self.alias.lower(),
  249. self.ra, self.raerr, self.dec, self.decerr,
  250. self.p, self.perr, self.pd, self.pderr,
  251. self.dm, self.dmerr, self.pepoch, self.binary)
  252. if self.binary:
  253. out = out + struct.pack("10d",
  254. self.pb, self.pberr, self.x, self.xerr,
  255. self.e, self.eerr, self.w, self.werr,
  256. self.To, self.Toerr)
  257. return out
  258. pulsars = {}
  259. num_binaries = 0
  260. binflag = 0
  261. presto_path = os.getenv("PRESTO")
  262. infile = open(os.path.join(presto_path, "lib", "psr_export.dat"))
  263. for line in infile.readlines()[1:]:
  264. if (line[0]=='J'):
  265. if (binflag==0):
  266. currentpulsar = psr(line)
  267. pulsars[currentpulsar.jname] = currentpulsar
  268. else:
  269. binprops = line.split()
  270. pulsars[binprops[0][1:]].add_bin_params(binprops)
  271. num_binaries += 1
  272. else:
  273. binflag = 1
  274. infile.close()
  275. if (0):
  276. binflag = 0
  277. infile = open("hep_export.dat")
  278. for line in infile.readlines()[1:]:
  279. if (line[0]=='J'):
  280. if (binflag==0):
  281. currentpulsar = psr(line)
  282. pulsars[currentpulsar.jname] = currentpulsar
  283. else:
  284. binprops = line.split()
  285. pulsars[binprops[0][1:]].add_bin_params(binprops)
  286. num_binaries += 1
  287. else:
  288. binflag = 1
  289. infile.close()
  290. binflag = 0
  291. infile = open("axp_export.dat")
  292. for line in infile.readlines()[1:]:
  293. if (line[0]=='J'):
  294. if (binflag==0):
  295. currentpulsar = psr(line)
  296. pulsars[currentpulsar.jname] = currentpulsar
  297. else:
  298. binprops = line.split()
  299. pulsars[binprops[0][1:]].add_bin_params(binprops)
  300. num_binaries += 1
  301. else:
  302. binflag = 1
  303. infile.close()
  304. infile = open(os.path.join(presto_path, "lib", "aliases.txt"))
  305. for line in infile.readlines()[1:]:
  306. if (line[0]=='J'):
  307. vals = line.split()
  308. if (len(vals)>=2):
  309. if (vals[1][0]=='B'):
  310. pulsars[vals[0][1:]].bname = vals[1][1:]
  311. else:
  312. pulsars[vals[0][1:]].alias = vals[1]
  313. if (len(vals)==3):
  314. pulsars[vals[0][1:]].alias = vals[2]
  315. infile.close()
  316. psrs = pulsars.values()
  317. psrs.sort()
  318. if __name__ == '__main__' :
  319. outfilename = "pulsars.cat"
  320. outfile = open(outfilename, "w")
  321. print "Writing %d pulsars (%d binaries) to %s" % \
  322. (len(psrs), num_binaries, outfilename)
  323. for ii, psr in enumerate(psrs):
  324. outfile.write(psr.pack_structs())
  325. outfile.close()