Sunday 17 August 2014

Slicing operations on 1-D spectra

For the last part of GSoc, I implemented slicing operations on spectrum objects. Pythonically, this implies overriding the __getitem__ method. This method is passed the slice object and it returns a new spectrum1D object with the requested slicing parameters applied. For example:
>>> from specutils.io import read_fits
>>> spectra = read_fits.read_fits_spectrum1d("test.fits")  # linear spectra file
>>> indexed_spectra = spectra[::2]  # this creates a new spectra object which skips
                                                # every alternate object from the original spectra
>>> spectra.dispersion
[320.66, 321.43, 322.77, 324.23, 326.97, 329.44]
>>> indexed_spectra.dispersion
[320.66, 322.77, 326.97]

The spectra can be sliced using the same interface as slicing for python lists. Internally, an indexer model is used to slice the WCS. This is because the WCS is not a list, it is a mapping from input to output. Python slicing operations cannot be directly applied to WCS'es. This indexer model provides an adapter to interface with the slice operations. It also allows WCS'es to slice from negative indices.

For GSoC, this is it. However, I will continue to contribute to Astropy, and specifically to the specutils package. The next step will be to enable transformations between different WCS'es and models, making it easier for users to convert to and fro between different formats.


Monday 4 August 2014

Adding new models to specutils

With the addition of new Models, specutils has started to become a very useful tool. This time, I have added Doppler WCS, Weighted Combination WCS, and Serial Composite WCS. These WCS'es, along with the previously implemented Polynomial, Legendre, Chebyshev and Bspline models  enable the user to make very complicated functions with simple building blocks.

Doppler WCS - This model applies doppler shift to the input. Instantiates the doppler shift model from the velocity (v). The doppler factor is computed using the following formula:
doppler factor = sqrt((1 + v/c)/(1 - v/c))
,where c is the speed of light
When the model is called on an input, input * doppler_factor is returned. The inverse of this model can also be obtained, which divides the input by the doppler factor. The Doppler shift model can also be instantiated from redshift(z) and the Doppler factor itself.

Weighted Combination WCS - This model combines multiple WCS using a weight and a zero point offset. Suppose there are n WCS'es. When called with an input, this WCS returns the following:

Sum over i = 1 to n
[weight_i * (zero_point_offset_i + WCS_i(input))
WCS can be added using the add_wcs method, along with it's weight and zero point offset.

Serial Composite WCS - This model applies multiple WCS in-order to a particular input. Suppose there are 4 WCS'es, When called, this WCS returns the following:
wcs_4(wcs_3(wcs_2(wcs_1(input))))
The WCS which is added first is applied first. WCS'es can be added using the add_wcs method.

The last two models, can be used to construct complicated WCS'es just by adding simple models. Even function pointers can be added to these WCS'es making them very flexible and useful.

Apart from these models, specutils now supports reading and writing multispec linear and log-linear formats as well. The IRAF classes were redesigned to use these building blocks, making the code more simpler and elegant. The next focus will be on implementing the slicing operations onto these models, and enabling transformations from one model to another.