Skip to content

Instantly share code, notes, and snippets.

@TomWagg
Last active May 17, 2022 18:21
Show Gist options
  • Save TomWagg/eb864ee7d12343f8110d977b597bff2c to your computer and use it in GitHub Desktop.
Save TomWagg/eb864ee7d12343f8110d977b597bff2c to your computer and use it in GitHub Desktop.
Recreate Appendix D from "Understanding Stellar Evolution" by Henry Lamers and Emily Levesque
import urllib
import pandas as pd
import numpy as np
def create_appendix_d(Zs=[0.014, 0.002], verbose=False,
masses=[0.8, 0.9, 1, 2, 4, 7, 12, 15, 20, 25, 32, 40, 60, 85, 120]):
"""Recreate Appendix D from "Understanding Stellar Evolution" by Henry Lamers and Emily Levesque.
Parameters
----------
Zs : `list`, optional
List of metallicities to get, by default [0.014, 0.002]
verbose : `bool`, optional
Whether to print out progress messages, by default False
masses : `list`, optional
List of masses to get, by default [0.8, 0.9, 1, 2, 4, 7, 12, 15, 20, 25, 32, 40, 60, 85, 120]
Returns
-------
dfs : `list of DataFrames`
List of Pandas DataFrames corresponding to each metallicity
"""
# constants for which lines refer to ZAMS and TAMS as defined by the textbook
ZAMS = 1
TAMS = 86
# holder for the dataframes
dfs = []
for Z in Zs:
if verbose:
print(f"Creating table for Z={Z}")
data = []
for mass in masses:
if verbose:
print(f" Getting m={mass}")
mass_str = f"{mass:03}".replace(".", "p")
Z_str = str(Z).split(".")[1]
# get the data from the Ekstrom+2012 files
url = f"https://obswww.unige.ch/Recherche/evol/tables_grids2011/Z{Z_str}/M{mass_str}Z{Z_str[1:]}V0.dat"
response = urllib.request.urlopen(url)
# read the files into a Pandas DataFrame
df = pd.read_csv(response, delim_whitespace=True, skiprows=[1], header=0)
df.set_index("line", inplace=True)
# grab the relevant data from the dataframe
data.append([df.loc[ZAMS]["lg(L)"].round(2), df.loc[ZAMS]["lg(Teff)"].round(2),
np.log10(df.loc[TAMS]["time"]).round(2), df.loc[TAMS]["mass"].round(1),
df.loc[TAMS]["lg(L)"].round(2), df.loc[TAMS]["lg(Teff)"].round(2)])
# convert the data into a dataframe
df = pd.DataFrame(data, columns=["lg(L_ZAMS)", "lg(Teff_ZAMS)", "lg(t_TAMS)",
"m_TAMS", "lg(L_TAMS)", "lg(Teff_TAMS)"],
index=pd.Index(masses, name="m_ZAMS"))
# save the file
df.to_csv(f"appendix_d_Z_{Z}.csv")
# add the dataframe to the list to be returned
dfs.append(df)
return dfs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment