Skip to content

Commit

Permalink
Merge branch 'main' into remove_wavelength_bounds_from_config
Browse files Browse the repository at this point in the history
  • Loading branch information
jvshields committed Jun 17, 2024
2 parents 49ebd0a + efbf373 commit 25c14fb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
12 changes: 10 additions & 2 deletions stardis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import logging


def run_stardis(config_fname, tracing_lambdas_or_nus):
def run_stardis(
config_fname, tracing_lambdas_or_nus, add_config_keys=None, add_config_vals=None
):
"""
Runs a STARDIS simulation.
Expand All @@ -20,6 +22,10 @@ def run_stardis(config_fname, tracing_lambdas_or_nus):
Numpy array of the frequencies or wavelengths to calculate the
spectrum for. Must have units attached to it, with dimensions
of either length or inverse time.
add_config_keys : list, optional
List of additional keys to add or overwrite for the configuration file.
add_config_vals : list, optional
List of corresponding additional values to add to the configuration file.
Returns
-------
Expand All @@ -29,7 +35,9 @@ def run_stardis(config_fname, tracing_lambdas_or_nus):

tracing_nus = tracing_lambdas_or_nus.to(u.Hz, u.spectral())

config, adata, stellar_model = parse_config_to_model(config_fname)
config, adata, stellar_model = parse_config_to_model(
config_fname, add_config_keys, add_config_vals
)
set_num_threads(config.n_threads)
stellar_plasma = create_stellar_plasma(stellar_model, adata, config)
stellar_radiation_field = create_stellar_radiation_field(
Expand Down
25 changes: 23 additions & 2 deletions stardis/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

from tardis.io.atom_data import AtomData
from tardis.io.configuration.config_validator import validate_yaml
from tardis.io.configuration.config_validator import validate_yaml, validate_dict
from tardis.io.configuration.config_reader import Configuration

from stardis.io.model.marcs import read_marcs_model
Expand All @@ -15,14 +15,18 @@
SCHEMA_PATH = BASE_DIR / "config_schema.yml"


def parse_config_to_model(config_fname):
def parse_config_to_model(config_fname, add_config_keys=None, add_config_vals=None):
"""
Parses the config and model files and outputs python objects to be passed into run stardis so they can be individually modified in python.
Parameters
----------
config_fname : str
Filepath to the STARDIS configuration. Must be a YAML file.
add_config_keys : list, optional
List of additional keys to add or overwrite for the configuration file.
add_config_vals : list, optional
List of corresponding additional values to add to the configuration file.
Returns
-------
Expand All @@ -40,6 +44,23 @@ def parse_config_to_model(config_fname):
except:
raise ValueError("Config failed to validate. Check the config file.")

if (
not add_config_keys
): # If a dictionary was passed, update the config with the dictionary
pass
else:
print("Updating config with additional keys and values")
try:
for key, val in zip(add_config_keys, add_config_vals):
config.set_config_item(key, val)
except:
config.set_config_item(add_config_keys, add_config_vals)

try:
config_dict = validate_dict(config, schemapath=SCHEMA_PATH)
except:
raise ValueError("Additional config keys and values failed to validate.")

adata = AtomData.from_hdf(config.atom_data)

# model
Expand Down
2 changes: 1 addition & 1 deletion stardis/io/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from stardis.io.model.marcs import read_marcs_model
from stardis.io.model.marcs import *
26 changes: 13 additions & 13 deletions stardis/io/model/marcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,43 +167,43 @@ def read_marcs_metadata(fpath, gzipped=True):
"""

METADATA_RE_STR = [
("(.+)\\n", "fname"),
(r"(.+)\n", "fname"),
(
" (\d+\.)\s+Teff \[(.+)\]\.\s+Last iteration; yyyymmdd=\d+",
r" (\d+\.)\s+Teff \[(.+)\]\.\s+Last iteration; yyyymmdd=\d+",
"teff",
"teff_units",
),
(" (\d+\.\d+E\+\d+) Flux \[(.+)\]", "flux", "flux_units"),
(r" (\d+\.\d+E\+\d+) Flux \[(.+)\]", "flux", "flux_units"),
(
" (\d+.\d+E\+\d+) Surface gravity \[(.+)\]",
r" (\d+.\d+E\+\d+) Surface gravity \[(.+)\]",
"surface_grav",
"surface_grav_units",
),
(
" (\d+\.\d+)\W+Microturbulence parameter \[(.+)\]",
r" (\d+\.\d+)\W+Microturbulence parameter \[(.+)\]",
"microturbulence",
"microturbulence_units",
),
(" (\d+\.\d+)\s+(No mass for plane-parallel models)", "plane_parallel_mass"),
(r" (\d+\.\d+)\s+(No mass for plane-parallel models)", "plane_parallel_mass"),
(
" (\+?\-?\d+.\d+) (\+?\-?\d+.\d+) Metallicity \[Fe\/H] and \[alpha\/Fe\]",
r" (\+?\-?\d+.\d+) (\+?\-?\d+.\d+) Metallicity \[Fe\/H] and \[alpha\/Fe\]",
"feh",
"afe",
),
(
" (\d+\.\d+E\+00) (1 cm radius for plane-parallel models)",
r" (\d+\.\d+E\+00) (1 cm radius for plane-parallel models)",
"radius for plane-parallel model",
),
(" (\d.\d+E-\d+) Luminosity \[(.+)\]", "luminosity", "luminosity_units"),
(r" (\d.\d+E-\d+) Luminosity \[(.+)\]", "luminosity", "luminosity_units"),
(
" (\d+.\d+) (\d+.\d+) (\d+.\d+) (\d+.\d+) are the convection parameters: alpha, nu, y and beta",
r" (\d+.\d+) (\d+.\d+) (\d+.\d+) (\d+.\d+) are the convection parameters: alpha, nu, y and beta",
"conv_alpha",
"conv_nu",
"conv_y",
"conv_beta",
),
(
" (0.\d+) (0.\d+) (\d.\d+E-\d+) are X, Y and Z, 12C\/13C=(\d+.?\d+)",
r" (0.\d+) (0.\d+) (\d.\d+E-\d+) are X, Y and Z, 12C\/13C=(\d+.?\d+)",
"x",
"y",
"z",
Expand Down Expand Up @@ -276,15 +276,15 @@ def read_marcs_data(fpath, gzipped=True):
fpath,
skiprows=LINES_BEFORE_UPPER_TABLE,
nrows=MARCS_MODEL_SHELLS,
delim_whitespace=True,
sep=r"\s+",
index_col="k",
)
marcs_model_data_lower_split = pd.read_csv(
fpath,
skiprows=LINES_BEFORE_LOWER_TABLE,
nrows=MARCS_MODEL_SHELLS,
index_col="k",
sep="(?:\s+)|(?<=\+\d{2})(?=-)",
sep=r"(?:\s+)|(?<=\+\d{2})(?=-)",
engine="python",
)

Expand Down
2 changes: 1 addition & 1 deletion stardis/io/model/mesa.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def read_mesa_data(fpath, mesa_shells):
mesa_model = pd.read_csv(
fpath,
skiprows=ROWS_TO_SKIP,
delim_whitespace=True,
sep=r"\s+",
nrows=mesa_shells,
index_col=0,
comment="!",
Expand Down

0 comments on commit 25c14fb

Please sign in to comment.