# WcsMap¶

class gammapy.maps.WcsMap(geom, data)[source]

Base class for WCS map classes.

Parameters: geom : WcsGeom A WCS geometry object. data : ndarray Data array.

Attributes Summary

 data Data array (ndarray) geom Map geometry (MapGeom)

Methods Summary

 create([map_type, npix, binsz, width, proj, …]) Factory method to create an empty WCS map. crop(crop_width) Crop the spatial dimension of the map. downsample(factor) Downsample the spatial dimension of the map by a given factor. fill_by_coords(coords[, weights]) Fill pixels at the given map coordinates with values in weights vector. fill_by_idx(idx[, weights]) Fill pixels at the given pixel indices with values in weights vector. fill_by_pix(pix[, weights]) Fill pixels at the given pixel coordinates with values in weights vector. from_hdulist(hdulist[, hdu, hdu_bands]) Make a WcsMap object from a FITS HDUList. get_by_coords(coords[, interp]) Return map values at the given map coordinates. get_by_idx(idx) Return map values at the given pixel indices. get_by_pix(pix[, interp]) Return map values at the given pixel coordinates. interp_by_coords(coords[, interp]) Interpolate map values at the given map coordinates. iter_by_coords([buffersize]) Iterate over elements of the map returning a tuple with values and map coordinates. iter_by_image() Iterate over image planes of the map returning a tuple with the image array and image plane index. iter_by_pix([buffersize]) Iterate over elements of the map returning a tuple with values and pixel coordinates. make_hdu([extname, extname_bands, sparse, conv]) Make a FITS HDU from this map. pad(pad_width) Pad the spatial dimension of the map by extending the edge of the map by the given number of pixels. read(filename, **kwargs) Read a map from a FITS file. reproject(geom[, order, mode]) Reproject this map to a different geometry. set_by_coords(coords, vals) Set pixels at the given map coordinates to the values in vals vector. set_by_idx(idx, vals) Set pixels at the given pixel indices to the values in vals vector. set_by_pix(pix, vals) Set pixels at the given pixel coordinates to the values in vals vector. sum_over_axes() Reduce to a 2D image by summing over non-spatial dimensions. to_hdulist([extname, extname_bands, sparse, …]) Convert to HDUList. upsample(factor) Upsample the spatial dimension of the map by a given factor. write(filename, **kwargs) Write to a FITS file.

Attributes Documentation

data

Data array (ndarray)

geom

Map geometry (MapGeom)

Methods Documentation

classmethod create(map_type=None, npix=None, binsz=0.1, width=None, proj='CAR', coordsys='CEL', refpix=None, axes=None, skydir=None, dtype='float32', conv='gadf')[source]

Factory method to create an empty WCS map.

Parameters: map_type : str Internal map representation. Valid types are WcsMapND/wcs and WcsMapSparse/wcs-sparse. 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 then this will be chosen to be center of the map. dtype : str, optional Data type, default is float32 conv : str, optional FITS format convention (‘fgst-ccube’, ‘fgst-template’, ‘gadf’). Default is ‘gadf’. map : WcsMap A WCS map object.
crop(crop_width)

Crop the spatial dimension of the map.

Parameters: crop_width : {sequence, array_like, int} Number of values cropped from the edges of each axis. Defined analogously to pad_with from pad. Cropped map.
downsample(factor)

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

Parameters: factor : int Downsampling factor. Downsampled map.
fill_by_coords(coords, weights=None)

Fill pixels at the given map coordinates with values in weights vector.

Parameters: coords : tuple or MapCoords MapCoords object or tuple of coordinate arrays for each dimension of the map. Tuple should be ordered as (lon, lat, x_0, …, x_n) where x_i are coordinates for non-spatial dimensions of the map. weights : ndarray Weights vector. If None then a unit weight will be assumed for each element in coords.
fill_by_idx(idx, weights=None)

Fill pixels at the given pixel indices with values in weights vector.

Parameters: idx : tuple Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps. weights : ndarray Weights vector. If None then a unit weight will be assumed for each element in idx.
fill_by_pix(pix, weights=None)

Fill pixels at the given pixel coordinates with values in weights vector.

Parameters: pix : tuple Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps. Pixel indices can be either float or integer type. Float indices will be rounded to the nearest integer. weights : ndarray Weights vector. If None then a unit weight will be assumed for each element in pix.
classmethod from_hdulist(hdulist, hdu=None, hdu_bands=None)[source]

Make a WcsMap 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. wcs_map : WcsMap Map object
get_by_coords(coords, interp=None)

Return map values at the given map coordinates.

Parameters: coords : tuple or MapCoords MapCoords object or tuple of coordinate arrays for each dimension of the map. Tuple should be ordered as (lon, lat, x_0, …, x_n) where x_i are coordinates for non-spatial dimensions of the map. interp : {None, ‘nearest’, ‘linear’, ‘cubic’, 0, 1, 2, 3} Method to interpolate data values. By default no interpolation is performed and the return value will be the amplitude of the pixel encompassing the given coordinate. Integer values can be used in lieu of strings to choose the interpolation method of the given order (0=’nearest’, 1=’linear’, 2=’quadratic’, 3=’cubic’). Note that only ‘nearest’ and ‘linear’ methods are supported for all map types. vals : ndarray Values of pixels in the map. np.nan used to flag coords outside of map.
get_by_idx(idx)

Return map values at the given pixel indices.

Parameters: idx : tuple Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps. vals : ndarray Array of pixel values. np.nan used to flag coordinate outside of map
get_by_pix(pix, interp=None)

