enviSaveRaw.m
1.93 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
%saves an ENVI file without any manipulation, assumes (X, Y, S)
function enviSaveRaw(M, filename, headername)
%if a header isn't provided, assume it's just the filename
% with '.hdr' added to the end
if nargin == 2
headername = [filename '.hdr'];
end
%open a file for writing
fid = fopen(filename, 'w');
%write the data to disk
fwrite(fid, M, class(M));
%close the file
fclose(fid);
%open a header file for writing
fid = fopen(headername, 'w');
fprintf(fid, 'ENVI\n');
fprintf(fid, 'description = {}\n');
fprintf(fid, 'samples = %d\n', size(M, 1));
fprintf(fid, 'lines = %d\n', size(M, 2));
fprintf(fid, 'bands = %d\n', size(M, 3));
fprintf(fid, 'header offset = 0\n');
fprintf(fid, 'file type = ENVI Standard\n');
%get a string representing the matlab data type
matlab_real = isreal(M);
if(isa(M, 'uchar'))
envi_type = 1;
elseif(isa(M, 'short'))
envi_type = 2;
elseif(isa(M, 'int'))
envi_type = 3;
elseif(isa(M, 'single') && matlab_real)
envi_type = 4;
elseif(isa(M, 'double') && matlab_real)
envi_type = 5;
elseif(isa(M, 'single') && ~matlab_real)
envi_type = 6;
elseif(isa(M, 'double') && ~matlab_real)
envi_type = 9;
elseif(isa(M, 'ushort'))
envi_type = 12;
elseif(isa(M, 'ulong'))
envi_type = 13;
elseif(isa(M, 'int64'))
envi_type = 14;
elseif(isa(M, 'uint64'))
envi_type = 15;
end
fprintf(fid, 'data type = %d\n', envi_type);
fprintf(fid, 'interleave = bsq\n');
fprintf(fid, 'sensor type = Unknown\n');
fprintf(fid, 'byte order = 0\n');
fprintf(fid, 'x start = 0\n');
fprintf(fid, 'y start = 0\n');
fprintf(fid, 'wavelength units = Unknown\n');
fprintf(fid, 'z plot titles = {Unknown, Unknown}\n');
fprintf(fid, 'pixel size = {1, 1, units=Meters}\n');
fclose(fid);