WcsGeom

class gammapy.maps.WcsGeom(wcs, npix, cdelt=None, crpix=None, axes=None, conv='gadf')[source]

Bases: gammapy.maps.MapGeom

Geometry class for WCS maps.

This class encapsulates both the WCS transformation object and the the image extent (number of pixels in each dimension). Provides methods for accessing the properties of the WCS object and performing transformations between pixel and world coordinates.

Parameters:

wcs : WCS

WCS projection object

npix : tuple

Number of pixels in each spatial dimension

cdelt : tuple

Pixel size in each image plane. If none then a constant pixel size will be used.

crpix : tuple

Reference pixel coordinate in each image plane.

axes : list

Axes for non-spatial dimensions

conv : {‘gadf’, ‘fgst-ccube’, ‘fgst-template’}

Serialization format convention. This sets the default format that will be used when writing this geometry to a file.

Attributes Summary

axes List of non-spatial axes.
center_coord Map coordinate of the center of the geometry.
center_pix Pixel coordinate of the center of the geometry.
center_skydir Sky coordinate of the center of the geometry.
conv Name of default FITS convention associated with this geometry.
coordsys Coordinate system of the projection, either Galactic (‘GAL’) or Equatorial (‘CEL’).
is_allsky Flag for all-sky maps.
is_regular Flag identifying whether this geometry is regular in non-spatial dimensions.
ndim
npix Tuple with image dimension in pixels in longitude and latitude.
pixel_area Pixel area in deg^2.
projection Map projection.
shape Shape of non-spatial axes.
wcs WCS projection object.
width Tuple with image dimension in deg in longitude and latitude.

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[, clip]) Convert map coordinates to pixel indices.
coord_to_pix(coords) Convert map coordinates to pixel coordinates.
coord_to_tuple(coord) Generate a coordinate tuple compatible with this geometry.
create([npix, binsz, proj, coordsys, …]) Create a WCS geometry object.
crop(crop_width) Crop the geometry at the edges.
distance_to_edge(skydir) Angular distance from the given direction and the edge of the projection.
downsample(factor) Downsample the spatial dimension of the geometry by a given factor.
from_hdulist(hdulist[, hdu, hdu_bands]) Load a geometry object from a FITS HDUList.
from_header(header[, hdu_bands]) Create a WCS geometry object from a FITS header.
get_coord([idx, flat]) Get the coordinate array for this geometry.
get_idx([idx, local, flat]) Get tuple of pixel indices for this geometry.
get_image_shape(idx) Get the shape of the image plane at index idx.
get_image_wcs(idx)
make_bands_hdu([hdu, hdu_skymap, conv])
make_header()
pad(pad_width) Pad the geometry at the edges.
pix_to_coord(pix) Convert pixel coordinates to map coordinates.
pix_to_idx(pix[, clip]) 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) Create a new geometry by cutting in the non-spatial dimensions of this geometry.
upsample(factor) Upsample the spatial dimension of the geometry by a given factor.

Attributes Documentation

axes

List of non-spatial axes.

center_coord

Map coordinate of the center of the geometry.

Returns:coord : tuple
center_pix

Pixel coordinate of the center of the geometry.

Returns:pix : tuple
center_skydir

Sky coordinate of the center of the geometry.

Returns:pix : SkyCoord
conv

Name of default FITS convention associated with this geometry.

coordsys

Coordinate system of the projection, either Galactic (‘GAL’) or Equatorial (‘CEL’).

is_allsky

Flag for all-sky maps.

is_regular

Flag identifying whether this geometry is regular in non-spatial dimensions. False for multi-resolution or irregular geometries. If true all image planes have the same pixel geometry.

ndim
npix

Tuple with image dimension in pixels in longitude and latitude.

pixel_area

Pixel area in deg^2.

projection

Map projection.

shape

Shape of non-spatial axes.

wcs

WCS projection object.

width

Tuple with image dimension in deg in longitude and latitude.

Methods Documentation

contains(coords)[source]

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

Parameters:

coords : tuple or MapCoord

Tuple of map coordinates.

Returns:

containment : ndarray

Bool array.

contains_pix(pix)

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

Parameters:

pix : tuple

Tuple of pixel coordinates.

Returns:

containment : ndarray

Bool array.

coord_to_idx(coords, clip=False)

Convert map coordinates to pixel indices.

Parameters:

coords : tuple or MapCoord

Coordinate values in each dimension of the map. This can either be a tuple of numpy arrays or a MapCoord 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.

