Unverified Commit 7914ba0c authored by AustinSanders's avatar AustinSanders Committed by GitHub
Browse files

maptrim app conversion + gtests (#4201)

* Maptrim app to function conversion and gtests

* Reopen projTestCube to save DNs to cube

* Fixed keyword typo

* Adjusted automos test histograms to account for DNs

* Fixed automos standard deviation

* Removed template comment

* Removed old makefiles

* Converted member vars/funcs to static

* Tests that the app log is populated with a mapping group.
parent c2186f0c
Loading
Loading
Loading
Loading
+11 −154
Original line number Diff line number Diff line
#include "Isis.h"

#include <QString>

#include "Application.h"
#include "FileName.h"
#include "IException.h"
#include "IString.h"
#include "LineManager.h"
#include "ProcessByLine.h"
#include "ProgramLauncher.h"
#include "Projection.h"
#include "ProjectionFactory.h"
#include "TProjection.h"
#include "SpecialPixel.h"
#include "Pvl.h"
#include "maptrim.h" 

using namespace std;
using namespace Isis;

void trim(Buffer &in, Buffer &out);
void getSize(Buffer &in);

double slat, elat, slon, elon;
int smallestLine, biggestLine, smallestSample, biggestSample;
TProjection *proj;

void IsisMain() {
  // Get the projection
  UserInterface &ui = Application::GetUserInterface();
  Pvl pvl(ui.GetFileName("FROM"));
  proj = (TProjection *) ProjectionFactory::CreateFromCube(pvl);

  // Determine ground range to crop and/or trim
  if(ui.WasEntered("MINLAT")) {
    slat = ui.GetDouble("MINLAT");
    elat = ui.GetDouble("MAXLAT");
    slon = ui.GetDouble("MINLON");
    elon = ui.GetDouble("MAXLON");
  }
  else if(proj->HasGroundRange()) {
    slat = proj->MinimumLatitude();
    elat = proj->MaximumLatitude();
    slon = proj->MinimumLongitude();
    elon = proj->MaximumLongitude();
  }
  else {
    string msg = "Latitude and longitude range not defined in projection";
    throw IException(IException::User, msg, _FILEINFO_);
  }

  QString mode = ui.GetString("MODE");
  IString tempFileName;
  
  if(mode != "TRIM") {
    smallestLine = smallestSample = INT_MAX;
    biggestLine = biggestSample = -INT_MAX;

    ProcessByLine p;
    p.SetInputCube("FROM");
    p.StartProcess(getSize);
    p.EndProcess();

    int samples = biggestSample - smallestSample + 1;
    int lines = biggestLine - smallestLine + 1;

    // Run external crop
    QString cropParams = "";
    cropParams += "from=" + ui.GetFileName("FROM");
    if(mode == "CROP") {
      cropParams += " to=" + ui.GetAsString("TO");
    }
    else {
      tempFileName = FileName::createTempFile("TEMPORARYcropped.cub").name();
      cropParams += " to=" + tempFileName.ToQt();
    }

    cropParams += " sample= "   + toString(smallestSample);
    cropParams += " nsamples= " + toString(samples);
    cropParams += " line= "     + toString(smallestLine);
    cropParams += " nlines= "   + toString(lines);

  Pvl appLog;
  try {
      ProgramLauncher::RunIsisProgram("crop", cropParams);
    }
    catch(IException &e) {
      QString msg = "Could not execute crop with params: [" + cropParams + "]";
      throw IException(IException::Programmer, msg, _FILEINFO_);
    maptrim(ui, &appLog);
  }
    if(mode == "BOTH") {
      delete proj;
      proj = NULL;
      Pvl pvl(tempFileName.ToQt());
      proj = (TProjection *) ProjectionFactory::CreateFromCube(pvl);
  catch (...) {
    for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) {
      Application::Log(*grpIt);
    }
    throw;
  }

  // Trim image if necessary
  if(mode != "CROP") {
    ProcessByLine p;
    CubeAttributeInput att;
    if(mode == "BOTH") {
      p.SetInputCube(tempFileName.ToQt(), att);
    }
    else { //if its trim
      p.SetInputCube("FROM");
    }
    p.SetOutputCube("TO");
    p.StartProcess(trim);
    p.EndProcess();
    if(mode == "BOTH") {
      remove(tempFileName.c_str());
  for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) {
    Application::Log(*grpIt);
  }
}
  // Add mapping to print.prt
  PvlGroup mapping = proj->Mapping();
  Application::Log(mapping);

  delete proj;
  proj = NULL;
}

// Size up the cropped area in terms of lines and samples
void getSize(Buffer &in) {
  double lat, lon;
  for(int i = 0; i < in.size(); i++) {
    proj->SetWorld((double)in.Sample(i), (double)in.Line(i));
    lat = proj->Latitude();
    lon = proj->Longitude();

    // Skip past pixels outside of lat/lon range
    if(lat < slat || lat > elat || lon < slon || lon > elon) {
      continue;
    }
    else {
      if(in.Line(i) < smallestLine) {
        smallestLine = in.Line(i);
      }
      if(in.Line(i) > biggestLine) {
        biggestLine = in.Line(i);
      }
      if(in.Sample(i) < smallestSample) {
        smallestSample = in.Sample(i);
      }
      if(in.Sample(i) > biggestSample) {
        biggestSample = in.Sample(i);
      }
    }
  }
}

// Line processing routine
void trim(Buffer &in, Buffer &out) {
  // Loop for each pixel in the line.  Find lat/lon of pixel, if outside
  // of range, set to NULL.
  double lat, lon;
  for(int i = 0; i < in.size(); i++) {
    proj->SetWorld((double)in.Sample(i), (double)in.Line(i));
    lat = proj->Latitude();
    lon = proj->Longitude();
    if(lat < slat || lat > elat || lon < slon || lon > elon) {
      out[i] = Isis::Null;
    }
    else {
      out[i] = in[i];
    }
  }
}
+172 −0
Original line number Diff line number Diff line
#include <QString>

#include "FileName.h"
#include "IException.h"
#include "IString.h"
#include "LineManager.h"
#include "ProcessByLine.h"
#include "ProgramLauncher.h"
#include "Projection.h"
#include "ProjectionFactory.h"
#include "Pvl.h"
#include "TProjection.h"
#include "SpecialPixel.h"
#include "UserInterface.h"

using namespace std;
namespace Isis{

  static void trim(Buffer &in, Buffer &out);
  static void getSize(Buffer &in);

  static double slat, elat, slon, elon;
  static int smallestLine, biggestLine, smallestSample, biggestSample;
  static TProjection *proj;

  void maptrim(UserInterface &ui, Pvl *log) {
    // Get the projection
    Pvl pvl(ui.GetFileName("FROM"));
    proj = (TProjection *) ProjectionFactory::CreateFromCube(pvl);

    // Determine ground range to crop and/or trim
    if(ui.WasEntered("MINLAT")) {
      slat = ui.GetDouble("MINLAT");
      elat = ui.GetDouble("MAXLAT");
      slon = ui.GetDouble("MINLON");
      elon = ui.GetDouble("MAXLON");
    }
    else if(proj->HasGroundRange()) {
      slat = proj->MinimumLatitude();
      elat = proj->MaximumLatitude();
      slon = proj->MinimumLongitude();
      elon = proj->MaximumLongitude();
    }
    else {
      string msg = "Latitude and longitude range not defined in projection";
      throw IException(IException::User, msg, _FILEINFO_);
    }

    QString mode = ui.GetString("MODE");
    IString tempFileName;

    if(mode != "TRIM") {
      smallestLine = smallestSample = INT_MAX;
      biggestLine = biggestSample = -INT_MAX;

      ProcessByLine p;
      CubeAttributeInput &inputAtt = ui.GetInputAttribute("FROM");
      p.SetInputCube(ui.GetFileName("FROM"), inputAtt);
      p.StartProcess(getSize);
      p.EndProcess();

      int samples = biggestSample - smallestSample + 1;
      int lines = biggestLine - smallestLine + 1;

      // Run external crop
      QString cropParams = "";
      cropParams += "from=" + ui.GetFileName("FROM");
      if(mode == "CROP") {
        cropParams += " to=" + ui.GetAsString("TO");
      }
      else {
        tempFileName = FileName::createTempFile("TEMPORARYcropped.cub").name();
        cropParams += " to=" + tempFileName.ToQt();
      }

      cropParams += " sample= "   + toString(smallestSample);
      cropParams += " nsamples= " + toString(samples);
      cropParams += " line= "     + toString(smallestLine);
      cropParams += " nlines= "   + toString(lines);

      try {
        ProgramLauncher::RunIsisProgram("crop", cropParams);
      }
      catch(IException &e) {
        QString msg = "Could not execute crop with params: [" + cropParams + "]";
        throw IException(IException::Programmer, msg, _FILEINFO_);
      }
      if(mode == "BOTH") {
        delete proj;
        proj = NULL;
        Pvl pvl(tempFileName.ToQt());
        proj = (TProjection *) ProjectionFactory::CreateFromCube(pvl);
      }
    }

    // Trim image if necessary
    if(mode != "CROP") {
      ProcessByLine p;
      CubeAttributeInput att;
      if(mode == "BOTH") {
        p.SetInputCube(tempFileName.ToQt(), att);
      }
      else { //if its trim
        CubeAttributeInput &inputAtt = ui.GetInputAttribute("FROM");
        p.SetInputCube(ui.GetFileName("FROM"), inputAtt);
      }

      CubeAttributeOutput &outputAtt = ui.GetOutputAttribute("TO");
      p.SetOutputCube(ui.GetFileName("TO"), outputAtt);
      p.StartProcess(trim);
      p.EndProcess();
      if(mode == "BOTH") {
        remove(tempFileName.c_str());
      }
    }
    // Add mapping to print.prt
    PvlGroup mapping = proj->Mapping();
    if (log){
      log->addGroup(mapping);
    }

    delete proj;
    proj = NULL;
  }

  // Size up the cropped area in terms of lines and samples
  void getSize(Buffer &in) {
    double lat, lon;
    for(int i = 0; i < in.size(); i++) {
      proj->SetWorld((double)in.Sample(i), (double)in.Line(i));
      lat = proj->Latitude();
      lon = proj->Longitude();

      // Skip past pixels outside of lat/lon range
      if(lat < slat || lat > elat || lon < slon || lon > elon) {
        continue;
      }
      else {
        if(in.Line(i) < smallestLine) {
          smallestLine = in.Line(i);
        }
        if(in.Line(i) > biggestLine) {
          biggestLine = in.Line(i);
        }
        if(in.Sample(i) < smallestSample) {
          smallestSample = in.Sample(i);
        }
        if(in.Sample(i) > biggestSample) {
          biggestSample = in.Sample(i);
        }
      }
    }
  }

  // Line processing routine
  void trim(Buffer &in, Buffer &out) {
    // Loop for each pixel in the line.  Find lat/lon of pixel, if outside
    // of range, set to NULL.
    double lat, lon;
    for(int i = 0; i < in.size(); i++) {
      proj->SetWorld((double)in.Sample(i), (double)in.Line(i));
      lat = proj->Latitude();
      lon = proj->Longitude();
      if(lat < slat || lat > elat || lon < slon || lon > elon) {
        out[i] = Isis::Null;
      }
      else {
        out[i] = in[i];
      }
    }
  }
}
+10 −0
Original line number Diff line number Diff line
#ifndef maptrim_h
#define maptrim_h

#include "UserInterface.h"

namespace Isis{
  extern void maptrim(UserInterface &ui, Pvl *log=nullptr);
}

#endif
+0 −4
Original line number Diff line number Diff line
BLANKS = "%-6s"    
LENGTH = "%-40s"

include $(ISISROOT)/make/isismake.tststree
+0 −9
Original line number Diff line number Diff line
APPNAME = maptrim

include $(ISISROOT)/make/isismake.tsts

commands:
	$(APPNAME) from=$(INPUT)/isisTruth.map.cub \
	to=$(OUTPUT)/cropAndTrimTruth.cub \
	mode=both \
	minlat=15 maxlat=20 minlon=222 maxlon=230> /dev/null;
Loading