diff --git a/CHANGELOG.md b/CHANGELOG.md index 1aa7a59..89781fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/multirtc/dem.py b/src/multirtc/dem.py index f071570..c7e818f 100644 --- a/src/multirtc/dem.py +++ b/src/multirtc/dem.py @@ -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', @@ -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. @@ -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 @@ -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. diff --git a/src/multirtc/fetch.py b/src/multirtc/fetch.py index 2f50081..1719f56 100644 --- a/src/multirtc/fetch.py +++ b/src/multirtc/fetch.py @@ -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) diff --git a/tests/test_dem.py b/tests/test_dem.py index f0c3576..3a782b8 100644 --- a/tests/test_dem.py +++ b/tests/test_dem.py @@ -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