cana.spec

Tools to handle asteroid spectra.

Functions

loadspec(filename[, unit, r_error_col, …]) Load a spectrum file in units of unit.
stack_spec(tup) Stack Spectra arrays.

Classes

Spectrum(w, r, r_unc, label, unit) Create a spectrum object.
class cana.spec.Spectrum(w: numpy.ndarray, r: numpy.ndarray, r_unc: numpy.ndarray = None, label: str = 'asteroid', unit: str = 'micron')[source]

Create a spectrum object.

A spectrum array is a subclass of a record or structured numpy array, with one axis representing the wavelength vector (w) and other the reflectance (r). The optional
Attributes:
w: numpy array

array corresponding to the wavelength vector

r: numpy array

array corresponding to the relative reflectance of the asteroid

unit: str

The wavelength units. Default is ‘microns’.

r_unc: numpy array (optional)

array corresponding to the relative reflectance uncertainty of the asteroid

path: None or str (optional)

The path of the spectrum file

label: None or str

The spectrum label

res: float

The spectrum resolution (number of points).

Methods

trim([w_low, w_up]) Trim to the desired wavelength range.
fit([order, ftype]) Fit the spectrum using a polynomial or a smooth spline.
autofit([degree_min, degree_max]) Find the best order of the polynomial fitting of the spectrum.
estimate_rms([ftype, order]) Estimate the signal-to-noise ratio in a spectrum.
clean_spec([method, sigma, fit]) Remove outliers from the spectrum.
mad([axis]) Calculate the median absolute deviation.
rebin([binsize, method, std, rem_trend]) Rebin the spectrum.
normalize([wnorm, window, interpolate]) Normalize the spectrum in a particular wavelength.
mask_region([region]) Exclude a region of the spectrum.
save(fname[, fmt, delimiter, header, …]) Save the spectrum data into file.
plot([fax, show, savefig, axistitles, …]) Quick plot of the Spectrum.
angstrom2micron()[source]

Convert wavenlength axis from angstrom to micron.

autofit(degree_min=1, degree_max=12)[source]

Find the best order of the polynomial fitting of the spectrum.

Parameters:
degree_min: int

The minimal order for a fit

degree_max: int

The maximal order for a fit

Returns:
fspec: Spectrum Object

The fitted spectrum array

fcoefs: array-like

the fitting coefficients

clean_spec(method='sigmaclip', sigma=3, fit='auto')[source]

Remove outliers from the spectrum.

Parameters:
method: str

Method for detecting outliers. Currently only ‘sigmaclip’ available Default is ‘sigmaclip’.

sigma: int

Remove points higher than sigma.

fit: ‘auto’ or integer

The order of the polynomial fit. If auto it will try to find automaticaly. Default is ‘auto’.

Returns:
spec: Spectrum

Spectrum object with outliers removed.

estimate_rms(ftype='auto', order=5)[source]

Estimate the signal-to-noise ratio in a spectrum.

Parameters:
ftype: str

Type of fitting for the snr estimation. Options are: ‘spline’, ‘polynomial’, ‘auto’. Default is ‘auto’.

order: int

Order of the adjust. Ignored if ftype is ‘auto’.

Returns:
rms: float

The estimated snr value

fit(order=4, ftype='spline')[source]

Fit the spectrum using a polynomial or a smooth spline.

Parameters:
order: int

Order of the fitting.

ftype: str

Type of algorithm to use for the fitting. Options are: ‘spline’ or ‘polynomial’.

Returns:
fspec: Spectrum Object

The fitted spectrum array

fcoefs: array-like

the fitting coefficients

mad(axis=None)[source]

Calculate the median absolute deviation.

Parameters:
axis: str

‘wave’, ‘ref’ or None. It will return the mad in the defined axis. If None, than returns the mad in both axis

Returns:
The median absolute deviation
mask_region(region=[(1.3, 1.45), (1.8, 1.95)])[source]

Exclude a region of the spectrum.

Parameters:
w_min: float

Wavelength lower limit of the masked region

w_max: float

Wavelength upper limit of the masked region

Returns:
masked_spec: Spectrum

The Spectrum array without the masked region

mask_region_aux(spec, wmin, wmax)[source]

Exclude a region of the spectrum.

Parameters:
w_min: float

Wavelength lower limit of the masked region

w_max: float

Wavelength upper limit of the masked region

Returns:
The Spectrum array without the masked region
micron2angstrom()[source]

Convert wavenlength axis from micron to angstrom.

normalize(wnorm=0.55, window=None, interpolate=True)[source]

Normalize the spectrum in a particular wavelength.

Parameters:
wnorm: float

Wavelength value to normalize the spectrum. If interpolate=False, The code will search the closest value.

window: None or float (optional)

The wavelenght window size for normalizing. If None it will normalize in the wnorm point only.

interpolate: boolean (optional)

If interpolate=False, The code will search the closest value. If True it will interpolate the value of wnorm.

Returns:
The normalized Spectrum
plot(fax=None, show=False, savefig=None, axistitles=True, speckwargs=None, legendkwargs=None)[source]

Quick plot of the Spectrum.

Parameters:
fax (Optional): matplotlib.axes

If desired to subplot image in a figure. Default is ‘None’, which will open a new plt.figure()

show (Optional): boolean

True if want to plt.show(). Default is True.

savefig (Optional): str

The path to save the figure. If set to None, wont save the figure. Default is None

axistitles: boolean

If True will label the axis. Default is True.

speckwargs: dict

Arguments for matplotlib plot function. default values: {‘c’:’0.9’, ‘lw’:’1’}.

legendkwargs: dict

Arguments for matplotlib legend function. default values: {‘loc’:’best’}.

Returns:
the matplotlib.axes of the figure
rebin(binsize=11, method='median', std=True, rem_trend=False)[source]

Rebin the spectrum.

Parameters:
binsize: int

The number of points in the bin.

method: str

The method for the rebinning. Options are:’mean and ‘median’.

std: boolean

If True, also returns the deviation. In the case of the median, returns the MAD (median absolute deviation).

rem_trend: boolean
Returns:
The rebined spectrum
ref_from_wavelength(w, interpolate=True)[source]

Get the spectrum reflectance in a particular wavelength.

Parameters:
w: float

Wavelength value If interpolate=False, The code will search the closest value.

interpolate: boolean (optional)

If interpolate=False, The code will search the closest value. If True it will interpolate the value of w.

Returns:
The reflectance value
cana.spec.loadspec(filename, unit='micron', r_error_col=None, masknull=True, label=None, **kwargs)[source]

Load a spectrum file in units of unit. Returns a new Spectrum object.

Parameters:
filename: str

Path for the spectrum file

unit: str

The wavelength unit. Possible values are: ‘micron’, ‘angstron’, ‘nanometer’.

r_error_col: None or integer (optional)

The column for the errors in the reflectance.

masknull: boolean (optional)

If True removes points where the wavelength is zero.

label: None or string (optional)

The spectrum label. If None it will take the file basename.

**kwargs: Other arguments. See numpy.loadtxt
Returns:
spec: Spectrum object
cana.spec.stack_spec(tup)[source]

Stack Spectra arrays.

Parameters:
tup : array of Spectrum objects

A sequence of Spectrum that must be the same shape along all but the second axis. 1-D arrays can be any length.

Returns:
stacked : Spectrum

The Spectrum array formed by stacking the given spectra, sorted by the wavelength.