Skip to main content
`Ctrl`+`K`

# extract_array

astropy.nddata.utils.extract_array(array_large, shape, position, mode='partial', fill_value=nan, return_position=False, *, limit_rounding_method=<ufunc 'ceil'>)

  Extract a smaller array of the given shape and position from a larger array.

  Parameters:
    array_large `ndarray`

      The array from which to extract the small array.

    shape `python:int` or `python:tuple` thereof

      The shape of the extracted array (for 1D arrays, this can be an `int`). See the `mode` keyword for additional details.

    position number or `python:tuple` thereof

      The position of the small array’s center with respect to the large array. The pixel coordinates should be in the same order as the array shape. Integer positions are at the pixel centers (for 1D arrays, this can be a number).

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

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

    fill_value number, optional

      If `mode='partial'`, the value to fill pixels in the extracted small array that do not overlap with the input `array_large`. `fill_value` will be changed to have the same `dtype` as the `array_large` array, with one exception. If `array_large` has integer type and `fill_value` is `np.nan`, then a `ValueError` will be raised.

    return_position bool, optional

      If `True`, return the coordinates of `position` in the coordinate system of the returned array.

    limit_rounding_method `python:callable()`

      The rounding method when calculating the minimum and maximum pixel indices. This must be a callable function. Examples: `ceil`, `floor`, `round`. Default is `ceil`.

  Returns:
    array_small `ndarray`

      The extracted array.

    new_position `python:tuple`

      If `return_position` is true, this tuple will contain the coordinates of the input `position` in the coordinate system of `array_small`. Note that for partially overlapping arrays, `new_position` might actually be outside of the `array_small`; `array_small[new_position]` might give wrong results if any element in `new_position` is negative.

  Examples

  We consider a large array with the shape 11x10, from which we extract a small array of shape 3x5:

      >>> import numpy as np
      >>> from astropy.nddata.utils import extract_array
      >>> large_array = np.arange(110).reshape((11, 10))
      >>> extract_array(large_array, (3, 5), (7, 7))
      array([[65, 66, 67, 68, 69],
             [75, 76, 77, 78, 79],
             [85, 86, 87, 88, 89]])