Skip to content

Commit

Permalink
[ARTS-2.6] Improve catalog downloads (#924)
Browse files Browse the repository at this point in the history
Display a progress bar during download.

When using an unreleased ARTS version, try to download the latest stable
catalogs.
  • Loading branch information
olemke authored Feb 5, 2025
2 parents a093690 + a37cb09 commit 23dafcd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions environment-dev-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
- sphinx
- sphinx-favicon
- sphinx_rtd_theme
- tqdm
- xarray
- zlib
- mesa-libgl-devel-cos7-x86_64
Expand Down
1 change: 1 addition & 0 deletions environment-dev-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
- sphinx
- sphinx-favicon
- sphinx_rtd_theme
- tqdm
- xarray
- zlib
channels:
Expand Down
39 changes: 34 additions & 5 deletions python/pyarts/cat/download.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import os
import urllib.request
import zipfile
from tqdm import tqdm


class _DownloadProgressBar(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
if tsize is not None:
self.total = tsize
return self.update(b * bsize - self.n)


def _download_and_extract(url, extract_dir=".", verbose=False):
Expand All @@ -15,13 +23,27 @@ def _download_and_extract(url, extract_dir=".", verbose=False):
if verbose:
print(f"Downloading {url}")
try:
zip_path, _ = urllib.request.urlretrieve(url)
with _DownloadProgressBar(
unit="B", unit_scale=True, miniters=1, desc=url.split("/")[-1]
) as t:
zip_path, _ = urllib.request.urlretrieve(url, reporthook=t.update_to)
except urllib.request.HTTPError as e:
raise RuntimeError(f"Failed to download {url}: {e}")
with zipfile.ZipFile(zip_path, "r") as f:
f.extractall(extract_dir)


def _get_latest_stable_version(version):
"""
Decrement version to previous stable version.
Parameters:
version (str): Version string in format "major.minor.micro".
"""
major, minor, micro = map(int, version.split("."))
return f"{major}.{minor}.{micro - 1 if micro % 2 else micro}"


def retrieve(download_dir=None, version=None, verbose=False):
"""
Download and extract the ARTS XML and catalog data files from github.
Expand All @@ -45,7 +67,15 @@ def retrieve(download_dir=None, version=None, verbose=False):
"""
if version is None:
from pyarts import __version__
version = __version__

version = _get_latest_stable_version(__version__)
if version != __version__:
if verbose:
print(
f"No downloadable catalogs are available for development versions of ARTS.\n"
f"Downloading the latest stable version {version} of the catalogs instead.\n"
f"Check out the catalogs with svn if you need the latest development version."
)

if download_dir is None:
download_dir = os.path.join(os.getenv("HOME"), ".cache", "arts")
Expand All @@ -61,16 +91,15 @@ def retrieve(download_dir=None, version=None, verbose=False):
if verbose:
print("Skipping download, environment variable ARTS_INCLUDE_PATH already set.")
return
if int(version[-1]) % 2:
raise RuntimeError(f"Version {version} is not a release version.\n"
f"Please check out the current catalogs with svn instead.")

def retrieve_catalog(catname):
if not os.path.exists(os.path.join(download_dir, catname)):
os.makedirs(download_dir, exist_ok=True)
_download_and_extract(GITHUB_URL + catname + ".zip",
download_dir,
verbose=verbose)
elif verbose:
print(f"Skipping download, data already exists at {download_dir}/{catname}")

retrieve_catalog(artsxmldata)
retrieve_catalog(artscatdata)
Expand Down
1 change: 1 addition & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def has_ext_modules(foo):
"numpy>=1.13",
"scipy>=0.15.1",
"setuptools>=0.7.2",
"tqdm",
],
extras_require={
"docs": ["sphinx_rtd_theme"],
Expand Down

0 comments on commit 23dafcd

Please sign in to comment.