6969envi_to_dtype = dict ((k , np .dtype (v ).char ) for (k , v ) in dtype_map )
7070dtype_to_envi = dict (tuple (reversed (item )) for item in list (envi_to_dtype .items ()))
7171
72- class EnviException (Exception ):
72+ from spectral import SpyException
73+ from .spyfile import FileNotFoundError , InvalidFileError
74+
75+ class EnviException (SpyException ):
7376 '''Base class for ENVI file-related exceptions.'''
7477 pass
7578
76- class EnviDataTypeError (TypeError ):
77- '''Exception raised when saving invalid image data type to ENVI format.
79+ class EnviDataTypeError (EnviException , TypeError ):
80+ '''Raised when saving invalid image data type to ENVI format.
7881 '''
7982 def __init__ (self , dtype ):
8083 msg = 'Image data type "{0}" can not be saved to ENVI data file. ' \
8184 'Call spectral.envi.get_supported_dtypes for a list of supported ' \
8285 'data type names.' .format (np .dtype (dtype ).name )
8386 super (EnviDataTypeError , self ).__init__ (msg )
8487
85- class EnviFeatureNotSupported (NotImplementedError ):
88+ class EnviFeatureNotSupported (EnviException , NotImplementedError ):
8689 '''A specified ENVI capability is not supported by the spectral module.'''
8790 pass
8891
89- class FileNotAnEnviHeader (EnviException ):
92+ class FileNotAnEnviHeader (EnviException , InvalidFileError ):
9093 '''Raised when "ENVI" does not appear on the first line of the file.'''
9194 def __init__ (self , msg ):
9295 super (FileNotAnEnviHeader , self ).__init__ (msg )
@@ -97,12 +100,16 @@ def __init__(self, param):
97100 msg = 'Mandatory parameter "%s" missing from header file.' % param
98101 super (MissingEnviHeaderParameter , self ).__init__ (msg )
99102
100- class EnviHeaderParsingError (EnviException ):
103+ class EnviHeaderParsingError (EnviException , InvalidFileError ):
101104 '''Raised upon failure to parse parameter/value pairs from a file.'''
102105 def __init__ (self ):
103106 msg = 'Failed to parse ENVI header file.'
104107 super (EnviHeaderParsingError , self ).__init__ (msg )
105108
109+ class EnviDataFileNotFoundError (EnviException , FileNotFoundError ):
110+ '''Raised when data file associated with a header is not found.'''
111+ pass
112+
106113def _validate_dtype (dtype ):
107114 '''Raises EnviDataTypeError if dtype can not be written to ENVI file.'''
108115 typename = np .dtype (dtype ).name
@@ -273,7 +280,7 @@ def open(file, image=None):
273280
274281 Raises:
275282
276- TypeError, IOError.
283+ TypeError, EnviDataFileNotFoundError
277284
278285 If the specified file is not found in the current directory, all
279286 directories listed in the SPECTRAL_DATA environment variable will be
@@ -313,7 +320,10 @@ def open(file, image=None):
313320 image = testname
314321 break
315322 if not image :
316- raise IOError ('Unable to determine image file name.' )
323+ msg = 'Unable to determine the ENVI data file name for the ' \
324+ 'given header file. You can specify the data file by passing ' \
325+ 'its name as the optional `image` argument to envi.open.'
326+ raise EnviDataFileNotFoundError (msg )
317327 else :
318328 image = find_file_path (image )
319329
@@ -367,15 +377,15 @@ def check_new_filename(hdr_file, img_ext, force):
367377 hdr_file = os .path .realpath (hdr_file )
368378 (base , ext ) = os .path .splitext (hdr_file )
369379 if ext .lower () != '.hdr' :
370- raise ValueError ('Header file name must end in ".hdr" or ".HDR".' )
380+ raise EnviException ('Header file name must end in ".hdr" or ".HDR".' )
371381 image_file = base + img_ext
372382 if not force :
373383 if os .path .isfile (hdr_file ):
374- raise Exception ('Header file %s already exists. Use `force` '
375- 'keyword to force overwrite.' % hdr_file )
384+ raise EnviException ('Header file %s already exists. Use `force` '
385+ 'keyword to force overwrite.' % hdr_file )
376386 if os .path .isfile (image_file ):
377- raise Exception ('Image file %s already exists. Use `force` '
378- 'keyword to force overwrite.' % image_file )
387+ raise EnviException ('Image file %s already exists. Use `force` '
388+ 'keyword to force overwrite.' % image_file )
379389 return (hdr_file , image_file )
380390
381391
@@ -818,15 +828,15 @@ def create_image(hdr_file, metadata=None, **kwargs):
818828
819829 # Verify minimal set of parameters have been provided
820830 if 'lines' not in metadata :
821- raise Exception ('Number of image rows is not defined.' )
831+ raise EnviException ('Number of image rows is not defined.' )
822832 elif 'samples' not in metadata :
823- raise Exception ('Number of image columns is not defined.' )
833+ raise EnviException ('Number of image columns is not defined.' )
824834 elif 'bands' not in metadata :
825- raise Exception ('Number of image bands is not defined.' )
835+ raise EnviException ('Number of image bands is not defined.' )
826836 elif 'samples' not in metadata :
827- raise Exception ('Number of image columns is not defined.' )
837+ raise EnviException ('Number of image columns is not defined.' )
828838 elif 'data type' not in metadata :
829- raise Exception ('Image data type is not defined.' )
839+ raise EnviException ('Image data type is not defined.' )
830840
831841 params = gen_params (metadata )
832842 dt = np .dtype (params .dtype ).char
0 commit comments