Skip to content

Commit

Permalink
Check if atom data download exists (tardis-sn#2479)
Browse files Browse the repository at this point in the history
* Refactor atom data download function and update
configuration file

* Update import statements in util.py and
shell_info.py

* Fix path concatenation in get_data_dir function

* Fix path issue in get_internal_configuration
function
  • Loading branch information
wkerzendorf authored Nov 29, 2023
1 parent 799e35b commit f7dd18a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
18 changes: 13 additions & 5 deletions tardis/io/atom_data/atom_web_download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import logging

from tardis.io.util import get_internal_data_path, download_from_url
Expand All @@ -21,7 +20,7 @@ def get_atomic_repo_config():
return yaml.load(open(atomic_repo_fname), Loader=yaml.CLoader)


def download_atom_data(atomic_data_name=None):
def download_atom_data(atomic_data_name=None, force_download=False):
"""
Download the atomic data from the repository
Expand All @@ -30,6 +29,9 @@ def download_atom_data(atomic_data_name=None):
atomic_data_name : str
if None
force_download : bool
if True, force download even if file exists
Returns
-------
: None
Expand All @@ -42,10 +44,16 @@ def download_atom_data(atomic_data_name=None):
if atomic_data_name not in atomic_repo:
raise ValueError(f"Atomic Data name {atomic_data_name} not known")

dst_dir = os.path.join(get_data_dir(), f"{atomic_data_name}.h5")
dst_fname = get_data_dir() / f"{atomic_data_name}.h5"

if dst_fname.exists() and not force_download:
logger.warning(
f"Atomic Data {atomic_data_name} already exists in {dst_fname}. Will not download - override with force_download=True."
)
return
src_url = atomic_repo[atomic_data_name]["url"]
mirrors = tuple(atomic_repo[atomic_data_name]["mirrors"])
checksum = atomic_repo[atomic_data_name]["md5"]

logger.info(f"Downloading atomic data from {src_url} to {dst_dir}")
download_from_url(src_url, dst_dir, checksum, mirrors)
logger.info(f"Downloading atomic data from {src_url} to {dst_fname}")
download_from_url(src_url, dst_fname, checksum, mirrors)
28 changes: 13 additions & 15 deletions tardis/io/configuration/config_internal.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from tardis import __path__ as TARDIS_PATH
import os, logging, shutil
import logging, shutil
import yaml
import os
from pathlib import Path

from tardis import __path__ as TARDIS_PATH
from astropy.config import get_config_dir

TARDIS_PATH = TARDIS_PATH[0]
DEFAULT_CONFIG_PATH = os.path.join(
TARDIS_PATH, "data", "default_tardis_internal_config.yml"
)
DEFAULT_DATA_DIR = os.path.join(
os.path.expanduser("~"), "Downloads", "tardis-data"
TARDIS_PATH = Path(TARDIS_PATH[0])
DEFAULT_CONFIG_PATH = (
TARDIS_PATH / "data" / "default_tardis_internal_config.yml"
)

DEFAULT_DATA_DIR = Path(os.path.expanduser("~")) / "Downloads" / "tardis-data"

logger = logging.getLogger(__name__)


def get_internal_configuration():

config_fpath = os.path.join(get_config_dir(), "tardis_internal_config.yml")
config_fpath = Path(get_config_dir()) / "tardis_internal_config.yml"
if not os.path.exists(config_fpath):
logger.warning(
f"Configuration File {config_fpath} does not exist - creating new one from default"
Expand All @@ -27,21 +28,18 @@ def get_internal_configuration():


def get_data_dir():

config = get_internal_configuration()
data_dir = config.get("data_dir", None)
if data_dir is None:
config_fpath = os.path.join(
get_config_dir(), "tardis_internal_config.yml"
)
config_fpath = Path(get_config_dir()) / "tardis_internal_config.yml"
logging.critical(
f"\n{'*' * 80}\n\nTARDIS will download different kinds of data (e.g. atomic) to its data directory {DEFAULT_DATA_DIR}\n\n"
f"TARDIS DATA DIRECTORY not specified in {config_fpath}:\n\n"
f"ASSUMING DEFAULT DATA DIRECTORY {DEFAULT_DATA_DIR}\n "
f"YOU CAN CHANGE THIS AT ANY TIME IN {config_fpath} \n\n"
f"{'*' * 80} \n\n"
)
if not os.path.exists(DEFAULT_DATA_DIR):
if not DEFAULT_DATA_DIR.exists():
os.makedirs(DEFAULT_DATA_DIR)
config["data_dir"] = DEFAULT_DATA_DIR
yaml.dump(config, open(config_fpath, "w"), default_flow_style=False)
Expand Down
2 changes: 1 addition & 1 deletion tardis/visualization/widgets/shell_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from tardis.base import run_tardis
from tardis.io.atom_data.util import download_atom_data
from tardis.io.atom_data.atom_web_download import download_atom_data
from tardis.util.base import (
atomic_number2element_symbol,
species_tuple_to_string,
Expand Down

0 comments on commit f7dd18a

Please sign in to comment.