Skip to content

Commit

Permalink
acquisition/create ArtticDEM vrt: warn if you stumble upon potentiall…
Browse files Browse the repository at this point in the history
…y wrong inputs
  • Loading branch information
iona5 committed Nov 8, 2024
1 parent 33de863 commit 15d0dde
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion darts-acquisition/src/darts_acquisition/arcticdem.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def download_arcticdem_extend(dem_data_dir: Path):
logger.info(f"Download completed in {time.time() - start:.2f} seconds")


def create_arcticdem_vrt(dem_data_dir: Path, vrt_target_dir: Path):
def create_arcticdem_vrt(dem_data_dir: Path, vrt_target_dir: Path): # noqa: C901
"""Create a VRT file from ArcticDEM data.
Args:
Expand Down Expand Up @@ -82,16 +82,32 @@ def create_arcticdem_vrt(dem_data_dir: Path, vrt_target_dir: Path):
if len(non_writable_files) > 0:
raise OSError(f"cannot write to {', '.join([f.name for f in non_writable_files])}")

# memorize if any files weren't written
file_written = dict.fromkeys(subdirs.keys(), False)

for name, subdir in subdirs.items():
output_file_path = vrt_target_dir / f"{name}.vrt"
# check the file first if we can write to it

ds_path = dem_data_dir / subdir
if not ds_path.exists():
logger.warning(f"{ds_path.absolute()} does NOT exist!")
continue

filelist = [str(f.resolve()) for f in ds_path.glob("*.tif")]
if len(filelist) < 1:
logger.warning(f"NO files found in {ds_path.absolute()}")
continue

logger.debug(f"Found {len(filelist)} files for {name} at {ds_path}.")
logger.debug(f"Writing VRT to '{output_file_path.resolve()}'")
src_nodata = "nan" if name == "slope" else 0
opt = gdal.BuildVRTOptions(srcNodata=src_nodata, VRTNodata=0)
gdal.BuildVRT(str(output_file_path.resolve()), filelist, options=opt)
file_written[name] = True

for name, is_written in file_written.items():
if not is_written:
logger.warning(f"VRT file for {name} was NOT created.")

logger.debug(f"Creation of VRT took {time.time() - start_time:.2f}s")

0 comments on commit 15d0dde

Please sign in to comment.