Skip to content

Commit 46440f3

Browse files
committed
Moved known data file extensions to module level and cleaned up file search code.
1 parent 44e9853 commit 46440f3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

spectral/io/envi.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555

5656
import numpy as np
5757

58+
# Known ENVI data file extensions. Upper and lower case versions will be
59+
# recognized, as well as interleaves ('bil', 'bip', 'bsq'), and no extension.
60+
KNOWN_EXTS = ['img', 'dat', 'sli', 'hyspex']
61+
5862
dtype_map = [('1', np.uint8), # unsigned byte
5963
('2', np.int16), # 16-bit int
6064
('3', np.int32), # 32-bit int
@@ -296,8 +300,8 @@ def open(file, image=None):
296300
import numpy
297301
import spectral
298302

299-
headerPath = find_file_path(file)
300-
h = read_envi_header(headerPath)
303+
header_path = find_file_path(file)
304+
h = read_envi_header(header_path)
301305
check_compatibility(h)
302306
p = gen_params(h)
303307

@@ -306,16 +310,15 @@ def open(file, image=None):
306310
# Validate image file name
307311
if not image:
308312
# Try to determine the name of the image file
309-
headerDir = os.path.split(headerPath)
310-
if headerPath[-4:].lower() == '.hdr':
311-
headerPathTitle = headerPath[:-4]
312-
exts = ['', 'img', 'IMG', 'dat', 'DAT', 'sli', 'SLI', 'hyspex'] +\
313-
[inter.lower(), inter.upper()]
313+
(header_path_title, header_ext) = os.path.splitext(header_path)
314+
if header_ext.lower() == '.hdr':
315+
exts = [ext.lower() for ext in KNOWN_EXTS] + [inter.lower()]
316+
exts = [''] + exts + [ext.upper() for ext in exts]
314317
for ext in exts:
315318
if len(ext) == 0:
316-
testname = headerPathTitle
319+
testname = header_path_title
317320
else:
318-
testname = headerPathTitle + '.' + ext
321+
testname = header_path_title + '.' + ext
319322
if os.path.isfile(testname):
320323
image = testname
321324
break

0 commit comments

Comments
 (0)