Skip to content

Functions

Download model files of a specified model from a remote source.

Parameters:

Name Type Description Default
file Path | str

URL or path to the MLM-STAC JSON metadata file

required
output_dir Path | str

Target directory for downloaded files

required

Returns:

Type Description
ModelLoader

ModelLoader instance pointing to the downloaded model

Raises:

Type Description
RuntimeError

If any of the specified files fail to download

Source code in mlstac/main.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def download(file: Path | str, output_dir: Path | str) -> ModelLoader:
    """
    Download model files of a specified model from a remote source.

    Args:
        file: URL or path to the MLM-STAC JSON metadata file
        output_dir: Target directory for downloaded files

    Returns:
        ModelLoader instance pointing to the downloaded model

    Raises:
        RuntimeError: If any of the specified files fail to download
    """
    output_dir = Path(output_dir)
    output_dir.mkdir(parents=True, exist_ok=True)

    item = load_stac_item(file)

    json_filename = Path(file).name if Path(file).suffix == ".json" else "mlm.json"
    mlm_file = output_dir / json_filename
    with open(mlm_file, "w", encoding="utf-8") as f:
        json.dump(item.to_dict(), f, indent=2, ensure_ascii=False)

    to_download = {}
    for key, value in item.assets.items():
        to_download[key] = value.href

    downloaded_files = []
    for name, source in tqdm(to_download.items(), desc="Downloading files"):
        try:
            file_path = download_file(source=source, outpath=output_dir)
            downloaded_files.append(file_path)
        except Exception as e:
            raise RuntimeError(f"Failed to download {name}: {e!s}") from e

    return ModelLoader(mlm_file.absolute().as_posix())