Skip to content

Commit 0e3609d

Browse files
Merge pull request #49 from forrestfwilliams/develop
Release v0.5.2
2 parents 0a08d8b + 4e6ee8a commit 0e3609d

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [PEP 440](https://www.python.org/dev/peps/pep-0440/)
77
and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## [0.5.2]
10+
11+
### Fixed
12+
* Removed hyp3lib to fix environment build issue related to pinning of GDAL version.
13+
914
## [0.5.1]
1015

1116
### Added

environment.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ dependencies:
1818
- sarpy
1919
- burst2safe
2020
- tqdm
21-
- hyp3lib
21+
- requests
22+
- urllib3
2223
# For multimetric
2324
- matplotlib
2425
- pandas

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ dependencies = [
3131
"sarpy",
3232
"burst2safe",
3333
"tqdm",
34-
"hyp3lib",
34+
"requests",
35+
"urllib3",
3536
"matplotlib",
3637
"pandas",
3738
"lmfit",

src/multirtc/dem.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
import numpy as np
1010
import shapely
11-
from hyp3lib.fetch import download_file
1211
from osgeo import gdal, osr
1312
from shapely.geometry import LinearRing, Polygon, box
1413

14+
from multirtc.fetch import download_file
15+
1516

1617
gdal.UseExceptions()
1718
URL = 'https://nisar.asf.earthdatacloud.nasa.gov/STATIC/DEM/v1.1/EPSG4326'

src/multirtc/fetch.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""Utilities for fetching things from external endpoints. Vendored from hyp3lib"""
2+
3+
import logging
4+
from email.message import Message
5+
from os.path import basename
6+
from pathlib import Path
7+
from urllib.parse import urlparse
8+
9+
import requests
10+
from requests.adapters import HTTPAdapter
11+
from urllib3.util.retry import Retry
12+
13+
14+
def _get_download_path(url: str, content_disposition: str | None = None, directory: Path | str = '.'):
15+
filename = None
16+
if content_disposition is not None:
17+
message = Message()
18+
message['content-type'] = content_disposition
19+
filename = message.get_param('filename')
20+
if not filename:
21+
filename = basename(urlparse(url).path)
22+
if not filename:
23+
raise ValueError(f'could not determine download path for: {url}')
24+
assert isinstance(filename, str)
25+
return Path(directory) / filename
26+
27+
28+
def download_file(
29+
url: str,
30+
directory: Path | str = '.',
31+
chunk_size=None,
32+
retries=2,
33+
backoff_factor=1,
34+
auth: tuple[str, str] | None = None,
35+
token: str | None = None,
36+
) -> str:
37+
"""Download a file
38+
39+
Args:
40+
url: URL of the file to download
41+
directory: Directory location to place files into
42+
chunk_size: Size to chunk the download into
43+
retries: Number of retries to attempt
44+
backoff_factor: Factor for calculating time between retries
45+
auth: Username and password for HTTP Basic Auth
46+
token: Token for HTTP Bearer authentication
47+
48+
Returns:
49+
download_path: The path to the downloaded file
50+
"""
51+
logging.info(f'Downloading {url}')
52+
53+
session = requests.Session()
54+
session.auth = auth
55+
if token:
56+
session.headers.update({'Authorization': f'Bearer {token}'})
57+
58+
retry_strategy = Retry(
59+
total=retries,
60+
backoff_factor=backoff_factor,
61+
status_forcelist=[429, 500, 502, 503, 504],
62+
)
63+
session.mount('https://', HTTPAdapter(max_retries=retry_strategy))
64+
session.mount('http://', HTTPAdapter(max_retries=retry_strategy))
65+
66+
with session.get(url, stream=True) as s:
67+
download_path = _get_download_path(s.url, s.headers.get('content-disposition'), directory)
68+
s.raise_for_status()
69+
with open(download_path, 'wb') as f:
70+
for chunk in s.iter_content(chunk_size=chunk_size):
71+
if chunk:
72+
f.write(chunk)
73+
session.close()
74+
75+
return str(download_path)

0 commit comments

Comments
 (0)