Skip to content

Commit

Permalink
Allow single file URL inputs for earthaccess.download
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbourbeau committed Nov 10, 2023
1 parent dd55562 commit 0cf7311
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 5 additions & 5 deletions earthaccess/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Type, Union
from typing import Any, Dict, List, Optional, Type, Union, cast

import earthaccess
import requests
Expand Down Expand Up @@ -151,7 +151,7 @@ def login(strategy: str = "all", persist: bool = False) -> Auth:


def download(
granules: Union[DataGranule, List[DataGranule], List[str]],
granules: Union[DataGranule, List[DataGranule], str, List[str]],
local_path: Union[str, None],
provider: Optional[str] = None,
threads: int = 8,
Expand All @@ -162,16 +162,16 @@ def download(
* If we run it outside AWS (us-west-2 region) and the dataset is cloud hostes we'll use HTTP links
Parameters:
granules: a granule, list of granules, or a list of granule links (HTTP)
granules: a granule, list of granules, a granule link (HTTP), or a list of granule links (HTTP)
local_path: local directory to store the remote data granules
provider: if we download a list of URLs we need to specify the provider.
threads: parallel number of threads to use to download the files, adjust as necessary, default = 8
Returns:
List of downloaded files
"""
if isinstance(granules, DataGranule):
granules = [granules]
if isinstance(granules, (DataGranule, str)):
granules = cast(Union[List[DataGranule], List[str]], [granules])
try:
results = earthaccess.__store__.get(granules, local_path, provider, threads)
except AttributeError as err:
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ def test_granules_search_returns_valid_results(kwargs):


@pytest.mark.parametrize("selection", [0, slice(None)])
def test_earthaccess_api_can_download_granules(tmp_path, selection):
@pytest.mark.parametrize("use_url", [True, False])
def test_download(tmp_path, selection, use_url):
results = earthaccess.search_data(
count=2,
short_name="ATL08",
cloud_hosted=True,
bounding_box=(-92.86, 16.26, -91.58, 16.97),
)
if use_url:
# Download via file URL instead of granule
results = [r.data_links(access="indirect") for r in results]
results = sum(results, start=[]) # flatten to a list of strings
result = results[selection]
files = earthaccess.download(result, str(tmp_path))
assertions.assertIsInstance(files, list)
Expand Down

0 comments on commit 0cf7311

Please sign in to comment.