Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ __pycache__*
/*.egg-info
*.ipynb_checkpoints*
/_build/
/venv/
.idea
SBJ-specdal_test.py

69 changes: 65 additions & 4 deletions specdal/containers/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ class Collection(object):
"""
Represents a dataset consisting of a collection of spectra
"""
def __init__(self, name, directory=None, spectra=None,
def __init__(self, name, directory=None, spectra=None, spectra_radiance=None,
measure_type='pct_reflect', metadata=None, flags=None):
self.name = name
self.spectra = spectra
self.spectra_radiance = spectra_radiance
self.measure_type = measure_type
self.metadata = metadata
self.flags = flags
Expand All @@ -122,6 +123,12 @@ def spectra(self):
A list of Spectrum objects in the collection
"""
return list(self._spectra.values())
@property
def spectra_radiance(self):
"""
A list of Spectrum objects in the collection
"""
return list(self._spectra_radiance.values())


@property
Expand All @@ -136,6 +143,16 @@ def spectra(self, value):
for spectrum in value:
assert spectrum.name not in self._spectra
self._spectra[spectrum.name] = spectrum

@spectra_radiance.setter
def spectra_radiance(self, value):
self._spectra_radiance = OrderedDict()
if value is not None:
# assume value is an iterable such as list
for spectrum in value:
assert spectrum.name not in self._spectra_radiance
self._spectra_radiance[spectrum.name] = spectrum

@property
def flags(self):
"""
Expand Down Expand Up @@ -204,6 +221,26 @@ def data(self):
print("Unexpected exception occurred")
raise e

@property
def radiance(self):
'''
Get measurements as a Pandas.DataFrame
'''
try:
self._check_uniform_wavelengths()
objs = [s.measurement for s in self.spectra_radiance]
keys = [s.name for s in self.spectra_radiance]
return pd.concat(objs=objs, keys=keys, axis=1)
except pd.core.indexes.base.InvalidIndexError as err:
# typically from duplicate index due to overlapping wavelengths
if not all([s.stitched for s in self.spectra_radiance]):
logging.warning('{}: Try after stitching the overlaps'.format(err))
raise err
except Exception as e:
print("Unexpected exception occurred")
raise e


def _unflagged_data(self):
try:
spectra = [s for s in self.spectra if not s.name in self.flags]
Expand All @@ -226,6 +263,15 @@ def append(self, spectrum):
assert spectrum.name not in self._spectra
assert isinstance(spectrum, Spectrum)
self._spectra[spectrum.name] = spectrum


def append_radiance(self, spectrum_radiance):
"""
insert spectrum with radiance to the collection
"""
assert spectrum_radiance.name not in self._spectra_radiance
assert isinstance(spectrum_radiance, Spectrum)
self._spectra_radiance[spectrum_radiance.name] = spectrum_radiance

def data_with_meta(self, data=True, fields=None):
"""
Expand All @@ -247,9 +293,11 @@ def data_with_meta(self, data=True, fields=None):

"""
if fields is None:
fields = ['file', 'instrument_type', 'integration_time',
'measurement_type', 'gps_time_tgt', 'gps_time_ref',
'wavelength_range']
fields = []
for s in self.spectra:
for key in s.metadata.keys():
if key not in fields:
fields.append(key)
meta_dict = {}
for field in fields:
meta_dict[field] = [s.metadata[field] if field in s.metadata
Expand Down Expand Up @@ -317,17 +365,30 @@ def interpolate(self, spacing=1, method='slinear'):
def stitch(self, method='max'):
'''
'''
#Stitch reflectance
for spectrum in self.spectra:
try:
spectrum.stitch(method)
except Exception as e:
logging.error("Error occurred while stitching {}".format(spectrum.name))
raise e
# Stitch radiance
for spectrum_rad in self.spectra_radiance:
try:
spectrum_rad.stitch(method)
except Exception as e:
logging.error("Error occurred while stitching {}".format(spectrum_rad.name))

raise e
def jump_correct(self, splices, reference, method='additive'):
'''
'''
#Jump correct reflectance
for spectrum in self.spectra:
spectrum.jump_correct(splices, reference, method)
# Jump correct radiance
for spectrum_rad in self.spectra_radiance:
spectrum_rad.jump_correct(splices, reference, method)
##################################################
# group operations
def groupby(self, separator, indices, filler=None):
Expand Down
6 changes: 4 additions & 2 deletions specdal/containers/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ class Spectrum(object):
pandas.Series with index named: "wavelength".

"""
def __init__(self, name=None, filepath=None, measurement=None,
measure_type='pct_reflect', metadata=None,
def __init__(self, measure_type=None, name=None, filepath=None, measurement=None,
metadata=None,
interpolated=False, stitched=False, jump_corrected=False,
verbose=False):
if name is None:
assert filepath is not None
name = os.path.splitext(os.path.basename(filepath))[0]
if measure_type is None:
measure_type = 'pct_reflect'
self.name = name
self.measurement = measurement
self.measure_type = measure_type
Expand Down
6 changes: 3 additions & 3 deletions specdal/operators/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def interpolate(series, spacing=1, method='slinear'):
for seq in get_monotonic_series(series):
int_index = np.round(seq.index)
# fill in gaps at 1 nm wavelength
int_index = int_index.reindex(np.arange(int_index.min(),
int_index.max() + 1,
spacing))[0]
int_index = pd.Index(np.arange(int_index.min(),
int_index.max() + 1,
spacing))
tmp_index = seq.index.union(int_index)
seq = seq.reindex(tmp_index)
# interpolate
Expand Down
Loading