Arithmetic

Operations along one or more axes

In Cube objects, the sum, mean and median methods return the sum, mean and median values along a given axis or axes of the cube:

  • If axis=0, the operation is performed along the wavelength axis and an Image is returned,
  • If axis=(1, 2), the operation is performed over the two spatial axes and a Spectrum is returned.
In [1]: import matplotlib.pyplot as plt

In [2]: from mpdaf.obj import Cube

In [3]: cube = Cube('../data/sdetect/minicube.fits')

In [4]: cube.info()
[INFO] 3681 x 40 x 40 Cube (../data/sdetect/minicube.fits)
[INFO] .data(3681 x 40 x 40) (1e-20 erg / (Angstrom cm2 s)), .var(3681 x 40 x 40)
[INFO] center:(10:27:56.3962,04:13:25.3588) size in arcsec:(8.000,8.000) step in arcsec:(0.200,0.200) rot:-0.0 deg
[INFO] wavelength: min:4749.89 max:9349.89 step:1.25 Angstrom

In [5]: spe = cube.mean(axis=(1,2))

In [6]: spe.info()
[INFO] 3681 Spectrum (../data/sdetect/minicube.fits)
[INFO] .data(3681) (1e-20 erg / (Angstrom cm2 s)), .var(3681)
[INFO] wavelength: min:4749.89 max:9349.89 step:1.25 Angstrom

In [7]: spe.plot()

# white image
In [8]: ima = cube.sum(axis=0)

In [9]: ima.info()
[INFO] 40 x 40 Image (../data/sdetect/minicube.fits)
[INFO] .data(40 x 40) (1e-20 erg / (Angstrom cm2 s)), .var(40 x 40)
[INFO] center:(10:27:56.3962,04:13:25.3588) size in arcsec:(8.000,8.000) step in arcsec:(0.200,0.200) rot:-0.0 deg

In [10]: plt.figure()
Out[10]: <matplotlib.figure.Figure at 0x7fb737f6a890>

In [11]: ima.plot()
Out[11]: <matplotlib.image.AxesImage at 0x7fb747b0a590>

In [12]: spe.sum(7100,7200)
Out[12]: 3344.1215340536173

In [13]: spe.integrate(7100, 7200)
Out[13]: <Quantity 4436.30589902401 1e-20 erg / (cm2 s)>
_images/Obj_arithm1.png _images/Obj_arithm2.png

The Spectrum.mean and Spectrum.sum methods compute the mean and total flux value over a given wavelength range. Similarly, Spectrum.integrate integrates the flux value over a given wavelength range.

Arithmetic Operations between objects

Arithmetic operations can be performed between MPDAF objects and scalar numbers, numpy arrays, masked arrays or other MPDAF objects. When an operation is performed between two MPDAF objects or between an MPDAF object and an array, their dimensions must either match, or be broadcastable to the same dimensions via the usual numpy rules. The broadcasting rules make it possible to perform arithmetic operations between a Cube and an Image or a Spectrum, assuming that they have compatible spatial and spectral world-coordinates. The following demonstration shows a cube being multiplied by an image that has the same spatial dimensions and world-coordinates as the cube, and also being divided by a spectrum that has the same spectral dimensions and wavelengths as the spectral axis of the cube.

In [14]: cube2 = cube * ima / spe

In [15]: cube2.info()
[INFO] 3681 x 40 x 40 Cube (../data/sdetect/minicube.fits)
[INFO] .data(3681 x 40 x 40) (1e-20 erg / (Angstrom cm2 s)), .var(3681 x 40 x 40)
[INFO] center:(10:27:56.3962,04:13:25.3588) size in arcsec:(8.000,8.000) step in arcsec:(0.200,0.200) rot:-0.0 deg
[INFO] wavelength: min:4749.89 max:9349.89 step:1.25 Angstrom

Generic object arithmetic:

Cube, Image and Spectrum objects are all derived from a base class called DataArray. This class implements a couple of arithmetic functions that operate on the data and variance arrays of these objects:

  • sqrt returns a new object with positive data square-rooted and negative data masked.
  • abs returns a new object containing the absolute values of the data.
In [16]: ima2 = ima.sqrt()

In [17]: ima2.plot()
Out[17]: <matplotlib.image.AxesImage at 0x7fb735841890>
_images/Obj_arithm3.png