Created
June 30, 2022 21:48
-
-
Save seba-perez/401aa474058515c191a78285af30109b to your computer and use it in GitHub Desktop.
Código para acceder la Open Exoplanet Catalogue y generar tablas de datos para actividades del Equipo Pedagógico CIRAS/YEMS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Equipo Pedagogico CIRAS/YEMS | |
from astroquery.open_exoplanet_catalogue import findvalue | |
from astroquery import open_exoplanet_catalogue as oec | |
import numpy as np | |
import re | |
def get_exoplanets(output='exoplanets.txt', SolarSystem=False): | |
"""Grabs all planets from Open Exoplanet Catalogue and saves them.""" | |
cata = oec.get_catalogue() | |
names = [] | |
masses = [] | |
orbits = [] | |
radii = [] | |
planets = [] | |
file = open(output, "w") | |
file.write("Nombre del planeta, Masa relativa a la Tierra, Radio relativo a la Tierra, Semieje mayor de la orbita (unidad astronomica), Periodo Orbital (dias)\n") | |
for planet in cata.findall(".//planet"): | |
name = findvalue(planet, 'name') | |
smax = findvalue(planet, 'semimajoraxis') | |
P = findvalue(planet, 'period') | |
mass = findvalue(planet, 'mass') | |
radi = findvalue(planet, 'radius') | |
year = findvalue(planet, 'discoveryyear') | |
# remove those entries which aren't complete | |
if mass is None or year is None or radi is None or smax is None or P is None: | |
continue | |
if 'None' in str(mass.value) or 'None' in str(radi.value): | |
continue | |
# remove solar system bodies | |
if year.value < 1990.0: | |
continue | |
names += [[name]] | |
masses += [[mass.value*317.8284]] # converting to Earth masses | |
orbits += [[smax.value]] | |
radii += [[radi.value*(71492/6378.1)]] # converting to Earth radii | |
planets += [[name, mass.value*317.8284, radi.value*(71492/6378.1), smax.value]] | |
file.write(str(name) + "," + str("{:0.2f}".format(mass.value*317.8284)) + "," + str("{:0.2f}".format(radi.value*(71492/6378.1)))+','+ str("{:0.3f}".format(smax.value))+','+ str("{:0.2f}".format(P.value))+ "\n") | |
file.close() | |
if SolarSystem: | |
masses = np.array([0.00017, 0.00256, 0.00315, 0.00034, 1, 0.299, 0.046, 0.054]) * 317.8284 | |
radii = np.array([0.4, 0.9, 1.0,0.5,11.2,9.4,4.0,3.9]) | |
# print(masses) | |
# plotting | |
# Dark on mode. | |
darkon=False | |
if darkon: | |
color = 'white' | |
else: | |
color = 'black' | |
from matplotlib import pyplot as plt | |
import matplotlib | |
matplotlib.use('Agg') | |
matplotlib.rcParams.update({'text.color':color, | |
'axes.edgecolor':color, | |
'axes.labelcolor':color, | |
'xtick.color':color, | |
'ytick.color':color, | |
'legend.facecolor':color}) | |
# Create figure. | |
f, ax = plt.subplots(ncols=1, nrows=1, figsize=(6.0/1.5, 5.2/1.5)) | |
ax.plot(masses, radii, | |
"o", markerfacecolor="#EF476F", markeredgecolor="black", | |
markersize="4", markeredgewidth=.6) | |
ax.set_xscale('log') | |
ax.set_yscale('log') | |
ax.set_ylim(0.1, 1e2) | |
ax.set_xlim(0.01, 100000) | |
ax.set_ylabel('Tamaño del exoplaneta (relativo a la Tierra)') | |
ax.set_xlabel('Masa del exoplaneta (relativa a la Tierra)') | |
if SolarSystem: | |
ax.set_ylabel('Tamaño del planeta (relativo a la Tierra)') | |
ax.set_xlabel('Masa del planeta (relativa a la Tierra)') | |
ax.xaxis.set_major_formatter(matplotlib.ticker.LogFormatter()) | |
f.savefig(re.sub(".txt",".png", output), bbox_inches='tight', dpi=300, transparent=True) | |
# ax.set_title("Exoplanet population as " + str(np.datetime64('today', 'D'))) | |
# names = np.array(names).astype(str) | |
# masses = np.array(masses).astype(float) | |
# radii = np.array(radii).astype(float) | |
# print(planets) | |
# planets = np.column_stack(planets) | |
# print(planets) | |
# planets = np.array(planets, dtype = [('Name', (np.str_, 10)), ('Mass', np.float64), ('Radius', np.float64)]) | |
# header = 'name, mass (Mjup), radius (Rjup)' | |
# np.savetxt(output, planets, header=header, fmt='%s %f %f') | |
# np.savetxt(output, (names, masses, radii), fmt=['%s' , '%f', '%f']) | |
# print('{} planets saved to {}.'.format(planets.shape[0], output)) | |
if __name__ == "__main__": | |
get_exoplanets() | |
get_exoplanets(output="exoplanets_all.txt") | |
get_exoplanets(output="solarsystem.txt", SolarSystem=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment