Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: properly implement bearer token authentication and function calling convention for plot_scan_strategy #653

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ You can install the latest {{wradlib}} release from PyPI via ``$ python -m pip i
* FIX: enable radolan backend to work without GDAL/pyproj, by falling back to trigonometric calculations ({issue}`648`, {pull}`649`) by {at}`kmuehlbauer`
* FIX: update util.cross_section_ppi to work with wradlib 2.0 (RadarVolume no longer available) ({pull}`650`) by {at}`JulianGiles`
* FIX: disentangle cg/normal plotting for better maintainability and apply explicit colorbar handling for cg plotting ({pull}`652`) by {at}`kmuehlbauer`
* FIX: properly implement bearer token authentication adn function calling convention for plot_scan_strategy with terrain=True ({issue}`651`) by {at}`JulianGiles`, ({pull}`652`) by {at}`kmuehlbauer`



## Version 2.0.0
Expand Down
40 changes: 27 additions & 13 deletions wradlib/io/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@
requests = util.import_optional("requests")


def init_header_redirect_session(token):
def init_header_redirect_session(token=None):
token = os.environ.get("WRADLIB_EARTHDATA_BEARER_TOKEN", token)
if token is None:
raise ValueError(

Check warning on line 34 in wradlib/io/dem.py

View check run for this annotation

Codecov / codecov/patch

wradlib/io/dem.py#L34

Added line #L34 was not covered by tests
"WRADLIB_EARTHDATA_BEARER_TOKEN environment variable missing. "
"Downloading SRTM data requires a NASA Earthdata Account and Bearer Token. "
"To obtain a NASA Earthdata Login account, "
"please visit https://urs.earthdata.nasa.gov/users/new/."
)

class HeaderRedirection(requests.Session):
AUTH_HOST = "urs.earthdata.nasa.gov"

Expand All @@ -53,7 +62,7 @@
return HeaderRedirection(token)


def download_srtm(filename, destination, *, resolution=3):
def download_srtm(filename, destination, *, resolution=3, session=None):
"""
Download NASA SRTM elevation data
Only available with login/password
Expand All @@ -64,8 +73,13 @@
srtm file to download
destination : str
output filename

Keyword Arguments
-----------------
resolution : int
resolution of SRTM data (1, 3 or 30)
session : object
session object to use
"""

website = "https://e4ftl01.cr.usgs.gov/MEASURES"
Expand All @@ -75,16 +89,10 @@
resolution = f"SRTMGL{resolution}.00{subres}"
source = "/".join([website, resolution, "2000.02.11"])
url = "/".join([source, filename])
token = os.environ.get("WRADLIB_EARTHDATA_BEARER_TOKEN", None)

if token is None:
raise ValueError(
"WRADLIB_EARTHDATA_BEARER_TOKEN environment variable missing. "
"Downloading SRTM data requires a NASA Earthdata Account and Bearer Token. "
"To obtain a NASA Earthdata Login account, "
"please visit https://urs.earthdata.nasa.gov/users/new/."
)
session = init_header_redirect_session(token)
if session is None:
session = init_header_redirect_session()

Check warning on line 94 in wradlib/io/dem.py

View check run for this annotation

Codecov / codecov/patch

wradlib/io/dem.py#L93-L94

Added lines #L93 - L94 were not covered by tests

status_code = 0
try:
r = session.get(url, stream=True, timeout=5)
Expand All @@ -94,6 +102,7 @@
with open(destination, "wb") as fd:
for chunk in r.iter_content(chunk_size=1024 * 1014):
fd.write(chunk)
del r

Check warning on line 105 in wradlib/io/dem.py

View check run for this annotation

Codecov / codecov/patch

wradlib/io/dem.py#L105

Added line #L105 was not covered by tests
except requests.exceptions.HTTPError as err:
status_code = err.response.status_code
if status_code != 404:
Expand Down Expand Up @@ -138,7 +147,7 @@
return filelist


def get_srtm(extent, *, resolution=3, merge=True):
def get_srtm(extent, *, resolution=3, merge=True, session=None):
"""
Get NASA SRTM elevation data

Expand All @@ -150,6 +159,8 @@
resolution of SRTM data (1, 3 or 30)
merge : bool
True to merge the tiles in one dataset
session : object
session object to use

Returns
-------
Expand All @@ -163,11 +174,14 @@
if not os.path.exists(srtm_path):
os.makedirs(srtm_path)
demlist = []
session = init_header_redirect_session()
for filename in filelist:
path = os.path.join(srtm_path, filename)
status_code = 0
if not os.path.exists(path):
status_code = download_srtm(filename, path, resolution)
status_code = download_srtm(

Check warning on line 182 in wradlib/io/dem.py

View check run for this annotation

Codecov / codecov/patch

wradlib/io/dem.py#L182

Added line #L182 was not covered by tests
filename, path, resolution=resolution, session=session
)
if status_code == 0:
demlist.append(path)

Expand Down
2 changes: 1 addition & 1 deletion wradlib/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
add_title = ""
if terrain is True:
add_title += f" - Azimuth {az[0]}°"
ll = georef.reproject(xyz, projection_source=rad)
ll = georef.reproject(xyz, src_crs=rad)

Check warning on line 446 in wradlib/vis.py

View check run for this annotation

Codecov / codecov/patch

wradlib/vis.py#L446

Added line #L446 was not covered by tests
# (down-)load srtm data
ds = io.get_srtm(
[ll[..., 0].min(), ll[..., 0].max(), ll[..., 1].min(), ll[..., 1].max()],
Expand Down