-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathENVITest.m
155 lines (120 loc) · 5.39 KB
/
ENVITest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
classdef ENVITest < matlab.unittest.TestCase
properties
testCube
end
properties (ClassSetupParameter)
height = struct('row', 1, ...
'small', 3, ...
'large', 10 ...
)
width = struct('column', 1, ...
'small', 3, ...
'large', 10 ...
)
nbands = struct('single', 1, ...
'rgb', 3, ...
'multi', 5, ...
'hyper', 100 ...
);
end
methods (TestClassSetup)
function createCube(testCase, width, height, nbands)
data = gallery('uniformdata', [height, width, nbands], 1);
wls = gallery('uniformdata', [1, nbands], 1);
fwhms= gallery('uniformdata', [1, nbands], 2);
testCase.testCube = Cube(data, ...
'quantity', 'Testdata',...
'wlu', 'nm', 'wl', wls, 'fwhm', fwhms);
end
end
methods (Test)
function writeReadDataIdentity(testCase)
% Writing a Cube to ENVI and reading it back should not change
% the data.
orig = testCase.testCube;
tmpfile = tempname;
ENVI.write(orig, tmpfile);
new = ENVI.read([tmpfile, '.dat']);
delete([tmpfile, '.dat']);
delete([tmpfile, '.hdr']);
testCase.verifyEqual(new.Data, orig.Data);
end
function writeReadMetaIdentity(testCase)
% Writing a Cube to ENVI preserves wavelength and fwhm
% metadata (up to 6 decimals)
orig = testCase.testCube;
tol = 1e-6;
tmpfile = tempname;
ENVI.write(orig, tmpfile);
new = ENVI.read([tmpfile, '.dat']);
delete([tmpfile, '.dat']);
delete([tmpfile, '.hdr']);
testCase.verifyEqual(new.WavelengthUnit, orig.WavelengthUnit, 'Wavelength unit changed');
testCase.verifyLessThan(abs(new.Wavelength - orig.Wavelength), tol, 'Wavelengths changed');
testCase.verifyLessThan(abs(new.FWHM - orig.FWHM), tol, 'Wavelengths changed');
end
function writeHeaderFileExistsError(testCase)
% Unless overwrite is set, write should error when the
% hdr file exists
cube = testCase.testCube;
tmpfile = tempname;
% Create an empty .hdr file
fclose(fopen([tmpfile, '.hdr'],'w'));
testCase.verifyError(@()ENVI.write(cube, tmpfile),'ENVI:ExistingHeaderFile');
delete([tmpfile, '.hdr']);
end
function writeDataFileExistsError(testCase)
% Unless overwrite is set, write should error when the
% data file exists
cube = testCase.testCube;
tmpfile = tempname;
% Create an empty .hdr file
fclose(fopen([tmpfile, '.dat'],'w'));
testCase.verifyError(@()ENVI.write(cube, tmpfile),'ENVI:ExistingDataFile');
delete([tmpfile, '.dat']);
end
function writeFileOverwrite(testCase)
% When overwrite is set, write should overwrite the files
orig = testCase.testCube;
tmpfile = tempname;
% Create an empty .hdr and dat files
fclose(fopen([tmpfile, '.dat'],'w'));
fclose(fopen([tmpfile, '.hdr'],'w'));
% Write over the empty file
ENVI.write(orig, tmpfile, 'overwrite');
% Read the file and check that we did overwrite it
new = ENVI.read([tmpfile, '.dat']);
testCase.verifyEqual(new.Data, orig.Data);
delete([tmpfile, '.dat']);
delete([tmpfile, '.hdr']);
end
function readDefaultQuantity(testCase)
% ENVI format does not contain the quantity, so reading a file
% without specifying it should result in Unknown quantity
orig = testCase.testCube;
tmpfile = tempname;
ENVI.write(orig, tmpfile);
new = ENVI.read([tmpfile, '.dat']);
delete([tmpfile, '.dat']);
delete([tmpfile, '.hdr']);
testCase.verifyEqual(new.Quantity, 'Unknown');
end
function readSuppliedQuantity(testCase)
% If a quantity is supplied, the Cube returned by ENVI.read
% should match the one given
orig = testCase.testCube;
tmpfile = tempname;
ENVI.write(orig, tmpfile);
new = ENVI.read([tmpfile, '.dat'], orig.Quantity);
delete([tmpfile, '.dat']);
delete([tmpfile, '.hdr']);
testCase.verifyEqual(new.Quantity, orig.Quantity);
end
function findhdrFileNotFound(testCase)
% findhdr should throw an error if the file does not exist
tmpfile = [tempname, '.hdr'];
testCase.assumeEqual(exist(tmpfile, 'file'),0);
testCase.verifyError(@()ENVI.findhdr(tmpfile),'ENVI:HeaderNotFound');
end
end
end