From cabe50fc513b1c68a5242c61b922e850aff5a23f Mon Sep 17 00:00:00 2001
From: Jesse Mapel <jam826@nau.edu>
Date: Mon, 28 Oct 2019 09:06:58 -0700
Subject: [PATCH] Improved error reporting and fixed a bug with the kaguyatc
 distortion model (#245)

* Improved error reporting and logging

* Fixed an error where kaguya mi distortion
---
 src/UsgsAstroLsSensorModel.cpp |   6 ++
 src/UsgsAstroPlugin.cpp        | 105 ++++++++++++++++++++++++++++-----
 src/Utilities.cpp              |   8 +--
 3 files changed, 99 insertions(+), 20 deletions(-)

diff --git a/src/UsgsAstroLsSensorModel.cpp b/src/UsgsAstroLsSensorModel.cpp
index 85e06be..0dfbe29 100644
--- a/src/UsgsAstroLsSensorModel.cpp
+++ b/src/UsgsAstroLsSensorModel.cpp
@@ -1920,6 +1920,8 @@ void UsgsAstroLsSensorModel::losToEcf(
          m_startingSample, 0.0,
          m_iTransS, m_iTransL,
          distortedFocalPlaneX, distortedFocalPlaneY);
+   MESSAGE_LOG(m_logger, "losToEcf: distorted focal plane coordinate {} {}",
+                         distortedFocalPlaneX, distortedFocalPlaneY)
 
    // Remove lens
    double undistortedFocalPlaneX, undistortedFocalPlaneY;
@@ -1927,10 +1929,14 @@ void UsgsAstroLsSensorModel::losToEcf(
                     undistortedFocalPlaneX, undistortedFocalPlaneY,
                     m_opticalDistCoeffs,
                     m_distortionType);
+   MESSAGE_LOG(m_logger, "losToEcf: undistorted focal plane coordinate {} {}",
+                         undistortedFocalPlaneX, undistortedFocalPlaneY)
 
   // Define imaging ray (look vector) in camera space
    double cameraLook[3];
    createCameraLookVector(undistortedFocalPlaneX, undistortedFocalPlaneY, m_zDirection, m_focalLength, cameraLook);
+   MESSAGE_LOG(m_logger, "losToEcf: uncorrected camera look vector {} {} {}",
+                         cameraLook[0], cameraLook[1], cameraLook[2])
 
    // Apply attitude correction
    double attCorr[9];
diff --git a/src/UsgsAstroPlugin.cpp b/src/UsgsAstroPlugin.cpp
index 69f1708..71ec8b4 100644
--- a/src/UsgsAstroPlugin.cpp
+++ b/src/UsgsAstroPlugin.cpp
@@ -115,13 +115,38 @@ csm::Version UsgsAstroPlugin::getModelVersion(const std::string &modelName) cons
 bool UsgsAstroPlugin::canModelBeConstructedFromState(const std::string &modelName,
                                                 const std::string &modelState,
                                                 csm::WarningList *warnings) const {
-    try {
-      csm::Model* model = constructModelFromState(modelState, warnings);
-      return (bool)model;
+  try {
+    csm::Model* model = constructModelFromState(modelState, warnings);
+    return (bool)model;
+  }
+  catch(std::exception& e) {
+    if(warnings) {
+      std::string msg = "Could not create model [";
+      msg += modelName;
+      msg += "] with error [";
+      msg += e.what();
+      msg += "]";
+      warnings->push_back(
+        csm::Warning(
+          csm::Warning::UNKNOWN_WARNING,
+          msg,
+          "UsgsAstroFrameSensorModel::canModelBeConstructedFromState()"));
     }
-    catch(...) {
-      return false;
+    return false;
+  }
+  catch(...) {
+    if(warnings) {
+      std::string msg = "Could not create model [";
+      msg += modelName;
+      msg += "] with an unknown error.";
+      warnings->push_back(
+        csm::Warning(
+          csm::Warning::UNKNOWN_WARNING,
+          msg,
+          "UsgsAstroFrameSensorModel::canModelBeConstructedFromState()"));
     }
+  }
+  return false;
 }
 
 
@@ -132,9 +157,33 @@ bool UsgsAstroPlugin::canModelBeConstructedFromISD(const csm::Isd &imageSupportD
     csm::Model* model = constructModelFromISD(imageSupportData, modelName, warnings);
     return (bool)model;
   }
+  catch(std::exception& e) {
+    if(warnings) {
+      std::string msg = "Could not create model [";
+      msg += modelName;
+      msg += "] with error [";
+      msg += e.what();
+      msg += "]";
+      warnings->push_back(
+        csm::Warning(
+          csm::Warning::UNKNOWN_WARNING,
+          msg,
+          "UsgsAstroFrameSensorModel::canModelBeConstructedFromISD()"));
+    }
+  }
   catch(...) {
-    return false;
+    if(warnings) {
+      std::string msg = "Could not create model [";
+      msg += modelName;
+      msg += "] with an unknown error.";
+      warnings->push_back(
+        csm::Warning(
+          csm::Warning::UNKNOWN_WARNING,
+          msg,
+          "UsgsAstroFrameSensorModel::canModelBeConstructedFromISD()"));
+    }
   }
+  return false;
 }
 
 
@@ -158,9 +207,13 @@ std::string UsgsAstroPlugin::loadImageSupportData(const csm::Isd &imageSupportDa
     jsonisd["image_identifier"] = filename;
     return jsonisd.dump();
 
-  } catch (...) {
-    std::string errorMessage = "Could not read metadata file associated with image: ";
-    errorMessage.append(isdFilename);
+  }
+  catch (std::exception& e) {
+    std::string errorMessage = "Could not read metadata file associated with image [";
+    errorMessage += isdFilename;
+    errorMessage += "] with error [";
+    errorMessage += e.what();
+    errorMessage += "]";
     throw csm::Error(csm::Error::FILE_READ, errorMessage,
                      "UsgsAstroPlugin::loadImageSupportData");
   }
@@ -191,8 +244,20 @@ bool UsgsAstroPlugin::canISDBeConvertedToModelState(const csm::Isd &imageSupport
   try {
        convertISDToModelState(imageSupportData, modelName, warnings);
   }
-  catch(...) {
-      return false;
+  catch(std::exception& e) {
+    if(warnings) {
+      std::string msg = "Could not create model [";
+      msg += modelName;
+      msg += "] state with error [";
+      msg += e.what();
+      msg += "]";
+      warnings->push_back(
+        csm::Warning(
+          csm::Warning::UNKNOWN_WARNING,
+          msg,
+          "UsgsAstroFrameSensorModel::canISDBeConvertedToModelState()"));
+    }
+    return false;
   }
   return true;
 }
@@ -227,9 +292,13 @@ csm::Model *UsgsAstroPlugin::constructModelFromISD(const csm::Isd &imageSupportD
           model->getLogger()->info("Constructed model: {}", modelName);
         }
       }
-      catch (...) {
+      catch (std::exception& e) {
         csm::Error::ErrorType aErrorType = csm::Error::SENSOR_MODEL_NOT_CONSTRUCTIBLE;
-        std::string aMessage = "Invalid ISD for Model " + modelName + ": ";
+        std::string aMessage = "Could not construct model [";
+        aMessage += modelName;
+        aMessage += "] with error [";
+        aMessage += e.what();
+        aMessage += "]";
         std::string aFunction = "UsgsAstroPlugin::constructModelFromISD()";
         throw csm::Error(aErrorType, aMessage, aFunction);
       }
@@ -240,9 +309,13 @@ csm::Model *UsgsAstroPlugin::constructModelFromISD(const csm::Isd &imageSupportD
       try {
         model->replaceModelState(model->constructStateFromIsd(stringIsd, warnings));
       }
-      catch (...) {
+      catch (std::exception& e) {
         csm::Error::ErrorType aErrorType = csm::Error::SENSOR_MODEL_NOT_CONSTRUCTIBLE;
-        std::string aMessage = "Invalid ISD for Model " + modelName + ": ";
+        std::string aMessage = "Could not construct model [";
+        aMessage += modelName;
+        aMessage += "] with error [";
+        aMessage += e.what();
+        aMessage += "]";
         std::string aFunction = "UsgsAstroPlugin::constructModelFromISD()";
         throw csm::Error(aErrorType, aMessage, aFunction);
       }
@@ -250,7 +323,7 @@ csm::Model *UsgsAstroPlugin::constructModelFromISD(const csm::Isd &imageSupportD
     }
     else {
       csm::Error::ErrorType aErrorType = csm::Error::SENSOR_MODEL_NOT_SUPPORTED;
-      std::string aMessage = "Model" + modelName + " not supported: ";
+      std::string aMessage = "Model [" + modelName + "] not supported: ";
       std::string aFunction = "UsgsAstroPlugin::constructModelFromISD()";
       throw csm::Error(aErrorType, aMessage, aFunction);
     }
diff --git a/src/Utilities.cpp b/src/Utilities.cpp
index 9b50217..8c8000a 100644
--- a/src/Utilities.cpp
+++ b/src/Utilities.cpp
@@ -843,11 +843,11 @@ std::vector<double> getDistortionCoeffs(json isd, csm::WarningList *list) {
     break;
     case DistortionType::KAGUYATC: {
       try {
-        std::vector<double> coefficientsX(4,0);
-        std::vector<double> coefficientsY(4,0);
 
-        coefficientsX = isd.at("optical_distortion").at("kaguyatc").at("x").get<std::vector<double>>();
-        coefficientsY = isd.at("optical_distortion").at("kaguyatc").at("y").get<std::vector<double>>();
+        std::vector<double> coefficientsX = isd.at("optical_distortion").at("kaguyatc").at("x").get<std::vector<double>>();
+        coefficientsX.resize(4, 0.0);
+        std::vector<double> coefficientsY = isd.at("optical_distortion").at("kaguyatc").at("y").get<std::vector<double>>();
+        coefficientsY.resize(4, 0.0);
         coefficientsX.insert(coefficientsX.end(), coefficientsY.begin(), coefficientsY.end());
 
         return coefficientsX;
-- 
GitLab