Skip to content

Commit af0d62c

Browse files
committed
Refactor ASTER code to support subclassing AsterDatabase
1 parent 2952f86 commit af0d62c

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

spectral/database/aster.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
# Send comments to:
2828
# Thomas Boggs, [email protected]
2929
#
30-
30+
'''Code for reading and managing ASTER spectral library data.'''
3131

3232
from __future__ import division, print_function, unicode_literals
33+
3334
from spectral.utilities.python23 import IS_PYTHON3
3435

3536
if IS_PYTHON3:
@@ -72,7 +73,7 @@ def __init__(self):
7273
self.measurement = {}
7374

7475

75-
def read_file(filename):
76+
def read_aster_file(filename):
7677
'''Reads an ASTER 2.x spectrum file.'''
7778
fin = open_file(filename)
7879

@@ -159,6 +160,7 @@ def read_file(filename):
159160

160161

161162
class AsterDatabase:
163+
'''A relational database to manage ASTER spectral library data.'''
162164
schemas = table_schemas
163165

164166
def _add_sample(self, name, sampleType, sampleClass, subClass,
@@ -229,12 +231,12 @@ def create(cls, filename, aster_data_dir=None):
229231
import os
230232
if os.path.isfile(filename):
231233
raise Exception('Error: Specified file already exists.')
232-
db = AsterDatabase()
234+
db = cls()
233235
db._connect(filename)
234236
for schema in cls.schemas:
235237
db.cursor.execute(schema)
236238
if aster_data_dir:
237-
db._import_aster_files(aster_data_dir)
239+
db._import_files(aster_data_dir)
238240
return db
239241

240242
def __init__(self, sqlite_filename=None):
@@ -258,15 +260,21 @@ def __init__(self, sqlite_filename=None):
258260
self.db = None
259261
self.cursor = None
260262

261-
def _import_aster_files(self, aster_data_dir):
263+
def read_file(self, filename):
264+
return read_aster_file(filename)
265+
266+
def _import_files(self, data_dir, ignore=bad_files):
262267
'''Read each file in the ASTER library and convert to AVIRIS bands.'''
263268
from glob import glob
264269
import numpy
265270
import os
266271

267-
if not os.path.isdir(aster_data_dir):
272+
if not os.path.isdir(data_dir):
268273
raise Exception('Error: Invalid directory name specified.')
269-
filesToIgnore = [aster_data_dir + '/' + f for f in bad_files]
274+
if ignore is not None:
275+
filesToIgnore = [data_dir + '/' + f for f in ignore]
276+
else:
277+
filesToIgnore = []
270278

271279
numFiles = 0
272280
numIgnored = 0
@@ -277,13 +285,13 @@ class Sig:
277285
pass
278286
sigs = []
279287

280-
for f in glob(aster_data_dir + '/*spectrum.txt'):
288+
for f in glob(data_dir + '/*spectrum.txt'):
281289
if f in filesToIgnore:
282290
numIgnored += 1
283291
continue
284-
print(('Importing %s.' % f))
292+
print('Importing %s.' % f)
285293
numFiles += 1
286-
sig = read_file(f)
294+
sig = self.read_file(f)
287295
s = sig.sample
288296
if s['particle size'].lower == 'liquid':
289297
phase = 'liquid'
@@ -315,8 +323,8 @@ class Sig:
315323
m['x units'], yUnit, m['first x value'],
316324
m['last x value'], sig.x, sig.y)
317325
if numFiles == 0:
318-
print('No ASTER data files were found in directory "%s".' \
319-
% aster_data_dir)
326+
print('No data files were found in directory "%s".' \
327+
% data_dir)
320328
else:
321329
print('Processed %d files.' % numFiles)
322330
if numIgnored > 0:

0 commit comments

Comments
 (0)