Skip to content

Commit

Permalink
FIX: properly implement bearer token authentication and function call…
Browse files Browse the repository at this point in the history
…ing convention for plot_scan_strategy (#653)

(cherry picked from commit a49d120)
  • Loading branch information
kmuehlbauer committed Nov 3, 2023
1 parent 038471c commit 0846d3b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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(
"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 @@ def rebuild_auth(self, request, response):
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 @@ def download_srtm(filename, destination, *, resolution=3):
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 @@ def download_srtm(filename, destination, *, resolution=3):
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()

status_code = 0
try:
r = session.get(url, stream=True, timeout=5)
Expand All @@ -94,6 +102,7 @@ def download_srtm(filename, destination, *, resolution=3):
with open(destination, "wb") as fd:
for chunk in r.iter_content(chunk_size=1024 * 1014):
fd.write(chunk)
del r
except requests.exceptions.HTTPError as err:
status_code = err.response.status_code
if status_code != 404:
Expand Down Expand Up @@ -138,7 +147,7 @@ def get_srtm_tile_names(extent):
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 @@ def get_srtm(extent, *, resolution=3, merge=True):
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 @@ def get_srtm(extent, *, resolution=3, merge=True):
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(
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 @@ def plot_scan_strategy(
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)
# (down-)load srtm data
ds = io.get_srtm(
[ll[..., 0].min(), ll[..., 0].max(), ll[..., 1].min(), ll[..., 1].max()],
Expand Down

0 comments on commit 0846d3b

Please sign in to comment.