Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for out_dem_gsd filename assumption #77

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ Options:
be generated. Default: None.
--stereo_directory TEXT Required directory of stereo files. Default:
stereo.
--dem_filename TEXT Optional DEM filename in the stereo
directory. Default: None, which will search
for the *-DEM.tif file in the stereo
directory. Specify it as the basename with
extension, e.g. my-custom-dem-name.tif.
--dem_gsd TEXT Optional ground sample distance of the DEM.
Default: None, which will search for the
*-DEM.tif file in the stereo directory. If
there is a GSD in the name of the file,
specify it here as a float or integer, e.g.
1, 1.5, etc.
--map_crs TEXT Projection for ICESat and bundle adjustment
plots. Default: None.
--reference_dem TEXT Optional reference DEM used in ASP
Expand Down Expand Up @@ -196,7 +207,13 @@ $ cd asp_plot
$ conda env create -f environment.yml
$ conda activate asp_plot
$ pip install -e .
$ python3 setup.py install
```

If you want to rebuild the package, for instance while testing changes to the CLI tool, then uninstall and reinstall via:

```
$ pip uninstall asp_plot
$ pip install -e .
```

### Run tests
Expand Down
19 changes: 17 additions & 2 deletions asp_plot/cli/asp_plot.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@
default="stereo",
help="Required directory of stereo files. Default: stereo.",
)
@click.option(
"--dem_filename",
prompt=False,
default=None,
help="Optional DEM filename in the stereo directory. Default: None, which will search for the *-DEM.tif file in the stereo directory. Specify it as the basename with extension, e.g. my-custom-dem-name.tif.",
)
@click.option(
"--dem_gsd",
prompt=False,
default=None,
help="Optional ground sample distance of the DEM. Default: None, which will search for the *-DEM.tif file in the stereo directory. If there is a GSD in the name of the file, specify it here as a float or integer, e.g. 1, 1.5, etc.",
)
@click.option(
"--map_crs",
prompt=False,
Expand Down Expand Up @@ -72,6 +84,8 @@ def main(
directory,
bundle_adjust_directory,
stereo_directory,
dem_filename,
dem_gsd,
map_crs,
reference_dem,
add_basemap,
Expand All @@ -85,7 +99,7 @@ def main(
os.makedirs(plots_directory, exist_ok=True)

if report_filename is None:
directory_name = os.path.split(directory.rstrip('/\\'))[-1]
directory_name = os.path.split(directory.rstrip("/\\"))[-1]
report_filename = f"asp_plot_report_{directory_name}.pdf"
report_pdf_path = os.path.join(directory, report_filename)

Expand Down Expand Up @@ -113,7 +127,8 @@ def main(
directory,
stereo_directory,
reference_dem=reference_dem,
out_dem_gsd=1,
dem_fn=dem_filename,
dem_gsd=dem_gsd,
title="Hillshade with details",
)

Expand Down
34 changes: 25 additions & 9 deletions asp_plot/stereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@

class StereoPlotter(Plotter):
def __init__(
self, directory, stereo_directory, reference_dem=None, out_dem_gsd=1, **kwargs
self,
directory,
stereo_directory,
dem_gsd=None,
dem_fn=None,
reference_dem=None,
**kwargs,
):
super().__init__(**kwargs)
self.directory = directory
Expand All @@ -40,8 +46,6 @@ def __init__(
if self.reference_dem:
print(f"\nReference DEM: {self.reference_dem}\n")

self.out_dem_gsd = out_dem_gsd

self.full_directory = os.path.join(self.directory, self.stereo_directory)
self.left_ortho_sub_fn = glob_file(self.full_directory, "*-L_sub.tif")
self.right_ortho_sub_fn = glob_file(self.full_directory, "*-R_sub.tif")
Expand All @@ -50,14 +54,26 @@ def __init__(
self.disparity_sub_fn = glob_file(self.full_directory, "*-D_sub.tif")
self.disparity_fn = glob_file(self.full_directory, "*-D.tif")

self.dem_fn = glob_file(
self.full_directory,
f"*-DEM_{self.out_dem_gsd}m.tif",
f"*{self.out_dem_gsd}m-DEM.tif",
)
self.dem_gsd = dem_gsd

if not dem_fn:
if self.dem_gsd is not None:
self.dem_fn = glob_file(
self.full_directory,
f"*-DEM_{self.dem_gsd}m.tif",
f"*{self.dem_gsd}m-DEM.tif",
)
else:
self.dem_fn = glob_file(
self.full_directory,
"*-DEM.tif",
)
else:
self.dem_fn = glob_file(self.full_directory, dem_fn)

if not self.dem_fn:
raise ValueError(
f"\n\nNo DEM found in {self.full_directory} with GSD {self.out_dem_gsd} m. Please run stereo processing with the desired output DEM GSD or correct your inputs here.\n\n"
f"\n\nDEM file not found in {self.full_directory}. Make sure it is there and possibly specify the GSD with the dem_gsd argument.\n\n"
)
else:
print(f"\nASP DEM: {self.dem_fn}\n")
Expand Down
8 changes: 5 additions & 3 deletions notebooks/stereo_plots.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -51,9 +51,11 @@
"plotter = StereoPlotter(\n",
" directory, \n",
" stereo_directory,\n",
" # optional:\n",
"\n",
" # optional args:\n",
" # dem_fn=\"my-custom-dem-name.tif\"\n",
" dem_gsd=1\n",
" # reference_dem=reference_dem,\n",
" out_dem_gsd=1\n",
")"
]
},
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "asp_plot"
version = "0.5.5"
version = "0.5.6"
authors = [
{ name="Ben Purinton", email="[email protected]" },
]
Expand Down
31 changes: 29 additions & 2 deletions tests/test_stereo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def stereo_plotter(self):
stereo_plotter = StereoPlotter(
directory="tests/test_data",
stereo_directory="stereo",
dem_gsd=1,
reference_dem="tests/test_data/ref_dem.tif",
out_dem_gsd=1,
title="Stereo Results",
)
return stereo_plotter
Expand All @@ -23,7 +23,28 @@ def stereo_plotter_no_ref_dem(self):
stereo_plotter = StereoPlotter(
directory="tests/test_data",
stereo_directory="stereo",
out_dem_gsd=1,
dem_gsd=1,
title="Stereo Results",
)
return stereo_plotter

@pytest.fixture
def stereo_plotter_no_gsd(self):
stereo_plotter = StereoPlotter(
directory="tests/test_data",
stereo_directory="stereo",
reference_dem="tests/test_data/ref_dem.tif",
title="Stereo Results",
)
return stereo_plotter

@pytest.fixture
def stereo_plotter_dem_fn(self):
stereo_plotter = StereoPlotter(
directory="tests/test_data",
stereo_directory="stereo",
reference_dem="tests/test_data/ref_dem.tif",
dem_fn="date_time_left_right_1m-DEM.tif",
title="Stereo Results",
)
return stereo_plotter
Expand Down Expand Up @@ -54,3 +75,9 @@ def test_plot_detailed_hillshade(self, stereo_plotter):

def test_instantiate_without_reference_dem(self, stereo_plotter_no_ref_dem):
assert stereo_plotter_no_ref_dem.reference_dem is not None

def test_instantiate_without_gsd(self, stereo_plotter_no_gsd):
assert stereo_plotter_no_gsd.dem_fn is not None

def test_instantiate_with_dem_fn(self, stereo_plotter_dem_fn):
assert stereo_plotter_dem_fn.dem_fn is not None