Skip to content

Commit c0c9b80

Browse files
authored
Add support for USGS spectral library (#109)
Add support for USGS spectral library
1 parent 8edbba3 commit c0c9b80

29 files changed

+1220
-87
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ build/
77
TAGS
88
.project
99
.pydevproject
10+
spectral_test_files/

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
packages=['spectral', 'spectral.algorithms', 'spectral.database',
3333
'spectral.graphics', 'spectral.io', 'spectral.tests',
3434
'spectral.tests.data', 'spectral.tests.data.ecostress',
35-
'spectral.utilities'],
35+
'spectral.tests.data.usgs', 'spectral.utilities'],
3636
package_data={'spectral.tests.data': ['*.spc'],
37-
'spectral.tests.data.ecostress': ['*.txt']},
37+
'spectral.tests.data.ecostress': ['*.txt'],
38+
'spectral.tests.data.usgs': ['*.txt']},
3839
platforms=['Platform-Independent'],
3940
install_requires=['numpy'],
4041
classifiers=['Development Status :: 4 - Beta',

spectral/database/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
from .aster import AsterDatabase
44
from .ecostress import EcostressDatabase
5+
from .usgs import USGSDatabase

spectral/database/aster.py

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from spectral.utilities.python23 import IS_PYTHON3, tobytes, frombytes
88

9+
from .spectral_database import SpectralDatabase
10+
911
if IS_PYTHON3:
1012
readline = lambda fin: fin.readline()
1113
open_file = lambda filename: open(filename, encoding='iso-8859-1')
@@ -132,7 +134,7 @@ def read_aster_file(filename):
132134
return s
133135

134136

135-
class AsterDatabase:
137+
class AsterDatabase(SpectralDatabase):
136138
'''A relational database to manage ASTER spectral library data.'''
137139
schemas = table_schemas
138140

@@ -307,12 +309,6 @@ class Sig:
307309

308310
return sigs
309311

310-
def _connect(self, sqlite_filename):
311-
'''Establishes a connection to the Specbase sqlite database.'''
312-
import sqlite3
313-
self.db = sqlite3.connect(sqlite_filename)
314-
self.cursor = self.db.cursor()
315-
316312
def get_spectrum(self, spectrumID):
317313
'''Returns a spectrum from the database.
318314
@@ -406,76 +402,6 @@ def get_signature(self, spectrumID):
406402
sig.y = list(y)
407403
return sig
408404

409-
def query(self, sql, args=None):
410-
'''Returns the result of an arbitrary SQL statement.
411-
412-
Arguments:
413-
414-
`sql` (str):
415-
416-
An SQL statement to be passed to the database. Use "?" for
417-
variables passed into the statement.
418-
419-
`args` (tuple):
420-
421-
Optional arguments which will replace the "?" placeholders in
422-
the `sql` argument.
423-
424-
Returns:
425-
426-
An :class:`sqlite3.Cursor` object with the query results.
427-
428-
Example::
429-
430-
>>> sql = r'SELECT SpectrumID, Name FROM Samples, Spectra ' +
431-
... 'WHERE Spectra.SampleID = Samples.SampleID ' +
432-
... 'AND Name LIKE "%grass%" AND MinWavelength < ?'
433-
>>> args = (0.5,)
434-
>>> cur = db.query(sql, args)
435-
>>> for row in cur:
436-
... print row
437-
...
438-
(356, u'dry grass')
439-
(357, u'grass')
440-
'''
441-
if args:
442-
return self.cursor.execute(sql, args)
443-
else:
444-
return self.cursor.execute(sql)
445-
446-
def print_query(self, sql, args=None):
447-
'''Prints the text result of an arbitrary SQL statement.
448-
449-
Arguments:
450-
451-
`sql` (str):
452-
453-
An SQL statement to be passed to the database. Use "?" for
454-
variables passed into the statement.
455-
456-
`args` (tuple):
457-
458-
Optional arguments which will replace the "?" placeholders in
459-
the `sql` argument.
460-
461-
This function performs the same query function as
462-
:meth:`spectral.database.Asterdatabase.query` except query results are
463-
printed to **stdout** instead of returning a cursor object.
464-
465-
Example:
466-
467-
>>> sql = r'SELECT SpectrumID, Name FROM Samples, Spectra ' +
468-
... 'WHERE Spectra.SampleID = Samples.SampleID ' +
469-
... 'AND Name LIKE "%grass%" AND MinWavelength < ?'
470-
>>> args = (0.5,)
471-
>>> db.print_query(sql, args)
472-
356|dry grass
473-
357|grass
474-
'''
475-
ret = self.query(sql, args)
476-
for row in ret:
477-
print("|".join([str(x) for x in row]))
478-
479405
def create_envi_spectral_library(self, spectrumIDs, bandInfo):
480406
'''Creates an ENVI-formatted spectral library for a list of spectra.
481407
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
3+
class SpectralDatabase:
4+
def _connect(self, sqlite_filename):
5+
'''Establishes a connection to the Specbase sqlite database.'''
6+
import sqlite3
7+
self.db = sqlite3.connect(sqlite_filename)
8+
self.cursor = self.db.cursor()
9+
10+
def query(self, sql, args=None):
11+
'''Returns the result of an arbitrary SQL statement.
12+
13+
Arguments:
14+
15+
`sql` (str):
16+
17+
An SQL statement to be passed to the database. Use "?" for
18+
variables passed into the statement.
19+
20+
`args` (tuple):
21+
22+
Optional arguments which will replace the "?" placeholders in
23+
the `sql` argument.
24+
25+
Returns:
26+
27+
An :class:`sqlite3.Cursor` object with the query results.
28+
29+
Example::
30+
31+
>>> sql = r'SELECT SpectrumID, Name FROM Samples, Spectra ' +
32+
... 'WHERE Spectra.SampleID = Samples.SampleID ' +
33+
... 'AND Name LIKE "%grass%" AND MinWavelength < ?'
34+
>>> args = (0.5,)
35+
>>> cur = db.query(sql, args)
36+
>>> for row in cur:
37+
... print row
38+
...
39+
(356, u'dry grass')
40+
(357, u'grass')
41+
'''
42+
if args:
43+
return self.cursor.execute(sql, args)
44+
else:
45+
return self.cursor.execute(sql)
46+
47+
def print_query(self, sql, args=None):
48+
'''Prints the text result of an arbitrary SQL statement.
49+
50+
Arguments:
51+
52+
`sql` (str):
53+
54+
An SQL statement to be passed to the database. Use "?" for
55+
variables passed into the statement.
56+
57+
`args` (tuple):
58+
59+
Optional arguments which will replace the "?" placeholders in
60+
the `sql` argument.
61+
62+
This function performs the same query function as
63+
:meth:`spectral.database.SpectralDatabase.query` except query results are
64+
printed to **stdout** instead of returning a cursor object.
65+
66+
Example:
67+
68+
>>> sql = r'SELECT SpectrumID, Name FROM Samples, Spectra ' +
69+
... 'WHERE Spectra.SampleID = Samples.SampleID ' +
70+
... 'AND Name LIKE "%grass%" AND MinWavelength < ?'
71+
>>> args = (0.5,)
72+
>>> db.print_query(sql, args)
73+
356|dry grass
74+
357|grass
75+
'''
76+
ret = self.query(sql, args)
77+
for row in ret:
78+
print("|".join([str(x) for x in row]))
79+

0 commit comments

Comments
 (0)