Skip to content

Commit

Permalink
Simplify example data, use pooch for download and cache
Browse files Browse the repository at this point in the history
  • Loading branch information
teutoburg committed Aug 1, 2024
1 parent 1591912 commit 7e9d588
Showing 1 changed file with 30 additions and 42 deletions.
72 changes: 30 additions & 42 deletions scopesim/server/example_data_utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-
"""Store the example data functions here instead of polluting database.py."""

import shutil
from warnings import warn
from pathlib import Path
from typing import Optional, Union
from collections.abc import Iterable

import httpx
import bs4

from astropy.utils.data import download_file
import pooch

from scopesim import rc

Expand Down Expand Up @@ -99,63 +98,52 @@ def print_file_list(the_files, loc=""):
return None


def download_example_data(file_path: Union[Iterable[str], str],
def download_example_data(*files: str,
save_dir: Optional[Union[Path, str]] = None,
url: Optional[str] = None,
from_cache: Optional[bool] = None) -> list[Path]:
url: Optional[str] = None) -> list[Path]:
"""
Download example fits files to the local disk.
Parameters
----------
file_path : str, list
Name(s) of FITS file(s) as given by ``list_example_data()``
files : str(s)
Name(s) of FITS file(s) as given by ``list_example_data()``.
save_dir : str
The place on the local disk where the downloaded files are to be saved.
If left as None, defaults to the current working directory.
If left as None, defaults to the "~.astar/scopesim".
url : str
The URL of the database HTTP server. If left as None, defaults to the
value in scopesim.rc.__config__["!SIM.file.server_base_url"]
from_cache : bool
Use the cached versions of the files. If None, defaults to the RC
value: ``!SIM.file.use_cached_downloads``
Returns
-------
save_path : Path or list of Paths
save_path : list of Paths
The absolute path(s) to the saved files
"""
if isinstance(file_path, Iterable) and not isinstance(file_path, str):
# Recursive
save_path = [download_example_data(thefile, save_dir, url)
for thefile in file_path]
return save_path

if not isinstance(file_path, str):
raise TypeError("file_path must be str or iterable of str, found "
f"{type(file_path) = }")
svrconf = rc.__config__["!SIM.file"]

Check warning on line 125 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L125

Added line #L125 was not covered by tests

if url is None:
url = rc.__config__["!SIM.file.server_base_url"]
if save_dir is None:
save_dir = Path.cwd()
url = url or (svrconf["server_base_url"] + svrconf["example_data_suburl"])
save_dir = save_dir or (Path.home() / ".astar/scopesim")

Check warning on line 128 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L127-L128

Added lines #L127 - L128 were not covered by tests
save_dir = Path(save_dir)
save_dir.mkdir(parents=True, exist_ok=True)
file_path = Path(file_path)

try:
if from_cache is None:
from_cache = rc.__config__["!SIM.file.use_cached_downloads"]
cache_path = download_file(f"{url}example_data/{file_path}",
cache=from_cache)
save_path = save_dir / file_path.name
file_path = shutil.copy2(cache_path, str(save_path))
except httpx.HTTPError as error:
msg = f"Unable to find file: {url + 'example_data/' + file_path}"
raise ValueError(msg) from error

save_path = save_path.absolute()
return save_path
retriever = pooch.create(

Check warning on line 131 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L131

Added line #L131 was not covered by tests
path=save_dir,
base_url=url,
retry_if_failed=3)
registry_file = Path(__file__).parent / svrconf["example_data_hash_file"]
retriever.load_registry(registry_file)

Check warning on line 136 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L135-L136

Added lines #L135 - L136 were not covered by tests

if isinstance(files[0], list): # to preserve combatibility with notebooks
warn("Passing a list to download_example_data is deprecated. "

Check warning on line 139 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L138-L139

Added lines #L138 - L139 were not covered by tests
"Simply pass filenames as *args, i.e. "
"download_example_data(\"foo.fits\", \"bar.fits\").",
DeprecationWarning, stacklevel=2)
files = files[0]

Check warning on line 143 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L143

Added line #L143 was not covered by tests

save_paths = []
for fname in files:
save_paths.append(Path(retriever.fetch(fname, progressbar=True)))

Check warning on line 147 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L145-L147

Added lines #L145 - L147 were not covered by tests

return save_paths

Check warning on line 149 in scopesim/server/example_data_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/server/example_data_utils.py#L149

Added line #L149 was not covered by tests

0 comments on commit 7e9d588

Please sign in to comment.