spectralpython / spectral

Python module for hyperspectral image processing
MIT License
574 stars 139 forks source link

Impossible to open ENVI spectral library #161

Closed AntanisMJR closed 11 months ago

AntanisMJR commented 11 months ago

Hi, I'm trying to use Spectral to open an ENVI spectral library.

As a consequence of some prior processing, I have two formats for the header file:

Screenshot 2023-10-27 160115

Other header for same file Screenshot 2023-10-27 160248

The code that I am using:

`import matplotlib matplotlib.use('WX') import spectral as sp1 import pandas as pd

SpecLib = sp1.envi.open("C:/mypath/file_name.hdr") SpecLib = pd.DataFrame(index=SpecLib.names, data=SpecLib.spectra, columns=SpecLib.bands.centers)

print(index) print(data) print(columns)`

The errors I get: Traceback (most recent call last): File "C:\Users\Utente\anaconda3\envs\library_converter\lib\site-packages\IPython\core\interactiveshell.py", line 3526, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in runfile('C:\\Correlograms\Envi_bsq_numpy_to_csv.py', wdir='C:\*\SPECTRAL_Process\Correlograms') File "C:\Program Files\JetBrains\PyCharm 2023.2.3\plugins\python\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Program Files\JetBrains\PyCharm 2023.2.3\plugins\python\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:\Users\\Envi_bsq_numpy_to_csv.py", line 7, in SpecLib = pd.DataFrame(index=SpecLib.names, data=SpecLib.spectra, columns=SpecLib.bands.centers) AttributeError: 'BsqFile' object has no attribute 'names'

Thanks a lot for the help!

tboggs commented 11 months ago

If you want your first file header to be loaded as a spectral library (which will populate the "names" attribute of the returned object), then you should change the "file type" parameter in the header to this:

file type = ENVI Spectral Library
AntanisMJR commented 11 months ago

Thanks a lot for the answer! How can I also save a new spectral library from the pandas dataframe?

tboggs commented 11 months ago

If you put all the spectra in a numpy array, then you just need to create the associated BandInfo object (you only need to populate the centers attribute) and a list of spectrum names. With that you can create a SpectralLibrary, which has a save method.