Blame view

envi/EnviHeader.h 4.29 KB
a47a23a9   dmayerich   added ENVI functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  #ifndef ENVIHEADER_H
  #define ENVIHEADER_H
  #include <string>
  #include <iostream>
  #include <fstream>
  #include <ctime>
  #include <sstream>
  using namespace std;
  
  enum InterleaveType {bip, bil, bsq};
  class EnviHeaderClass	//"EnviheaderClass" will be used to store data into variables sX, sY, etc.
  {
  public:
  	unsigned int sX;				//samples = "sX"
  	unsigned int sY;				//lines = "sY"
  	unsigned int nF;				//bands = "nF"
  	unsigned int headersize;		//header offset = "headersize"
  	unsigned int datatype;		//data type = "datatype"
  	InterleaveType type;
  	string units;		//wavelength units = "units" (Usully in Wavenumber or Wavelength)
  	int LoadHeader(string filename)
  	{
          ifstream inFile;
          //Load the header file in the same directory, for future note, might want to modify code to either "cin" the file name of have a feature in the GUI which take into account different fileneames
          inFile.open (filename.c_str());
  
          //If or not there is a file
          if (!inFile)
          {
              cout<<"Error"<<endl;
              return 1;
          }
  
          string line;
          getline(inFile,line);
  
          //If the 1st line of text file does not read ENVI, spit out an error.
          if (line.compare("ENVI"))
          {
              cout<<"Error, not an ENVI file"<<endl;
              return 1;
          }
  
          while(inFile) //Loops through entire file
          {
                  getline(inFile,line);
                  if(line.find("samples")==0)
                  {
                      sX=getNumber(line);				//Calls getNumber function for "samples", stores in header.sX
                  }
                  if(line.find("lines")==0)
                  {
                      sY=getNumber(line);				//Calls getNumber function for "lines", stores in header.sY
                  }
                  if(line.find("bands")==0)
                  {
                      nF=getNumber(line);				//Calls getNumber function for "bands", stores in header.nF
                  }
                  if(line.find("header offset")==0)
                  {
                      headersize=getNumber(line);		//Calls getNumber function for "header offset", stores in header.headersize
                  }
                  if(line.find("data type")==0)
                  {
                      datatype=getNumber(line);		//Calls getNumber function for "data type", stores in header.datatype
                  }
  
                  line.find("interleave");
                  if(line.find("interleave")==0)
                  {
                      string typeStr;
                      typeStr=getString(line);
                      if (typeStr.find("bip")==0)
                          type=bip;
                      else if(typeStr.find("bsq")==0)
                          type=bsq;
                      else if(typeStr.find("bil")==0)
                          type=bil;
                      else
                      {
                          //throw an error and exit
                          cout<<"Type not recognized"<<endl;
                          return 1;
                      }
  
                  }
                  if(line.find("wavelength units")==0)
                  {
                      //Calls getString function for "wavelength units", stores in header.units
                      units=getString(line);
                  }
              }
          return 0;
      }
  
  
  private:
      //retrieves a numerical parameter from a line of text
  	int getNumber(string line)
  	{
          int result=0;
          int position;
          position=line.find("="); //Finds the position of the equal sign
  
          string numStr; //Creates a string for storing data called "numStr"
          numStr=line.substr(position+2); //Moves from the position of "=" 2 spaces to the right...
          istringstream convert(numStr);  //...and converts the string into numbers using "istringstream"
  
          convert>>result; //converts result from previous line
  
          return result;
      }
      //retrieves a string parameter from a line of text
      string getString(string line)
      {
          int position;
          position=line.find("="); //Finds the position of the equal sign
  
          string word; //Creates a string for storing word data called "word"
          word=line.substr(position+2); //Moves from the position of "=" 2 spaces to the right
                                        //Note: We do not need to convert word using "istringstream"
  
          return word;
      }
  };
  #endif