Unverified Commit 5356b7c4 authored by Amy Stamile's avatar Amy Stamile Committed by GitHub
Browse files

Isis2pds App and Gtests (#4198)

* Isis2pds App and gtests

* Added pds4 conversion test

* isis2pds data added

* isis2pds Makefiles removed

* Added second function with cube parameter and removed unnecessary error checking.

* Updated added function with copy/paste error.

* Added bypass solution to segfault issue.

* Updated isis2pds(ui) function with SetVirtualBands.

* Updated isis2pds(ui) function with SetVirtualBands.

* Updated isis2pds(ui) function.

* Resolved segfault issues.
parent 67b77137
Loading
Loading
Loading
Loading
+247 −0
Original line number Diff line number Diff line
#include <QString>

#include "Cube.h"
#include "ExportDescription.h"
#include "FileName.h"
#include "isis2pds.h"
#include "Process.h"
#include "ProcessExportPds.h"
#include "ProcessExportPds4.h"
#include "Pvl.h"
#include "PvlKeyword.h"
#include "PvlToXmlTranslationManager.h"


using namespace std;
namespace Isis{

  enum Pixtype { NONE, NEG, BOTH };

  void setRangeAndPixels(UserInterface &ui, ProcessExport &p,
                         double &min, double &max, Pixtype ptype);

  void isis2pds(UserInterface &ui, Pvl *log){
    Cube icube;
    CubeAttributeInput inAtt = ui.GetInputAttribute("FROM");
    if (inAtt.bands().size() != 0) {
      icube.setVirtualBands(inAtt.bands());
    }
    icube.open(ui.GetFileName("FROM"));
    isis2pds(&icube, ui, log);
  }

  void isis2pds(Cube *icube, UserInterface &ui, Pvl *log) {

    if (ui.GetString("PDSVERSION") == "PDS3") {
      // Set the processing object
      ProcessExportPds p;

      // Setup the input cube
      p.SetInputCube(icube);

      if (ui.GetString("STRETCH") == "LINEAR") {
        if (ui.GetString("BITTYPE") != "32BIT") {
          p.SetInputRange(ui);
        }
      }
      if (ui.GetString("STRETCH") == "MANUAL") {
        p.SetInputRange(ui.GetDouble("MINIMUM"), ui.GetDouble("MAXIMUM"));
      }

      double min = -DBL_MAX;
      double max = DBL_MAX;

      if (ui.GetString("BITTYPE") == "8BIT") {
        p.SetOutputType(Isis::UnsignedByte);
        min = 0.0;
        max = 255.0;
        setRangeAndPixels(ui, p, min, max, BOTH);
      }
      else if (ui.GetString("BITTYPE") == "S16BIT") {
        p.SetOutputType(Isis::SignedWord);
        min = -32768.0;
        max = 32767.0;
        setRangeAndPixels(ui, p, min, max, NEG);
      }
      else if (ui.GetString("BITTYPE") == "U16BIT") {
        p.SetOutputType(Isis::UnsignedWord);
        min = 0.0;
        max = 65535.0;
        setRangeAndPixels(ui, p, min, max, BOTH);
      }
      else {
        p.SetOutputType(Isis::Real);
        p.SetOutputNull(Isis::NULL4);
        p.SetOutputLrs(Isis::LOW_REPR_SAT4);
        p.SetOutputLis(Isis::LOW_INSTR_SAT4);
        p.SetOutputHrs(Isis::HIGH_REPR_SAT4);
        p.SetOutputHis(Isis::HIGH_INSTR_SAT4);
        setRangeAndPixels(ui, p, min, max, NONE);
      }

      if (ui.GetString("ENDIAN") == "MSB") {
        p.SetOutputEndian(Isis::Msb);
      }
      else if (ui.GetString("ENDIAN") == "LSB") {
        p.SetOutputEndian(Isis::Lsb);
      }

      if (ui.GetString("LABTYPE") == "FIXED") {
        p.SetExportType(ProcessExportPds::Fixed);
      }

      if (ui.GetBoolean("CHECKSUM")) {
        p.setCanGenerateChecksum(true);
      }

      // Set the resolution to  Kilometers
      p.SetPdsResolution(ProcessExportPds::Kilometer);

      p.StandardPdsLabel(ProcessExportPds::Image);

      FileName outFile(ui.GetFileName("TO", "img"));
      QString outFileName(outFile.expanded());
      ofstream oCube(outFileName.toLatin1().data());
      p.OutputLabel(oCube);
      p.StartProcess(oCube);
      if (ui.GetBoolean("CHECKSUM")) {
        p.updateChecksumInLabel(oCube);
      }
      oCube.close();
      p.EndProcess();

      //Records what it did to the print.prt file
      PvlGroup results("DNs Used");
      results += PvlKeyword("Null", toString(p.OutputNull()));
      results += PvlKeyword("LRS", toString(p.OutputLrs()));
      results += PvlKeyword("LIS", toString(p.OutputLis()));
      results += PvlKeyword("HIS", toString(p.OutputHis()));
      results += PvlKeyword("HRS", toString(p.OutputHrs()));
      results += PvlKeyword("ValidMin", toString(min));
      results += PvlKeyword("ValidMax", toString(max));
      if (log){
        log->addGroup(results);
      }
    }
    else {
      // Setup the process and set the input cube
      ProcessExportPds4 process;

      process.SetInputCube(icube);

      PvlObject *label= icube->label();
      if (!label->hasObject("IsisCube")) {
        QString msg = "Input file [" + ui.GetFileName("FROM") +
                      "] does not appear to be an ISIS3 cube.";
        throw  IException(IException::User, msg, _FILEINFO_);
      }

      FileName outFile(ui.GetFileName("TO", "img"));
      QString outFileName(outFile.expanded());

      if (ui.GetString("STRETCH") == "LINEAR") {
        if (ui.GetString("BITTYPE") != "32BIT") {
          process.SetInputRange(ui);
        }
      }
      if (ui.GetString("STRETCH") == "MANUAL") {
        process.SetInputRange(ui.GetDouble("MINIMUM"), ui.GetDouble("MAXIMUM"));
      }

      double min = -DBL_MAX;
      double max = DBL_MAX;

      if (ui.GetString("BITTYPE") == "8BIT") {
        process.SetOutputType(Isis::UnsignedByte);
        min = 0.0;
        max = 255.0;
        setRangeAndPixels(ui, process, min, max, BOTH);
      }
      else if (ui.GetString("BITTYPE") == "S16BIT") {
        process.SetOutputType(Isis::SignedWord);
        min = -32768.0;
        max = 32767.0;
        setRangeAndPixels(ui, process, min, max, NEG);
      }
      else if (ui.GetString("BITTYPE") == "U16BIT") {
        process.SetOutputType(Isis::UnsignedWord);
        min = 0.0;
        max = 65535.0;
        setRangeAndPixels(ui, process, min, max, BOTH);
      }
      else {
        process.SetOutputType(Isis::Real);
        process.SetOutputNull(Isis::NULL4);
        process.SetOutputLrs(Isis::LOW_REPR_SAT4);
        process.SetOutputLis(Isis::LOW_INSTR_SAT4);
        process.SetOutputHrs(Isis::HIGH_REPR_SAT4);
        process.SetOutputHis(Isis::HIGH_INSTR_SAT4);
        setRangeAndPixels(ui, process, min, max, NONE);
      }

      if (ui.GetString("ENDIAN") == "MSB") {
        process.SetOutputEndian(Isis::Msb);
      }
      else if (ui.GetString("ENDIAN") == "LSB") {
        process.SetOutputEndian(Isis::Lsb);
      }

      // Records what it did to the print.prt file
      PvlGroup results("DNs Used");
      results += PvlKeyword("Null", toString(process.OutputNull()));
      results += PvlKeyword("LRS", toString(process.OutputLrs()));
      results += PvlKeyword("LIS", toString(process.OutputLis()));
      results += PvlKeyword("HIS", toString(process.OutputHis()));
      results += PvlKeyword("HRS", toString(process.OutputHrs()));
      results += PvlKeyword("ValidMin", toString(min));
      results += PvlKeyword("ValidMax", toString(max));
      if (log){
        log->addGroup(results);
      }

      process.StandardPds4Label();
      process.WritePds4(outFileName);
    }

    return;
  }

  //Sets up special pixels and valid pixel ranges
  void setRangeAndPixels(UserInterface &ui, ProcessExport &p, double &min, double &max, Pixtype ptype) {
    if (ptype == NEG) {
      if (ui.GetBoolean("NULL")) {
        p.SetOutputNull(min++);
      }
      if (ui.GetBoolean("LRS")) {
        p.SetOutputLrs(min++);
      }
      if (ui.GetBoolean("LIS")) {
        p.SetOutputLis(min++);
      }
      if (ui.GetBoolean("HIS")) {
        p.SetOutputHis(min++);
      }
      if (ui.GetBoolean("HRS")) {
        p.SetOutputHrs(min++);
      }
    }
    else if (ptype == BOTH) {
      if (ui.GetBoolean("NULL")) {
        p.SetOutputNull(min++);
      }
      if (ui.GetBoolean("LRS")) {
        p.SetOutputLrs(min++);
      }
      if (ui.GetBoolean("LIS")) {
        p.SetOutputLis(min++);
      }
      if (ui.GetBoolean("HRS")) {
        p.SetOutputHrs(max--);
      }
      if (ui.GetBoolean("HIS")) {
        p.SetOutputHis(max--);
      }
    }
    p.SetOutputRange(min, max);
  }
}
+12 −0
Original line number Diff line number Diff line
#ifndef isis2pds_h
#define isis2pds_h

#include "Cube.h"
#include "UserInterface.h"

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

#endif
+10 −224
Original line number Diff line number Diff line
#include "Isis.h"

#include <QString>

#include "Application.h"
#include "Cube.h"
#include "ExportDescription.h"
#include "FileName.h"
#include "Process.h"
#include "ProcessExportPds.h"
#include "ProcessExportPds4.h"
#include "Pvl.h"
#include "PvlKeyword.h"
#include "PvlToXmlTranslationManager.h"
#include "isis2pds.h"

using namespace std;
using namespace Isis;

enum Pixtype { NONE, NEG, BOTH };

void setRangeAndPixels(UserInterface &ui, ProcessExport &p,
                       double &min, double &max, Pixtype ptype);
void IsisMain() {
  UserInterface &ui = Application::GetUserInterface();

  // Check if input file is indeed, a cube
  if (ui.GetFileName("FROM").right(3) != "cub") {
    QString msg = "Input file [" + ui.GetFileName("FROM") +
                "] does not appear to be a cube";
    throw  IException(IException::User, msg, _FILEINFO_);
  }

  if (ui.GetString("PDSVERSION") == "PDS3") {
    // Set the processing object
    ProcessExportPds p;

    // Setup the input cube
    p.SetInputCube("FROM");

    if (ui.GetString("STRETCH") == "LINEAR") {
      if (ui.GetString("BITTYPE") != "32BIT") {
        p.SetInputRange();
      }
    }
    if (ui.GetString("STRETCH") == "MANUAL") {
      p.SetInputRange(ui.GetDouble("MINIMUM"), ui.GetDouble("MAXIMUM"));
    }

    double min = -DBL_MAX;
    double max = DBL_MAX;

    if (ui.GetString("BITTYPE") == "8BIT") {
      p.SetOutputType(Isis::UnsignedByte);
      min = 0.0;
      max = 255.0;
      setRangeAndPixels(ui, p, min, max, BOTH);
    }
    else if (ui.GetString("BITTYPE") == "S16BIT") {
      p.SetOutputType(Isis::SignedWord);
      min = -32768.0;
      max = 32767.0;
      setRangeAndPixels(ui, p, min, max, NEG);
    }
    else if (ui.GetString("BITTYPE") == "U16BIT") {
      p.SetOutputType(Isis::UnsignedWord);
      min = 0.0;
      max = 65535.0;
      setRangeAndPixels(ui, p, min, max, BOTH);
    }
    else {
      p.SetOutputType(Isis::Real);
      p.SetOutputNull(Isis::NULL4);
      p.SetOutputLrs(Isis::LOW_REPR_SAT4);
      p.SetOutputLis(Isis::LOW_INSTR_SAT4);
      p.SetOutputHrs(Isis::HIGH_REPR_SAT4);
      p.SetOutputHis(Isis::HIGH_INSTR_SAT4);
      setRangeAndPixels(ui, p, min, max, NONE);
    }

    if (ui.GetString("ENDIAN") == "MSB") {
      p.SetOutputEndian(Isis::Msb);
    }
    else if (ui.GetString("ENDIAN") == "LSB") {
      p.SetOutputEndian(Isis::Lsb);
    }

    if (ui.GetString("LABTYPE") == "FIXED") {
      p.SetExportType(ProcessExportPds::Fixed);
    }

    if (ui.GetBoolean("CHECKSUM")) {
      p.setCanGenerateChecksum(true);
    }

    // Set the resolution to  Kilometers
    p.SetPdsResolution(ProcessExportPds::Kilometer);

    p.StandardPdsLabel(ProcessExportPds::Image);

    FileName outFile(ui.GetFileName("TO", "img"));
    QString outFileName(outFile.expanded());
    ofstream oCube(outFileName.toLatin1().data());
    p.OutputLabel(oCube);
    p.StartProcess(oCube);
    if (ui.GetBoolean("CHECKSUM")) {
      p.updateChecksumInLabel(oCube);
    }
    oCube.close();
    p.EndProcess();

    //Records what it did to the print.prt file
    PvlGroup results("DNs Used");
    results += PvlKeyword("Null", toString(p.OutputNull()));
    results += PvlKeyword("LRS", toString(p.OutputLrs()));
    results += PvlKeyword("LIS", toString(p.OutputLis()));
    results += PvlKeyword("HIS", toString(p.OutputHis()));
    results += PvlKeyword("HRS", toString(p.OutputHrs()));
    results += PvlKeyword("ValidMin", toString(min));
    results += PvlKeyword("ValidMax", toString(max));
    Application::Log(results);
  }
  else {
    // Setup the process and set the input cube
    ProcessExportPds4 process;
    Cube *icube = process.SetInputCube("FROM");

    PvlObject *label= icube->label();
    if (!label->hasObject("IsisCube")) {
      QString msg = "Input file [" + ui.GetFileName("FROM") +
                    "] does not appear to be an ISIS3 cube.";
      throw  IException(IException::User, msg, _FILEINFO_);
    }
    
    FileName outFile(ui.GetFileName("TO", "img"));
    QString outFileName(outFile.expanded());
    
    if (ui.GetString("STRETCH") == "LINEAR") {
      if (ui.GetString("BITTYPE") != "32BIT") {
        process.SetInputRange();
  Pvl appLog;
  try {
    isis2pds(ui, &appLog);
  }
  catch (...) {
    for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) {
      Application::Log(*grpIt);
    }
    if (ui.GetString("STRETCH") == "MANUAL") {
      process.SetInputRange(ui.GetDouble("MINIMUM"), ui.GetDouble("MAXIMUM"));
    throw;
  }

    double min = -DBL_MAX;
    double max = DBL_MAX;

    if (ui.GetString("BITTYPE") == "8BIT") {
      process.SetOutputType(Isis::UnsignedByte);
      min = 0.0;
      max = 255.0;
      setRangeAndPixels(ui, process, min, max, BOTH);
    }
    else if (ui.GetString("BITTYPE") == "S16BIT") {
      process.SetOutputType(Isis::SignedWord);
      min = -32768.0;
      max = 32767.0;
      setRangeAndPixels(ui, process, min, max, NEG);
    }
    else if (ui.GetString("BITTYPE") == "U16BIT") {
      process.SetOutputType(Isis::UnsignedWord);
      min = 0.0;
      max = 65535.0;
      setRangeAndPixels(ui, process, min, max, BOTH);
    }
    else {
      process.SetOutputType(Isis::Real);
      process.SetOutputNull(Isis::NULL4);
      process.SetOutputLrs(Isis::LOW_REPR_SAT4);
      process.SetOutputLis(Isis::LOW_INSTR_SAT4);
      process.SetOutputHrs(Isis::HIGH_REPR_SAT4);
      process.SetOutputHis(Isis::HIGH_INSTR_SAT4);
      setRangeAndPixels(ui, process, min, max, NONE);
    }

    if (ui.GetString("ENDIAN") == "MSB") {
      process.SetOutputEndian(Isis::Msb);
    }
    else if (ui.GetString("ENDIAN") == "LSB") {
      process.SetOutputEndian(Isis::Lsb);
    }

    // Records what it did to the print.prt file
    PvlGroup results("DNs Used");
    results += PvlKeyword("Null", toString(process.OutputNull()));
    results += PvlKeyword("LRS", toString(process.OutputLrs()));
    results += PvlKeyword("LIS", toString(process.OutputLis()));
    results += PvlKeyword("HIS", toString(process.OutputHis()));
    results += PvlKeyword("HRS", toString(process.OutputHrs()));
    results += PvlKeyword("ValidMin", toString(min));
    results += PvlKeyword("ValidMax", toString(max));
    Application::Log(results);

    process.StandardPds4Label();
    process.WritePds4(outFileName);
  }

  return;
}

//Sets up special pixels and valid pixel ranges
void setRangeAndPixels(UserInterface &ui, ProcessExport &p, double &min, double &max, Pixtype ptype) {
  if (ptype == NEG) {
    if (ui.GetBoolean("NULL")) {
      p.SetOutputNull(min++);
    }
    if (ui.GetBoolean("LRS")) {
      p.SetOutputLrs(min++);
    }
    if (ui.GetBoolean("LIS")) {
      p.SetOutputLis(min++);
    }
    if (ui.GetBoolean("HIS")) {
      p.SetOutputHis(min++);
    }
    if (ui.GetBoolean("HRS")) {
      p.SetOutputHrs(min++);
    }
  }
  else if (ptype == BOTH) {
    if (ui.GetBoolean("NULL")) {
      p.SetOutputNull(min++);
    }
    if (ui.GetBoolean("LRS")) {
      p.SetOutputLrs(min++);
    }
    if (ui.GetBoolean("LIS")) {
      p.SetOutputLis(min++);
    }
    if (ui.GetBoolean("HRS")) {
      p.SetOutputHrs(max--);
    }
    if (ui.GetBoolean("HIS")) {
      p.SetOutputHis(max--);
    }
  for (auto grpIt = appLog.beginGroup(); grpIt!= appLog.endGroup(); grpIt++) {
    Application::Log(*grpIt);
  }
  p.SetOutputRange(min, max);
}
+0 −20
Original line number Diff line number Diff line
APPNAME = isis2pds

include $(ISISROOT)/make/isismake.tsts

commands:
	$(APPNAME) from=$(INPUT)/8bittruth.cub to=$(OUTPUT)/8bitdefault.img \
	bittype=8bit > /dev/null;
	catlab from=$(OUTPUT)/8bitdefault.img to=$(OUTPUT)/8bitdefaultlabels.pvl > /dev/null;
	pds2isis from=$(OUTPUT)/8bitdefault.img to=$(OUTPUT)/8bitdefault.cub > /dev/null;
	$(RM) $(OUTPUT)/8bitdefault.img > /dev/null;
	$(APPNAME) from=$(INPUT)/8bittruth.cub to=$(OUTPUT)/8bitnonull.img \
	bittype=8bit null=no > /dev/null;
	catlab from=$(OUTPUT)/8bitnonull.img to=$(OUTPUT)/8bitnonulllabels.pvl > /dev/null;
	pds2isis from=$(OUTPUT)/8bitnonull.img to=$(OUTPUT)/8bitnonull.cub > /dev/null;
	$(RM) $(OUTPUT)/8bitnonull.img > /dev/null;
	$(APPNAME) from=$(INPUT)/8bittruth.cub to=$(OUTPUT)/8bitspecialpixels.img \
	bittype=8bit lrs=yes lis=yes his=yes hrs=yes > /dev/null;
	catlab from=$(OUTPUT)/8bitspecialpixels.img to=$(OUTPUT)/8bitspecialpixelslabels.pvl > /dev/null;
	pds2isis from=$(OUTPUT)/8bitspecialpixels.img to=$(OUTPUT)/8bitspecialpixels.cub > /dev/null;
	$(RM) $(OUTPUT)/8bitspecialpixels.img > /dev/null;
+0 −4
Original line number Diff line number Diff line
BLANKS = "%-6s"    
LENGTH = "%-40s"

include $(ISISROOT)/make/isismake.tststree
Loading