示例#1
0
    def create_envi_spectral_library(self, spectrumIDs, bandInfo):
        '''Creates an ENVI-formatted spectral library for a list of spectra.

        Arguments:

            `spectrumIDs` (list of ints):

                List of **SampleID** values for of spectra in the "Samples"
                table of the USGS database.

            `bandInfo` (:class:`~spectral.BandInfo`):

                The spectral bands to which the original USGS library spectra
                will be resampled.

        Returns:

            A :class:`~spectral.io.envi.SpectralLibrary` object.

        The IDs passed to the method should correspond to the SampleID field
        of the USGS database "Samples" table.  All specified spectra will be
        resampled to the same discretization specified by the bandInfo
        parameter. See :class:`spectral.BandResampler` for details on the
        resampling method used.
        Note that expected units for bands are micrometers. 
        '''
        from spectral.algorithms.resampling import BandResampler
        from spectral.io.envi import SpectralLibrary
        import numpy
        import unicodedata
        spectra = numpy.empty((len(spectrumIDs), len(bandInfo.centers)))
        cursor = self.cursor.execute(
            '''
                                    SELECT a.ValuesArray, b.ValuesArray, a.Description, b.Unit
                                    FROM Samples AS a INNER JOIN SpectrometerData AS b
                                    ON a.AssumedWLSpmeterDataID = b.SpectrometerDataID
                                    WHERE a.SampleID IN ({})'''.format(
                ','.join(['?'] * len(spectrumIDs))), spectrumIDs)

        names = []

        for i, s in enumerate(cursor):
            y = array_from_blob(s[0])
            x = array_from_blob(s[1])
            name = s[2]
            unit = s[3]
            if unit == 'nanometers':
                x /= 1000
            resample = BandResampler(x, bandInfo.centers, None,
                                     bandInfo.bandwidths)
            spectra[i] = resample(y)
            names.append(
                unicodedata.normalize('NFKD', name).encode('ascii', 'ignore'))

        header = {}
        header['wavelength units'] = 'um'
        header['spectra names'] = names
        header['wavelength'] = bandInfo.centers
        header['fwhm'] = bandInfo.bandwidths
        return SpectralLibrary(spectra, header, {})
示例#2
0
    def create_envi_spectral_library(self, spectrumIDs, bandInfo):
        '''Creates an ENVI-formatted spectral library for a list of spectra.

        Arguments:

            `spectrumIDs` (list of ints):

                List of **SpectrumID** values for of spectra in the "Spectra"
                table of the ASTER database.

            `bandInfo` (:class:`~spectral.BandInfo`):

                The spectral bands to which the original ASTER library spectra
                will be resampled.

        Returns:

            A :class:`~spectral.io.envi.SpectralLibrary` object.

        The IDs passed to the method should correspond to the SpectrumID field
        of the ASTER database "Spectra" table.  All specified spectra will be
        resampled to the same discretization specified by the bandInfo
        parameter. See :class:`spectral.BandResampler` for details on the
        resampling method used.
        '''
        from spectral.algorithms.resampling import BandResampler
        from spectral.io.envi import SpectralLibrary
        import numpy
        import unicodedata
        spectra = numpy.empty((len(spectrumIDs), len(bandInfo.centers)))
        names = []
        for i in range(len(spectrumIDs)):
            sig = self.get_signature(spectrumIDs[i])
            resample = BandResampler(sig.x, bandInfo.centers, None,
                                     bandInfo.bandwidths)
            spectra[i] = resample(sig.y)
            names.append(
                unicodedata.normalize('NFKD', sig.sample_name).encode(
                    'ascii', 'ignore'))
        header = {}
        header['wavelength units'] = 'um'
        header['spectra names'] = names
        header['wavelength'] = bandInfo.centers
        header['fwhm'] = bandInfo.bandwidths
        return SpectralLibrary(spectra, header, {})