MapGeom

class gammapy.maps.MapGeom[source]

Bases: object

Base class for WCS and HEALPix geometries.

Attributes Summary

center_coord
center_pix
center_skydir
is_allsky

Methods Summary

contains(coords) Check if a given map coordinate is contained in the geometry.
contains_pix(pix) Check if a given pixel coordinate is contained in the geometry.
coord_to_idx(coords) Convert map coordinates to pixel indices.
coord_to_pix(coords) Convert map coordinates to pixel coordinates.
from_hdulist(hdulist[, hdu, hdu_bands]) Load a geometry object from a FITS HDUList.
get_coords([idx, flat]) Get the coordinate array for this geometry.
get_idx([idx, local, flat]) Get tuple of pixel indices for this geometry.
make_bands_hdu()
pix_to_coord(pix) Convert pixel coordinates to map coordinates.
pix_to_idx(pix) Convert pixel coordinates to pixel indices.
read(filename, **kwargs) Create a geometry object from a FITS file.
to_cube(axes) Create a new geometry by appending a list of non-spatial axes to the present geometry.
to_image() Create a 2D geometry by dropping all non-spatial dimensions of this geometry.
to_slice(slices[, drop_axes]) Create a new geometry by cutting in the non-spatial dimensions of this geometry.

Attributes Documentation

center_coord
center_pix
center_skydir
is_allsky

Methods Documentation

contains(coords)[source]

Check if a given map coordinate is contained in the geometry.

Parameters:

coords : tuple or MapCoords

Tuple of map coordinates.

Returns:

containment : ndarray

Bool array.

contains_pix(pix)[source]

Check if a given pixel coordinate is contained in the geometry.

Parameters:

coords : tuple

Tuple of pixel coordinates.

Returns:

containment : ndarray

Bool array.

coord_to_idx(coords)[source]

Convert map coordinates to pixel indices.

Parameters:

coords : tuple

Coordinate values in each dimension of the map. This can either be a tuple of numpy arrays or a MapCoords object. If passed as a tuple then the ordering should be (longitude, latitude, c_0, …, c_N) where c_i is the coordinate vector for axis i.

Returns:

pix : tuple

Tuple of pixel indices in image and band dimensions. Elements set to -1 correspond to coordinates outside the map.

coord_to_pix(coords)[source]

Convert map coordinates to pixel coordinates.

Parameters:

coords : tuple

Coordinate values in each dimension of the map. This can either be a tuple of numpy arrays or a MapCoords object. If passed as a tuple then the ordering should be (longitude, latitude, c_0, …, c_N) where c_i is the coordinate vector for axis i.

Returns:

pix : tuple

Tuple of pixel coordinates in image and band dimensions.

classmethod from_hdulist(hdulist, hdu=None, hdu_bands=None)[source]

Load a geometry object from a FITS HDUList.

Parameters:

hdulist : HDUList

HDU list containing HDUs for map data and bands.

hdu : str

Name or index of the HDU with the map data.

hdu_bands : str

Name or index of the HDU with the BANDS table. If not defined this will be inferred from the FITS header of the map HDU.

Returns:

geom : MapGeom

Geometry object.

get_coords(idx=None, flat=False)[source]

Get the coordinate array for this geometry.

Returns a coordinate array with the same shape as the data array. Pixels outside the geometry are set to NaN. Coordinates for a single image plane can be accessed by setting idx to the index tuple of a plane.

Parameters:

idx : tuple, optional

A tuple of indices with one index for each non-spatial dimension. If defined only coordinates for the image plane with this index will be returned. If none then coordinates for all pixels will be returned.

flat : bool, optional

Return a flattened array containing only coordinates for pixels contained in the geometry.

Returns:

coords : tuple

Tuple of coordinate vectors with one vector for each dimension.

get_idx(idx=None, local=False, flat=False)[source]

Get tuple of pixel indices for this geometry.

Returns all pixels in the geometry by default. Pixel indices for a single image plane can be accessed by setting idx to the index tuple of a plane.

Parameters:

idx : tuple, optional

A tuple of indices with one index for each non-spatial dimension. If defined only pixels for the image plane with this index will be returned. If none then all pixels will be returned.

local : bool

Flag to return local or global pixel indices. Local indices run from 0 to the number of pixels in a given image plane.

flat : bool, optional

Return a flattened array containing only indices for pixels contained in the geometry.

Returns:

idx : tuple

Tuple of pixel index vectors with one vector for each dimension.

make_bands_hdu()[source]
pix_to_coord(pix)[source]

Convert pixel coordinates to map coordinates.

Parameters:

pix : tuple

Tuple of pixel coordinates.

Returns:

coords : tuple

Tuple of map coordinates.

pix_to_idx(pix)[source]

Convert pixel coordinates to pixel indices. Returns -1 for pixel coordinates that lie outside of the map.

Parameters:

pix : tuple

Tuple of pixel coordinates.

Returns:

idx : tuple

Tuple of pixel indices.

classmethod read(filename, **kwargs)[source]

Create a geometry object from a FITS file.

Parameters:

filename : str

Name of the FITS file.

hdu : str

Name or index of the HDU with the map data.

hdu_bands : str

Name or index of the HDU with the BANDS table. If not defined this will be inferred from the FITS header of the map HDU.

Returns:

geom : MapGeom

Geometry object.

to_cube(axes)[source]

Create a new geometry by appending a list of non-spatial axes to the present geometry. This will result in a new geometry with N+M dimensions where N is the number of current dimensions and M is the number of axes in the list.

Parameters:

axes : list

Axes that will be appended to this geometry.

Returns:

geom : MapGeom

Map geometry.

to_image()[source]

Create a 2D geometry by dropping all non-spatial dimensions of this geometry.

Returns:

geom : MapGeom

Image geometry.

to_slice(slices, drop_axes=True)[source]

Create a new geometry by cutting in the non-spatial dimensions of this geometry.

Parameters:

slices : tuple

Tuple of integers or slice objects. Contains one element for each non-spatial dimension.

drop_axes : bool

Drop axes for which the slice reduces the size of that dimension to one.

Returns:

geom : MapGeom

Sliced geometry.