RearrangingΒΆ

The crop method can be used to reduce the size of a Cube, Image or Spectrum to the smallest sub-array that retains all unmasked pixels. In the following example, the pixels outside of an elliptical region of an image are first masked, and then the crop method is used to select the sub-image that just contains the unmasked elliptical region:

In [1]: import numpy as np

In [2]: import matplotlib.pyplot as plt

In [3]: from mpdaf.obj import Image

In [4]: ima = Image('../data/obj/a370II.fits')

In [5]: center=[-1.5642, 39.9620]

In [6]: ima.mask_ellipse(center=center, radius=(80, 110), posangle=ima.get_rot(), inside=False)

In [7]: ima.shape
Out[7]: (1797, 1909)

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

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

In [10]: ima.crop()
Out[10]: [slice(385, 1218, None), slice(85, 1112, None)]

In [11]: ima.shape
Out[11]: (833, 1027)

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

In [13]: ima.plot()
Out[13]: <matplotlib.image.AxesImage at 0x7fb745744b10>
_images/Obj_transfo1.png _images/Obj_transfo2.png

The Spectrum.truncate, Image.truncate and Cube.truncate methods return a sub-object that is bounded by specified wavelength or/and spatial world-coordinates:

In the following example, the image from the previous example is truncated to just enclose a region of the sky whose width in right ascension is 150 arc-seconds, and whose height in declination is also 150 arc-seconds. Since the ellipse of the previous example was deliberately aligned with the declination and right ascension axes, this effectively truncates the axes of the ellipse.

In [14]: ymin, xmin = np.array(center) - 75./3600

In [15]: ymax, xmax = np.array(center) + 75./3600

In [16]: ima2 = ima.truncate(ymin, ymax, xmin, xmax)

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

In [18]: ima2.plot()
Out[18]: <matplotlib.image.AxesImage at 0x7fb7361128d0>

In [19]: ima2.get_rot()
Out[19]: 113.8261800053088
_images/Obj_transfo3.png

The ranges x_min to x_max and y_min to y_max, specify a rectangular region of the sky in world coordinates. The truncate function returns the sub-image that just encloses this region. In the above example, the world coordinate axes are not parallel to the array axes, so there are some pixels in the image that are outside the specified world-coordinate region. These pixels are masked.

The methods Spectrum.rebin, Image.rebin and Cube.rebin reduce the array dimensions of these objects by integer factors, without changing the area of sky that they cover. They do this by creating a new object whose pixels are the mean of several neighboring pixels of the input object.

In [20]: ima = Image('../data/obj/a370II.fits')

In [21]: ima.info()
[INFO] 1797 x 1909 Image (../data/obj/a370II.fits)
[INFO] .data(1797 x 1909) (no unit), no noise
[INFO] center:(-01:35:06.1921,02:39:51.6357) size in arcsec:(369.366,392.816) step in arcsec:(0.206,0.206) rot:113.8 deg

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

In [23]: ima.plot(zscale=True)
Out[23]: <matplotlib.image.AxesImage at 0x7fb7443d0f90>

In [24]: ima2 = ima.rebin(factor=10)

In [25]: ima2.info()
[INFO] 179 x 190 Image (../data/obj/a370II.fits)
[INFO] .data(179 x 190) (no unit), no noise
[INFO] center:(-01:35:06.0563,02:39:51.6392) size in arcsec:(367.927,390.964) step in arcsec:(2.055,2.058) rot:113.8 deg

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

In [27]: ima2.plot(zscale=True)
Out[27]: <matplotlib.image.AxesImage at 0x7fb735f49c50>
_images/Obj_transfo4.png _images/Obj_transfo5.png

The methods Spectrum.resample and Image.resample resample a spectrum or image to a new world-coordinate grid. The following example resamples an image to change its angular resolution and also to change which sky position appears at the center of pixel [0,0]:

In [28]: ima = Image('../data/obj/a370II.fits')

In [29]: ima.info()
[INFO] 1797 x 1909 Image (../data/obj/a370II.fits)
[INFO] .data(1797 x 1909) (no unit), no noise
[INFO] center:(-01:35:06.1921,02:39:51.6357) size in arcsec:(369.366,392.816) step in arcsec:(0.206,0.206) rot:113.8 deg

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

In [31]: ima.plot(zscale=True)
Out[31]: <matplotlib.image.AxesImage at 0x7fb745e41110>

In [32]: newdim = (np.array(ima.shape)/4.5).astype(np.int)

In [33]: import astropy.units as u

In [34]: newstep = ima.get_step(unit=u.arcsec) * 4.5

In [35]: newstart =  np.array(center) + 50./3600

In [36]: ima2 = ima.resample(newdim, newstart, newstep)

In [37]: ima2.info()
[INFO] 399 x 424 Image (../data/obj/a370II.fits)
[INFO] .data(399 x 424) (no unit), no noise
[INFO] center:(-01:37:14.9682,02:39:48.2648) size in arcsec:(369.058,392.610) step in arcsec:(0.925,0.926) rot:113.8 deg

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

In [39]: ima2.plot(zscale=True)
Out[39]: <matplotlib.image.AxesImage at 0x7fb735d04950>
_images/Obj_transfo6.png _images/Obj_transfo7.png