nddata_cutout2d¶
- 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
andnddata.mask
(if present) arrays. Ifnddata.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.- Parameters
- nddata
NDData
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 aSkyCoord
, in which casenddata.wcs
must exist.- sizeint, array-like,
Quantity
The size of the cutout array along each axis. If
size
is a scalar number or a scalarQuantity
, then a square cutout ofsize
will be created. Ifsize
has two elements, they should be in(ny, nx)
order. Scalar numbers insize
are assumed to be in units of pixels.size
can also be aQuantity
object or containQuantity
objects. SuchQuantity
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 themode
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 inputnddata.data
array is sufficient. For the'strict'
mode, the cutout array has to be fully contained within thenddata.data
array, otherwise anPartialOverlapError
is raised. In all modes, non-overlapping arrays will raise aNoOverlapError
. In'partial'
mode, positions in the cutout array that do not overlap with thenddata.data
array will be filled withfill_value
. In'trim'
mode only the overlapping elements are returned, thus the resulting cutout array may be smaller than the requestedsize
.- fill_valuenumber, optional
If
mode='partial'
, the value to fill pixels in the cutout array that do not overlap with the inputnddata.data
.fill_value
must have the samedtype
as the inputnddata.data
array.
- nddata
- Returns
Examples
>>> 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")