-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_pds3file.py
274 lines (223 loc) · 8.99 KB
/
test_pds3file.py
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
156
157
158
159
160
161
162
163
164
165
166
167
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# -*- coding: utf-8 -*-
import pytest
import os
import numpy
from numpy.testing import assert_almost_equal
from planetaryimage.pds3image import PDS3Image, Pointer
from pvl import Units
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data/')
filename = os.path.join(DATA_DIR, 'pds3_1band.IMG')
filename_3bands = os.path.join(DATA_DIR, 'pds3_3bands.IMG')
filename_float = os.path.join(DATA_DIR, 'pds3_1band_float.IMG')
gzipped_filename = os.path.join(DATA_DIR, 'pds3_1band.IMG.gz')
bz2_filename = os.path.join(DATA_DIR, 'pds3_1band.IMG.bz2')
@pytest.fixture
def expected():
return numpy.loadtxt(os.path.join(DATA_DIR, 'pds3_1band.txt')).reshape(
(1, 10, 10)
)
def test_pds3_1band_labels(expected):
image = PDS3Image.open(filename)
assert image.filename == filename
assert image.bands == 1
assert image.lines == 10
assert image.samples == 10
assert image.format == 'BAND_SEQUENTIAL'
assert image.dtype == numpy.dtype('>i2')
assert image.start_byte == 640
assert image.shape == (1, 10, 10)
# FIXME: Doublecheck that consolidating pixel_type and byte order
# is actually OK for PDS images. I think here at the object level
# its OK even though in the PDS labels the information is separate.
assert image.size == 100
assert image.compression is None
# Testing .label
assert image.label['FILE_RECORDS'] == 42
assert image.label['IMAGE']['SAMPLE_TYPE'] == 'MSB_INTEGER'
# Testing .data
assert image.data.shape == (1, 10, 10)
assert image.data.dtype == numpy.dtype('>i2')
assert_almost_equal(image.data, expected)
def test_gz_pds3_1band_labels(expected):
image = PDS3Image.open(gzipped_filename)
assert image.filename == gzipped_filename
assert image.bands == 1
assert image.lines == 10
assert image.samples == 10
assert image.format == 'BAND_SEQUENTIAL'
assert image.dtype == numpy.dtype('>i2')
assert image.start_byte == 640
assert image.shape == (1, 10, 10)
assert image.size == 100
assert image.compression == 'gz'
# Testing .label
assert image.label['FILE_RECORDS'] == 42
assert image.label['IMAGE']['SAMPLE_TYPE'] == 'MSB_INTEGER'
# Testing .data
assert image.data.shape == (1, 10, 10)
assert image.data.dtype == numpy.dtype('>i2')
assert_almost_equal(image.data, expected)
def test_image_save():
image = PDS3Image.open(filename)
image.save('Temp_Image.IMG')
new_image = PDS3Image.open('Temp_Image.IMG')
assert image.bands == new_image.bands
assert image.lines == new_image.lines
assert image.samples == new_image.samples
assert image.format == new_image.format
assert image.dtype == new_image.dtype
assert image.start_byte == new_image.start_byte
assert image.shape == new_image.shape
assert image.size == new_image.size
# Testing .label
assert image.label['FILE_RECORDS'] == new_image.label['FILE_RECORDS']
label_sample_type = image.label['IMAGE']['SAMPLE_TYPE']
assert label_sample_type == new_image.label['IMAGE']['SAMPLE_TYPE']
# Testing .data
assert image.data.shape == new_image.data.shape
assert image.data.dtype == image.data.dtype
os.remove('Temp_Image.IMG')
def test_image_save_overwrite():
image = PDS3Image.open(filename)
image.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
image_temp.save(overwrite=True)
new_image = PDS3Image.open(image_temp.filename)
assert image_temp.filename == new_image.filename
assert image_temp.bands == new_image.bands
assert image_temp.lines == new_image.lines
assert image_temp.samples == new_image.samples
assert image_temp.format == new_image.format
assert image_temp.dtype == new_image.dtype
assert image_temp.start_byte == new_image.start_byte
assert image_temp.shape == new_image.shape
assert image_temp.size == new_image.size
# Testing .label
assert image_temp.label['FILE_RECORDS'] == new_image.label['FILE_RECORDS']
label_sample_type = image_temp.label['IMAGE']['SAMPLE_TYPE']
assert label_sample_type == new_image.label['IMAGE']['SAMPLE_TYPE']
# Testing .data
assert image_temp.data.shape == new_image.data.shape
assert image_temp.data.dtype == image.data.dtype
os.remove('Temp_Image.IMG')
def test_image_save_samefile():
image = PDS3Image.open(filename)
with pytest.raises(IOError):
image.save(image.filename)
def test_image_save_lines_linesamples():
image = PDS3Image.open(filename)
image.data = image.data[0, 3:8, 3:8]
image.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
assert image_temp.lines == 5
assert image_temp.samples == 5
os.remove('Temp_Image.IMG')
def test_image_save_3bands():
image = PDS3Image.open(filename)
temp_data = numpy.array([image.data, image.data, image.data])
image.data = temp_data.reshape(3, 10, 10)
image.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
assert image_temp.bands == 3
os.remove('Temp_Image.IMG')
def test_image_save_1band():
image = PDS3Image.open(filename_3bands)
image.data = image.data[0, :, :]
image.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
assert image_temp.bands == 1
os.remove('Temp_Image.IMG')
def test_image_save_int_to_float():
image = PDS3Image.open(filename)
ref_image = PDS3Image.open(filename_float)
image.data = image.data.astype('>f4')
image.data *= 1.5
image.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
assert image_temp.dtype == ref_image.dtype
assert_almost_equal(image.data, ref_image.data)
os.remove('Temp_Image.IMG')
def test_image_save_float_to_int():
image = PDS3Image.open(filename_float)
ref_image = PDS3Image.open(filename)
image.data /= 1.5
image.data = image.data.astype('>i2')
image.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
assert image_temp.dtype == ref_image.dtype
assert_almost_equal(image.data, ref_image.data)
os.remove('Temp_Image.IMG')
def test_numpy_array_save_i2():
array = numpy.arange(100, dtype='>i2')
array = array.reshape(1, 10, 10)
temp = PDS3Image(array)
temp.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
assert image_temp.bands == 1
assert image_temp.lines == 10
assert image_temp.samples == 10
assert image_temp.format == 'BAND_SEQUENTIAL'
assert image_temp.dtype == '>i2'
assert image_temp.shape == (1, 10, 10)
assert image_temp.size == 100
assert_almost_equal(image_temp.data, array)
os.remove('Temp_Image.IMG')
def test_numpy_array_save_f4():
array = numpy.arange(100)
array = array.reshape(1, 10, 10)
array = array * 1.5
array = array.astype('>f4')
temp = PDS3Image(array)
temp.save('Temp_Image.IMG')
image_temp = PDS3Image.open('Temp_Image.IMG')
assert image_temp.bands == 1
assert image_temp.lines == 10
assert image_temp.samples == 10
assert image_temp.format == 'BAND_SEQUENTIAL'
assert image_temp.dtype == '>f4'
assert image_temp.shape == (1, 10, 10)
assert image_temp.size == 100
assert_almost_equal(image_temp.data, array)
os.remove('Temp_Image.IMG')
def test_bz2_pds3_1band_labels(expected):
image = PDS3Image.open(bz2_filename)
assert image.filename == bz2_filename
assert image.bands == 1
assert image.lines == 10
assert image.samples == 10
assert image.format == 'BAND_SEQUENTIAL'
assert image.dtype == numpy.dtype('>i2')
assert image.start_byte == 640
assert image.shape == (1, 10, 10)
assert image.size == 100
assert image.compression == 'bz2'
# Testing .label
assert image.label['FILE_RECORDS'] == 42
assert image.label['IMAGE']['SAMPLE_TYPE'] == 'MSB_INTEGER'
# Testing .data
assert image.data.shape == (1, 10, 10)
assert image.data.dtype == numpy.dtype('>i2')
assert_almost_equal(image.data, expected)
def test_parse_pointer():
# ^PTR = nnn
# Example tests/mission_data/1p432690858esfc847p2111l2m1.img
assert Pointer.parse(56, 640) == Pointer(None, 35200)
# ^PTR = nnn <BYTES>
assert Pointer.parse(Units(101337, 'BYTES'), 0) == Pointer(None, 101337)
# ^PTR = "filename"
assert Pointer.parse('W1782844276_1.IMG', 1024) == Pointer('W1782844276_1.IMG', 0)
# ^PTR = ("filename")
assert Pointer.parse(['W1782844276_1.IMG'], 1024) == Pointer('W1782844276_1.IMG', 0)
# ^PTR = ("filename", nnn)
# Example tests/mission_data/W1782844276_1.LBL
assert Pointer.parse(['W1782844276_1.IMG', 5], 1024) == Pointer('W1782844276_1.IMG', 4096)
# ^PTR = ("filename", nnn <BYTES>)
assert Pointer.parse(['W1782844276_1.IMG', Units(101337, 'BYTES')], 1024) == Pointer('W1782844276_1.IMG', 101337)
# Test bad type
with pytest.raises(ValueError):
Pointer.parse(None, 64)
# Test wrong sized arrays
with pytest.raises(ValueError):
Pointer.parse([], 64)
with pytest.raises(ValueError):
Pointer.parse(['W1782844276_1.IMG', 5, 6], 64)