astroimtools.utils.nddata_cutout2d(nddata, position, size, mode='trim', fill_value=nan)[source]

Create a 2D cutout of a NDData object.

Specifically, cutouts will made for the nddata.data and nddata.mask (if present) arrays. If nddata.wcs exists, then it will also be updated.

Note that cutouts will not be made for nddata.uncertainty (if present) because they are general objects and not arrays.


The 2D NDData from which the cutout is taken.

positiontuple or SkyCoord

The position of the cutout array’s center with respect to the nddata.data array. The position can be specified either as a (x, y) tuple of pixel coordinates or a SkyCoord, in which case nddata.wcs must exist.

sizeint, array-like, Quantity

The size of the cutout array along each axis. If size is a scalar number or a scalar Quantity, then a square cutout of size will be created. If size has two elements, they should be in (ny, nx) order. Scalar numbers in size are assumed to be in units of pixels. size can also be a Quantity object or contain Quantity objects. Such Quantity objects must be in pixel or angular units. For all cases, size will be converted to an integer number of pixels, rounding the the nearest integer. See the mode keyword for additional details on the final cutout size.

mode{‘trim’, ‘partial’, ‘strict’}, optional

The mode used for creating the cutout data array. For the 'partial' and 'trim' modes, a partial overlap of the cutout array and the input nddata.data array is sufficient. For the 'strict' mode, the cutout array has to be fully contained within the nddata.data array, otherwise an PartialOverlapError is raised. In all modes, non-overlapping arrays will raise a NoOverlapError. In 'partial' mode, positions in the cutout array that do not overlap with the nddata.data array will be filled with fill_value. In 'trim' mode only the overlapping elements are returned, thus the resulting cutout array may be smaller than the requested size.

fill_valuenumber, optional

If mode='partial', the value to fill pixels in the cutout array that do not overlap with the input nddata.data. fill_value must have the same dtype as the input nddata.data array.


A NDData object with cutouts for the data and mask, if input.


>>> from astropy.nddata import NDData
>>> import astropy.units as u
>>> from astroimtools import nddata_cutout2d
>>> data = np.random.random((500, 500))
>>> unit = u.electron / u.s
>>> mask = (data > 0.7)
>>> meta = {'exptime': 1234 * u.s}
>>> nddata = NDData(data, mask=mask, unit=unit, meta=meta)
>>> cutout = nddata_cutout2d(nddata, (100, 100), (10, 10))
>>> cutout.data.shape
(10, 10)
>>> cutout.mask.shape
(10, 10)
>>> cutout.unit
Unit("electron / s")