Commit a3ff6986 authored by Kaitlyn Lee's avatar Kaitlyn Lee
Browse files

Refactored and reorganized.

parent 3c8b8e4b
Loading
Loading
Loading
Loading
+96 −54
Original line number Diff line number Diff line
@@ -9,38 +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;
  int nlines = 0;
  int nbands = 1;
  int noffset = 0;
  int bittype = 0;
  int nbytes = 0;
  int dataTypeByte = 0;

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

  map<int, int> dataTypes = {
    {1450901768, 1},
    {1450902032, 2},
    {1450902288, 2},
    {1450902560, 4},
    {1450902816, 4},
    {1450903072, 4},
    {1450903360, 8},
    {8, 1},
    {16, 2},
    {48, 2}
  };

  ifstream fin;

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

   EndianSwapper swp("MSB");

   /*
   union {
     char readChars[4];
     long readLong;
     float readFloat;
     int readInt;
   } readBytes;
   */
   // Used to store the read 4 bytes
   char inChar[4];

  // Verify that the file is a .ddd by reading in the first 4 bytes and
  // comparing the magic numbers
  fin.seekg(0);
  fin.read(inChar, 4);

  if( swp.Int(inChar) != 1659) {
    string msg = "Input file [" + from + "] does not appear to be in ddd format";
    throw IException(IException::Io, msg, _FILEINFO_);
  }
  /*
  readBytes.readLong = 0;
  fin.seekg(0);
  fin.read(readBytes.readChars, 4);
@@ -71,18 +65,38 @@ void IsisMain() {
    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(inChar, 4);
  int nLines = swp.Int(inChar);
  /*
  fin.read(readBytes.readChars, 4);
  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(inChar, 4);
  int nBytes = swp.Int(inChar);

  /*
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);
  nbytes = (int)readBytes.readLong;
  int nBytes = (int) readBytes.readLong;
  */

  // Read bytes 12-15 to get the bit type
  fin.read(inChar, 4);

  if( fin.fail() || fin.eof() ) {
    string msg = "An error ocurred when reading the input file [" + from + "]";
    throw IException(IException::Io, msg, _FILEINFO_);
  }
  int totalBandBits = swp.Int(inChar);

  /*
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);

@@ -91,38 +105,64 @@ void IsisMain() {
    throw IException(IException::Io, msg, _FILEINFO_);
  }

  bittype = readBytes.readLong;
  int bitType = readBytes.readLong;
  */

  // Maps the data type of the file to the number of bytes
  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 data type
  // Map the data type to the number of bytes and store in dataTypeByte
  fin.read(readBytes.readChars, 4);
  readBytes.readFloat = swp.Float(readBytes.readChars);
  dataTypeByte = dataTypes.find( (int)readBytes.readLong ) ->second;
  cout << "Number of bytes: " << dataTypes.find(dataTypeByte) -> first<< " = " << dataTypes.find(dataTypeByte) -> second <<endl;
  // Map the data type to the number of bytes and store in dataTypeBytes
  fin.read(inChar, 4);
  int bitType = swp.Int(inChar);
  cout<<"DATA TYPE: " << bitType << endl;
  int dataTypeBytes = dataTypes.find( bitType ) -> second;

  // Read bytes 20-23 to get offset
  fin.read(inChar, 4);
  int nOffset = swp.Int(inChar);
  if (nOffset < 1024) {
    nOffset = 1024;
  }

  /*
  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;
  }
  */

  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 = (bittype / 8) / dataTypeByte;
  results += PvlKeyword( "NumberOfBands", toString(nbands) );
  results += PvlKeyword( "LabelBytes", toString(noffset) );
  results += PvlKeyword( "NumberOfLines", toString(nLines) );
  results += PvlKeyword( "NumberOfBytesPerLine", toString(nBytes) );
  results += PvlKeyword( "BitType", toString(totalBandBits) );
  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;

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

    p.SetDimensions(nsamples, nlines, nbands);
    p.SetFileHeaderBytes(noffset);
    if (nBands > 1) {
      p.SetOrganization(ProcessImport::BIP);
      cout << "set BIP" << endl;
    }
    p.SetDimensions(nSamples, nLines, nBands);
    p.SetFileHeaderBytes(nOffset);
    p.SetByteOrder(Isis::Msb);
    p.SetInputFile( ui.GetFileName("FROM") );
    p.SetOutputCube("TO");
@@ -148,6 +192,4 @@ void IsisMain() {
    p.StartProcess();
    p.EndProcess();
  }

  return;
}