Skip to content

Commit

Permalink
Allow single file URL inputs for earthaccess.download (#347)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Fisher <[email protected]>
  • Loading branch information
jrbourbeau and mfisher87 authored Nov 29, 2023
1 parent 1870e51 commit c37a656
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions earthaccess/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,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 @@ -177,7 +177,7 @@ 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
Expand All @@ -188,6 +188,8 @@ def download(
provider = _normalize_location(provider)
if isinstance(granules, DataGranule):
granules = [granules]
elif isinstance(granules, str):
granules = [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 string instead of DataGranule object
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 c37a656

Please sign in to comment.