enviRead.m
1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function S = rtsEnviRead(fid, batchSize)
%This function reads a batch of spectra from the given ENVI file ID
% fid = ENVI file ID created using rtsEnviOpen
% batchSize = number of spectra to read
%if there is no mask, just load each spectrum in order
if(fid.mask == 1)
%compute the new batch size in case we are near the eof
nSpectra = fid.header.samples * fid.header.lines;
remainingSpectra = nSpectra - fid.fpos;
if(batchSize > remainingSpectra)
batchSize = remainingSpectra;
end
S = fread(fid.fid, [fid.header.bands batchSize], '*float32');
%increment the fid counter
fid.fpos = fid.fpos + batchSize;
%otherwise only load valid spectra
else
%compute the new batch size in case we are near the eof
if(fid.fpos == 0)
remainingSpectra = nnz(fid.mask);
else
nSpectra = nnz(fid.mask);
maskRead = fid.mask(1:fid.fpos+1);
remainingSpectra = nSpectra - nnz(maskRead);
end
if(batchSize > remainingSpectra)
batchSize = remainingSpectra;
end
%allocate space for the spectra
S = zeros(fid.header.bands, batchSize);
%for each spectrum in the batch
for s = 1:batchSize
%while the current spectrum is invalid
skip = 0;
while (~fid.mask(fid.fpos + 1))
%read the invalid spectrum
%invalid = fread(fid.fid, [fid.header.bands 1], '*float32');
skip = skip + 1;
%increment the file position
fid.fpos = fid.fpos + 1;
end
fseek(fid.fid, skip * fid.header.bands * 4, 'cof');
test = fread(fid.fid, [fid.header.bands 1], '*float32');
if size(test) ~= size(S(:, s))
size(test)
size(S(:, s))
end
S(:, s) = test;
fid.fpos = fid.fpos + 1;
end
end