forked from OpenExoplanetCatalogue/oec_tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateASCIITables.python
executable file
·117 lines (107 loc) · 5.13 KB
/
generateASCIITables.python
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
import xml.etree.ElementTree as ET, glob, csv
def getParameter(treeobject,parameter):
try:
ret = treeobject.findtext("./"+parameter).strip()
except:
ret = ""
return ret
def getParameter_all(treeobject,parameter):
'''
returns a list of all values in treeobject for the supplied parameter
'''
try:
ret = ", ".join([x.text for x in treeobject.iter(parameter)])
except Exception as err:
print(err)
ret = ""
return ret
fields = []
fields.append(["name","Primary identifier of planet"])
fields.append(["binaryflag","Binary flag [0=no known stellar binary companion; 1=P-type binary (circumbinary); 2=S-type binary; 3=orphan planet (no star)]"])
fields.append(["mass","Planetary mass [Jupiter masses]"])
fields.append(["radius","Radius [Jupiter radii]"])
fields.append(["period","Period [days]"])
fields.append(["semimajoraxis","Semi-major axis [Astronomical Units]"])
fields.append(["eccentricity","Eccentricity"])
fields.append(["periastron","Periastron [degree]"])
fields.append(["longitude","Longitude [degree]"])
fields.append(["ascendingnode","Ascending node [degree]"])
fields.append(["inclination","Inclination [degree]"])
fields.append(["temperature","Surface or equilibrium temperature [K]"])
fields.append(["age","Age [Gyr]"])
fields.append(["discoverymethod","Discovery method"])
fields.append(["discoveryyear","Discovery year [yyyy]"])
fields.append(["lastupdate","Last updated [yy/mm/dd]"])
fields.append(["system_rightascension","Right ascension [hh mm ss]"])
fields.append(["system_declination","Declination [+/-dd mm ss]"])
fields.append(["system_distance","Distance from Sun [parsec]"])
fields.append(["hoststar_mass","Host star mass [Solar masses]"])
fields.append(["hoststar_radius","Host star radius [Solar radii]"])
fields.append(["hoststar_metallicity","Host star metallicity [log relative to solar]"])
fields.append(["hoststar_temperature","Host star temperature [K]"])
fields.append(["hoststar_age","Host star age [Gyr]"])
fields.append(["list","A list of lists the planet is on"])
fieldnames = [a[0] for a in fields]
with open("FIELDS.md","w") as f:
f.write("Fields description\n")
f.write("==================\n\n")
for i, field in enumerate(fields):
f.write("%d. `%s` %s\n\n"%(i+1,field[0],field[1]))
def outputFilesFromDataset(filename, dataset):
csvwriters = []
csvwriters.append(csv.DictWriter(open("comma_separated/"+filename+".txt", "w"), delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL, fieldnames=fieldnames))
csvwriters.append(csv.DictWriter(open("tab_separated/"+filename+".txt", "w"), delimiter="\t", quotechar='"', quoting=csv.QUOTE_MINIMAL, fieldnames=fieldnames))
for csvwriter in csvwriters:
csvwriter.writeheader()
# Loop over all files and create new tables
for filename in glob.glob("open_exoplanet_catalogue/"+dataset+"/*.xml"):
system = ET.parse(open(filename,'r'))
planets = system.findall(".//planet")
stars = system.findall(".//star")
for planet in planets:
# Find host star
hoststar = None
for star in stars:
planetsaroundstar = star.findall("./planet")
if planet in planetsaroundstar:
hoststar = star
parameters = []
for field in fields:
if field[0]=="binaryflag":
binaryflag = 0
# check if planet is circuminary planet
ptypeplanets = system.findall(".//binary/planet")
if planet in ptypeplanets:
binaryflag = 1
# check if planet is S-type binary planet
starsinbinaries = system.findall(".//binary/star")
for starinbinary in starsinbinaries:
# is host star in a binary?
if starinbinary == hoststar:
binaryflag = 2
# check if planet is an orphan planet
if len(stars)==0:
binaryflag = 3
# first, find host star
ptypeplanets = system.findall(".//binary/planet")
if planet in ptypeplanets:
binaryflag = 1
parameters.append(binaryflag)
continue
if field[0][0:7]=="system_":
parameters.append(getParameter(system,field[0][7:]))
continue
if field[0][0:9]=="hoststar_":
parameters.append(getParameter(hoststar,field[0][9:]))
continue
if field[0]=="list":
parameters.append(getParameter_all(planet,field[0]))
continue
else:
parameters.append(getParameter(planet,field[0]))
continue
for csvwriter in csvwriters:
csvwriter.writerow(dict(zip(fieldnames,parameters)))
outputFilesFromDataset("open_exoplanet_catalogue","systems");
outputFilesFromDataset("open_exoplanet_catalogue_kepler","systems_kepler");