Skip to content

Commit d0d7b28

Browse files
committed
assign zero for DEM data over ocean
1 parent 8d413ae commit d0d7b28

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

hypergas/dem.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
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
1110
import logging
1211
import rasterio
12+
import numpy as np
1313
from pathlib import Path
14+
from rasterio.transform import from_bounds
1415
from dem_stitcher.stitcher import stitch_dem
16+
from dem_stitcher.exceptions import NoDEMCoverage
1517

1618

1719
LOG = 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)

hypergas/orthorectification.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _get_default_transform(self):
116116
def _download_dem(self):
117117
"""Download GLO-30 DEM data."""
118118
# download DEM data and update config
119-
dem = DEM(self.data.attrs['filename'], self.bounds)
119+
dem = DEM(filename=self.data.attrs['filename'], bounds=self.bounds)
120120
self.file_dem = dem.file_dem
121121
self.dem_name = dem.dem_name
122122
dem.download()

0 commit comments

Comments
 (0)