Skip to content

Commit

Permalink
Renamed the spec_res parameter in add_model to wavel_sampling, deprec…
Browse files Browse the repository at this point in the history
…ated the smooth parameter in ReadModel and ReadPlanck since the spec_res parameter is sufficient, renamed the get_spec_res method in ReadModel to get_sampling, moved the convert_model_name function to the model_util module
  • Loading branch information
tomasstolker committed Feb 20, 2024
1 parent 4e1fa87 commit 2416d7c
Show file tree
Hide file tree
Showing 20 changed files with 455 additions and 468 deletions.
53 changes: 24 additions & 29 deletions species/core/species_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ def __init__(self) -> None:
print(species_msg)
print(len(species_msg) * "=")

working_folder = os.path.abspath(os.getcwd())
print(f"\nWorking folder: {working_folder}")

config_file = os.path.join(working_folder, "species_config.ini")

try:
pypi_url = "https://pypi.org/pypi/species/json"

Expand All @@ -55,12 +50,20 @@ def __init__(self) -> None:
latest_version = None

if latest_version is not None and species_version != latest_version:
print(f" -> A new version ({latest_version}) is available!")
print(f"\n -> A new version ({latest_version}) is available!")
print(" -> It is recommended to update to the latest version")
print(" -> See https://github.com/tomasstolker/species for details")

if not os.path.isfile(config_file):
print("Creating species_config.ini...", end="", flush=True)
working_folder = os.path.abspath(os.getcwd())
print(f"\nWorking folder: {working_folder}")

config_file = os.path.join(working_folder, "species_config.ini")

if os.path.isfile(config_file):
print(f"\nConfiguration file: {config_file}")

else:
print("\nCreating species_config.ini...", end="", flush=True)

with open(config_file, "w", encoding="utf-8") as file_obj:
file_obj.write("[species]\n\n")
Expand Down Expand Up @@ -102,46 +105,38 @@ def __init__(self) -> None:
file_obj.write("\n; Folder where data will be downloaded\n")
file_obj.write("data_folder = ./data/\n")

# if "interp_method" in config["species"]:
# interp_method = config["species"]["interp_method"]
#
# else:
# interp_method = "linear"
#
# with open(config_file, "a", encoding="utf-8") as file_obj:
# file_obj.write("\n; Method for the grid interpolation\n")
# file_obj.write(
# "; Options: linear, nearest, slinear, " "cubic, quintic, pchip\n"
# )
# file_obj.write("interp_method = linear\n")

if "vega_mag" in config["species"]:
vega_mag = config["species"]["vega_mag"]

else:
vega_mag = 0.03

with open(config_file, "a", encoding="utf-8") as file_obj:
file_obj.write("\n; Magnitude of Vega for all filters\n")
file_obj.write("; Magnitude of Vega for all filters\n")
file_obj.write("vega_mag = 0.03\n")

print("\nConfiguration settings:")
print(f" - Database: {database_file}")
print(f" - Data folder: {data_folder}")
# print(f" - Interpolation method: {interp_method}")
print(f" - Magnitude of Vega: {vega_mag}")
if os.path.isfile(database_file):
print(f"Database file: {database_file}")

if not os.path.isfile(database_file):
else:
print("Creating species_database.hdf5...", end="", flush=True)
h5_file = h5py.File(database_file, "w")
h5_file.close()
print(" [DONE]")

if not os.path.exists(data_folder):
if os.path.exists(data_folder):
print(f"Data folder: {data_folder}")

else:
print("Creating data folder...", end="", flush=True)
os.makedirs(data_folder)
print(" [DONE]")

print("\nConfiguration settings:")
print(f" - Database: {database_file}")
print(f" - Data folder: {data_folder}")
print(f" - Magnitude of Vega: {vega_mag}")

if find_spec("mpi4py") is None:
print("\nMultiprocessing: mpi4py not installed")

Expand Down
88 changes: 41 additions & 47 deletions species/data/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ def available_models(self, verbose: bool = False) -> Dict:
f" - Wavelength range (um): {model_dict['wavelength range']}"
)

if "resolution" in model_dict:
if "lambda/d_lambda" in model_dict:
print(
f" - Resolution lambda/Dlambda: {model_dict['resolution']}"
f" - Sampling (lambda/d_lambda): {model_dict['lambda/d_lambda']}"
)

