Skip to content

Instantly share code, notes, and snippets.

@DanielAndreasen
Created May 25, 2015 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielAndreasen/03ee4f4dce8de6037771 to your computer and use it in GitHub Desktop.
Save DanielAndreasen/03ee4f4dce8de6037771 to your computer and use it in GitHub Desktop.
SWEETCat in python with PyAstronomy
"""
This small example that shows how to use python to work with SWEET-Cat
https://www.astro.up.pt/resources/sweet-cat/
It use the PyAstronomy package which can be found here
https://github.com/sczesla/PyAstronomy
http://www.hs.uni-hamburg.de/DE/Ins/Per/Czesla/PyA/PyA/index.html
The package can be installed with pip
pip install PyAstronomy
or from source
git clone https://github.com/sczesla/PyAstronomy.git
cd PyAstronomy
python setup.py install --with-ext
Check the dependencies of PyAstronomy if there is problems.
You may have to remove and reinstall PyAstronomy if it's already on your system.
"""
from PyAstronomy import pyasl
import matplotlib.pyplot as plt
# Create the SWEETCat object. This download SC to your local computer.
# SC is downloaded automatically if the file is more than 7 days old.
sc = pyasl.SWEETCat()
# If you want to force a fresh update, run
sc.downloadData()
# Extract the data. This is a pandas DataFrame
data = sc.data
# Print the top of the table and the attributes
print data.head()
print data.keys()
# Let's plot the V mag vs. the effective temperature
data.plot('teff', 'vmag', kind='scatter')
plt.show()
# We can also create another column for our table, e.g. luminosity so we can make a HR diagram
# Note that this works even though we have missing values
data['lum'] = (data.teff/5777)**4 * data.mass
data.plot('teff', 'lum', kind='scatter', xlim=(8500, 2500))
# If we only are interested in a shorter table, with stars with Teff below 5000K, we can do so
data_cool = data[data.teff < 5000]
# See that it worked (print rows and columns)
print data.shape
print data_cool.shape
# And the plot
plt.figure()
data.plot('teff', 'lum', kind='scatter', xlim=(8500, 2500))
plt.plot(data_cool.teff, data_cool.lum, 'oy')
plt.show()
@DanielAndreasen
Copy link
Author

DanielAndreasen commented Aug 30, 2016

To search by HD number

print data.columns  # To be sure that "hd" is one of the columns
data[data.hd == number]

@jason-neal
Copy link

Thanks it worked a charm.
To get a single value out of the the selected row of a pandas dataframe I needed to use the .iloc method suggested on stackoverflow
E.g. to get the temperature value of HD4747

sc = pyasl.SWEETCat()
data = sc.data
hd_number = 4747
if hd_number in sc.data.hd.values:
    hd_entry = data[data.hd == hd_number]    
    teff = hd_entry.iloc[0]["teff"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment