Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions asf_search/download/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from asf_search.exceptions import ASFAuthenticationError, ASFDownloadError, ASFSearch4xxError
from asf_search import ASFSession

#from pathlib import Path
#import ipywidgets
from tqdm.auto import tqdm

def _download_url(arg):
url, path, session = arg
Expand All @@ -16,11 +19,9 @@ def _download_url(arg):
path=path,
session=session)


def download_urls(urls: Iterable[str], path: str, session: ASFSession = None, processes: int = 1):
"""
Downloads all products from the specified URLs to the specified location.

:param urls: List of URLs from which to download
:param path: Local path in which to save the product
:param session: The session to use, in most cases should be authenticated beforehand
Expand All @@ -41,39 +42,30 @@ def download_urls(urls: Iterable[str], path: str, session: ASFSession = None, pr
pool.join()


def download_url(url: str, path: str, filename: str = None, session: ASFSession = None) -> None:
def download_url(url: str, path: str, filename: str = None, session: ASFSession = None ) -> None:
"""
Downloads a product from the specified URL to the specified location and (optional) filename.

:param url: URL from which to download
:param path: Local path in which to save the product
:param filename: Optional filename to be used, extracted from the URL by default
:param session: The session to use, in most cases should be authenticated beforehand
:return:
"""

if filename is None:
filename = os.path.split(urllib.parse.urlparse(url).path)[1]

if not os.path.isdir(path):
raise ASFDownloadError(f'Error downloading {url}: directory not found: {path}')

if os.path.isfile(os.path.join(path, filename)):
warnings.warn(f'File already exists, skipping download: {os.path.join(path, filename)}')
return

if session is None:
session = ASFSession()


def strip_auth_if_aws(r, *args, **kwargs):
if 300 <= r.status_code <= 399 and 'amazonaws.com' in urllib.parse.urlparse(r.headers['location']).netloc:
location = r.headers['location']
r.headers.clear()
r.headers['location'] = location

response = session.get(url, stream=True, hooks={'response': strip_auth_if_aws})

try:
response.raise_for_status()
except HTTPError as e:
Expand All @@ -82,6 +74,11 @@ def strip_auth_if_aws(r, *args, **kwargs):

raise e

with open(os.path.join(path, filename), 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
#with open(os.path.join(path, filename), 'wb') as f:
with tqdm.wrapattr(open(os.path.join(path, filename),'wb'),
'write', miniters=1,
desc=filename,
total=int(response.headers.get('content-length', 0))) as f:
#for chunk in response.iter_content(chunk_size=8192):
for chunk in response.iter_content(chunk_size=31457280):
f.write(chunk)
31 changes: 31 additions & 0 deletions tests/ProgressBar/test_progressbar_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import asf_search as asf
from datetime import date
import os
from pathlib import Path

download_files = True
path = os.getcwd()

USERNAME = ''
PASSWORD = ''

aoi = 'POLYGON((10.2134 43.957,10.2369 43.957,10.2369 43.9674,10.2134 43.9674,10.2134 43.957))'

opts = {
'platform': asf.PLATFORM.SENTINEL1,
'processingLevel': [asf.PRODUCT_TYPE.SLC],
'relativeOrbit': 168,
'start': date(2021, 6, 1),
'end': date(2022, 1, 31),
#'maxResults': 1
}

results = asf.geo_search(intersectsWith=aoi, **opts)
print(f'Total Images Found: {len(results)}')
print(results)

##### download bar works only if processes = 1:
if download_files == True:
session = asf.ASFSession().auth_with_creds(USERNAME, PASSWORD)
print("download in :",path)
results.download(path = path, session = session, processes = 1 )