forked from OpenExoplanetCatalogue/oec_links
/
exoplanetarchive.py
executable file
·90 lines (77 loc) · 4.79 KB
/
exoplanetarchive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
import urllib
import os
import xml.etree.ElementTree as ET
import xmltools
import genhash
#####################
# Exoplanet Archive
#####################
url_exoplanetarchive = "http://exoplanetarchive.ipac.caltech.edu/cgi-bin/nstedAPI/nph-nstedAPI?table=exoplanets&format=csv&select=*"
def get():
xmltools.ensure_empty_dir("exoplanetarchive")
urllib.urlretrieve (url_exoplanetarchive, "exoplanetarchive/exoplanetarchive.csv")
def parse():
# delete old data
xmltools.ensure_empty_dir("systems_exoplanetarchive")
# parse data into default xml format
f = open("exoplanetarchive/exoplanetarchive.csv")
header = [x.strip() for x in f.readline().split(",")]
for line in f:
p = dict(zip(header, [x.strip() for x in line.split(",")]))
outputfilename = "systems_exoplanetarchive/"+p["pl_hostname"]+".xml"
if os.path.exists(outputfilename):
system = ET.parse(outputfilename).getroot()
star = system.find(".//star")
else:
system = ET.Element("system")
ET.SubElement(system, "name").text = p["pl_hostname"]
tempra = ""
tempra += p["ra_str"].split("h")[0] # hours
tempra += " " + p["ra_str"].split("h")[1].split("m")[0] # minutes
tempra += " %.2i" % (round(float(p["ra_str"].split("h")[1].split("m")[1].split("s")[0]))) # seconds
ET.SubElement(system, "rightascension").text = tempra
tempdec = ""
tempdec += p["dec_str"].split("d")[0] # hours
tempdec += " " + p["dec_str"].split("d")[1].split("m")[0] # minutes
tempdec += " %.2i" % (round(float(p["dec_str"].split("d")[1].split("m")[1].split("s")[0]))) # seconds
ET.SubElement(system, "declination").text = tempdec
star = ET.SubElement(system,"star")
ET.SubElement(star, "name").text = p["pl_hostname"]
ET.SubElement(star, "radius", errorminus=p['st_raderr'], errorplus=p['st_raderr']).text = p["st_rad"]
ET.SubElement(star, "magV", errorminus=p['st_vjerr'], errorplus=p['st_vjerr']).text = p["st_vj"]
ET.SubElement(star, "magI", errorminus=p['st_icerr'], errorplus=p['st_icerr']).text = p["st_ic"]
ET.SubElement(star, "magJ", errorminus=p['st_jerr'], errorplus=p['st_jerr']).text = p["st_j"]
ET.SubElement(star, "magH", errorminus=p['st_herr'], errorplus=p['st_herr']).text = p["st_h"]
ET.SubElement(star, "magK", errorminus=p['st_kerr'], errorplus=p['st_kerr']).text = p["st_k"]
ET.SubElement(star, "mass", errorminus=p['st_masserr'], errorplus=p['st_masserr']).text = p["st_mass"]
ET.SubElement(star, "temperature", errorminus=p['st_tefferr'], errorplus=p['st_tefferr']).text = p["st_teff"]
ET.SubElement(star, "metallicity").text = p["st_metratio"]
planet = ET.SubElement(star,"planet")
ET.SubElement(planet, "name").text = p["pl_hostname"]+" "+p["pl_letter"]
ET.SubElement(planet, "semimajoraxis", errorminus=p["pl_orbsmaxerr2"], errorplus=p["pl_orbsmaxerr1"]).text = p["pl_orbsmax"]
ET.SubElement(planet, "eccentricity", errorminus=p['pl_orbeccenerr2'], errorplus=p['pl_orbeccenerr1']).text = p["pl_orbeccen"]
ET.SubElement(planet, "periastron", errorminus=p['pl_orblpererr2'], errorplus=p['pl_orblpererr1']).text = p["pl_orblper"]
ET.SubElement(planet, "inclination", errorminus=p['pl_orbinclerr2'], errorplus=p['pl_orbinclerr1']).text = p["pl_orbincl"]
ET.SubElement(planet, "period", errorminus=p['pl_orbpererr2'], errorplus=p['pl_orbpererr1']).text = p["pl_orbper"]
# check for both kinds of masses
if p['pl_massj'] == "" or p['pl_massj'] == None:
# use msini
ET.SubElement(planet, "mass", errorminus=p['pl_msinijerr2'], errorplus=p['pl_msinijerr1']).text = p["pl_msinij"]
else:
# use mass jupiter
ET.SubElement(planet, "mass", errorminus=p['pl_massjerr2'], errorplus=p['pl_massjerr1']).text = p["pl_massj"]
ET.SubElement(planet, "radius", errorminus=p['pl_radjerr2'], errorplus=p['pl_radjerr1']).text = p["pl_radj"]
ET.SubElement(planet, "temperature", errorminus=p['pl_eqterr2'], errorplus=p['pl_eqterr1']).text = p["pl_eqt"]
ET.SubElement(planet, "discoverymethod").text = p["pl_discmethod"]
ET.SubElement(planet, "discoveryyear").text = p["pl_disc"]
ET.SubElement(planet, "lastupdate").text = p["rowupdate"]
# Need to check if BJD
ET.SubElement(planet, "transittime", errorminus=p['pl_tranmiderr2'], errorplus=p['pl_tranmiderr1']).text = p["pl_tranmid"]
# Cleanup and write file
xmltools.removeemptytags(system)
xmltools.indent(system)
ET.ElementTree(system).write(outputfilename)
if __name__=="__main__":
get()
parse()