Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [PEP 440](https://www.python.org/dev/peps/pep-0440/)
and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.3]
* Change "OPERA DEM" references to "Modified Copernicus DEM for NISAR"
* Update Modified Copernicus DEM for NISAR endpoints for v1.2
* Add `fetch.download_file()` exception handling and delete partial downloads

## [0.5.2]

### Fixed
Expand Down
10 changes: 5 additions & 5 deletions src/multirtc/dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


gdal.UseExceptions()
URL = 'https://nisar.asf.earthdatacloud.nasa.gov/STATIC/DEM/v1.1/EPSG4326'
URL = 'https://nisar.asf.earthdatacloud.nasa.gov/NISAR/DEM/v1.2/EPSG4326'
EGM2008_GEOID = {
'WORLD': [
'/vsicurl/https://asf-dem-west.s3.amazonaws.com/GEOID/us_nga_egm2008_1.tif',
Expand Down Expand Up @@ -118,7 +118,7 @@ def check_antimeridean(poly: Polygon) -> list[Polygon]:


def get_dem_granule_url(lat: int, lon: int) -> str:
"""Generate the URL for the OPERA DEM granule based on latitude and longitude.
"""Generate the URL for the Modified Copernicus DEM for NISAR granule based on latitude and longitude.

Args:
lat: Latitude in degrees.
Expand All @@ -133,8 +133,8 @@ def get_dem_granule_url(lat: int, lon: int) -> str:
lon_tens = np.floor_divide(lon, 20) * 20
lon_cardinal = 'W' if lon_tens < 0 else 'E'

prefix = f'{lat_cardinal}{np.abs(lat_tens):02d}_{lon_cardinal}{np.abs(lon_tens):03d}'
filename = f'DEM_{lat_cardinal}{np.abs(lat):02d}_00_{lon_cardinal}{np.abs(lon):03d}_00.tif'
prefix = f'{lat_cardinal}{np.abs(lat_tens):02d}/{lat_cardinal}{np.abs(lat_tens):02d}_{lon_cardinal}{np.abs(lon_tens):03d}'
filename = f'DEM{lat_cardinal}{np.abs(lat):02d}_00_{lon_cardinal}{np.abs(lon):03d}_00_C01.tif'
file_url = f'{URL}/{prefix}/{filename}'
return file_url

Expand All @@ -156,7 +156,7 @@ def get_latlon_pairs(polygon: Polygon) -> list[tuple[float, float]]:

def download_opera_dem_for_footprint(output_path: Path, footprint: Polygon, buffer: float = 0.2) -> None:
"""
Download the OPERA DEM for a given footprint and save it to the specified output path.
Download the Modified Copernicus DEM for NISAR for a given footprint and save it to the specified output path.

Args:
output_path: Path where the DEM will be saved.
Expand Down
25 changes: 17 additions & 8 deletions src/multirtc/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ def download_file(
session.mount('https://', HTTPAdapter(max_retries=retry_strategy))
session.mount('http://', HTTPAdapter(max_retries=retry_strategy))

with session.get(url, stream=True) as s:
download_path = _get_download_path(s.url, s.headers.get('content-disposition'), directory)
s.raise_for_status()
with open(download_path, 'wb') as f:
for chunk in s.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
session.close()
download_path = None
try:
with session.get(url, stream=True) as s:
download_path = _get_download_path(s.url, s.headers.get('content-disposition'), directory)
s.raise_for_status()
with open(download_path, 'wb') as f:
for chunk in s.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)
logging.info(f'Download successful: {url}')
except requests.exceptions.RequestException as e:
logging.exception(f'Download failed: {url}')
if download_path is not None:
download_path.unlink(missing_ok=True) # delete any partial downloads
raise
finally:
session.close()

return str(download_path)
2 changes: 1 addition & 1 deletion tests/test_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def test_get_granule_url():
test_url = 'https://nisar.asf.earthdatacloud.nasa.gov/STATIC/DEM/v1.1/EPSG4326/S10_W020/DEM_S01_00_W001_00.tif'
test_url = 'https://nisar.asf.earthdatacloud.nasa.gov/NISAR/DEM/v1.2/EPSG4326/S10/S10_W020/DEMS01_00_W001_00_C01.tif'
url = dem.get_dem_granule_url(-1, -1)
assert url == test_url

Expand Down