From ea725408c590f5065fe8802ea928ab523e9b309e Mon Sep 17 00:00:00 2001
From: "Laura, Jason R" <jlaura@usgs.gov>
Date: Fri, 22 Mar 2024 12:16:01 -0700
Subject: [PATCH] Updates how libusgscsm is found

---
 CHANGELOG.md       |  6 +++++-
 knoten/__init__.py | 25 +++++++++++++++----------
 2 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8f7f78b..cb73010 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,8 +35,12 @@ release.
 
 ## Unreleased
 
+### Added
+- A check to `generate_ground_point` when a GeoDataset is used to raise a `ValueError` if the algorithm intersects a no data value in the passed DEM. This ensures that valid heights are used in the intersection computation. Fixes [#120](https://github.com/DOI-USGS/knoten/issues/120)
+
 ### Changed
 - Removed all `pyproj` calls from csm.py, abstracting them into the reprojection and pyproj.Transformer code inside utils.py. Updated the transformations to use the new pipeline style syntax to avoid deprecation warnings about old syntax.
 
 ### Fixed
-- Added a check to `generate_ground_point` when a GeoDataset is used to raise a `ValueError` if the algorithm intersects a no data value in the passed DEM. This ensures that valid heights are used in the intersection computation.
+- The init method that searches for the libusgscsm to support searching in the `csmplugins` subdirectory. This approach depends on being able to find `csmapi` in a standard location and then assumes that the `libusgscsm` shared library is in a subdirectoy of that `lib` directory. Fixes [#118](https://github.com/DOI-USGS/knoten/issues/118)
+
diff --git a/knoten/__init__.py b/knoten/__init__.py
index fc1595c..66169ed 100644
--- a/knoten/__init__.py
+++ b/knoten/__init__.py
@@ -1,20 +1,25 @@
 import ctypes
 from ctypes.util import find_library
-from distutils import sysconfig
+from glob import glob
 import os
 import warnings
 
-from . import csm
-
-from csmapi import csmapi
-
 # Register the usgscam plugin with the csmapi
 libusgscsm_path = find_library('usgscsm')
-
+    
 if not libusgscsm_path:
-    warnings.warn('libusgscsm not installed, unable to load shared library.')
+    libcsmapi_path = find_library('csmapi')
+    usgscsm_folder = os.path.join(os.path.split(libcsmapi_path)[0], "csmplugins")
+    libusgscsm_path = ""
+    if os.path.exists(usgscsm_folder):
+        results = glob("*[0-9].[0-9].[0-9].dylib", root_dir=usgscsm_folder)
+        results.sort()
+        libusgscsm_path = os.path.join(usgscsm_folder, results[-1])
 
-libusgscsm = ctypes.CDLL(libusgscsm_path)
+if not os.path.exists(libusgscsm_path):
+    warnings.warn('libusgscsm not installed, unable to find shared library.')
 
-if not libusgscsm._name:
-    warnings.warn('Unable to load usgscsm shared library')
+try:
+    libusgscsm = ctypes.CDLL(libusgscsm_path)
+except OSError:
+    warnings.warn(f'Unable to load usgscsm shared library at {libusgscsm_path}')
-- 
GitLab