Return map values at the given pixel coordinates.

Parameters: pix : tuple Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps. Pixel indices can be either float or integer type. interp : {None, ‘nearest’, ‘linear’, ‘cubic’, 0, 1, 2, 3} Method to interpolate data values. By default no interpolation is performed and the return value will be the amplitude of the pixel encompassing the given coordinate. Integer values can be used in lieu of strings to choose the interpolation method of the given order (0=’nearest’, 1=’linear’, 2=’quadratic’, 3=’cubic’). Note that only ‘nearest’ and ‘linear’ methods are supported for all map types. vals : ndarray Array of pixel values. np.nan used to flag coordinates outside of map
interp_by_coords(coords, interp=None)

Interpolate map values at the given map coordinates.

Parameters: coords : tuple or MapCoords MapCoords object or tuple of coordinate arrays for each dimension of the map. Tuple should be ordered as (lon, lat, x_0, …, x_n) where x_i are coordinates for non-spatial dimensions of the map. interp : {None, ‘nearest’, ‘linear’, ‘cubic’, 0, 1, 2, 3} Method to interpolate data values. By default no interpolation is performed and the return value will be the amplitude of the pixel encompassing the given coordinate. Integer values can be used in lieu of strings to choose the interpolation method of the given order (0=’nearest’, 1=’linear’, 2=’quadratic’, 3=’cubic’). Note that only ‘nearest’ and ‘linear’ methods are supported for all map types. vals : ndarray Values of pixels in the flattened map. np.nan used to flag coords outside of map
iter_by_coords(buffersize=1)

Iterate over elements of the map returning a tuple with values and map coordinates.

Parameters: buffersize : int Set the size of the buffer. The map will be returned in chunks of the given size. Map values. coords : tuple Tuple of map coordinates.
iter_by_image()

Iterate over image planes of the map returning a tuple with the image array and image plane index.

Returns: val : ndarray Array of image plane values. idx : tuple Index of image plane.
iter_by_pix(buffersize=1)

Iterate over elements of the map returning a tuple with values and pixel coordinates.

Parameters: buffersize : int Set the size of the buffer. The map will be returned in chunks of the given size. Map values. pix : tuple Tuple of pixel coordinates.
make_hdu(extname='SKYMAP', extname_bands=None, sparse=False, conv=None)[source]

Make a FITS HDU from this map.

Parameters: extname : str The HDU extension name. extname_bands : str The HDU extension name for BANDS table. sparse : bool Set INDXSCHM to SPARSE and sparsify the map by only writing pixels with non-zero amplitude. HDU containing the map data.
pad(pad_width)

Pad the spatial dimension of the map by extending the edge of the map by the given number of pixels.

Parameters: pad_width : {sequence, array_like, int} Number of values padded to the edges of each axis, passed to numpy.pad Padded map.
read(filename, **kwargs)

Read a map 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. map_out : MapBase Map object
reproject(geom, order=1, mode='interp')

Reproject this map to a different geometry.

Parameters: geom : MapGeom Geometry of projection. mode : str Method for reprojection. ‘interp’ method interpolates at pixel centers. ‘exact’ method integrates over intersection of pixels. order : int or str Order of interpolating polynomial (0 = nearest-neighbor, 1 = linear, 2 = quadratic, 3 = cubic). Reprojected map.
set_by_coords(coords, vals)

Set pixels at the given map coordinates to the values in vals vector.

Parameters: coords : tuple or MapCoords MapCoords object or tuple of coordinate arrays for each dimension of the map. Tuple should be ordered as (lon, lat, x_0, …, x_n) where x_i are coordinates for non-spatial dimensions of the map. vals : ndarray Values vector. Pixels at coords will be set to these values.
set_by_idx(idx, vals)

Set pixels at the given pixel indices to the values in vals vector.

Parameters: idx : tuple Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps. vals : ndarray Values vector. Pixels at idx will be set to these values.
set_by_pix(pix, vals)

Set pixels at the given pixel coordinates to the values in vals vector.

Parameters: pix : tuple Tuple of pixel index arrays for each dimension of the map. Tuple should be ordered as (I_lon, I_lat, I_0, …, I_n) for WCS maps and (I_hpx, I_0, …, I_n) for HEALPix maps. Pixel indices can be either float or integer type. Float indices will be rounded to the nearest integer. vals : ndarray Values vector. Pixels at pix will be set to these values.
sum_over_axes()

Reduce to a 2D image by summing over non-spatial dimensions.

to_hdulist(extname=None, extname_bands=None, sparse=False, conv=None)[source]

Convert to HDUList.

Parameters: TODO
upsample(factor)

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

Parameters: factor : int Upsampling factor. Upsampled map.
write(filename, **kwargs)

Write to a FITS file.

Parameters: filename : str Output file name. extname : str Set the name of the image extension. By default this will be set to SKYMAP (for BINTABLE HDU) or PRIMARY (for IMAGE HDU). extname_bands : str Set the name of the bands table extension. By default this will be set to BANDS. conv : str FITS format convention. By default files will be written to the gamma-astro-data-formats (GADF) format. This option can be used to write files that are compliant with format conventions required by specific software (e.g. the Fermi Science Tools). Supported conventions are ‘gadf’, ‘fgst-ccube’, ‘fgst-ltcube’, ‘fgst-bexpcube’, ‘fgst-template’, ‘fgst-srcmap’, ‘fgst-srcmap-sparse’, ‘galprop’, and ‘galprop2’. sparse : bool Sparsify the map by dropping pixels with zero amplitude. This option is only compatible with the ‘gadf’ format.