Commit 8924709d authored by Kaitlyn Lee's avatar Kaitlyn Lee
Browse files

Merge branch 'm0703' of https://github.com/kdl222/ISIS3 into m0703

parents f77c4fc9 9cbc7ac2
Loading
Loading
Loading
Loading
+109 −61
Original line number Diff line number Diff line
@@ -9,19 +9,11 @@ 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 +33,115 @@ 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
  readBytes.readLong = 0;
  fin.seekg(0);
  fin.read(readBytes.readChars, 4);
  if( fin.fail() || fin.eof() ) {
    string msg = "An error ocurred when reading 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 = "An error ocurred when reading 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 = "An error ocurred when reading 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 + "]";
    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
  map<int, int> dataTypes = {
    {1450901768, 1},
    {1450902032, 2},
    {1450902288, 2},
    {1450902560, 4},
    {1450902816, 4},
    {1450903072, 4},
    {1450903360, 8},
    {8, 1},
    {16, 2},
    {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;
  //Old header format has no bit type
  if (bitType == 0) {
    dataTypeBytes = dataTypes.find(totalBandBits) -> second;
  }
  else {
    dataTypeBytes = dataTypes.find(bitType) -> second;
  }

  // Read bytes 20-23 to get offset
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);
  noffset = (int)readBytes.readLong;
  if (noffset < 1024) {
    noffset = 1024;
  int nOffset = (int) readBytes.readLong;
  if (nOffset < 1024) {
    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 +152,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 +172,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;
+19 −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)/errorTruth.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)/errorTruth.txt > /dev/null` ]; \
		then true; \
		fi;