From 2d9daedb3f30db65984d8ee999b683b28f1d19ba Mon Sep 17 00:00:00 2001 From: Kaitlyn Lee Date: Thu, 6 Feb 2020 10:09:04 -0700 Subject: [PATCH] Abstracted campt app. --- isis/src/base/apps/campt/campt.cpp | 263 ++++++++++++++++++++++++++++ isis/src/base/apps/campt/campt.h | 12 ++ isis/src/base/apps/campt/main.cpp | 267 ++--------------------------- 3 files changed, 289 insertions(+), 253 deletions(-) create mode 100644 isis/src/base/apps/campt/campt.cpp create mode 100644 isis/src/base/apps/campt/campt.h diff --git a/isis/src/base/apps/campt/campt.cpp b/isis/src/base/apps/campt/campt.cpp new file mode 100644 index 0000000000..fa81429cc9 --- /dev/null +++ b/isis/src/base/apps/campt/campt.cpp @@ -0,0 +1,263 @@ +#include "campt.h" + +#include +#include + +#include "Brick.h" +#include "Camera.h" +#include "CameraPointInfo.h" +#include "CSVReader.h" +#include "Distance.h" +#include "IException.h" +#include "iTime.h" +#include "Longitude.h" +#include "Progress.h" +#include "PvlGroup.h" +#include "SpecialPixel.h" +#include "TProjection.h" + +using namespace std; +using namespace Isis; + +namespace Isis{ + + QList< QPair > getPoints(const UserInterface &ui, bool usePointList); + QList getCameraPointInfo(const UserInterface &ui, + QList< QPair > points, + CameraPointInfo &campt); + void writePoints(const UserInterface &ui, QList camPoints, Pvl *log); + + void campt(UserInterface &ui, Pvl *log) { + // Setup our input cube + CameraPointInfo campt; + + QString fileFormat = ui.GetString("FORMAT"); + if(fileFormat=="PVL") + campt.SetCSVOutput(false); + else + campt.SetCSVOutput(true); + + campt.SetCube( ui.GetFileName("FROM") + "+" + ui.GetInputAttribute("FROM").toString() ); + + // Grab the provided points (coordinates) + QList< QPair > points = getPoints(ui, ui.WasEntered("COORDLIST")); + + // Get the camera point info for coordiante + QList camPoints = getCameraPointInfo(ui, points, campt); + + writePoints(ui, camPoints, log); + } + + + // We can grab our coordinates, either from the ui position parameters or the coordlist parameter + // This method returns a list of double pairs (i.e. a list of coordinates) + QList< QPair > getPoints(const UserInterface &ui, bool usePointList) { + double point1 = 0.0; + double point2 = 0.0; + QList< QPair > points; + QString pointType = ui.GetString("TYPE"); + + // Check if the provided coordinate list is valid, i.e. a Samp/Line or Lat/Long coordinate per row + if (usePointList) { + + CSVReader reader; + reader.read(FileName(ui.GetFileName("COORDLIST")).expanded()); + + if (!reader.isTableValid(reader.getTable()) || reader.columns() != 2) { + QString msg = "Coordinate file formatted incorrectly.\n" + "Each row must have two columns: a sample,line or a latitude,longitude pair."; + throw IException(IException::User, msg, _FILEINFO_); + } + + for (int row = 0; row < reader.rows(); row++) { + point1 = toDouble(reader.getRow(row)[0]); + point2 = toDouble(reader.getRow(row)[1]); + points.append(QPair(point1, point2)); + } + + } + // Grab the coordinate from the ui position parameters if no coordinate list is provided + else { + if (pointType == "IMAGE") { + if (ui.WasEntered("SAMPLE")) + point1 = ui.GetDouble("SAMPLE"); + if (ui.WasEntered("LINE")) + point2 = ui.GetDouble("LINE"); + } + else { + point1 = ui.GetDouble("LATITUDE"); + point2 = ui.GetDouble("LONGITUDE"); + } + points.append(QPair(point1, point2)); + } + + return points; + } + + + // Gets the camera information for each point (coordinate). + // Passed in a list of coordinates, passed in by reference a CameraPointInfo object. + // Returns a list of PvlGroup pointers - these groups contain the camera info for each coordinate. + QList getCameraPointInfo(const UserInterface &ui, + QList< QPair > points, + CameraPointInfo &campt) { + // Setup our parameters from ui and variables + QList cameraPoints; + bool usePointList = ui.WasEntered("COORDLIST"); + bool allowOutside = ui.GetBoolean("ALLOWOUTSIDE"); + QString type; + if (ui.WasEntered("COORDLIST")) { + type = ui.GetString("COORDTYPE"); + } + else { + type = ui.GetString("TYPE"); + } + PvlGroup *camPoint = NULL; + + // Depending on what type is selected, set values accordingly + for (int i = 0; i < points.size(); i++) { + + QPair pt = points[i]; + if (type == "GROUND") { + camPoint = campt.SetGround(pt.first, pt.second, allowOutside, usePointList); + } + else { + if (usePointList) { + camPoint = campt.SetImage(pt.first, pt.second, allowOutside, usePointList); + } + else { + if (ui.WasEntered("SAMPLE") && ui.WasEntered("LINE")) { + camPoint = campt.SetImage(pt.first, pt.second, allowOutside); + } + else { + if (ui.WasEntered("SAMPLE")) { + camPoint = campt.SetSample(pt.first, allowOutside); + } + else if (ui.WasEntered("LINE")) { + camPoint = campt.SetLine(pt.second, allowOutside); + } + else { + camPoint = campt.SetCenter(allowOutside); + } + } + } + } + cameraPoints.append(camPoint); + } + camPoint = NULL; + return cameraPoints; + } + + + // Write our point coordinates to std out in pvl format, or to a pvl or some type of flat file + void writePoints(const UserInterface &ui, QList camPoints, Pvl *log) { + // Progress should increment for each point we process + Progress prog; + prog.SetMaximumSteps(camPoints.size()); + QString outFile; + // Get user params from ui + if (ui.WasEntered("TO")) { + outFile = FileName(ui.GetFileName("TO")).expanded(); + } + bool append = ui.GetBoolean("APPEND"); + QString fileFormat = ui.GetString("FORMAT"); + PvlGroup *point = NULL; + + for (int p = 0; p < camPoints.size(); p++) { + bool fileExists = FileName(outFile).fileExists(); + + prog.CheckStatus(); + point = camPoints[p]; + + // Remove units on look direction vectors + point -> findKeyword("LookDirectionBodyFixed").setUnits(""); + point -> findKeyword("LookDirectionJ2000").setUnits(""); + point -> findKeyword("LookDirectionCamera").setUnits(""); + + // write to output file + if (ui.WasEntered("TO")) { + // Write the pvl group out to the file + if (fileFormat == "PVL") { + Pvl temp; + temp.setTerminator(""); + temp.addGroup((*point)); + + // we don't want to overwrite successive points in outfile + if (append || p > 0) { + temp.append(outFile); + } + else { + temp.write(outFile); + } + } + // Create a flatfile from PVL data + // The flatfile is comma delimited and can be imported into Excel + else { + ofstream os; + bool writeHeader = false; + if (append || p > 0) { + os.open(outFile.toLatin1().data(), ios::app); + if (!fileExists) { + writeHeader = true; + } + } + else { + os.open(outFile.toLatin1().data(), ios::out); + writeHeader = true; + } + + if (writeHeader) { + for (int i = 0; i < (*point).keywords(); i++) { + if ((*point)[i].size() == 3) { + os << (*point)[i].name() << "X," + << (*point)[i].name() << "Y," + << (*point)[i].name() << "Z"; + } + else { + os << (*point)[i].name(); + } + + if (i < point->keywords() - 1) { + os << ","; + } + } + os << endl; + } + + + + for (int i = 0; i < (*point).keywords(); i++) { + if ((*point)[i].size() == 3) { + os << (QString)(*point)[i][0] << "," + << (QString)(*point)[i][1] << "," + << (QString)(*point)[i][2]; + } + else { + os << (QString)(*point)[i]; + } + + if (i < (*point).keywords() - 1) { + os << ","; + } + } + os << endl; + } + } + + // No output file specified + else { + // don't log data - + if (ui.GetString("FORMAT") == "FLAT") { + string msg = "Flat file must have a name."; + throw IException(IException::User, msg, _FILEINFO_); + } + } + + // we still want to output the results + log->addGroup((*point)); + delete point; + point = NULL; + } + prog.CheckStatus(); + } +} \ No newline at end of file diff --git a/isis/src/base/apps/campt/campt.h b/isis/src/base/apps/campt/campt.h new file mode 100644 index 0000000000..0c16f06703 --- /dev/null +++ b/isis/src/base/apps/campt/campt.h @@ -0,0 +1,12 @@ +#ifndef campt_h +#define campt_h + +#include "Pvl.h" +#include "UserInterface.h" + +namespace Isis{ + + extern void campt(UserInterface &ui, Pvl *log); +} + +#endif \ No newline at end of file diff --git a/isis/src/base/apps/campt/main.cpp b/isis/src/base/apps/campt/main.cpp index 897b4742c2..7ebd2b3188 100644 --- a/isis/src/base/apps/campt/main.cpp +++ b/isis/src/base/apps/campt/main.cpp @@ -1,266 +1,27 @@ #include "Isis.h" -#include -#include +#include "campt.h" -#include "Brick.h" -#include "Camera.h" -#include "CameraPointInfo.h" -#include "CSVReader.h" -#include "Distance.h" -#include "IException.h" -#include "iTime.h" -#include "Longitude.h" -#include "Progress.h" -#include "PvlGroup.h" -#include "SpecialPixel.h" -#include "TProjection.h" +#include "Application.h" +#include "Pvl.h" using namespace std; using namespace Isis; -QList< QPair > getPoints(const UserInterface &ui, bool usePointList); -QList getCameraPointInfo(const UserInterface &ui, - QList< QPair > points, - CameraPointInfo &campt); -void writePoints(const UserInterface &ui, QList camPoints); - - - - void IsisMain() { UserInterface &ui = Application::GetUserInterface(); - - // Setup our input cube - CameraPointInfo campt; - - QString fileFormat = ui.GetString("FORMAT"); - if(fileFormat=="PVL") - campt.SetCSVOutput(false); - else - campt.SetCSVOutput(true); - - campt.SetCube( ui.GetFileName("FROM") + "+" + ui.GetInputAttribute("FROM").toString() ); - - // Grab the provided points (coordinates) - QList< QPair > points = getPoints(ui, ui.WasEntered("COORDLIST")); - - // Get the camera point info for coordiante - - QList camPoints = getCameraPointInfo(ui, points, campt); - - writePoints(ui, camPoints); -} - - -// We can grab our coordinates, either from the ui position parameters or the coordlist parameter -// This method returns a list of double pairs (i.e. a list of coordinates) -QList< QPair > getPoints(const UserInterface &ui, bool usePointList) { - double point1 = 0.0; - double point2 = 0.0; - QList< QPair > points; - QString pointType = ui.GetString("TYPE"); - - // Check if the provided coordinate list is valid, i.e. a Samp/Line or Lat/Long coordinate per row - if (usePointList) { - - CSVReader reader; - reader.read(FileName(ui.GetFileName("COORDLIST")).expanded()); - - if (!reader.isTableValid(reader.getTable()) || reader.columns() != 2) { - QString msg = "Coordinate file formatted incorrectly.\n" - "Each row must have two columns: a sample,line or a latitude,longitude pair."; - throw IException(IException::User, msg, _FILEINFO_); - } - - for (int row = 0; row < reader.rows(); row++) { - point1 = toDouble(reader.getRow(row)[0]); - point2 = toDouble(reader.getRow(row)[1]); - points.append(QPair(point1, point2)); - } - + Pvl appLog; + try { + campt(ui, &appLog); } - // Grab the coordinate from the ui position parameters if no coordinate list is provided - else { - if (pointType == "IMAGE") { - if (ui.WasEntered("SAMPLE")) - point1 = ui.GetDouble("SAMPLE"); - if (ui.WasEntered("LINE")) - point2 = ui.GetDouble("LINE"); - } - else { - point1 = ui.GetDouble("LATITUDE"); - point2 = ui.GetDouble("LONGITUDE"); + catch (...) { + for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) { + Application::Log(*grpIt); } - points.append(QPair(point1, point2)); - } - - return points; -} - - -// Gets the camera information for each point (coordinate). -// Passed in a list of coordinates, passed in by reference a CameraPointInfo object. -// Returns a list of PvlGroup pointers - these groups contain the camera info for each coordinate. -QList getCameraPointInfo(const UserInterface &ui, - QList< QPair > points, - CameraPointInfo &campt) { - // Setup our parameters from ui and variables - QList cameraPoints; - bool usePointList = ui.WasEntered("COORDLIST"); - bool allowOutside = ui.GetBoolean("ALLOWOUTSIDE"); - QString type; - if (ui.WasEntered("COORDLIST")) { - type = ui.GetString("COORDTYPE"); + throw; } - else { - type = ui.GetString("TYPE"); - } - PvlGroup *camPoint = NULL; - - // Depending on what type is selected, set values accordingly - for (int i = 0; i < points.size(); i++) { - - QPair pt = points[i]; - if (type == "GROUND") { - camPoint = campt.SetGround(pt.first, pt.second, allowOutside, usePointList); - } - else { - if (usePointList) { - camPoint = campt.SetImage(pt.first, pt.second, allowOutside, usePointList); - } - else { - if (ui.WasEntered("SAMPLE") && ui.WasEntered("LINE")) { - camPoint = campt.SetImage(pt.first, pt.second, allowOutside); - } - else { - if (ui.WasEntered("SAMPLE")) { - camPoint = campt.SetSample(pt.first, allowOutside); - } - else if (ui.WasEntered("LINE")) { - camPoint = campt.SetLine(pt.second, allowOutside); - } - else { - camPoint = campt.SetCenter(allowOutside); - } - } - } - } - cameraPoints.append(camPoint); - } - camPoint = NULL; - return cameraPoints; -} - - -// Write our point coordinates to std out in pvl format, or to a pvl or some type of flat file -void writePoints(const UserInterface &ui, QList camPoints) { - // Progress should increment for each point we process - Progress prog; - prog.SetMaximumSteps(camPoints.size()); - QString outFile; - // Get user params from ui - if (ui.WasEntered("TO")) { - outFile = FileName(ui.GetFileName("TO")).expanded(); - } - bool append = ui.GetBoolean("APPEND"); - QString fileFormat = ui.GetString("FORMAT"); - PvlGroup *point = NULL; - - for (int p = 0; p < camPoints.size(); p++) { - bool fileExists = FileName(outFile).fileExists(); - - prog.CheckStatus(); - point = camPoints[p]; - - // Remove units on look direction vectors - point -> findKeyword("LookDirectionBodyFixed").setUnits(""); - point -> findKeyword("LookDirectionJ2000").setUnits(""); - point -> findKeyword("LookDirectionCamera").setUnits(""); - - // write to output file - if (ui.WasEntered("TO")) { - // Write the pvl group out to the file - if (fileFormat == "PVL") { - Pvl temp; - temp.setTerminator(""); - temp.addGroup((*point)); - - // we don't want to overwrite successive points in outfile - if (append || p > 0) { - temp.append(outFile); - } - else { - temp.write(outFile); - } - } - // Create a flatfile from PVL data - // The flatfile is comma delimited and can be imported into Excel - else { - ofstream os; - bool writeHeader = false; - if (append || p > 0) { - os.open(outFile.toLatin1().data(), ios::app); - if (!fileExists) { - writeHeader = true; - } - } - else { - os.open(outFile.toLatin1().data(), ios::out); - writeHeader = true; - } - - if (writeHeader) { - for (int i = 0; i < (*point).keywords(); i++) { - if ((*point)[i].size() == 3) { - os << (*point)[i].name() << "X," - << (*point)[i].name() << "Y," - << (*point)[i].name() << "Z"; - } - else { - os << (*point)[i].name(); - } - - if (i < point->keywords() - 1) { - os << ","; - } - } - os << endl; - } - - - - for (int i = 0; i < (*point).keywords(); i++) { - if ((*point)[i].size() == 3) { - os << (QString)(*point)[i][0] << "," - << (QString)(*point)[i][1] << "," - << (QString)(*point)[i][2]; - } - else { - os << (QString)(*point)[i]; - } - - if (i < (*point).keywords() - 1) { - os << ","; - } - } - os << endl; - } - } - - // No output file specified - else { - // don't log data - - if (ui.GetString("FORMAT") == "FLAT") { - string msg = "Flat file must have a name."; - throw IException(IException::User, msg, _FILEINFO_); - } - } - - // we still want to output the results - Application::Log((*point)); - delete point; - point = NULL; + + for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) { + Application::Log(*grpIt); } - prog.CheckStatus(); -} +} \ No newline at end of file