Unverified Commit a9f6d788 authored by Makayla Shepherd's avatar Makayla Shepherd Committed by GitHub
Browse files

Merge pull request #244 from kdl222/m0703

ddd2isis importing multiband ddd files. Fixes #703
parents 2730619b 5f8a03a7
Loading
Loading
Loading
Loading
+110 −62
Original line number Original line Diff line number Diff line
@@ -9,19 +9,11 @@ using namespace std;
using namespace Isis;
using namespace Isis;


void IsisMain() {
void IsisMain() {

  UserInterface &ui = Application::GetUserInterface();
  UserInterface &ui = Application::GetUserInterface();
  ProcessImport p;
  IString from = ui.GetFileName("FROM");
  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;
  ifstream fin;

  fin.open(from.c_str(), ios::in | ios::binary);
  fin.open(from.c_str(), ios::in | ios::binary);
  if( !fin.is_open() ) {
  if( !fin.is_open() ) {
    string msg = "Cannot open input file [" + from + "]";
    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. The magic number for a ddd file is 1659.
  readBytes.readLong = 0;
  readBytes.readLong = 0;
  fin.seekg(0);
  fin.seekg(0);
  fin.read(readBytes.readChars, 4);
  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);
  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";
    string msg = "Input file [" + from + "] does not appear to be in ddd format";
    throw IException(IException::Io, msg, _FILEINFO_);
    throw IException(IException::Io, msg, _FILEINFO_);
  }
  }


  // Read bytes 4-7 to get number of lines
  fin.read(readBytes.readChars, 4);
  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);
  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);
  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);
  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);
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);

  if( fin.fail() || fin.eof() ) {
  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_);
    throw IException(IException::Io, msg, _FILEINFO_);
  }
  }

  readBytes.readFloat = swp.Float(readBytes.readChars);
  bittype = readBytes.readLong;
  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);
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);
  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);
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);
  readBytes.readFloat = swp.Float(readBytes.readChars);
  noffset = (int)readBytes.readLong;
  int nOffset = (int) readBytes.readLong;
  if (noffset < 1024) {
  if (nOffset < 1024) {
    noffset = 1024;
    nOffset = 1024;
  }
  }


  fin.close();

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


  fin.close();
  ProcessImport p;


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


    p.SetDimensions(nsamples, nlines, nbands);
  // ddd files with more than one band are pixel interleaved
    p.SetFileHeaderBytes(noffset);
  // 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.SetByteOrder(Isis::Msb);
  p.SetInputFile( ui.GetFileName("FROM") );
  p.SetInputFile( ui.GetFileName("FROM") );
  p.SetOutputCube("TO");
  p.SetOutputCube("TO");
@@ -120,7 +172,3 @@ void IsisMain() {
  p.StartProcess();
  p.StartProcess();
  p.EndProcess();
  p.EndProcess();
}
}

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


  <description>
  <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>
  </description>


  <history>
  <history>
@@ -30,6 +31,16 @@
      will need to be read in using a combination of the raw2isis and crop
      will need to be read in using a combination of the raw2isis and crop
      programs. Fixes #1713.
      programs. Fixes #1713.
    </change>
    </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>
  </history>


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


commands:
commands:
	$(APPNAME) FROM=$(INPUT)/vis1flat.ddd TO=$(OUTPUT)/vis1flat.cub > /dev/null;
	$(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 Original line 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;