clip : bool

Choose whether to clip indices to the valid range of the geometry. If false then indices for coordinates outside the geometry range will be set -1.

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 MapCoord 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.

coord_to_tuple(coord)

Generate a coordinate tuple compatible with this geometry.

Parameters:coord : MapCoord
classmethod create(npix=None, binsz=0.5, proj='CAR', coordsys='CEL', refpix=None, axes=None, skydir=None, width=None, conv='gadf')[source]

Create a WCS geometry object.

Pixelization of the map is set with binsz and one of either npix or width arguments. For maps with non-spatial dimensions a different pixelization can be used for each image plane by passing a list or array argument for any of the pixelization parameters. If both npix and width are None then an all-sky geometry will be created.

Parameters:

npix : int or tuple or list

Width of the map in pixels. A tuple will be interpreted as parameters for longitude and latitude axes. For maps with non-spatial dimensions, list input can be used to define a different map width in each image plane. This option supersedes width.

width : float or tuple or list

Width of the map in degrees. A tuple will be interpreted as parameters for longitude and latitude axes. For maps with non-spatial dimensions, list input can be used to define a different map width in each image plane.

binsz : float or tuple or list

Map pixel size in degrees. A tuple will be interpreted as parameters for longitude and latitude axes. For maps with non-spatial dimensions, list input can be used to define a different bin size in each image plane.

skydir : tuple or SkyCoord

Sky position of map center. Can be either a SkyCoord object or a tuple of longitude and latitude in deg in the coordinate system of the map.

coordsys : {‘CEL’, ‘GAL’}, optional

Coordinate system, either Galactic (‘GAL’) or Equatorial (‘CEL’).

axes : list

List of non-spatial axes.

proj : string, optional

Any valid WCS projection type. Default is ‘CAR’ (cartesian).

refpix : tuple

Reference pixel of the projection. If None this will be set to the center of the map.

conv : string, optional

FITS format convention (‘fgst-ccube’, ‘fgst-template’, ‘gadf’). Default is ‘gadf’.

Returns:

geom : WcsGeom

A WCS geometry object.

Examples

>>> from gammapy.maps import WcsGeom
>>> from gammapy.maps import MapAxis
>>> axis = MapAxis.from_bounds(0,1,2)
>>> geom = WcsGeom.create(npix=(100,100), binsz=0.1)
>>> geom = WcsGeom.create(npix=[100,200], binsz=[0.1,0.05], axes=[axis])
>>> geom = WcsGeom.create(width=[5.0,8.0], binsz=[0.1,0.05], axes=[axis])
>>> geom = WcsGeom.create(npix=([100,200],[100,200]), binsz=0.1, axes=[axis])
crop(crop_width)[source]

Crop the geometry at the edges.

Parameters:

crop_width : {sequence, array_like, int}

Number of values cropped from the edges of each axis.

Returns:

geom : MapGeom

Cropped geometry.

distance_to_edge(skydir)[source]

Angular distance from the given direction and the edge of the projection.

downsample(factor)[source]

Downsample the spatial dimension of the geometry by a given factor.

Parameters:

factor : int

Downsampling factor.

Returns:

geom : MapGeom

Downsampled geometry.

from_hdulist(hdulist, hdu=None, hdu_bands=None)

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.

classmethod from_header(header, hdu_bands=None)[source]

Create a WCS geometry object from a FITS header.

Parameters:

header : Header

The FITS header

hdu_bands : BinTableHDU

The BANDS table HDU.

Returns:

wcs : WcsGeom

WCS geometry object.

get_coord(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.

get_image_shape(idx)[source]

Get the shape of the image plane at index idx.

get_image_wcs(idx)[source]
make_bands_hdu(hdu=None, hdu_skymap=None, conv=None)
make_header()[source]
pad(pad_width)[source]

Pad the geometry at the edges.

Parameters:

pad_width : {sequence, array_like, int}

Number of values padded to the edges of each axis.

Returns:

geom : MapGeom

Padded geometry.

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, clip=False)[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.

clip : bool

Choose whether to clip indices to the valid range of the geometry. If false then indices for coordinates outside the geometry range will be set -1.

Returns:

idx : tuple

Tuple of pixel indices.

read(filename, **kwargs)

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)[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.

upsample(factor)[source]

Upsample the spatial dimension of the geometry by a given factor.

Parameters:

factor : int

Upsampling factor.

Returns:

geom : MapGeom

Upsampled geometry.