|
1 | 1 | """Function to test the CLI"""
|
2 |
| - |
| 2 | +import os |
3 | 3 | import subprocess
|
4 | 4 |
|
| 5 | +import rasterio |
| 6 | + |
5 | 7 | import xdem
|
| 8 | +from xdem import dem_coregistration |
6 | 9 |
|
7 | 10 |
|
8 | 11 | class TestCLI:
|
9 | 12 | # Define paths to the DEM files using xDEM examples
|
10 | 13 | ref_dem_path = xdem.examples.get_path("longyearbyen_ref_dem")
|
11 | 14 | tba_dem_path = xdem.examples.get_path("longyearbyen_tba_dem")
|
| 15 | + aligned_dem_path = "aligned_dem.tiff" |
| 16 | + inlier_mask_path = "inlier_mask.tiff" |
12 | 17 |
|
13 |
| - def test_xdem_cli(self) -> None: |
| 18 | + def test_xdem_cli_coreg(self) -> None: |
14 | 19 | try:
|
15 | 20 | # Run the xDEM CLI command with the reference and secondary DEM files
|
16 | 21 | result = subprocess.run(
|
17 |
| - ["xdem", self.ref_dem_path, self.tba_dem_path], |
| 22 | + ["xdem", "coregister", self.ref_dem_path, self.tba_dem_path], |
18 | 23 | capture_output=True,
|
19 | 24 | text=True,
|
20 | 25 | )
|
21 |
| - assert "hello world" in result.stdout |
| 26 | + |
| 27 | + # Assert ClI ran successfully |
22 | 28 | assert result.returncode == 0
|
23 | 29 |
|
| 30 | + # Verify the existence of the output files |
| 31 | + assert os.path.exists(self.aligned_dem_path), f"Aligned DEM not found: {self.aligned_dem_path}" |
| 32 | + assert os.path.exists(self.inlier_mask_path), f"Inlier mask not found: {self.inlier_mask_path}" |
| 33 | + |
| 34 | + # Retrieve ground truth |
| 35 | + true_coreg_dem, coreg_method, out_stats, true_inlier_mask = dem_coregistration( |
| 36 | + xdem.DEM(self.tba_dem_path), xdem.DEM(self.ref_dem_path), self.aligned_dem_path |
| 37 | + ) |
| 38 | + |
| 39 | + # Load elements processed by the xDEM CLI command |
| 40 | + aligned_dem = xdem.DEM(self.aligned_dem_path) |
| 41 | + with rasterio.open(self.inlier_mask_path) as src: |
| 42 | + inlier_mask = src.read(1) |
| 43 | + |
| 44 | + # Verify match with ground truth |
| 45 | + assert aligned_dem == true_coreg_dem, "Aligned DEM does not match the ground truth." |
| 46 | + assert inlier_mask.all() == true_inlier_mask.all(), "Inlier mask does not match the ground truth." |
| 47 | + |
| 48 | + # Erase files |
| 49 | + os.remove(self.aligned_dem_path) |
| 50 | + os.remove(self.inlier_mask_path) |
| 51 | + |
24 | 52 | except FileNotFoundError as e:
|
25 | 53 | # In case 'xdem' is not found
|
26 | 54 | raise AssertionError(f"CLI command 'xdem' not found : {e}")
|
|
0 commit comments