-
Notifications
You must be signed in to change notification settings - Fork 10
/
plot_discoveryyear.python
84 lines (73 loc) · 2.54 KB
/
plot_discoveryyear.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
#!/usr/bin/python
import xml.etree.ElementTree as ET
import subprocess, glob, os, math, datetime
# Open pipe gnuplot. You may want to change the terminal from svg to pdf for publication quality plots.
gnuplot = subprocess.Popen(['gnuplot',"-persist"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
gnuplot.stdin.write("""
set terminal svg
set output "plot_discoveryyear.svg"
set xlabel "Discovery year"
set autoscale xfix
set ylabel "Number of new planets"
set style fill solid 0.7
set key top left
set label "Data taken from the Open Exoplanet Catalogue. Last updated on """+datetime.date.today().isoformat()+"""." at screen 0.31,0.15 front font ",8"
set style data histograms
set style histogram rowstacked
plot \
"-" using 2:xtic(1) title "Radial velocity planets" ,\
"" using 3 title "Transiting planets" ,\
"" using 4 title "Imaged planets" ,\
"" using 5 title "Planets detected by timing" ,\
"" using 6 lt 6 title "All others detection methods"
""")
def plotPlanetsDiscoveredBy(_discoverymethod=None):
data = {}
for filename in glob.glob("open_exoplanet_catalogue/systems/*.xml"):
system = ET.parse(open(filename, 'r'))
planets = system.findall(".//planet")
for planet in planets:
try:
discoverymethod = planet.findtext("./discoverymethod")
discoveryyear = int(planet.findtext("./discoveryyear"))
if _discoverymethod==None: #plot all others
if discoverymethod!="RV" and discoverymethod!="transit" and discoverymethod!="imaging" and discoverymethod!="timing":
try:
data[discoveryyear] += 1
except:
data[discoveryyear] = 1
else:
if discoverymethod==_discoverymethod:
try:
data[discoveryyear] += 1
except:
data[discoveryyear] = 1
except:
pass
return data
methods = []
methods.append(plotPlanetsDiscoveredBy("RV"))
methods.append(plotPlanetsDiscoveredBy("transit"))
methods.append(plotPlanetsDiscoveredBy("imaging"))
methods.append(plotPlanetsDiscoveredBy("timing"))
methods.append(plotPlanetsDiscoveredBy())
def printToGnuplot():
for year in range(1990,datetime.date.today().year+1):
if year%5==0: #only print some labels
gnuplot.stdin.write("%d\t"%(year))
else:
gnuplot.stdin.write("\"\"\t")
for method in methods:
try:
gnuplot.stdin.write("%d\t"% (int(method[year])))
except:
gnuplot.stdin.write("%d\t"% 0)
gnuplot.stdin.write("\n")
gnuplot.stdin.write("\ne\n")
# output needs to be repeated 5 times because we are using a pipe and not a file.
printToGnuplot()
printToGnuplot()
printToGnuplot()
printToGnuplot()
printToGnuplot()
gnuplot.stdin.close()