resample

speclite.resample.resample(data_in, x_in, x_out, y, data_out=None, kind='linear')[source] [edit on github]

Resample the data of one spectrum using interpolation.

Dependent variables y1, y2, … in the input data are resampled in the independent variable x using interpolation models y1(x), y2(x), … evaluated on a new grid of x values. The independent variable will typically be a wavelength or frequency and the independent variables can be fluxes, inverse variances, etc.

Interpolation is intended for cases where the input and output grids have comparable densities. When neighboring samples are correlated, the resampling process should be essentially lossless. When the output grid is sparser than the input grid, it may be more appropriate to “downsample”, i.e., average dependent variables over consecutive ranges of input samples.

The basic usage of this function is:

>>> data = np.ones((5,),
... [('wlen', float), ('flux', float), ('ivar', float)])
>>> data['wlen'] = np.arange(4000, 5000, 200)
>>> wlen_out = np.arange(4100, 4700, 200)
>>> out = resample(data, 'wlen', wlen_out, ('flux', 'ivar'))
>>> np.all(out ==
... np.array([(4100, 1.0, 1.0), (4300, 1.0, 1.0), (4500, 1.0, 1.0)],
... dtype=[('wlen', '<i8'), ('flux', '<f8'), ('ivar', '<f8')]))
True

The input grid can also be external to the structured array of spectral data, for example:

>>> data = np.ones((5,), [('flux', float), ('ivar', float)])
>>> wlen_in = np.arange(4000, 5000, 200)
>>> wlen_out = np.arange(4100, 4900, 200)
>>> out = resample(data, wlen_in, wlen_out, ('flux', 'ivar'))
>>> np.all(out ==
... np.array([(1.0, 1.0), (1.0, 1.0), (1.0, 1.0), (1.0, 1.0)],
... dtype=[('flux', '<f8'), ('ivar', '<f8')]))
True

If the output grid extends beyond the input grid, a masked array will be returned with any values requiring extrapolation masked:

>>> wlen_out = np.arange(3500, 5500, 500)
>>> out = resample(data, wlen_in, wlen_out, 'flux')
>>> np.all(out.mask ==
... np.array([(True,), (False,), (False,), (True,)],
... dtype=[('flux', 'bool')]))
True

If the input data is masked, any output interpolated values that depend on an input masked value will be masked in the output:

>>> data = ma.ones((5,), [('flux', float), ('ivar', float)])
>>> data['flux'][2] = ma.masked
>>> wlen_out = np.arange(4100, 4900, 200)
>>> out = resample(data, wlen_in, wlen_out, 'flux')
>>> np.all(out.mask ==
... np.array([(False,), (True,), (True,), (False,)],
... dtype=[('flux', 'bool')]))
True

Interpolation is performed using scipy.interpolate.inter1pd.

Parameters:
data_innumpy.ndarray or numpy.ma.MaskedArray

Structured numpy array of input spectral data to resample. The input array must be one-dimensional.

x_instring or numpy.ndarray

A field name in data_in containing the independent variable to use for interpolation, or else an array of values with the same shape as the input data.

x_outnumpy.ndarray

An array of values for the independent variable where interpolation models should be evaluated to calculate the output values.

ystring or iterable of strings.

A field name or a list of field names present in the input data that should be resampled by interpolation and included in the output.

data_outnumpy.ndarray or None

Structured numpy array where the output result should be written. If None is specified, then an appropriately sized array will be allocated and returned. Use this method to take control of the memory allocation and, for example, re-use the same array when resampling many spectra.

kindstring or integer

Specify the kind of interpolation models to build using any of the forms allowed by scipy.interpolate.inter1pd. If any input dependent values are masked, only the nearest` and ``linear values are allowed.

Returns:
numpy.ndarray or numpy.ma.MaskedArray

Structured numpy array of the resampled result containing all y fields and (if x_in is specified as a string) the output x field. The output will be a numpy.ma.MaskedArray if x_out extends beyond x_in or if data_in is masked.