diff --git a/xdem/__init__.py b/xdem/__init__.py index f7de0ef7..749e53ca 100644 --- a/xdem/__init__.py +++ b/xdem/__init__.py @@ -1,3 +1,9 @@ +import logging +import os + +import geoutils +import rasterio + from xdem import ( # noqa coreg, dem, @@ -8,6 +14,7 @@ terrain, volume, ) +from xdem.coreg.workflows import dem_coregistration from xdem.ddem import dDEM # noqa from xdem.dem import DEM # noqa from xdem.demcollection import DEMCollection # noqa @@ -24,8 +31,57 @@ ) -def run(reference_dem: str, dem_to_be_aligned: str, verbose: str) -> None: +def run(ref_dem_path: str, tba_dem_path: str, verbose: str) -> None: """ - Function to compare DEMs + Function to compare and coregister Digital Elevation Models (DEMs). + + This function verifies the existence of the provided DEM paths, + loads the reference DEM and the DEM to be aligned, and performs + coregistration. The aligned DEM and an inlier mask are then saved + to disk. + + :param ref_dem_path: Path to the reference DEM file. + :param tba_dem_path: Path to the DEM that needs to be aligned to the reference. + :param verbose: Verbosity level for logging. Options include "DEBUG", "INFO", "WARNING", "ERROR", and "CRITICAL". + :return: + :raises FileNotFoundError: if the reference DEM or the DEM to be aligned does not exist. """ - print("hello world") + + # Set logging level + logging.basicConfig(level=verbose) + + # Verify that both DEM paths exist + if not os.path.exists(ref_dem_path): + raise FileNotFoundError(f"Reference DEM path does not exist: {ref_dem_path}") + if not os.path.exists(tba_dem_path): + raise FileNotFoundError(f"DEM to be aligned path does not exist: {tba_dem_path}") + + logging.info("Loading DEMs: %s, %s", ref_dem_path, tba_dem_path) + + # Load the reference and secondary DEMs + reference_dem, to_be_aligned_dem = geoutils.raster.load_multiple_rasters([ref_dem_path, tba_dem_path]) + + # Execute coregistration + logging.info("Starting coregistration...") + coreg_dem, coreg_method, out_stats, inlier_mask = dem_coregistration( + to_be_aligned_dem, reference_dem, "aligned_dem.tiff" + ) + + # Save outputs + logging.info("Saving aligned DEM and inlier mask...") + metadata = { + "driver": "GTiff", + "dtype": "uint8", + "count": 1, + "width": coreg_dem.width, + "height": coreg_dem.height, + "crs": coreg_dem.crs, + "transform": coreg_dem.transform, + } + with rasterio.open("inlier_mask.tiff", "w", **metadata) as dst: + dst.write(inlier_mask, 1) + + # Print the coregistration details + print("Coregistration metadata: ", metadata) + print("Coregistration statistics:\n", out_stats) + logging.info("Coregistration completed")