Unverified Commit ba193d07 authored by Kristin's avatar Kristin Committed by GitHub
Browse files

Merge pull request #338 from TracieSucharski/ipceDocks

Ipce docks
parents a77b761c e150d935
Loading
Loading
Loading
Loading
+116 −63
Original line number Diff line number Diff line
@@ -4,24 +4,17 @@
#include "SpecialPixel.h"
#include "FileName.h"
#include "Pvl.h"
#include <QMap>

using namespace std;
using namespace Isis;

void IsisMain() {

  UserInterface &ui = Application::GetUserInterface();
  ProcessImport p;
  IString from = ui.GetFileName("FROM");
  EndianSwapper swp("MSB");
  int nsamples = 0, nlines = 0, nbands = 1, noffset = 0, bittype = 0, nbytes = 0;

  union {
    char readChars[4];
    long readLong;
    float readFloat;
  } readBytes;

  ifstream fin;

  fin.open(from.c_str(), ios::in | ios::binary);
  if( !fin.is_open() ) {
    string msg = "Cannot open input file [" + from + "]";
@@ -41,60 +34,119 @@ void IsisMain() {
   *
   */

  // Verify the magic number
   // ifstream read() needs a char* to read values into, so the union
   // is used to store read values
   union {
     char readChars[4];
     long readLong;
     float readFloat;
   } readBytes;

   // ddd files are LSB
   EndianSwapper swp("MSB");

  // Verify that the file is a ddd by reading in the first 4 bytes and
  // comparing the magic numbers. The magic number for a ddd file is 1659.
  readBytes.readLong = 0;
  fin.seekg(0);
  fin.read(readBytes.readChars, 4);
  if( fin.fail() || fin.eof() ) {
    string msg = "Could not read the magic number in the input file [" + from + "]";
    throw IException(IException::Io, msg, _FILEINFO_);
  }
  readBytes.readFloat = swp.Float(readBytes.readChars);

  if(readBytes.readLong != 0x67B) {
  if(readBytes.readLong != 1659) {
    string msg = "Input file [" + from + "] does not appear to be in ddd format";
    throw IException(IException::Io, msg, _FILEINFO_);
  }

  // Read bytes 4-7 to get number of lines
  fin.read(readBytes.readChars, 4);
  if( fin.fail() || fin.eof() ) {
    string msg = "Could not read the number of lines in the input file [" + from + "]";
    throw IException(IException::Io, msg, _FILEINFO_);
  }
  readBytes.readFloat = swp.Float(readBytes.readChars);
  nlines = (int)readBytes.readLong;
  int nLines = (int) readBytes.readLong;

  // Read bytes 8-11 to get number of bytes
  fin.read(readBytes.readChars, 4);
  if( fin.fail() || fin.eof() ) {
    string msg = "Could not read the number of bytes in the input file [" + from + "]";
    throw IException(IException::Io, msg, _FILEINFO_);
  }
  readBytes.readFloat = swp.Float(readBytes.readChars);
  nbytes = (int)readBytes.readLong;
  int nBytes = (int) readBytes.readLong;

  // Read bytes 12-15 to get the total number of bits out of all the bands
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);

  if( fin.fail() || fin.eof() ) {
    string msg = "An error ocurred when reading the input file [" + from + "]";
    string msg = "Could not read the number of bits in the input file [" + from + "]";
    throw IException(IException::Io, msg, _FILEINFO_);
  }

  bittype = readBytes.readLong;

  readBytes.readFloat = swp.Float(readBytes.readChars);
  int totalBandBits = readBytes.readLong;

  // Maps the bit type of the file to the number of bytes of that type
  // Taken directly from a given python program that reads in ddd data
  QMap<int, int> dataTypes;
  dataTypes.insert(1450901768, 1);
  dataTypes.insert(1450902032, 2);
  dataTypes.insert(1450902288, 2);
  dataTypes.insert(1450902560, 4);
  dataTypes.insert(1450902816, 4);
  dataTypes.insert(1450903072, 4);
  dataTypes.insert(1450903360, 8);
  dataTypes.insert(8, 1);
  dataTypes.insert(16, 2);
  dataTypes.insert(48, 2);

  // Read bytes 16-19 to get the bit type
  // Map the bit type to the number of bytes of that data type
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);
  int bitType = (int) readBytes.readLong;

  int dataTypeBytes;
  int nOffset;
  // Check for new header format. Taken from the python program.
  if ( (bitType & 0xfffff000) == 0x567b0000 ) {
    dataTypeBytes = dataTypes.value(bitType);

    // Read bytes 20-23 to get offset
    // New header format may have different offsets
    fin.read(readBytes.readChars, 4);
    readBytes.readFloat = swp.Float(readBytes.readChars);
  noffset = (int)readBytes.readLong;
  if (noffset < 1024) {
    noffset = 1024;
    nOffset = (int) readBytes.readLong;
    if (nOffset < 1024) {
      nOffset = 1024;
    }
  }
  else {
    // Old header format does not have a bit type
    // Old header format's offset is always 1024.
     dataTypeBytes = dataTypes.value(totalBandBits);
     nOffset = 1024;
  }

  fin.close();

  PvlGroup results("FileInfo");
  results += PvlKeyword("NumberOfLines", toString(nlines));
  results += PvlKeyword("NumberOfBytesPerLine", toString(nbytes));
  results += PvlKeyword("BitType", toString(bittype));
  nsamples = nbytes / (bittype / 8);
  results += PvlKeyword("NumberOfSamples", toString(nsamples));
  nbands = nbytes / nsamples;
  results += PvlKeyword("NumberOfBands", toString(nbands));
  results += PvlKeyword("LabelBytes", toString(noffset));
  results += PvlKeyword( "NumberOfLines", toString(nLines) );
  results += PvlKeyword( "NumberOfBytesPerLine", toString(nBytes) );
  results += PvlKeyword( "BitType", toString(bitType) );
  int nSamples = nBytes / (totalBandBits / 8);
  results += PvlKeyword( "NumberOfSamples", toString(nSamples) );
  int nBands = (totalBandBits / 8) / dataTypeBytes;
  results += PvlKeyword( "NumberOfBands", toString(nBands) );
  results += PvlKeyword( "LabelBytes", toString(nOffset) );
  Application::Log(results);

  fin.close();
  ProcessImport p;

  if (ui.WasEntered("TO")) {
    switch(bittype) {
  int bitsPerBand = totalBandBits / nBands;
  switch(bitsPerBand) {
    case 8:
      p.SetPixelType(Isis::UnsignedByte);
      break;
@@ -105,14 +157,19 @@ void IsisMain() {
      p.SetPixelType(Isis::Real);
      break;
    default:
        IString msg = "Unsupported bit per pixel count [" + IString(bittype) + "]. ";
        msg += "(Use the raw2isis and crop programs to import the file in case it is ";
        msg += "line or sample interleaved.)";
      IString msg = "Unsupported bit per pixel count [" + IString(bitsPerBand) + "] ";
      msg += "from [" + from + "]";
      throw IException(IException::Io, msg, _FILEINFO_);
  }

    p.SetDimensions(nsamples, nlines, nbands);
    p.SetFileHeaderBytes(noffset);
  // ddd files with more than one band are pixel interleaved
  // Having one band is similar to BIP, but this is here for clarification
  if (nBands > 1) {
    p.SetOrganization(ProcessImport::BIP);
  }

  p.SetDimensions(nSamples, nLines, nBands);
  p.SetFileHeaderBytes(nOffset);
  p.SetByteOrder(Isis::Msb);
  p.SetInputFile( ui.GetFileName("FROM") );
  p.SetOutputCube("TO");
@@ -120,7 +177,3 @@ void IsisMain() {
  p.StartProcess();
  p.EndProcess();
}

  return;
}
+12 −2
Original line number Diff line number Diff line
@@ -7,7 +7,8 @@
  </brief>

  <description>
    This program will import a ddd image into an Isis cube. The ddd format files are created by Malin Space Science Systems.
    This program will import a ddd image into an Isis cube. The ddd format files
    are created by Malin Space Science Systems.
  </description>

  <history>
@@ -30,6 +31,16 @@
      will need to be read in using a combination of the raw2isis and crop
      programs. Fixes #1713.
    </change>
    <change name="Kaitlyn Lee" date="2018-03-01">
      We were given a python program that reads in data from a ddd file
      to use as an example. In the python program, the formula they used to
      calculate the number of bands is different from the one we previously used.
      The old formula did the number of total band bits / 8; the formula is now
      (the number of total band bits / 8) / the number of bytes of the data type
      of the file's bit type. Added the ability to process files with multiple
      bands. Removed the internal default of the output parameter set to None so
      that an output file is now requried. Fixes #703.
    </change>
  </history>

  <category>
@@ -55,7 +66,6 @@
      <parameter name="TO">
         <type>cube</type>
         <fileMode>output</fileMode>
         <internalDefault>None</internalDefault>
         <brief>
           Output Isis cube
         </brief>
+2 −0
Original line number Diff line number Diff line
@@ -4,3 +4,5 @@ include $(ISISROOT)/make/isismake.tsts

commands:
	$(APPNAME) FROM=$(INPUT)/vis1flat.ddd TO=$(OUTPUT)/vis1flat.cub > /dev/null;
	$(APPNAME) FROM=$(INPUT)/0023MD0000140000101507C00_DXXX_16b.ddd \
		TO=$(OUTPUT)/0023MD0000140000101507C00_DXXX_16b.cub > /dev/null;
+24 −0
Original line number Diff line number Diff line
APPNAME = ddd2isis

include $(ISISROOT)/make/isismake.tsts

commands:
	# TEST: Throws an error when trying to open the file
	if [ `$(APPNAME) \
		FROM=$(INPUT)/vis1flat.ddd \
		TO=$(OUTPUT)/vis1flat.cub \
		2> $(OUTPUT)/temp.txt > /dev/null` ]; \
		then true; \
		fi;

	# TEST: Throws an error when trying to read from a cub instead of ddd
	if [ `$(APPNAME) \
		FROM=$(INPUT)/vis1flat.cub \
		TO=$(OUTPUT)/vis1flat.cub \
		2>> $(OUTPUT)/temp.txt > /dev/null` ]; \
		then true; \
		fi;

	# Removes input file path up until input
	$(SED) 's+\[.*/input+[input+' $(OUTPUT)/temp.txt > $(OUTPUT)/errorTruth.txt;
	$(RM) $(OUTPUT)/temp.txt
+29 −26
Original line number Diff line number Diff line
@@ -24,10 +24,13 @@
        Process data tiles instead of a pixel at a time; Test for
        valid output map coordinate at every map pixel. References #2035
    </change>
    <change name="Kaitlyn Lee" date="2018-03-10">
        Changed the category to Import and Export. Fixes #5188.
    </change>
</history>

  <category>
    <categoryItem>Map Projection</categoryItem>
    <categoryItem>Import and Export</categoryItem>
  </category>

  <groups>
Loading