if "information" in model_dict:
Expand Down Expand Up @@ -644,21 +644,25 @@ def add_model(
self,
model: str,
wavel_range: Optional[Tuple[float, float]] = None,
spec_res: Optional[float] = None,
wavel_sampling: Optional[float] = None,
teff_range: Optional[Tuple[float, float]] = None,
unpack_tar: bool = True,
) -> None:
"""
Function for adding a grid of model spectra to the database.
All spectra have been resampled to logarithmically-spaced
wavelengths. The spectral resolution is returned with the
:func:`~species.read.read_model.ReadModel.get_spec_res`
method of :class:`~species.read.read_model.ReadModel`, but
is typically of the order of several thousand.
It should be noted that the original spectra were often
calculated with a constant step size in wavenumber,
so the original spectral resolution decreased from short
to long wavelengths.
wavelengths (see
:func:`~species.data.database.Database.available_models`),
typically at the order of several thousand. It should be
noted that the original spectra were typically calculated
with a constant step size in wavenumber, so the original
wavelength sampling decreased from short to long wavelengths.
When fitting medium/high- resolution spectra, it is best to
carefully check the result to determine if the sampling of
the input grid was sufficient for modeling the spectra at
the considered wavelength regime. See also
:func:`~species.data.database.Database.add_custom_model`
for adding a custom grid to the database.
Parameters
----------
Expand All @@ -676,12 +680,13 @@ def add_model(
Wavelength range (um) for adding a subset of the spectra.
The full wavelength range is used if the argument is set
to ``None``.
spec_res : float, None
Spectral resolution to which the spectra will be resampled.
This parameter is optional since the spectra have already
been resampled to a lower, constant resolution (typically
:math:`R = 5000`). The argument is only used if
``wavel_range`` is not ``None``.
wavel_sampling : float, None
Wavelength spacing :math:`\\lambda/\\Delta\\lambda` to which
the spectra will be resampled. Typically this parameter is
not needed so the argument can be set to ``None``. The only
benefit of using this parameter is limiting the storage
in the HDF5 database. The parameter should be used in
combination with setting the ``wavel_range``.
teff_range : tuple(float, float), None
Range of effective temperatures (K) of which the spectra
are extracted from the TAR file and added to the HDF5
Expand All @@ -706,17 +711,14 @@ def add_model(
from species.data.model_data.model_spectra import add_model_grid

with h5py.File(self.database, "a") as hdf5_file:
if "models" not in hdf5_file:
hdf5_file.create_group("models")

add_model_grid(
model,
self.data_folder,
hdf5_file,
wavel_range,
teff_range,
spec_res,
unpack_tar,
model_tag=model,
input_path=self.data_folder,
database=hdf5_file,
wavel_range=wavel_range,
teff_range=teff_range,
wavel_sampling=wavel_sampling,
unpack_tar=unpack_tar,
)

@typechecked
Expand All @@ -726,7 +728,7 @@ def add_custom_model(
data_path: str,
parameters: List[str],
wavel_range: Optional[Tuple[float, float]] = None,
spec_res: Optional[float] = None,
wavel_sampling: Optional[float] = None,
teff_range: Optional[Tuple[float, float]] = None,
) -> None:
"""
Expand All @@ -743,9 +745,9 @@ def add_custom_model(
should contain the same number and values of wavelengths. The
wavelengths should be logarithmically sampled, so at a constant
resolution, :math:`\\lambda/\\Delta\\lambda`. If not, then the
``wavel_range`` and ``spec_res`` parameters should be used
such that the wavelengths are resampled when reading the data
into the ``species`` database.
``wavel_range`` and ``wavel_sampling`` parameters should be
used such that the wavelengths are resampled when reading the
data into the ``species`` database.
Parameters
----------
Expand All @@ -768,14 +770,13 @@ def add_custom_model(
Wavelength range (:math:`\\mu\\text{m}`) for adding a
subset of the spectra. The full wavelength range is
used if the argument is set to ``None``.
spec_res : float, None
Spectral resolution to which the spectra will be resampled.
This parameter should be used in combination with
``wavel_range`` if the input spectra at ``data_path``
are not sampled at a constant
:math:`\\lambda/\\Delta\\lambda`. The argument is
only used if ``wavel_range`` is not ``None`` and it is
not used if set to ``None``.
wavel_sampling : float, None
Wavelength spacing :math:`\\lambda/\\Delta\\lambda` to which
the spectra will be resampled. Typically this parameter is
not needed so the argument can be set to ``None``. The only
benefit of using this parameter is limiting the storage
in the HDF5 database. The parameter should be used in
combination with setting the ``wavel_range``.
teff_range : tuple(float, float), None
Effective temperature range (K) for adding a subset of the
model grid. The full parameter grid will be added if the
Expand All @@ -790,17 +791,14 @@ def add_custom_model(
from species.data.model_data.custom_model import add_custom_model_grid

with h5py.File(self.database, "a") as hdf5_file:
if "models" not in hdf5_file:
hdf5_file.create_group("models")

add_custom_model_grid(
model,
data_path,
parameters,
hdf5_file,
wavel_range,
teff_range,
spec_res,
wavel_sampling,
)

@typechecked
Expand Down Expand Up @@ -2347,7 +2345,6 @@ def get_mcmc_spectra(
specbox = readmodel.get_spectrum(
model_param,
spec_res,
smooth=True,
wavel_resample=wavel_resample,
)

Expand All @@ -2372,7 +2369,6 @@ def get_mcmc_spectra(
param_0,
spec_res=spec_res,
wavel_resample=wavel_resample,
smooth=True,
ext_filter=ext_filter,
)

Expand All @@ -2382,7 +2378,6 @@ def get_mcmc_spectra(
param_1,
spec_res=spec_res,
wavel_resample=wavel_resample,
smooth=True,
ext_filter=ext_filter,
)

Expand All @@ -2405,7 +2400,6 @@ def get_mcmc_spectra(
model_param,
spec_res=spec_res,
wavel_resample=wavel_resample,
smooth=True,
ext_filter=ext_filter,
)

Expand Down
33 changes: 16 additions & 17 deletions species/data/model_data/custom_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def add_custom_model_grid(
database: h5py._hl.files.File,
wavel_range: Optional[Tuple[float, float]],
teff_range: Optional[Tuple[float, float]],
spec_res: Optional[float],
wavel_sampling: Optional[float],
) -> None:
"""
Function for adding a custom grid of model spectra to the
Expand All @@ -38,9 +38,9 @@ def add_custom_model_grid(
two columns with the wavelengths in :math:`\\mu\\text{m}` and
the fluxes in :math:`\\text{W} \\text{m}^{-2} \\mu\\text{m}^{-1}`.
Each file should contain the same number and values of wavelengths.
The wavelengths should be logarithmically sampled, so at a constant
resolution, :math:`\\lambda/\\Delta\\lambda`. If not, then the
``wavel_range`` and ``spec_res`` parameters should be used
The wavelengths should be logarithmically sampled, so with a
constant :math:`\\lambda/\\Delta\\lambda`. If not, then the
``wavel_range`` and ``wavel_sampling`` parameters should be used
such that the wavelengths are resampled when reading the data
into the ``species`` database.
Expand Down Expand Up @@ -70,14 +70,13 @@ def add_custom_model_grid(
Effective temperature range (K) for adding a subset of the
model grid. The full parameter grid will be added if the
argument is set to ``None``.
spec_res : float, None
Spectral resolution to which the spectra will be resampled.
This parameter should be used in combination with
``wavel_range`` if the input spectra at ``data_path``
are not sampled at a constant
:math:`\\lambda/\\Delta\\lambda`. The argument is
only used if ``wavel_range`` is not ``None`` and it is
not used if set to ``None``.
wavel_sampling : float, None
Wavelength spacing :math:`\\lambda/\\Delta\\lambda` to which
the spectra will be resampled. Typically this parameter is
not needed so the argument can be set to ``None``. The only
benefit of using this parameter is limiting the storage
in the HDF5 database. The parameter should be used in
combination with setting the ``wavel_range``.
Returns
-------
Expand Down Expand Up @@ -124,11 +123,11 @@ def add_custom_model_grid(
if wavel_range is not None:
print(f"Wavelength range (um) = {wavel_range[0]} - {wavel_range[1]}")

if spec_res is not None:
wavelength = create_wavelengths(wavel_range, spec_res)
print(f"Spectral resolution = {spec_res}")
if wavel_sampling is not None:
wavelength = create_wavelengths(wavel_range, wavel_sampling)
print(f"Sampling (lambda/d_lambda) = {wavel_sampling}")

if wavel_range is None or spec_res is None:
if wavel_range is None or wavel_sampling is None:
wavelength = None
wavel_select = None

Expand Down Expand Up @@ -186,7 +185,7 @@ def add_custom_model_grid(
os.path.join(data_path, filename), unpack=True
)

if wavel_range is not None and spec_res is not None:
if wavel_range is not None and wavel_sampling is not None:
flux_resample = spectres.spectres(
wavelength,
data_wavel,
Expand Down
Loading

0 comments on commit 2416d7c

Please sign in to comment.