77# hypergas is a library to retrieve trace gases from hyperspectral satellite data
88"""Download DEM data for the region observed by the hyperspectral satellite."""
99
10- import os
1110import logging
1211import rasterio
12+ import numpy as np
1313from pathlib import Path
14+ from rasterio .transform import from_bounds
1415from dem_stitcher .stitcher import stitch_dem
16+ from dem_stitcher .exceptions import NoDEMCoverage
1517
1618
1719LOG = logging .getLogger (__name__ )
@@ -85,6 +87,30 @@ def __init__(
8587 ymax + buffer ,
8688 ]
8789
90+ def _zero_dem_fallback (self ):
91+ resolution = 1 / 3600 # GLO-30
92+
93+ minx , miny , maxx , maxy = self .bounds
94+
95+ width = int (np .ceil ((maxx - minx ) / resolution ))
96+ height = int (np .ceil ((maxy - miny ) / resolution ))
97+
98+ dem_array = np .zeros ((height , width ), dtype = np .float32 )
99+
100+ transform = from_bounds (minx , miny , maxx , maxy , width , height )
101+
102+ profile = {
103+ "driver" : "GTiff" ,
104+ "dtype" : "float32" ,
105+ "width" : width ,
106+ "height" : height ,
107+ "count" : 1 ,
108+ "crs" : "EPSG:4326" ,
109+ "transform" : transform ,
110+ }
111+
112+ return dem_array , profile
113+
88114 def download (self , overwrite : bool = False ) -> Path :
89115 """
90116 Download the DEM and save it to disk.
@@ -111,12 +137,21 @@ def download(self, overwrite: bool = False) -> Path:
111137 )
112138
113139 # Fetch DEM data
114- dem_array , profile = stitch_dem (
115- self .bounds ,
116- dem_name = self .dem_name ,
117- dst_ellipsoidal_height = self .dst_ellipsoidal_height ,
118- dst_area_or_point = self .dst_area_or_point ,
119- )
140+ try :
141+ dem_array , profile = stitch_dem (
142+ self .bounds ,
143+ dem_name = self .dem_name ,
144+ dst_ellipsoidal_height = self .dst_ellipsoidal_height ,
145+ dst_area_or_point = self .dst_area_or_point ,
146+ )
147+ except (NoDEMCoverage , ValueError , RuntimeError ) as exc :
148+ LOG .warning (
149+ "DEM unavailable for bounds %s. "
150+ "Assuming ocean-only region. Reason: %s" ,
151+ self .bounds ,
152+ exc ,
153+ )
154+ dem_array , profile = self ._zero_dem_fallback ()
120155
121156 # Ensure output directory exists
122157 self .file_dem .parent .mkdir (parents = True , exist_ok = True )
0 commit comments