Blame view

stim/envi/envi.h 78.6 KB
e8eb202f   David Mayerich   added a new ENVI ...
1
2
3
  #ifndef STIM_ENVI_H
  #define STIM_ENVI_H
  
37e61850   David Mayerich   added an is_open(...
4
5
6
7
8
  #include <stim/envi/envi_header.h>
  #include <stim/envi/bsq.h>
  #include <stim/envi/bip.h>
  #include <stim/envi/bil.h>
  #include <stim/math/fd_coefficients.h>
9d34229e   David Mayerich   restructured the ...
9
  #include <stim/parser/filename.h>
91d472ac   David Mayerich   added file_size f...
10
  #include <stim/util/filesize.h>
bfe9c6db   heziqi   added feature_mat...
11
  #include <iostream>
48f356fa   David Mayerich   bug fixes for FFT...
12
  #include <fstream>
2ed641f0   David Mayerich   removed the depen...
13
  //#include "../image/image.h"
e8eb202f   David Mayerich   added a new ENVI ...
14
  
8a86bd56   David Mayerich   changed rts names...
15
  namespace stim{
e8eb202f   David Mayerich   added a new ENVI ...
16
  
cb455f98   David Mayerich   removed old ENVI ...
17
18
19
20
21
  /** This class implements reading of ENVI hyperspectral files. These files can be stored in multiple orientations
  	(including BSQ, BIP, and BIL) in order to optimize streaming speed depending on applications. Basic ENVI
  	files are stored on disk as a large binary file with a corresponding header. Code for reading and processing
  	ENVI header files is in the envi_header class.
  */
f92397d2   David Mayerich   fixed a Win32 bug...
22
  class envi{
e8eb202f   David Mayerich   added a new ENVI ...
23
24
  
  	void* file;		//void pointer to the relevant file reader (bip, bsq, or bil - with appropriate data type)
8057237a   David Mayerich   added an envi::op...
25
  	std::string fname;					//file name used for repeated opening and closing
e8eb202f   David Mayerich   added a new ENVI ...
26
  
735a2a24   David Mayerich   started testing o...
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
  	//allocate sufficient space for a spectrum based on the data type and number of bands
  	void* alloc_array(size_t len){
  		switch(header.data_type){
  		case envi_header::int8:
  			return malloc(len);
  		case envi_header::int16:
  		case envi_header::uint16:
  			return malloc(2 * len);
  		case envi_header::int32:
  		case envi_header::uint32:
  		case envi_header::float32:
  			return malloc(4 * len);
  		case envi_header::int64:
  		case envi_header::uint64:
  		case envi_header::float64:
  		case envi_header::complex32:
  			return malloc(8 * len);
  		case envi_header::complex64:
  			return malloc(16 * len);
  		default:
  			std::cout<<"ERROR stim::envi data type not recognized for spectral allocation"<<std::endl;
  			exit(1);
  		}
  	}
  
  	//cast a value to DEST type from SRC type
  	template<typename DST, typename SRC>
  	inline void cast(DST* dst, SRC* src){
  		(*dst) = (DST)(*src);
  	}
  
  	//cast an array from type SRC to type DEST
  	template<typename DST, typename SRC>
  	inline void cast(DST* dst, SRC* src, size_t len){
  		for(size_t i = 0; i < len; i++)
  			cast(&dst[i], &src[i]);
  	}
08d1c3b9   David Mayerich   fixed casting war...
64
  	
e8eb202f   David Mayerich   added a new ENVI ...
65
  public:
08d1c3b9   David Mayerich   fixed casting war...
66
  	envi_header header;
e8eb202f   David Mayerich   added a new ENVI ...
67
  
08d1c3b9   David Mayerich   fixed casting war...
68
  	
edb975b0   David Mayerich   created a stim::e...
69
70
71
72
73
  	/// Default constructor
  	envi(){
  		file = NULL;				//set the file pointer to NULL
  	}
  
37e61850   David Mayerich   added an is_open(...
74
  	envi(std::string filename, std::string headername) : envi(){
ec722ff9   David Mayerich   added spero mosai...
75
76
77
78
79
80
  		header.load(headername);
  
  		fname = filename;					//save the filename
  
  		allocate();
  	}
9d34229e   David Mayerich   restructured the ...
81
82
  	//used to test if the current ENVI file is valid
  	operator bool(){
37e61850   David Mayerich   added an is_open(...
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
  		if (header.interleave == envi_header::BSQ) {		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
  				return ((bsq<float>*)file)->is_open();
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->is_open();
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIL) {		//if the infile is bil file
  			if (header.data_type == envi_header::float32)
  				return ((bil<float>*)file)->is_open();
  			else if (header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->is_open();
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIP) {		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
  				return ((bip<float>*)file)->is_open();
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->is_open();
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  		else {
  			std::cout << "ERROR: unidentified file type" << std::endl;
  			exit(1);
  		}
f1365506   David Mayerich   modifications to ...
113
  		return false;
9d34229e   David Mayerich   restructured the ...
114
  	}
ec722ff9   David Mayerich   added spero mosai...
115
  
9d34229e   David Mayerich   restructured the ...
116
117
  	//test to determine if the specified file is an ENVI file
  	static bool is_envi(std::string fname, std::string hname = ""){
9d34229e   David Mayerich   restructured the ...
118
119
120
121
122
123
124
125
126
127
128
129
  		stim::filename data_file(fname);
  		stim::filename header_file;
  		if(hname == ""){								//if the header isn't provided
  			header_file = data_file;					//assume that it's the same name as the data file, with a .hdr extension
  			header_file = header_file.extension("hdr");
  		}
  		else header_file = hname;						//otherwise load the passed header
  
  		stim::envi_header H;
  		if(H.load(header_file) == false)				//load the header file, if it doesn't load return false
  			return false;
  		size_t targetBytes = H.data_bytes();			//get the number of bytes that SHOULD be in the data file
91d472ac   David Mayerich   added file_size f...
130
  		size_t bytes = stim::file_size(fname);
9d34229e   David Mayerich   restructured the ...
131
132
  		if(bytes != targetBytes) return false;			//if the data doesn't match the header, return false
  		return true;									//otherwise everything looks fine
41e2f5ab   David Mayerich   fixed finite mask...
133
  
9d34229e   David Mayerich   restructured the ...
134
  	}
08d1c3b9   David Mayerich   fixed casting war...
135
  	
4a6f666c   heziqi   Added mask method
136
  
735a2a24   David Mayerich   started testing o...
137
138
139
140
141
142
143
144
  	void* malloc_spectrum(){
  		return alloc_array(header.bands);
  	}
  
  	void* malloc_band(){
  		return alloc_array(header.samples * header.lines);
  	}
  
c6251f8b   David Mayerich   allows user to de...
145
  	void set_buffer_frac(double memfrac = 0.5){
008ae990   David Mayerich   implemented Windo...
146
147
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
c6251f8b   David Mayerich   allows user to de...
148
  				((bsq<float>*)file)->set_buffer_frac(memfrac);
008ae990   David Mayerich   implemented Windo...
149
  			else if(header.data_type == envi_header::float64)
c6251f8b   David Mayerich   allows user to de...
150
  				((bsq<double>*)file)->set_buffer_frac(memfrac);
008ae990   David Mayerich   implemented Windo...
151
152
153
154
155
156
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
c6251f8b   David Mayerich   allows user to de...
157
  				((bil<float>*)file)->set_buffer_frac(memfrac);
008ae990   David Mayerich   implemented Windo...
158
  			else if(header.data_type == envi_header::float64)
c6251f8b   David Mayerich   allows user to de...
159
  				((bil<double>*)file)->set_buffer_frac(memfrac);
008ae990   David Mayerich   implemented Windo...
160
161
162
163
164
165
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
c6251f8b   David Mayerich   allows user to de...
166
  				((bip<float>*)file)->set_buffer_frac(memfrac);
008ae990   David Mayerich   implemented Windo...
167
  			else if(header.data_type == envi_header::float64)
c6251f8b   David Mayerich   allows user to de...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  				((bip<double>*)file)->set_buffer_frac(memfrac);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
  	void set_buffer_raw(size_t bytes){
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
  				((bsq<float>*)file)->set_buffer_raw(bytes);
  			else if(header.data_type == envi_header::float64)
  				((bsq<double>*)file)->set_buffer_raw(bytes);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
  				((bil<float>*)file)->set_buffer_raw(bytes);
  			else if(header.data_type == envi_header::float64)
  				((bil<double>*)file)->set_buffer_raw(bytes);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
  				((bip<float>*)file)->set_buffer_raw(bytes);
  			else if(header.data_type == envi_header::float64)
  				((bip<double>*)file)->set_buffer_raw(bytes);
008ae990   David Mayerich   implemented Windo...
203
204
205
206
207
208
209
210
211
212
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
f92397d2   David Mayerich   fixed a Win32 bug...
213
214
215
216
217
218
219
220
  	/// Returns the size of the data type in bytes
  	unsigned int type_size(){
  		if(header.data_type == envi_header::float32) return 4;
  		if(header.data_type == envi_header::float64) return 8;
  
  		exit(1);
  	}
  
c6251f8b   David Mayerich   allows user to de...
221
222
223
224
225
226
227
228
229
230
  	size_t X(){ return header.samples; }
  	size_t Y(){ return header.lines; }
  	size_t Z(){ return header.bands; }
  	size_t B(){ return Z();	}
  
  	/// Return the size of the data set in bytes
  	size_t bytes(){
  		return X() * Y() * Z() * type_size();
  	}
  
cbce396e   David Mayerich   added support for...
231
232
233
234
235
  	/// Returns the progress of the current processing operation as a percentage
  	void reset_progress(){
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
236
  				((bsq<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
237
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
238
  				((bsq<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
239
240
241
242
243
244
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
245
  				((bil<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
246
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
247
  				((bil<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
248
249
250
251
252
253
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
254
  				((bip<float>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
255
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
256
  				((bip<double>*)file)->reset_progress();
cbce396e   David Mayerich   added support for...
257
258
259
260
261
262
263
264
265
266
267
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
  	/// Returns the progress of the current processing operation as a percentage
9d3ba0b1   David Mayerich   added stim::hsi a...
268
  	double progress(){
cbce396e   David Mayerich   added support for...
269
270
271
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
272
  				return ((bsq<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
273
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
274
  				return ((bsq<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
275
276
277
278
279
280
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
281
  				return ((bil<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
282
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
283
  				return ((bil<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
284
285
286
287
288
289
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
290
  				return ((bip<float>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
291
  			else if(header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
292
  				return ((bip<double>*)file)->get_progress();
cbce396e   David Mayerich   added support for...
293
294
295
296
297
298
299
300
301
302
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  		}
  		return 0;
  	}
  
c6251f8b   David Mayerich   allows user to de...
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  	/// Returns the progress of the current processing operation as a percentage
  	size_t data_rate(){
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
  				return ((bsq<float>*)file)->get_data_rate();
  			else if(header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->get_data_rate();
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
  				return ((bil<float>*)file)->get_data_rate();
  			else if(header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->get_data_rate();
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
  				return ((bip<float>*)file)->get_data_rate();
  			else if(header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->get_data_rate();
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  		}
  		return 0;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
339
  	/// Allocate memory for a new ENVI file based on the current interleave format (BIP, BIL, BSQ) and data type.
9d3ba0b1   David Mayerich   added stim::hsi a...
340
  	void allocate(){
e8eb202f   David Mayerich   added a new ENVI ...
341
342
343
  
  		file = NULL;	//set file to a NULL pointer
  
6708cc25   heziqi   Ziqi added envi c...
344
  		if(header.interleave == envi_header::BSQ){
e8eb202f   David Mayerich   added a new ENVI ...
345
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
346
  				file = new bsq<float>();
6708cc25   heziqi   Ziqi added envi c...
347
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
348
  				file = new bsq<double>();
6708cc25   heziqi   Ziqi added envi c...
349
350
351
  		}
  		else if(header.interleave == envi_header::BIP){
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
352
  				file = new bip<float>();
6708cc25   heziqi   Ziqi added envi c...
353
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
354
  				file = new bip<double>();
6708cc25   heziqi   Ziqi added envi c...
355
356
357
  		}
  		else if(header.interleave == envi_header::BIL){
  			if(header.data_type ==envi_header::float32)
9d3ba0b1   David Mayerich   added stim::hsi a...
358
  				file = new bil<float>();
6708cc25   heziqi   Ziqi added envi c...
359
  			else if(header.data_type == envi_header::float64)
9d3ba0b1   David Mayerich   added stim::hsi a...
360
  				file = new bil<double>();
6708cc25   heziqi   Ziqi added envi c...
361
  		}
e8eb202f   David Mayerich   added a new ENVI ...
362
  
e8eb202f   David Mayerich   added a new ENVI ...
363
364
  	}
  
8057237a   David Mayerich   added an envi::op...
365
  	/// Open a previously opened ENVI file
bb457896   David Mayerich   improved error me...
366
  	bool open(stim::iotype io = stim::io_in){
e8eb202f   David Mayerich   added a new ENVI ...
367
368
  
  		//load the file
6708cc25   heziqi   Ziqi added envi c...
369
370
  		if(header.interleave == envi_header::BSQ) {		//if the infile is bsq file
  			if(header.data_type == envi_header::float32) {
8057237a   David Mayerich   added an envi::op...
371
  				return ((bsq<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
372
373
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
374
  				return ((bsq<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
375
376
377
378
  			}
  			else
  				return false;
  		}
e8eb202f   David Mayerich   added a new ENVI ...
379
  
6708cc25   heziqi   Ziqi added envi c...
380
381
  		else if(header.interleave == envi_header::BIL) {		//if the infile is bil file
  			if(header.data_type == envi_header::float32) {
8057237a   David Mayerich   added an envi::op...
382
  				return ((bil<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
383
384
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
385
  				return ((bil<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
386
387
388
389
  			}
  			else
  				return false;
  		}
f92397d2   David Mayerich   fixed a Win32 bug...
390
  
6708cc25   heziqi   Ziqi added envi c...
391
392
  		else if(header.interleave == envi_header::BIP) {		//if the infile is bip file
  			if(header.data_type == envi_header::float32) {
8057237a   David Mayerich   added an envi::op...
393
  				return ((bip<float>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
394
395
  			}
  			else if(header.data_type == envi_header::float64) {
8057237a   David Mayerich   added an envi::op...
396
  				return ((bip<double>*)file)->open(fname, header.samples, header.lines, header.bands, header.header_offset, header.wavelength);
6708cc25   heziqi   Ziqi added envi c...
397
398
  			}
  			else
f92397d2   David Mayerich   fixed a Win32 bug...
399
  				return false;
6708cc25   heziqi   Ziqi added envi c...
400
401
  		}
  
f92397d2   David Mayerich   fixed a Win32 bug...
402
403
  		return true;
  
8057237a   David Mayerich   added an envi::op...
404
405
406
  
  	}
  
48f356fa   David Mayerich   bug fixes for FFT...
407
408
409
410
411
412
413
414
415
416
  	/// Open an Agilent binary file as an ENVI stream
  	bool open_agilent(std::string filename){
  		fname = filename;												//store the file name
  
  		//Open the file temporarily to get the header information
  		FILE* f = fopen(filename.c_str(), "r");							//open the binary file for reading
  		if(f == NULL) return false;										//return false if no file is opened
  
  		fseek(f, 9, SEEK_SET);											//seek to the number of bands
  		short b;														//allocate space for the number of bands
618dce30   David Mayerich   removed warnings ...
417
418
419
420
421
  		size_t nread = fread(&b, sizeof(short), 1, f);					//read the number of bands
  		if(nread != 1){
  			std::cout<<"Error reading band number from Agilent file."<<std::endl;
  			exit(1);
  		}
48f356fa   David Mayerich   bug fixes for FFT...
422
423
  		fseek(f, 13, SEEK_CUR);											//skip the the x and y dimensions
  		short x, y;
618dce30   David Mayerich   removed warnings ...
424
425
426
427
428
429
430
431
432
433
  		nread = fread(&x, sizeof(short), 1, f);									//read the image x and y size
  		if(nread != 1){
  			std::cout<<"Error reading X dimension from Agilent file."<<std::endl;
  			exit(1);
  		}
  		nread = fread(&y, sizeof(short), 1, f);
  		if(nread != 1){
  			std::cout<<"Error reading Y dimension from Agilent file."<<std::endl;
  			exit(1);
  		}
48f356fa   David Mayerich   bug fixes for FFT...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
  		fclose(f);														//close the file
  
  		//store the information from the Agilent header in the ENVI header
  		header.bands = b;
  		header.samples = x;
  		header.lines = y;
  		header.data_type = envi_header::float32;						//all values are 32-bit floats
  		header.header_offset = 1020;									//number of bytes in an Agilent binary header
  		header.interleave = envi_header::BSQ;							//all Agilent binary files are BSQ
  
  		allocate();														//allocate the streaming file object
  		open();															//open the file for streaming
  
  		return true;
  	}
  
8057237a   David Mayerich   added an envi::op...
450
451
452
453
  	/// Open an existing ENVI file given the filename and a header structure
  
  	/// @param filename is the name of the ENVI binary file
  	/// @param header is an ENVI header structure
bb457896   David Mayerich   improved error me...
454
  	bool open(std::string filename, stim::envi_header h, stim::iotype io = stim::io_in){
9d3ba0b1   David Mayerich   added stim::hsi a...
455
  		
8057237a   David Mayerich   added an envi::op...
456
457
458
459
  
  		header = h;							//store the header
  		fname = filename;					//save the filename
  
9d3ba0b1   David Mayerich   added stim::hsi a...
460
461
  		allocate();
  
bb457896   David Mayerich   improved error me...
462
  		return open(io);						//open the ENVI file;
8057237a   David Mayerich   added an envi::op...
463
464
  		
  
f92397d2   David Mayerich   fixed a Win32 bug...
465
466
467
468
469
470
  	}
  
  	/// Open an existing ENVI file given the file and header names.
  
  	/// @param filename is the name of the ENVI binary file
  	/// @param headername is the name of the ENVI header file
bb457896   David Mayerich   improved error me...
471
  	bool open(std::string filename, std::string headername, stim::iotype io = stim::io_in){
f92397d2   David Mayerich   fixed a Win32 bug...
472
473
474
475
476
  
  		//allocate memory
  		//allocate();
  
  		stim::envi_header h;
bb457896   David Mayerich   improved error me...
477
478
479
480
  		if (!h.load(headername)) {
  			std::cout << "Error loading header file: " << headername << std::endl;
  			return false;
  		}
f92397d2   David Mayerich   fixed a Win32 bug...
481
482
483
484
  
  		//load the header
  		//header.load(headername);
  
bb457896   David Mayerich   improved error me...
485
  		return open(filename, h, io);
e8eb202f   David Mayerich   added a new ENVI ...
486
487
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
488
489
490
491
492
  	/// Normalize a hyperspectral ENVI file given a band number and threshold.
  
  	/// @param outfile is the name of the normalized file to be output
  	/// @param band is the band label to be output
  	/// @param threshold is a threshold value specified such that normalization will only be done to values in the band > threshold (preventing division by small numbers)
c30dd3e6   David Mayerich   added vector norm...
493
  	bool ratio(std::string outfile, double band, unsigned char* mask = NULL, bool PROGRESS = false){
a2bf1d08   David Mayerich   general bug fixes...
494
  		header.save(outfile + ".hdr");
6708cc25   heziqi   Ziqi added envi c...
495
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
e8eb202f   David Mayerich   added a new ENVI ...
496
  			if(header.data_type ==envi_header::float32)
c30dd3e6   David Mayerich   added vector norm...
497
  				return ((bsq<float>*)file)->ratio(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
498
  			else if(header.data_type == envi_header::float64)
c30dd3e6   David Mayerich   added vector norm...
499
  				return ((bsq<double>*)file)->ratio(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
500
501
502
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
e8eb202f   David Mayerich   added a new ENVI ...
503
  
6708cc25   heziqi   Ziqi added envi c...
504
505
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
c30dd3e6   David Mayerich   added vector norm...
506
  				return ((bil<float>*)file)->ratio(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
507
  			else if(header.data_type == envi_header::float64)
c30dd3e6   David Mayerich   added vector norm...
508
  				return ((bil<double>*)file)->ratio(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
509
510
511
512
513
514
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
c30dd3e6   David Mayerich   added vector norm...
515
  				return ((bip<float>*)file)->ratio(outfile, band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
516
  			else if(header.data_type == envi_header::float64)
c30dd3e6   David Mayerich   added vector norm...
517
  				return ((bip<double>*)file)->ratio(outfile,band, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
518
519
520
521
522
523
524
525
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
e933c14e   David Mayerich   cleaned up the code
526
  		return false;
6708cc25   heziqi   Ziqi added envi c...
527
528
  	}
  
c30dd3e6   David Mayerich   added vector norm...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  	/// Perform vector normalization on the ENVI image
  	void normalize(std::string outfile, unsigned char* mask = NULL, bool PROGRESS = false){
  		header.save(outfile + ".hdr");
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
  				((bsq<float>*)file)->normalize(outfile, mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bsq<double>*)file)->normalize(outfile, mask, PROGRESS);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
  				((bil<float>*)file)->normalize(outfile, mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bil<double>*)file)->normalize(outfile, mask, PROGRESS);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
  				((bip<float>*)file)->normalize(outfile, mask, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bip<double>*)file)->normalize(outfile, mask, PROGRESS);
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
b31c02eb   David Mayerich   added a --select ...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  	bool select(std::string outfile, std::vector<double> bandlist, unsigned char* MASK = NULL, bool PROGRESS = false) {
  		stim::envi_header new_header = header;					//copy all of the data from the current header file to the new one
  		new_header.bands = bandlist.size();						//the number of bands in the new file is equal to the number of bands provided by the user
  		new_header.wavelength = bandlist;						//the wavelength values in the output file are the same as those specified by the user
  		new_header.band_names.empty();							//no band names will be provided in the output file
  		new_header.save(outfile + ".hdr");						//save the output header file
  
  		if (header.interleave == envi_header::BSQ) {		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
  				return ((bsq<float>*)file)->select(outfile, bandlist, MASK, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->select(outfile, bandlist, MASK, PROGRESS);
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  		else if (header.interleave == envi_header::BIL) {		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
  				return ((bil<float>*)file)->select(outfile, bandlist, MASK, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->select(outfile, bandlist, MASK, PROGRESS);
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  		else if (header.interleave == envi_header::BIP) {		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
  				return ((bip<float>*)file)->select(outfile, bandlist, MASK, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->select(outfile, bandlist, MASK, PROGRESS);
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
600
601
602
603
  	/// Performs piecewise linear baseline correction of a hyperspectral file/
  
  	/// @param outfile is the file name for the baseline corrected output
  	/// @param w is a list of band labels to serve as baseline points (zero values)
798cea97   David Mayerich   added progress ba...
604
605
  	bool baseline(std::string outfile, std::vector<double> w, unsigned char* mask = NULL, bool PROGRESS = false){
  		header.save(outfile + ".hdr");
6708cc25   heziqi   Ziqi added envi c...
606
607
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
608
  				return ((bsq<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
609
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
610
  				return ((bsq<double>*)file)->baseline(outfile,w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
611
612
613
614
615
616
617
618
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
619
  				return ((bil<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
620
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
621
  				return ((bil<double>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
622
623
624
625
626
627
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
f92397d2   David Mayerich   fixed a Win32 bug...
628
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
6708cc25   heziqi   Ziqi added envi c...
629
  			if(header.data_type ==envi_header::float32)
798cea97   David Mayerich   added progress ba...
630
  				return ((bip<float>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
631
  			else if(header.data_type == envi_header::float64)
798cea97   David Mayerich   added progress ba...
632
  				return ((bip<double>*)file)->baseline(outfile, w, mask, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
633
634
635
636
637
638
639
640
641
642
643
644
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  	}
  
dd5aab2f   David Mayerich   fixed errors in s...
645
646
647
  	/// Project an array of coefficients onto a basis matrix
  
  	void project(std::string outfile, double* center, double* basis, size_t M, std::vector<double> bands, unsigned char* mask, int cuda_device = 0, bool PROGRESS = false) {
63fc1df8   David Mayerich   added an inverse ...
648
649
650
651
652
653
654
655
656
657
658
659
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			std::cout<<"ERROR: BSQ projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			std::cout<<"ERROR: BIL projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
dd5aab2f   David Mayerich   fixed errors in s...
660
  				((bip<float>*)file)->project(outfile, center, basis, M, mask, cuda_device, PROGRESS);
63fc1df8   David Mayerich   added an inverse ...
661
  			else if(header.data_type == envi_header::float64)
dd5aab2f   David Mayerich   fixed errors in s...
662
  				((bip<double>*)file)->project(outfile, center, basis, M, mask, cuda_device, PROGRESS);
63fc1df8   David Mayerich   added an inverse ...
663
664
665
666
667
668
669
670
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		stim::envi_header out_hdr = header;							
  		out_hdr.bands = M;											//set the number of bands in the output header
dd5aab2f   David Mayerich   fixed errors in s...
671
  		out_hdr.wavelength = bands;
9d3ba0b1   David Mayerich   added stim::hsi a...
672
  		out_hdr.band_names.clear();
63fc1df8   David Mayerich   added an inverse ...
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
  		out_hdr.save(outfile + ".hdr");								//save the output header
  	}
  
  	void inverse(std::string outfile, double* center, double* basis, unsigned long long B, unsigned long long C = 0, bool PROGRESS = false){
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			std::cout<<"ERROR: BSQ projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			std::cout<<"ERROR: BIL projection not supported"<<std::endl;
  			exit(1);
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
  				((bip<float>*)file)->inverse(outfile, center, basis, B, C, PROGRESS);
  			else if(header.data_type == envi_header::float64)
  				((bip<double>*)file)->inverse(outfile, center, basis, B, C, PROGRESS);
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		stim::envi_header out_hdr = header;							
  		out_hdr.bands = B;											//set the number of bands in the output header
  		out_hdr.save(outfile + ".hdr");								//save the output header
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
703
704
705
706
  	/// Converts ENVI files between interleave types (BSQ, BIL, and BIP)
  
  	/// @param outfile is the file name for the converted output
  	/// @param interleave is the interleave format for the destination file
c6251f8b   David Mayerich   allows user to de...
707
  	bool convert(std::string outfile, stim::envi_header::interleaveType interleave, bool PROGRESS = false, bool VERBOSE = false, bool OPTIMIZATION = true){
a2bf1d08   David Mayerich   general bug fixes...
708
  		
6708cc25   heziqi   Ziqi added envi c...
709
710
  		if(header.interleave == envi_header::BSQ){			//if the infile is bsq file
  
a2bf1d08   David Mayerich   general bug fixes...
711
  			if(header.data_type ==envi_header::float32){		//ERROR
6708cc25   heziqi   Ziqi added envi c...
712
713
714
715
  				if(interleave == envi_header::BSQ){
  					std::cout<<"ERROR:  is already BSQ file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
716
  				else if(interleave == envi_header::BIL)			//convert BSQ -> BIL
c6251f8b   David Mayerich   allows user to de...
717
  					((bsq<float>*)file)->bil(outfile, PROGRESS, VERBOSE, OPTIMIZATION);
a2bf1d08   David Mayerich   general bug fixes...
718
  				else if(interleave == envi_header::BIP){			//ERROR
cf5b4c92   David Mayerich   simplified asynch...
719
  					//std::cout<<"ERROR: conversion from BSQ to BIP isn't practical; use BSQ->BIL->BIP instead"<<std::endl;
474c351a   David Mayerich   output timing dat...
720
  					((bsq<float>*)file)->bip(outfile, PROGRESS, VERBOSE, OPTIMIZATION);
cf5b4c92   David Mayerich   simplified asynch...
721
  					//exit(1);
63fc1df8   David Mayerich   added an inverse ...
722
  				}
6708cc25   heziqi   Ziqi added envi c...
723
724
725
  			}
  
  			else if(header.data_type == envi_header::float64){		//if the data type is float
a2bf1d08   David Mayerich   general bug fixes...
726
  				if(interleave == envi_header::BSQ){							//ERROR
6708cc25   heziqi   Ziqi added envi c...
727
728
729
  					std::cout<<"ERROR:  is already BSQ file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
730
  				else if(interleave == envi_header::BIL)					//convert BSQ -> BIL
c6251f8b   David Mayerich   allows user to de...
731
  					((bsq<double>*)file)->bil(outfile, PROGRESS, OPTIMIZATION);
a2bf1d08   David Mayerich   general bug fixes...
732
  				else if(interleave == envi_header::BIP){					//ERROR
cf5b4c92   David Mayerich   simplified asynch...
733
  					//std::cout<<"ERROR: conversion from BSQ to BIP isn't practical; use BSQ->BIL->BIP instead"<<std::endl;
f7cf19e4   David Mayerich   fixed warnings, t...
734
  					((bsq<double>*)file)->bip(outfile, PROGRESS, OPTIMIZATION);
cf5b4c92   David Mayerich   simplified asynch...
735
  					//exit(1);
63fc1df8   David Mayerich   added an inverse ...
736
  				}
6708cc25   heziqi   Ziqi added envi c...
737
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
738
  
6708cc25   heziqi   Ziqi added envi c...
739
740
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
741
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
742
743
744
745
746
  			}
  		}
  
  		else if(header.interleave == envi_header::BIL){
  
a2bf1d08   David Mayerich   general bug fixes...
747
  			if(header.data_type ==envi_header::float32){						//ERROR
6708cc25   heziqi   Ziqi added envi c...
748
749
750
751
  				if(interleave == envi_header::BIL){
  					std::cout<<"ERROR:  is already BIL file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
752
753
754
755
  				else if(interleave == envi_header::BSQ)							//BIL -> BSQ
  					((bil<float>*)file)->bsq(outfile, PROGRESS);
  				else if(interleave == envi_header::BIP)							//BIL -> BIP
  					((bil<float>*)file)->bip(outfile, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
756
757
  			}
  
a2bf1d08   David Mayerich   general bug fixes...
758
759
  			else if(header.data_type == envi_header::float64){
  				if(interleave == envi_header::BIL){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
760
761
762
  					std::cout<<"ERROR:  is already BIL file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
763
764
765
766
  				else if(interleave == envi_header::BSQ)							//BIL -> BSQ
  					((bil<double>*)file)->bsq(outfile, PROGRESS);
  				else if(interleave == envi_header::BIP)							//BIL -> BIP
  					((bil<double>*)file)->bip(outfile, PROGRESS);
6708cc25   heziqi   Ziqi added envi c...
767
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
768
  
6708cc25   heziqi   Ziqi added envi c...
769
770
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
771
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
772
773
774
775
  			}
  		}
  
  		else if(header.interleave == envi_header::BIP){
f92397d2   David Mayerich   fixed a Win32 bug...
776
  
a2bf1d08   David Mayerich   general bug fixes...
777
778
  			if(header.data_type ==envi_header::float32){
  				if(interleave == envi_header::BIP){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
779
780
781
  					std::cout<<"ERROR:  is already BIP file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
782
783
784
  				else if(interleave == envi_header::BIL)							//BIP -> BIL
  					((bip<float>*)file)->bil(outfile, PROGRESS);
  				else if(interleave == envi_header::BSQ){						//ERROR
63fc1df8   David Mayerich   added an inverse ...
785
786
787
788
  					std::cout<<"ERROR: conversion from BIP to BSQ isn't practical; use BIP->BIL->BSQ instead"<<std::endl;
  					//return ((bsq<float>*)file)->bip(outfile, PROGRESS);
  					exit(1);
  				}
6708cc25   heziqi   Ziqi added envi c...
789
790
  			}
  
a2bf1d08   David Mayerich   general bug fixes...
791
792
  			else if(header.data_type == envi_header::float64){
  				if(interleave == envi_header::BIP){								//ERROR
6708cc25   heziqi   Ziqi added envi c...
793
794
795
  					std::cout<<"ERROR:  is already BIP file"<<std::endl;
  					exit(1);
  				}
a2bf1d08   David Mayerich   general bug fixes...
796
797
798
  				else if(interleave == envi_header::BIL)							//BIP -> BIL
  					((bip<double>*)file)->bil(outfile, PROGRESS);
  				else if(interleave == envi_header::BSQ){						//ERROR
63fc1df8   David Mayerich   added an inverse ...
799
800
801
802
  					std::cout<<"ERROR: conversion from BIP to BSQ isn't practical; use BIP->BIL->BSQ instead"<<std::endl;
  					//return ((bsq<float>*)file)->bip(outfile, PROGRESS);
  					exit(1);
  				}
6708cc25   heziqi   Ziqi added envi c...
803
  			}
f92397d2   David Mayerich   fixed a Win32 bug...
804
  
6708cc25   heziqi   Ziqi added envi c...
805
806
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
f92397d2   David Mayerich   fixed a Win32 bug...
807
  				exit(1);
6708cc25   heziqi   Ziqi added envi c...
808
809
  			}
  		}
f92397d2   David Mayerich   fixed a Win32 bug...
810
  
6708cc25   heziqi   Ziqi added envi c...
811
812
813
814
  		else{
  			std::cout<<"ERROR: unidentified interleave type"<<std::endl;
  			exit(1);
  		}
a2bf1d08   David Mayerich   general bug fixes...
815
816
817
818
819
  		stim::envi_header h = header;
  		h.interleave = interleave;
  		h.save(outfile + ".hdr");
  
  		return true;
f92397d2   David Mayerich   fixed a Win32 bug...
820
  
6708cc25   heziqi   Ziqi added envi c...
821
822
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
823
824
825
826
827
  	/// Builds a mask from a band image and threshold value
  
  	/// @param mask_band is the label for the band that will be used to build the mask
  	/// @param threshold is a value selected such that all band values greater than threshold will have a mask value of 'true'
  	/// @param p is memory of size X*Y that will store the resulting mask
e37ce7bf   David Mayerich   added the ability...
828
  	bool build_mask(unsigned char* out_mask, double mask_band, double lower, double upper, unsigned char* mask = NULL, bool PROGRESS = false)	{
4a6f666c   heziqi   Added mask method
829
830
831
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
e37ce7bf   David Mayerich   added the ability...
832
  				return ((bsq<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
4a6f666c   heziqi   Added mask method
833
  			else if(header.data_type == envi_header::float64)
e37ce7bf   David Mayerich   added the ability...
834
  				return ((bsq<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
4a6f666c   heziqi   Added mask method
835
836
837
838
839
840
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
e37ce7bf   David Mayerich   added the ability...
841
  				return ((bil<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
4a6f666c   heziqi   Added mask method
842
  			else if(header.data_type == envi_header::float64)
e37ce7bf   David Mayerich   added the ability...
843
  				return ((bil<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
4a6f666c   heziqi   Added mask method
844
845
846
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
cacc3cdc   heziqi   attempt to fix CImg
847
  
4a6f666c   heziqi   Added mask method
848
849
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
e37ce7bf   David Mayerich   added the ability...
850
  				return ((bip<float>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
4a6f666c   heziqi   Added mask method
851
  			else if(header.data_type == envi_header::float64)
e37ce7bf   David Mayerich   added the ability...
852
  				return ((bip<double>*)file)->build_mask(out_mask, mask_band, lower, upper, mask, PROGRESS);
4a6f666c   heziqi   Added mask method
853
854
855
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
bba4f4d9   David Mayerich   minor warning fixes
856
857
  
  		return false;
740f8cd2   heziqi   added apply_mask
858
859
  	}
  
ba51ae6a   David Mayerich   fixed metric calc...
860
  	/// Creates a mask with a true value for all pixels that contain finite values
e37ce7bf   David Mayerich   added the ability...
861
  	void mask_finite(unsigned char* out_mask, unsigned char* mask = NULL, bool PROGRESS = false){
ba51ae6a   David Mayerich   fixed metric calc...
862
863
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
e37ce7bf   David Mayerich   added the ability...
864
  				((bsq<float>*)file)->mask_finite(out_mask, mask, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
865
  			else if(header.data_type == envi_header::float64)
e37ce7bf   David Mayerich   added the ability...
866
  				((bsq<double>*)file)->mask_finite(out_mask, mask, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
867
868
869
870
871
872
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
e37ce7bf   David Mayerich   added the ability...
873
  				((bil<float>*)file)->mask_finite(out_mask, mask, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
874
  			else if(header.data_type == envi_header::float64)
e37ce7bf   David Mayerich   added the ability...
875
  				((bil<double>*)file)->mask_finite(out_mask, mask, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
876
877
878
879
880
881
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
e37ce7bf   David Mayerich   added the ability...
882
  				((bip<float>*)file)->mask_finite(out_mask, mask, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
883
  			else if(header.data_type == envi_header::float64)
e37ce7bf   David Mayerich   added the ability...
884
  				((bip<double>*)file)->mask_finite(out_mask, mask, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
885
886
887
888
889
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
890
891
892
893
  	/// Applies a mask to the ENVI file.
  
  	/// @param outfile is the name of the resulting masked output file
  	/// @param p is memory of size X*Y containing the mask (0 = false, all other values are true)
6a46c8ff   David Mayerich   fixed bugs in app...
894
  	void apply_mask(std::string outfile, unsigned char* p, bool PROGRESS = false)
740f8cd2   heziqi   added apply_mask
895
  	{
e843658b   Brad Deutsch   Previous push did...
896
897
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
6a46c8ff   David Mayerich   fixed bugs in app...
898
  				((bsq<float>*)file)->apply_mask(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
899
  			else if (header.data_type == envi_header::float64)
6a46c8ff   David Mayerich   fixed bugs in app...
900
  				((bsq<double>*)file)->apply_mask(outfile, p, PROGRESS);
740f8cd2   heziqi   added apply_mask
901
  			else
e843658b   Brad Deutsch   Previous push did...
902
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
903
904
  		}
  
e843658b   Brad Deutsch   Previous push did...
905
906
  		else if (header.interleave == envi_header::BIL){		//if the infile is bil file
  			if (header.data_type == envi_header::float32)
6a46c8ff   David Mayerich   fixed bugs in app...
907
  				((bil<float>*)file)->apply_mask(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
908
  			else if (header.data_type == envi_header::float64)
6a46c8ff   David Mayerich   fixed bugs in app...
909
  				((bil<double>*)file)->apply_mask(outfile, p, PROGRESS);
740f8cd2   heziqi   added apply_mask
910
  			else
e843658b   Brad Deutsch   Previous push did...
911
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
912
913
  		}
  
e843658b   Brad Deutsch   Previous push did...
914
915
  		else if (header.interleave == envi_header::BIP){		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
6a46c8ff   David Mayerich   fixed bugs in app...
916
  				((bip<float>*)file)->apply_mask(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
917
  			else if (header.data_type == envi_header::float64)
6a46c8ff   David Mayerich   fixed bugs in app...
918
  				((bip<double>*)file)->apply_mask(outfile, p, PROGRESS);
740f8cd2   heziqi   added apply_mask
919
  			else
e843658b   Brad Deutsch   Previous push did...
920
  				std::cout << "ERROR: unidentified data type" << std::endl;
740f8cd2   heziqi   added apply_mask
921
  		}
4a6f666c   heziqi   Added mask method
922
923
  
  		else{
e843658b   Brad Deutsch   Previous push did...
924
925
926
  			std::cout << "ERROR: unidentified file type" << std::endl;
  			exit(1);
  		}
6a46c8ff   David Mayerich   fixed bugs in app...
927
  		header.save(outfile + ".hdr");
e843658b   Brad Deutsch   Previous push did...
928
929
  	}
  
3d0b6243   David Mayerich   fixed hsiproc bug...
930
931
932
  	/// Copies all spectra corresponding to nonzero values of a mask into a pre-allocated matrix of size (P x B)
  	///		where P is the number of masked pixels and B is the number of bands. The allocated memory can be accessed
  	///		using the following indexing: i = b*P + p
43dec788   David Mayerich   added code for si...
933
934
  	/// @param matrix is the destination for the pixel data
  	/// @param p is the mask
814eb271   David Mayerich   fixed problems wi...
935
  	bool sift(void* matrix, unsigned char* p = NULL, bool PROGRESS = false){
43dec788   David Mayerich   added code for si...
936
937
938
  
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
814eb271   David Mayerich   fixed problems wi...
939
  				return ((bsq<float>*)file)->sift((float*)matrix, p, PROGRESS);
43dec788   David Mayerich   added code for si...
940
  			else if (header.data_type == envi_header::float64)
814eb271   David Mayerich   fixed problems wi...
941
  				return ((bsq<double>*)file)->sift((double*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
942
  			else{
43dec788   David Mayerich   added code for si...
943
  				std::cout << "ERROR: unidentified data type" << std::endl;
ba51ae6a   David Mayerich   fixed metric calc...
944
945
  				exit(1);
  			}
43dec788   David Mayerich   added code for si...
946
  		}
3d0b6243   David Mayerich   fixed hsiproc bug...
947
948
  
  		if (header.interleave == envi_header::BIP){
ba51ae6a   David Mayerich   fixed metric calc...
949
  			if (header.data_type == envi_header::float32)
814eb271   David Mayerich   fixed problems wi...
950
  				return ((bip<float>*)file)->sift((float*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
951
  			else if (header.data_type == envi_header::float64)
814eb271   David Mayerich   fixed problems wi...
952
  				return ((bip<double>*)file)->sift((double*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
953
954
955
956
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
3d0b6243   David Mayerich   fixed hsiproc bug...
957
958
  		}
  		if (header.interleave == envi_header::BIL){
ba51ae6a   David Mayerich   fixed metric calc...
959
  			if (header.data_type == envi_header::float32)
814eb271   David Mayerich   fixed problems wi...
960
  				return ((bil<float>*)file)->sift((float*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
961
  			else if (header.data_type == envi_header::float64)
814eb271   David Mayerich   fixed problems wi...
962
  				return ((bil<double>*)file)->sift((double*)matrix, p, PROGRESS);
ba51ae6a   David Mayerich   fixed metric calc...
963
964
965
966
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
3d0b6243   David Mayerich   fixed hsiproc bug...
967
  		}
43dec788   David Mayerich   added code for si...
968
969
970
971
  		return false;
  
  	}
  
3d0b6243   David Mayerich   fixed hsiproc bug...
972
  	/// Saves in an array only those spectra corresponding to nonzero values of the mask.
43dec788   David Mayerich   added code for si...
973
974
  	/// @param outfile is the name of the sifted output file
  	/// @param p is the mask
c1078e19   David Mayerich   HSIproc bug fixes...
975
  	bool sift(std::string outfile, unsigned char* p, bool PROGRESS = false)
e843658b   Brad Deutsch   Previous push did...
976
977
  	{
  
2fcab4f0   David Mayerich   added unsifting f...
978
  		//calculate the number of non-zero values in the mask
9d3ba0b1   David Mayerich   added stim::hsi a...
979
980
981
  		unsigned long long nnz = 0;
  		unsigned long long npixels = header.lines * header.samples;
  		for(unsigned long long i = 0; i < npixels; i++)
2fcab4f0   David Mayerich   added unsifting f...
982
983
984
985
986
  			if( p[i] > 0 ) nnz++;
  
  		//create a new header
  		envi_header new_header = header;
  
f7e99517   David Mayerich   added sifting for...
987
988
  		//if a BIL file is sifted, it's saved as a BIP
  		if(header.interleave == envi_header::BIL)
f92397d2   David Mayerich   fixed a Win32 bug...
989
  			new_header.interleave = envi_header::BIP;
f7e99517   David Mayerich   added sifting for...
990
  
2fcab4f0   David Mayerich   added unsifting f...
991
992
993
994
995
  		//set the number of lines to 1 (this is a matrix with 1 line and N samples)
  		new_header.lines = 1;
  		new_header.samples = nnz;
  		new_header.save(outfile + ".hdr");
  
e843658b   Brad Deutsch   Previous push did...
996
997
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
c1078e19   David Mayerich   HSIproc bug fixes...
998
  				return ((bsq<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
999
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
1000
  				return ((bsq<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
1001
1002
1003
1004
1005
1006
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIL){		//if the infile is bil file
  			if (header.data_type == envi_header::float32)
c1078e19   David Mayerich   HSIproc bug fixes...
1007
  				return ((bil<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
1008
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
1009
  				return ((bil<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
1010
1011
1012
1013
1014
1015
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIP){		//if the infile is bip file
  			if (header.data_type == envi_header::float32)
c1078e19   David Mayerich   HSIproc bug fixes...
1016
  				return ((bip<float>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
1017
  			else if (header.data_type == envi_header::float64)
c1078e19   David Mayerich   HSIproc bug fixes...
1018
  				return ((bip<double>*)file)->sift(outfile, p, PROGRESS);
e843658b   Brad Deutsch   Previous push did...
1019
1020
1021
1022
1023
1024
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else{
  			std::cout << "ERROR: unidentified file type" << std::endl;
4a6f666c   heziqi   Added mask method
1025
1026
  			exit(1);
  		}
4a6f666c   heziqi   Added mask method
1027
  		return false;
cacc3cdc   heziqi   attempt to fix CImg
1028
1029
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
1030
  	bool unsift(std::string outfile, unsigned char* mask, unsigned long long samples, unsigned long long lines, bool PROGRESS = false){
2fcab4f0   David Mayerich   added unsifting f...
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  
  		//create a new header
  		envi_header new_header = header;
  
  		//set the number of lines and samples in the output file (that's all that changes)
  		new_header.lines = lines;
  		new_header.samples = samples;
  		new_header.save(outfile + ".hdr");
  
  
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
a2bf1d08   David Mayerich   general bug fixes...
1043
  				return ((bsq<float>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
2fcab4f0   David Mayerich   added unsifting f...
1044
  			else if (header.data_type == envi_header::float64)
a2bf1d08   David Mayerich   general bug fixes...
1045
  				return ((bsq<double>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
2fcab4f0   David Mayerich   added unsifting f...
1046
1047
1048
1049
1050
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIL){		//if the infile is bil file
f92397d2   David Mayerich   fixed a Win32 bug...
1051
  
2fcab4f0   David Mayerich   added unsifting f...
1052
1053
1054
1055
  				std::cout << "ERROR in stim::envi::unsift - BIL files aren't supported yet" << std::endl;
  		}
  
  		else if (header.interleave == envi_header::BIP){		//if the infile is bip file
f92397d2   David Mayerich   fixed a Win32 bug...
1056
  
8550e94f   David Mayerich   added sifting and...
1057
  			if (header.data_type == envi_header::float32)
a2bf1d08   David Mayerich   general bug fixes...
1058
  				return ((bip<float>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
8550e94f   David Mayerich   added sifting and...
1059
  			else if (header.data_type == envi_header::float64)
a2bf1d08   David Mayerich   general bug fixes...
1060
  				return ((bip<double>*)file)->unsift(outfile, mask, samples, lines, PROGRESS);
8550e94f   David Mayerich   added sifting and...
1061
1062
  			else
  				std::cout << "ERROR: unidentified data type" << std::endl;
2fcab4f0   David Mayerich   added unsifting f...
1063
1064
1065
1066
  		}
  
  		else{
  			std::cout << "ERROR: unidentified file type" << std::endl;
2fcab4f0   David Mayerich   added unsifting f...
1067
  		}
cbce396e   David Mayerich   added support for...
1068
  		return 0;
2fcab4f0   David Mayerich   added unsifting f...
1069
1070
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1071
1072
1073
1074
1075
1076
1077
1078
1079
  	/// Compute the ratio of two baseline-corrected peaks. The result is stored in a pre-allocated array.
  
  	/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
  	/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
  	/// @param pos1 is the label value for the first peak (numerator) position
  	/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
  	/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
  	/// @param pos2 is the label value for the second peak (denominator) position
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
1080
  	bool ph_to_ph(void * result, double lb1, double rb1, double pos1, double lb2, double rb2, double pos2, unsigned char* mask){
20c212c0   heziqi   Ziqi added functi...
1081
1082
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1083
  				return ((bsq<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
1084
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1085
  				return ((bsq<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
1086
  			else
11ff9d7e   David Mayerich   improved user out...
1087
  				std::cout<<"envi::ph_to_ph ERROR - unidentified data type"<<std::endl;
20c212c0   heziqi   Ziqi added functi...
1088
1089
1090
1091
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1092
  				return ((bil<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
1093
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1094
  				return ((bil<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
1095
  			else
11ff9d7e   David Mayerich   improved user out...
1096
  				std::cout<<"envi::ph_to_ph ERROR - unidentified data type"<<std::endl;
20c212c0   heziqi   Ziqi added functi...
1097
1098
1099
1100
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1101
  				return ((bip<float>*)file)->ph_to_ph((float*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
20c212c0   heziqi   Ziqi added functi...
1102
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1103
  				return ((bip<double>*)file)->ph_to_ph((double*)result, lb1, rb1, pos1, lb2, rb2, pos2, mask);
70407ea9   heziqi   Ziqi completed he...
1104
  			else
11ff9d7e   David Mayerich   improved user out...
1105
  				std::cout<<"envi::ph_to_ph ERROR - unidentified data type"<<std::endl;
70407ea9   heziqi   Ziqi completed he...
1106
1107
1108
  		}
  
  		else{
11ff9d7e   David Mayerich   improved user out...
1109
  			std::cout<<"envi::ph_to_ph ERROR - unidentified file type"<<std::endl;
70407ea9   heziqi   Ziqi completed he...
1110
1111
1112
1113
1114
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1115
1116
1117
1118
1119
1120
1121
1122
1123
  	/// Compute the ratio between a peak area and peak height.
  
  	/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
  	/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
  	/// @param pos1 is the label value for the first peak (numerator) position
  	/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
  	/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
  	/// @param pos2 is the label value for the second peak (denominator) position
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
1124
  	bool pa_to_ph(void* result, double lb1, double rb1, double lab1, double rab1, double lb2, double rb2, double pos, unsigned char* mask = NULL){
70407ea9   heziqi   Ziqi completed he...
1125
1126
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1127
  				return ((bsq<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
1128
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1129
  				return ((bsq<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
1130
1131
1132
1133
1134
1135
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1136
  				return ((bil<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
1137
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1138
  				return ((bil<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
1139
1140
1141
1142
1143
1144
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1145
  				return ((bip<float>*)file)->pa_to_ph((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
1146
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1147
  				return ((bip<double>*)file)->pa_to_ph((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, pos, mask);
70407ea9   heziqi   Ziqi completed he...
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1159
1160
1161
1162
1163
1164
1165
1166
1167
  	/// Compute the ratio between two peak areas.
  
  	/// @param lb1 is the label value for the left baseline point for the first peak (numerator)
  	/// @param rb1 is the label value for the right baseline point for the first peak (numerator)
  	/// @param lab1 is the label value for the left bound (start of the integration) of the first peak (numerator)
  	/// @param rab1 is the label value for the right bound (end of the integration) of the first peak (numerator)
  	/// @param lb2 is the label value for the left baseline point for the second peak (denominator)
  	/// @param rb2 is the label value for the right baseline point for the second peak (denominator)
  	/// @param lab2 is the label value for the left bound (start of the integration) of the second peak (denominator)
f92397d2   David Mayerich   fixed a Win32 bug...
1168
  	/// @param rab2 is the label value for the right bound (end of the integration) of the second peak (denominator)
cb455f98   David Mayerich   removed old ENVI ...
1169
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
1170
1171
  	bool pa_to_pa(void* result, double lb1, double rb1, double lab1, double rab1,
  					double lb2, double rb2, double lab2, double rab2, unsigned char* mask = NULL){
70407ea9   heziqi   Ziqi completed he...
1172
1173
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1174
  				return ((bsq<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
1175
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1176
  				return ((bsq<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
1177
1178
1179
1180
1181
1182
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1183
  				return ((bil<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
1184
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1185
  				return ((bil<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
1186
1187
1188
1189
1190
1191
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1192
  				return ((bip<float>*)file)->pa_to_pa((float*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
70407ea9   heziqi   Ziqi completed he...
1193
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1194
  				return ((bip<double>*)file)->pa_to_pa((double*)result, lb1, rb1, lab1, rab1, lb2, rb2, lab2, rab2, mask);
20c212c0   heziqi   Ziqi added functi...
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1206
1207
1208
1209
1210
1211
1212
  	/// Compute the centroid of a baseline corrected peak.
  
  	/// @param lb is the label value for the left baseline point
  	/// @param rb is the label value for the right baseline point
  	/// @param lab is the label for the start of the peak
  	/// @param rab is the label for the end of the peak
  	/// @param result is a pointer to a pre-allocated array at least X * Y * sizeof(T) in size
ba51ae6a   David Mayerich   fixed metric calc...
1213
  	bool centroid(void* result, double lb1, double rb1, double lab1, double rab1, unsigned char* mask = NULL){
517876d6   heziqi   metrics finished ...
1214
1215
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1216
  				return ((bsq<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1217
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1218
  				return ((bsq<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1219
1220
1221
1222
1223
1224
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIL){		//if the infile is bil file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1225
  				return ((bil<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1226
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1227
  				return ((bil<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1228
1229
1230
1231
1232
1233
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else if(header.interleave == envi_header::BIP){		//if the infile is bip file
  			if(header.data_type ==envi_header::float32)
ba51ae6a   David Mayerich   fixed metric calc...
1234
  				return ((bip<float>*)file)->centroid((float*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1235
  			else if(header.data_type == envi_header::float64)
ba51ae6a   David Mayerich   fixed metric calc...
1236
  				return ((bip<double>*)file)->centroid((double*)result, lb1, rb1, lab1, rab1, mask);
517876d6   heziqi   metrics finished ...
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
  			else
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  		}
  
  		else{
  			std::cout<<"ERROR: unidentified file type"<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1248
  	/// Closes the ENVI file.
edb975b0   David Mayerich   created a stim::e...
1249
1250
  	void close(){
  		if(file == NULL) return;
6708cc25   heziqi   Ziqi added envi c...
1251
1252
  		if(header.interleave == envi_header::BSQ){
  			if(header.data_type ==envi_header::float32)
edb975b0   David Mayerich   created a stim::e...
1253
  				((bsq<float>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1254
  			else if(header.data_type == envi_header::float64)
edb975b0   David Mayerich   created a stim::e...
1255
  				((bsq<double>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1256
1257
1258
1259
1260
1261
1262
1263
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		else if(header.interleave == envi_header::BIL){
  			if(header.data_type ==envi_header::float32)
edb975b0   David Mayerich   created a stim::e...
1264
  				((bil<float>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1265
  			else if(header.data_type == envi_header::float64)
edb975b0   David Mayerich   created a stim::e...
1266
  				((bil<double>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1267
1268
1269
1270
1271
1272
1273
1274
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  
  		else if(header.interleave == envi_header::BIP){
  			if(header.data_type ==envi_header::float32)
edb975b0   David Mayerich   created a stim::e...
1275
  				((bip<float>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1276
  			else if(header.data_type == envi_header::float64)
edb975b0   David Mayerich   created a stim::e...
1277
  				((bip<double>*)file)->close();
6708cc25   heziqi   Ziqi added envi c...
1278
1279
1280
1281
1282
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
e8eb202f   David Mayerich   added a new ENVI ...
1283
  	}
cb455f98   David Mayerich   removed old ENVI ...
1284
1285
1286
1287
1288
  
  	/// Retrieve a single pixel and stores it in pre-allocated memory.
  
  	/// @param p is a pointer to pre-allocated memory at least sizeof(T) in size.
  	/// @param n is an integer index to the pixel using linear array indexing.
bfe9c6db   heziqi   added feature_mat...
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
  	bool pixel(void * p, unsigned n){
  		if(header.interleave == envi_header::BSQ){
  			if(header.data_type ==envi_header::float32)
  				return ((bsq<float>*)file)->pixel((float*)p, n);
  			else if(header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->pixel((double*)p, n);
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  		else if(header.interleave == envi_header::BIL){
  			if(header.data_type ==envi_header::float32)
  				return ((bil<float>*)file)->pixel((float*)p, n);
  			else if(header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->pixel((double*)p, n);
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  		else if(header.interleave == envi_header::BIP){
  			if(header.data_type ==envi_header::float32)
  				return ((bip<float>*)file)->pixel((float*)p, n);
  			else if(header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->pixel((double*)p, n);
  			else{
  				std::cout<<"ERROR: unidentified data type"<<std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
e8eb202f   David Mayerich   added a new ENVI ...
1322
  
cb455f98   David Mayerich   removed old ENVI ...
1323
  	/// Saves a header file describing the current ENVI file parameters.
e8eb202f   David Mayerich   added a new ENVI ...
1324
1325
1326
1327
1328
1329
1330
1331
  	bool save_header(std::string filename){
  
  		//save the header file here
  		header.save(filename);
  
  		return true;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1332
1333
1334
1335
  	/// Retrieve a single band (by numerical label) and stores it in pre-allocated memory.
  
  	/// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
  	/// @param wavelength is a floating point value (usually a wavelength in spectral data) used as a label for the band to be copied.
63fc1df8   David Mayerich   added an inverse ...
1336
  	bool band(void* ptr, double wavelength, bool PROGRESS = false){
d1d8ef91   David Mayerich   added band readin...
1337
1338
1339
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1340
  				return ((bsq<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1341
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1342
  				return ((bsq<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1343
1344
1345
1346
1347
1348
1349
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1350
  				return ((bil<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1351
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1352
  				return ((bil<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1353
1354
1355
1356
1357
1358
1359
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1360
  				return ((bip<float>*)file)->band((float*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1361
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1362
  				return ((bip<double>*)file)->band((double*)ptr, wavelength, PROGRESS);
081f2c9f   heziqi   made bip bil file...
1363
1364
1365
1366
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
d1d8ef91   David Mayerich   added band readin...
1367
  		}
ee4dea28   David Mayerich   fixed errors in c...
1368
  		return false;
1582ce07   David Mayerich   added band_index(...
1369
1370
  	}
  
735a2a24   David Mayerich   started testing o...
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
  	// Retrieve a spectrum at the specified 1D location
  
  	/// @param ptr is a pointer to pre-allocated memory of size B*sizeof(T)
  	/// @param x is the 1D coordinate of the spectrum
  	template<typename T>
  	void spectrum(T* ptr, size_t n, bool PROGRESS = false){
  
  		void* temp = alloc_array(header.bands);		//allocate space for the output array
  
  		if(header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if(header.data_type ==envi_header::float32){
  				((bsq<float>*)file)->spectrum((float*)temp, n, PROGRESS);
  				cast<T, float>(ptr, (float*)temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bsq<double>*)file)->spectrum((double*)temp, n, PROGRESS);
  				cast<T, double>(ptr, (double*)temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32){
  				((bil<float>*)file)->spectrum((float*)temp, n, PROGRESS);
  				cast<T, float>(ptr, (float*)temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bil<double>*)file)->spectrum((double*)temp, n, PROGRESS);
  				cast<T, double>(ptr, (double*)temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32){
  				((bip<float>*)file)->spectrum((float*)temp, n, PROGRESS);
d4aa4cf7   David Mayerich   fixed the in-memo...
1411
  				float test = ((float*)temp)[0];
735a2a24   David Mayerich   started testing o...
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
  				cast<T, float>(ptr, (float*)temp, header.bands);
  			}
  			else if (header.data_type == envi_header::float64){
  				((bip<double>*)file)->spectrum((double*)temp, n, PROGRESS);
  				cast<T, double>(ptr, (double*)temp, header.bands);
  			}
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  	}
  
  	/// Retrieve a spectrum from the specified (x, y) location
  
  	/// @param ptr is a pointer to pre-allocated memory of size B*sizeof(T)
  	/// @param x is the x-coordinate of the spectrum
  	/// @param y is the y-coordinate of the spectrum
  	template<typename T>
d0ef98e3   David Mayerich   fixed a Linux com...
1431
  	void spectrum(T* ptr, size_t x, size_t y, bool PROGRESS = false){
735a2a24   David Mayerich   started testing o...
1432
  
c4dd399a   David Mayerich   fixed a Linux com...
1433
  		spectrum<T>(ptr, y * header.samples + x, PROGRESS);
724ec347   David Mayerich   simplified the EN...
1434
1435
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1436
1437
1438
1439
  	/// Retrieve a single band (based on index) and stores it in pre-allocated memory.
  
  	/// @param p is a pointer to an allocated region of memory at least X * Y * sizeof(T) in size.
  	/// @param page <= B is the integer number of the band to be copied.
9d3ba0b1   David Mayerich   added stim::hsi a...
1440
  	bool band_index(void* ptr, unsigned long long b){
081f2c9f   heziqi   made bip bil file...
1441
1442
  		if (header.interleave == envi_header::BSQ){		//if the infile is bsq file
  			if (header.data_type == envi_header::float32)
1582ce07   David Mayerich   added band_index(...
1443
  				return ((bsq<float>*)file)->band_index((float*)ptr, b);
081f2c9f   heziqi   made bip bil file...
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->band_index((double*)ptr, b);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
  				return ((bil<float>*)file)->band_index((float*)ptr, b);
  			else if (header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->band_index((double*)ptr, b);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				return ((bip<float>*)file)->band_index((float*)ptr, b);
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->band_index((double*)ptr, b);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
1582ce07   David Mayerich   added band_index(...
1470
1471
  		}
  		return false;
d1d8ef91   David Mayerich   added band readin...
1472
  	}
f92397d2   David Mayerich   fixed a Win32 bug...
1473
  
cb455f98   David Mayerich   removed old ENVI ...
1474
1475
1476
1477
  	/// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum
  
  	/// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum
  	/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
35a7195f   David Mayerich   added median spec...
1478
  	bool mean_spectrum(double * p, double* std, unsigned char* mask, bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1479
1480
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
35a7195f   David Mayerich   added median spec...
1481
  				return ((bsq<float>*)file)->mean_spectrum(p, std, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1482
  			else if (header.data_type == envi_header::float64)
35a7195f   David Mayerich   added median spec...
1483
  				return ((bsq<double>*)file)->mean_spectrum(p, std, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1484
1485
1486
1487
1488
1489
1490
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
35a7195f   David Mayerich   added median spec...
1491
  				return ((bil<float>*)file)->mean_spectrum(p, std, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1492
  			else if (header.data_type == envi_header::float64)
35a7195f   David Mayerich   added median spec...
1493
  				return ((bil<double>*)file)->mean_spectrum(p, std, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1494
1495
1496
1497
1498
1499
1500
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
35a7195f   David Mayerich   added median spec...
1501
  				return ((bip<float>*)file)->mean_spectrum(p, std, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1502
  			else if (header.data_type == envi_header::float64)
35a7195f   David Mayerich   added median spec...
1503
  				return ((bip<double>*)file)->mean_spectrum(p, std, mask, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1504
1505
1506
1507
1508
1509
1510
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
e8eb202f   David Mayerich   added a new ENVI ...
1511
  
35a7195f   David Mayerich   added median spec...
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
  	/// Calculate the mean value for all masked (or valid) pixels in a band and returns the average spectrum
  
  	/// @param p is a pointer to pre-allocated memory of size [B * sizeof(T)] that stores the mean spectrum
  	/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
  	bool median_spectrum(double* m, unsigned char* mask, bool PROGRESS = false){
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				return ((bsq<float>*)file)->median_spectrum(m, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->median_spectrum(m, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else{
  			std::cout<<"ERROR: median calculation is only supported for BSQ interleave types. Convert to process."<<std::endl;
  			exit(1);
  		}
  		return false;
  	}
  
cb455f98   David Mayerich   removed old ENVI ...
1534
1535
1536
1537
1538
  	/// Calculate the covariance matrix for all masked pixels in the image.
  
  	/// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix
  	/// @param avg is a pointer to memory of size B that stores the average spectrum
  	/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
dd5aab2f   David Mayerich   fixed errors in s...
1539
  	bool co_matrix(double* co, double* avg, unsigned char* mask, int cuda_device, bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1540
  		if (header.interleave == envi_header::BSQ){
63fc1df8   David Mayerich   added an inverse ...
1541
1542
  			std::cout<<"ERROR: calculating the covariance matrix for a BSQ file is impractical; convert to BIL or BIP first"<<std::endl;
  			exit(1);
a43c4fe1   heziqi   Added crop in env...
1543
1544
1545
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
dd5aab2f   David Mayerich   fixed errors in s...
1546
  				return ((bil<float>*)file)->co_matrix(co, avg, mask, cuda_device, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1547
  			else if (header.data_type == envi_header::float64)
dd5aab2f   David Mayerich   fixed errors in s...
1548
  				return ((bil<double>*)file)->co_matrix(co, avg, mask, cuda_device, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1549
1550
1551
1552
1553
1554
1555
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
dd5aab2f   David Mayerich   fixed errors in s...
1556
  				return ((bip<float>*)file)->co_matrix(co, avg, mask, cuda_device, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1557
  			else if (header.data_type == envi_header::float64)
dd5aab2f   David Mayerich   fixed errors in s...
1558
  				return ((bip<double>*)file)->co_matrix(co, avg, mask, cuda_device, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1559
1560
1561
1562
1563
1564
1565
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
0df38ff3   heziqi   fixed head detached
1566
  
334547d8   sam   changes for mnf: ...
1567
1568
1569
1570
1571
  	/// Calculate the covariance of noise matrix for all masked pixels in the image.
  
  	/// @param co is a pointer to pre-allocated memory of size [B * B] that stores the resulting covariance matrix
  	/// @param avg is a pointer to memory of size B that stores the average spectrum
  	/// @param mask is a pointer to memory of size [X * Y] that stores the mask value at each pixel location
dd5aab2f   David Mayerich   fixed errors in s...
1572
  	bool coNoise_matrix(double* coN, double* avg, unsigned char* mask, int cuda_device = 0, bool PROGRESS = false){
334547d8   sam   changes for mnf: ...
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
  		if (header.interleave == envi_header::BSQ){
  			std::cout<<"ERROR: calculating the covariance matrix of noise for a BSQ file is impractical; convert to BIP first"<<std::endl;
  			exit(1);
  		}
  
  
  		else if (header.interleave == envi_header::BIL){
  		        std::cout<<"ERROR: calculating the covariance matrix of noise for a BIL file is impractical; convert to BIP first"<<std::endl;
                  exit(1);
  			 }
  
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
dd5aab2f   David Mayerich   fixed errors in s...
1586
  				return ((bip<float>*)file)->coNoise_matrix(coN, avg, mask, cuda_device, PROGRESS);
334547d8   sam   changes for mnf: ...
1587
  			else if (header.data_type == envi_header::float64)
dd5aab2f   David Mayerich   fixed errors in s...
1588
  				return ((bip<double>*)file)->coNoise_matrix(coN, avg, mask, cuda_device, PROGRESS);
334547d8   sam   changes for mnf: ...
1589
1590
1591
1592
1593
1594
1595
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
e8eb202f   David Mayerich   added a new ENVI ...
1596
  
cb455f98   David Mayerich   removed old ENVI ...
1597
1598
1599
1600
1601
1602
1603
  	/// Crop a region of the image and save it to a new file.
  
  	/// @param outfile is the file name for the new cropped image
  	/// @param x0 is the lower-left x pixel coordinate to be included in the cropped image
  	/// @param y0 is the lower-left y pixel coordinate to be included in the cropped image
  	/// @param x1 is the upper-right x pixel coordinate to be included in the cropped image
  	/// @param y1 is the upper-right y pixel coordinate to be included in the cropped image
c8c976a9   David Mayerich   replaced CImg wit...
1604
1605
1606
1607
1608
1609
1610
1611
  	bool crop(std::string outfile,
  			  unsigned long long x0, 
  			  unsigned long long y0, 
  			  unsigned long long x1, 
  			  unsigned long long y1, 
  			  unsigned long long b0, 
  			  unsigned long long b1, 
  			  bool PROGRESS = false){
a43c4fe1   heziqi   Added crop in env...
1612
  
177c7863   David Mayerich   updated the ENVI ...
1613
1614
  		//save the header for the cropped file
  		stim::envi_header new_header = header;
f78ceb64   David Mayerich   fixed BIP croppin...
1615
1616
1617
  		new_header.samples = x1 - x0 + 1;
  		new_header.lines = y1 - y0 + 1;
  		new_header.bands = b1 - b0 + 1;
177c7863   David Mayerich   updated the ENVI ...
1618
  		std::vector<double>::const_iterator first = new_header.wavelength.begin() + b0;
f78ceb64   David Mayerich   fixed BIP croppin...
1619
  		std::vector<double>::const_iterator last = new_header.wavelength.begin() + b1 + 1;
177c7863   David Mayerich   updated the ENVI ...
1620
1621
1622
  		new_header.wavelength = std::vector<double>(first, last);
  		new_header.save(outfile + ".hdr");
  
a43c4fe1   heziqi   Added crop in env...
1623
1624
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1625
  				return ((bsq<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1626
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1627
  				return ((bsq<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1628
1629
1630
1631
1632
1633
1634
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1635
  				return ((bil<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1636
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1637
  				return ((bil<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1638
1639
1640
1641
1642
1643
1644
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
63fc1df8   David Mayerich   added an inverse ...
1645
  				return ((bip<float>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1646
  			else if (header.data_type == envi_header::float64)
63fc1df8   David Mayerich   added an inverse ...
1647
  				return ((bip<double>*)file)->crop(outfile, x0, y0, x1, y1, b0, b1, PROGRESS);
a43c4fe1   heziqi   Added crop in env...
1648
1649
1650
1651
1652
1653
1654
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		return false;
  	}
0df38ff3   heziqi   fixed head detached
1655
  
bb61cb18   David Mayerich   added subimage fu...
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
  	void subimages(std::string outfile, size_t nx, size_t ny, unsigned char* mask, bool PROGRESS = false){
  		
  		size_t nnz = 0;													//initialize the number of subimages to zero
  		for(size_t i = 0; i < header.lines * header.samples; i++)		//for each pixel in the mask
  			if(mask[i]) nnz++;											//if the pixel is valid, add a subimage
  
  
  		//save the header for the cropped file
  		stim::envi_header new_header = header;
  		new_header.samples = nx;									//calculate the width of the output image (concatenated subimages)
  		new_header.lines = nnz * ny;											//calculate the height of the output image (height of subimages)
  		
  
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				((bsq<float>*)file)->subimages(outfile, nx, ny, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bsq<double>*)file)->subimages(outfile, nx, ny, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			std::cout << "ERROR: unidentified data type" << std::endl;
  			exit(1);
  		}
  		else if (header.interleave == envi_header::BIP){
  			std::cout << "ERROR: unidentified data type" << std::endl;
  			exit(1);
  		}
  
  		new_header.save(outfile + ".hdr");									//save the header for the output file
  	}
  
dc8eb8aa   David Mayerich   added band trimmi...
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
  	/// Remove a list of bands from the ENVI file
  
  	/// @param outfile is the file name for the output hyperspectral image (with trimmed bands)
  	/// @param b is an array of bands to be eliminated
  	void trim(std::string outfile, std::vector<size_t> trimmed, bool PROGRESS = false){
  		
  		envi_header h = header;
  		h.bands = header.bands - trimmed.size();			//calculate the new number of bands
  		if(header.wavelength.size() != 0)
  			h.wavelength.resize(h.bands);
  		if(header.band_names.size() != 0)
  			h.band_names.resize(h.bands);
  		size_t it = 0;									//allocate an index into the trimmed bands array
  		size_t i = 0;
  		for(size_t b = 0; b < header.bands; b++){		//for each band
  			if(b != trimmed[it]){
  				if(h.wavelength.size()) h.wavelength[i] = header.wavelength[b];
  				if(h.band_names.size()) h.band_names[i] = header.band_names[i];
  				i++;
  			}
  			else it++;
  		}
  		h.save(outfile + ".hdr");
  
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				return ((bsq<float>*)file)->trim(outfile, trimmed, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bsq<double>*)file)->trim(outfile, trimmed, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
  				return ((bil<float>*)file)->trim(outfile, trimmed, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bil<double>*)file)->trim(outfile, trimmed, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				return ((bip<float>*)file)->trim(outfile, trimmed, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				return ((bip<double>*)file)->trim(outfile, trimmed, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		
  
  	}
  
2ce6954b   David Mayerich   added the ability...
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
  	/// Combine two ENVI images along the Y axis
  
  	/// @param outfile is the combined file to be output
  	/// @param C is the ENVI object for the image to be combined
  	void combine(std::string outfile, envi C, long long x, long long y, bool PROGRESS = false){
  		envi_header h = header;
  		
  		long long left = std::min<long long>(0, x);													//calculate the left edge of the final image
  		long long right = std::max<long long>((long long)header.samples, C.header.samples + x);		//calculate the right edge of the final image
  		long long top = std::min<long long>(0, y);													//calculate the top edge of the final image
  		long long bottom = std::max<long long>((long long)header.lines, C.header.lines + y);		//calculate the bottom edge of the final image
  
  		h.samples = right - left;
  		h.lines = bottom - top;
  	
  		h.save(outfile + ".hdr");
  
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				((bsq<float>*)file)->combine(outfile, (bsq<float>*)C.file, x, y, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bsq<double>*)file)->combine(outfile, (bsq<double>*)C.file, x, y, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
  				((bil<float>*)file)->combine(outfile, (bil<float>*)C.file, x, y, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bil<double>*)file)->combine(outfile, (bil<double>*)C.file, x, y, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				((bip<float>*)file)->combine(outfile, (bip<float>*)C.file, x, y, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bip<double>*)file)->combine(outfile, (bip<double>*)C.file, x, y, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  	}
9b2a5d71   David Mayerich   implemented finit...
1797
  
82a7c08c   David Mayerich   added an append c...
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
  	void append(std::string outfile, envi C, bool PROGRESS = false) {
  		if (C.header.samples != header.samples ||									//verify that the images are the same size
  			C.header.lines != header.lines) {
  			std::cout << "ERROR - appended images must be the same size: input = [" << header.samples << " x " << header.lines << "], output = [" << C.header.samples << " x " << C.header.lines << "]" << std::endl;
  			exit(1);
  		}
  		if (C.header.interleave != header.interleave) {
  			std::cout << "ERROR - appended images must have the same interleave format" << std::endl;
  			exit(1);
  		}
  
  		stim::envi_header new_header = header;												//create a header for the output image
  		new_header.bands = header.bands + C.header.bands;									//calculate the number of bands in the new image
  
  		if (header.wavelength.size() != 0 && C.header.wavelength.size() != 0) {				//if both files contain wavelength information
  			for (size_t b = 0; b < C.header.wavelength.size(); b++)
  				new_header.wavelength.push_back(C.header.wavelength[b]);					//append the wavelength labels to the new header array
  		}
  		else new_header.wavelength.clear();
  
  		if (header.band_names.size() != 0 && C.header.band_names.size() != 0) {				//if both files contain band name information
  			for (size_t b = 0; b < C.header.band_names.size(); b++)
  				new_header.band_names.push_back(C.header.band_names[b]);					//append the wavelength labels to the new header array
  		}
  		else new_header.wavelength.clear();
  
  		new_header.save(outfile + ".hdr");													//save the output
  
  		if (header.interleave == envi_header::BSQ) {
  			if (header.data_type == envi_header::float32)
  				((bsq<float>*)file)->append(outfile, (bsq<float>*)C.file, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bsq<double>*)file)->append(outfile, (bsq<double>*)C.file, PROGRESS);
  			else {
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL) {
  			if (header.data_type == envi_header::float32)
  				((bil<float>*)file)->append(outfile, (bil<float>*)C.file, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bil<double>*)file)->append(outfile, (bil<double>*)C.file, PROGRESS);
  			else {
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP) {
  			if (header.data_type == envi_header::float32)
  				((bip<float>*)file)->append(outfile, (bip<float>*)C.file, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bip<double>*)file)->append(outfile, (bip<double>*)C.file, PROGRESS);
  			else {
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  
  	}
  
9b2a5d71   David Mayerich   implemented finit...
1859
1860
1861
1862
1863
1864
1865
  	/// Convolve the given band range with a kernel specified by a vector of coefficients.
  
  	/// @param outfile is the combined file to be output
  	/// @param c is an array of coefficients
  	/// @param start is the band to start processing (the first coefficient starts here)
  	/// @param nbands is the number of bands to process
  	/// @param center is the index for the center coefficient for the kernel (used to set the wavelengths in the output file)
9d77bbd9   David Mayerich   updates for HSIvi...
1866
  	void convolve(std::string outfile, std::vector<double> C, size_t start, size_t end, size_t center = 0, unsigned char* mask = NULL, bool PROGRESS = false){
9b2a5d71   David Mayerich   implemented finit...
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
  		size_t nbands = end - start + 1;
  		envi_header h = header;												//copy the current header
  		h.bands = nbands;													//set the number of new bands
  		if(header.wavelength.size() != 0){
  			h.wavelength.resize(nbands);									//set the number of wavelengths to the number of bands
  			for(size_t b = 0; b < nbands; b++)
  				h.wavelength[b] = header.wavelength[b+center];
  		}
  		if(header.band_names.size() != 0){
  			h.band_names.resize(nbands);
  			for(size_t b = 0; b < nbands; b++)
  				h.band_names[b] = header.band_names[b+center];
  		}
  		h.save(outfile + ".hdr");											//save the new header
  
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1884
  				((bsq<float>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1885
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1886
  				((bsq<double>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1887
1888
1889
1890
1891
1892
1893
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1894
  				((bil<float>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1895
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1896
  				((bil<double>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1897
1898
1899
1900
1901
1902
1903
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1904
  				((bip<float>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1905
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1906
  				((bip<double>*)file)->convolve(outfile, C, start, end, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  	}
  
  	/// Approximates the nth derivative of the spectra to the specified order
  
  	/// @param outfile is the file where the derivative approximation will be saved
  	/// @n is the derivative to be calculated
  	/// @order is the order of the error (must be even)
9d77bbd9   David Mayerich   updates for HSIvi...
1919
  	void deriv(std::string outfile, size_t d, size_t order, unsigned char* mask = NULL, bool PROGRESS = false){
9b2a5d71   David Mayerich   implemented finit...
1920
1921
1922
  		header.save(outfile + ".hdr");
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1923
  				((bsq<float>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1924
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1925
  				((bsq<double>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1926
1927
1928
1929
1930
1931
1932
1933
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1934
  				((bil<float>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1935
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1936
  				((bil<double>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1937
1938
1939
1940
1941
1942
1943
1944
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
9d77bbd9   David Mayerich   updates for HSIvi...
1945
  				((bip<float>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1946
  			else if (header.data_type == envi_header::float64)
9d77bbd9   David Mayerich   updates for HSIvi...
1947
  				((bip<double>*)file)->deriv(outfile, d, order, header.wavelength, mask, PROGRESS);
9b2a5d71   David Mayerich   implemented finit...
1948
1949
1950
1951
1952
1953
1954
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		exit(1);
  	}
278c622b   David Mayerich   finished Windows ...
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
  
  	void multiply(std::string outfile, double v, unsigned char* mask = NULL, bool PROGRESS = false){
  		header.save(outfile + ".hdr");
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				((bsq<float>*)file)->multiply(outfile, v, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bsq<double>*)file)->multiply(outfile, v, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
  				((bil<float>*)file)->multiply(outfile, v, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bil<double>*)file)->multiply(outfile, v, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				((bip<float>*)file)->multiply(outfile, v, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bip<double>*)file)->multiply(outfile, v, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		exit(1);
  	}
  
  	void add(std::string outfile, double v, unsigned char* mask = NULL, bool PROGRESS = false){
  		header.save(outfile + ".hdr");
  		if (header.interleave == envi_header::BSQ){
  			if (header.data_type == envi_header::float32)
  				((bsq<float>*)file)->add(outfile, v, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bsq<double>*)file)->add(outfile, v, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  
  		else if (header.interleave == envi_header::BIL){
  			if (header.data_type == envi_header::float32)
  				((bil<float>*)file)->add(outfile, v, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bil<double>*)file)->add(outfile, v, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  
  		else if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				((bip<float>*)file)->add(outfile, v, mask, PROGRESS);
  			else if (header.data_type == envi_header::float64)
  				((bip<double>*)file)->add(outfile, v, mask, PROGRESS);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		exit(1);
  	}
9d34229e   David Mayerich   restructured the ...
2029
2030
2031
2032
2033
  
  	
  
  
  	void fft(std::string outfile, double band_min, double band_max, size_t samples = 0, void* ratio = NULL, size_t rx = 0, size_t ry = 0, bool PROGRESS = false, int cuda_device = 0){
6e9cf1ac   David Mayerich   fixed image bug i...
2034
  		if(samples == 0) samples = header.bands;
f1365506   David Mayerich   modifications to ...
2035
  		//double B = (double)header.bands;
9d34229e   David Mayerich   restructured the ...
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
  		double delta = header.wavelength[1] - header.wavelength[0];					//calculate spacing in the current domain
  		double span = samples * delta;											//calculate the span in the current domain
  		double fft_delta = 1.0 / span;												//calculate the span in the FFT domain
  		double fft_max = fft_delta * samples/2;										//calculate the maximum range of the FFT
  
  		if(band_max > fft_max) band_max = fft_max;									//the user gave a band outside of the FFT range, reset the band to the maximum available
  		size_t start_i = (size_t)std::ceil(band_min / fft_delta);					//calculate the first band to store
  		size_t size_i = (size_t)std::floor(band_max / fft_delta) - start_i + 1;		//calculate the number of bands to store
  		size_t end_i = start_i + size_i - 1;										//last band number
  
  		envi_header new_header = header;
  		new_header.bands = size_i;
  		new_header.set_wavelengths(start_i * fft_delta, fft_delta);
6e9cf1ac   David Mayerich   fixed image bug i...
2049
  		new_header.wavelength_units = "inv_" + header.wavelength_units;
9d34229e   David Mayerich   restructured the ...
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
  		new_header.save(outfile + ".hdr");
  		
  		if (header.interleave == envi_header::BIP){
  			if (header.data_type == envi_header::float32)
  				((bip<float>*)file)->fft(outfile, start_i, end_i, samples, (float*)ratio, rx, ry, PROGRESS, cuda_device);
  			else if (header.data_type == envi_header::float64)
  				((bip<double>*)file)->fft(outfile, start_i, end_i, samples, (double*)ratio, rx, ry, PROGRESS, cuda_device);
  			else{
  				std::cout << "ERROR: unidentified data type" << std::endl;
  				exit(1);
  			}
  		}
  		else{
  			std::cout<<"ERROR: only BIP files supported for FFT"<<std::endl;
  			exit(1);
  		}
  	}
278c622b   David Mayerich   finished Windows ...
2067
  };	//end ENVI
e8eb202f   David Mayerich   added a new ENVI ...
2068
2069
2070
2071
  
  }	//end namespace rts
  
  #endif