WCS

class astropy.wcs.wcs.WCS(header=None, fobj=None, key=' ', minerr=0.0, relax=True, naxis=None, keysel=None, colsel=None, fix=True) [edit on github][source]

Bases: astropy.wcs.WCSBase

WCS objects perform standard WCS transformations, and correct for SIP and Paper IV table-lookup distortions, based on the WCS keywords and supplementary data read from a FITS file.

Parameters :

header : astropy.io.fits header object, string, dict-like, or None, optional

If header is not provided or None, the object will be initialized to default values.

fobj : An astropy.io.fits file (hdulist) object, optional

It is needed when header keywords point to a Paper IV Lookup table distortion stored in a different extension.

key : string, optional

The name of a particular WCS transform to use. This may be either ' ' or 'A'-'Z' and corresponds to the "a" part of the CTYPEia cards. key may only be provided if header is also provided.

minerr : float, optional

The minimum value a distortion correction must have in order to be applied. If the value of CQERRja is smaller than minerr, the corresponding distortion is not applied.

relax : bool or int, optional

Degree of permissiveness:

  • True (default): Admit all recognized informal extensions of the WCS standard.
  • False: Recognize only FITS keywords defined by the published WCS standard.
  • int: a bit field selecting specific extensions to accept. See Header-reading relaxation constants for details.

naxis : int or sequence, optional

Extracts specific coordinate axes using sub(). If a header is provided, and naxis is not None, naxis will be passed to sub() in order to select specific axes from the header. See sub() for more details about this parameter.

keysel : sequence of flags, optional

A sequence of flags used to select the keyword types considered by wcslib. When None, only the standard image header keywords are considered (and the underlying wcspih() C function is called). To use binary table image array or pixel list keywords, keysel must be set.

Each element in the list should be one of the following strings:

  • ‘image’: Image header keywords
  • ‘binary’: Binary table image array keywords
  • ‘pixel’: Pixel list keywords

Keywords such as EQUIna or RFRQna that are common to binary table image arrays and pixel lists (including WCSNna and TWCSna) are selected by both ‘binary’ and ‘pixel’.

colsel : sequence of int, optional

A sequence of table column numbers used to restrict the WCS transformations considered to only those pertaining to the specified columns. If None, there is no restriction.

fix : bool, optional

When True (default), call fix on the resulting object to fix any non-standard uses in the header. FITSFixedWarning Warnings will be emitted if any changes were made.

Raises :

MemoryError :

Memory allocation failed.

ValueError :

Invalid key.

KeyError :

Key not found in FITS header.

AssertionError :

Lookup table distortion present in the header but fobj was not provided.

Notes

  1. astropy.wcs supports arbitrary n dimensions for the core WCS (the transformations handled by WCSLIB). However, the Paper IV lookup table and SIP distortions must be two dimensional. Therefore, if you try to create a WCS object where the core WCS has a different number of dimensions than 2 and that object also contains a Paper IV lookup table or SIP distortion, a ValueError exception will be raised. To avoid this, consider using the naxis kwarg to select two dimensions from the core WCS.

  2. The number of coordinate axes in the transformation is not determined directly from the NAXIS keyword but instead from the highest of:

    • NAXIS keyword
    • WCSAXESa keyword
    • The highest axis number in any parameterized WCS keyword. The keyvalue, as well as the keyword, must be syntactically valid otherwise it will not be considered.

    If none of these keyword types is present, i.e. if the header only contains auxiliary WCS keywords for a particular coordinate representation, then no coordinate description is constructed for it.

    The number of axes, which is set as the naxis member, may differ for different coordinate representations of the same image.

  3. When the header includes duplicate keywords, in most cases the last encountered is used.

Attributes Summary

naxis1

Deprecated since version 0.2.

naxis2

Deprecated since version 0.2.

Methods Summary

get_naxis(*args, **kwargs)

Deprecated since version 0.2.

pix2foc(*args) Convert pixel coordinates to focal plane coordinates using the SIP polynomial distortion convention and Paper IV table-lookup distortion correction.
calcFootprint([header, undistort, axes]) Calculates the footprint of the image on the sky.
wcs_sky2pix(*args, **kwargs)

Deprecated since version 0.0.

printwcs() Temporary function for internal use.
deepcopy() Return a deep copy of the object.
sip_foc2pix(*args) Convert focal plane coordinates to pixel coordinates using the SIP polynomial distortion convention.
to_header_string([relax]) Identical to to_header, but returns a string containing the header cards.
sub(axes) Extracts the coordinate description for a subimage from a WCS object.
footprint_to_file([filename, color, width]) Writes out a ds9 style regions file.
to_fits([relax]) Generate an astropy.io.fits.HDUList object with all of the information stored in this object.
wcs_pix2world(*args, **kwargs) Transforms pixel coordinates to world coordinates by doing only the basic wcslib transformation.
wcs_world2pix(*args, **kwargs) Transforms world coordinates to pixel coordinates, using only the basic wcslib WCS transformation.
p4_pix2foc(*args) Convert pixel coordinates to focal plane coordinates using Paper IV table-lookup distortion correction.
rotateCD(theta)
wcs_pix2sky(*args, **kwargs)

Deprecated since version 0.0.

get_axis_types() Similar to self.wcsprm.axis_types but provides the information in a more Python-friendly format.
det2im(*args) Convert detector coordinates to image plane coordinates using Paper IV table-lookup distortion correction.
copy() Return a shallow copy of the object.
all_pix2world(*args, **kwargs) Transforms pixel coordinates to world coordinates.
to_header([relax]) Generate an astropy.io.fits.Header object with the basic WCS and SIP information stored in this object.
sip_pix2foc(*args) Convert pixel coordinates to focal plane coordinates using the SIP polynomial distortion convention.
all_pix2sky(*args, **kwargs)

Deprecated since version 0.0.

Attributes Documentation

naxis1

Deprecated since version 0.2: The naxis1 attribute is deprecated and may be removed in a future version.

naxis2

Deprecated since version 0.2: The naxis2 attribute is deprecated and may be removed in a future version.

Methods Documentation

get_naxis(*args, **kwargs) [edit on github][source]

Deprecated since version 0.2: This method should not be public

pix2foc(*args) [edit on github][source]

Convert pixel coordinates to focal plane coordinates using the SIP polynomial distortion convention and Paper IV table-lookup distortion correction.

Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x 2 array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

Returns :

result : array

Returns the focal coordinates. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

ValueError :

Invalid coordinate transformation parameters.

calcFootprint(header=None, undistort=True, axes=None) [edit on github][source]

Calculates the footprint of the image on the sky.

A footprint is defined as the positions of the corners of the image on the sky after all available distortions have been applied.

Parameters :

header : astropy.io.fits header object, optional

undistort : bool, optional

If True, take SIP and distortion lookup table into account

axes : length 2 sequence ints, optional

If provided, use the given sequence as the shape of the image. Otherwise, use the NAXIS1 and NAXIS2 keywords from the header that was used to create this WCS object.

Returns :

coord : (4, 2) array of (x, y) coordinates.

wcs_sky2pix(*args, **kwargs) [edit on github][source]

Deprecated since version 0.0: Use wcs_world2pix instead.

printwcs() [edit on github][source]

Temporary function for internal use.

deepcopy() [edit on github][source]

Return a deep copy of the object.

Convenience method so user doesn’t have to import the copy stdlib module.

sip_foc2pix(*args) [edit on github][source]

Convert focal plane coordinates to pixel coordinates using the SIP polynomial distortion convention.

Paper IV table lookup distortion correction is not applied, even if that information existed in the FITS file that initialized this WCS object.

Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x 2 array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

Returns :

result : array

Returns the pixel coordinates. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

ValueError :

Invalid coordinate transformation parameters.

to_header_string(relax=False) [edit on github][source]

Identical to to_header, but returns a string containing the header cards.

sub(axes) [edit on github][source]

Extracts the coordinate description for a subimage from a WCS object.

The world coordinate system of the subimage must be separable in the sense that the world coordinates at any point in the subimage must depend only on the pixel coordinates of the axes extracted. In practice, this means that the PCi_ja matrix of the original image must not contain non-zero off-diagonal terms that associate any of the subimage axes with any of the non-subimage axes.

sub can also add axes to a wcsprm object. The new axes will be created using the defaults set by the Wcsprm constructor which produce a simple, unnamed, linear axis with world coordinates equal to the pixel coordinate. These default values can be changed before invoking set.

Parameters :

axes : int or a sequence.

  • If an int, include the first N axes in their original order.
  • If a sequence, may contain a combination of image axis numbers (1-relative) or special axis identifiers (see below). Order is significant; axes[0] is the axis number of the input image that corresponds to the first axis in the subimage, etc. Use an axis number of 0 to create a new axis using the defaults.
  • If 0, [] or None, do a deep copy.

Coordinate axes types may be specified using either strings or special integer constants. The available types are:

  • 'longitude' / WCSSUB_LONGITUDE: Celestial longitude
  • 'latitude' / WCSSUB_LATITUDE: Celestial latitude
  • 'cubeface' / WCSSUB_CUBEFACE: Quadcube CUBEFACE axis
  • 'spectral' / WCSSUB_SPECTRAL: Spectral axis
  • 'stokes' / WCSSUB_STOKES: Stokes axis
  • 'celestial' / WCSSUB_CELESTIAL: An alias for the combination of 'longitude', 'latitude' and 'cubeface'.
Returns :

new_wcs : WCS object

Raises :

MemoryError :

Memory allocation failed.

InvalidSubimageSpecificationError :

Invalid subimage specification (no spectral axis).

NonseparableSubimageCoordinateSystem :

Non-separable subimage coordinate system.

Notes

Combinations of subimage axes of particular types may be extracted in the same order as they occur in the input image by combining the integer constants with the ‘binary or’ (|) operator. For example:

wcs.sub([WCSSUB_LONGITUDE | WCSSUB_LATITUDE | WCSSUB_SPECTRAL])

would extract the longitude, latitude, and spectral axes in the same order as the input image. If one of each were present, the resulting object would have three dimensions.

For convenience, WCSSUB_CELESTIAL is defined as the combination WCSSUB_LONGITUDE | WCSSUB_LATITUDE | WCSSUB_CUBEFACE.

The codes may also be negated to extract all but the types specified, for example:

wcs.sub([
  WCSSUB_LONGITUDE,
  WCSSUB_LATITUDE,
  WCSSUB_CUBEFACE,
  -(WCSSUB_SPECTRAL | WCSSUB_STOKES)])

The last of these specifies all axis types other than spectral or Stokes. Extraction is done in the order specified by axes, i.e. a longitude axis (if present) would be extracted first (via axes[0]) and not subsequently (via axes[3]). Likewise for the latitude and cubeface axes in this example.

The number of dimensions in the returned object may be less than or greater than the length of axes. However, it will never exceed the number of axes in the input image.

footprint_to_file(filename=None, color='green', width=2) [edit on github][source]

Writes out a ds9 style regions file. It can be loaded directly by ds9.

Parameters :

filename : string, optional

Output file name - default is 'footprint.reg'

color : string, optional

Color to use when plotting the line.

width : int, optional

Width of the region line.

to_fits(relax=False) [edit on github][source]

Generate an astropy.io.fits.HDUList object with all of the information stored in this object. This should be logically identical to the input FITS file, but it will be normalized in a number of ways.

See to_header for some warnings about the output produced.

Parameters :

relax : bool or int, optional

Degree of permissiveness:

  • False (default): Write all extensions that are considered to be safe and recommended.
  • True: Write all recognized informal extensions of the WCS standard.
  • int: a bit field selecting specific extensions to write. See Header-writing relaxation constants for details.
Returns :

hdulist : astropy.io.fits.HDUList

wcs_pix2world(*args, **kwargs) [edit on github][source]

Transforms pixel coordinates to world coordinates by doing only the basic wcslib transformation.

No SIP or Paper IV table lookup distortion correction is applied. To perform distortion correction, see all_pix2world, sip_pix2foc, p4_pix2foc, or pix2foc.

Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x naxis array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

For a transformation that is not two-dimensional, the two-argument form must be used.

ra_dec_order : bool, optional

When True will ensure that world coordinates are always given and returned in as (ra, dec) pairs, regardless of the order of the axes specified by the in the CTYPE keywords. Default is False.

Returns :

result : array

Returns the world coordinates, in degrees. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

SingularMatrixError :

Linear transformation matrix is singular.

InconsistentAxisTypesError :

Inconsistent or unrecognized coordinate axis types.

ValueError :

Invalid parameter value.

ValueError :

Invalid coordinate transformation parameters.

ValueError :

x- and y-coordinate arrays are not the same size.

InvalidTransformError :

Invalid coordinate transformation parameters.

InvalidTransformError :

Ill-conditioned coordinate transformation parameters.

Notes

The order of the axes for the result is determined by the CTYPEia keywords in the FITS header, therefore it may not always be of the form (ra, dec). The lat, lng, lattyp and lngtyp members can be used to determine the order of the axes.

wcs_world2pix(*args, **kwargs) [edit on github][source]

Transforms world coordinates to pixel coordinates, using only the basic wcslib WCS transformation. No SIP or Paper IV table lookup distortion is applied.

Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x naxis array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

For a transformation that is not two-dimensional, the two-argument form must be used.

ra_dec_order : bool, optional

When True will ensure that world coordinates are always given and returned in as (ra, dec) pairs, regardless of the order of the axes specified by the in the CTYPE keywords. Default is False.

Returns :

result : array

Returns the pixel coordinates. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

SingularMatrixError :

Linear transformation matrix is singular.

InconsistentAxisTypesError :

Inconsistent or unrecognized coordinate axis types.

ValueError :

Invalid parameter value.

ValueError :

Invalid coordinate transformation parameters.

ValueError :

x- and y-coordinate arrays are not the same size.

InvalidTransformError :

Invalid coordinate transformation parameters.

InvalidTransformError :

Ill-conditioned coordinate transformation parameters.

Notes

The order of the axes for the input world array is determined by the CTYPEia keywords in the FITS header, therefore it may not always be of the form (ra, dec). The lat, lng, lattyp and lngtyp members can be used to determine the order of the axes.

p4_pix2foc(*args) [edit on github][source]

Convert pixel coordinates to focal plane coordinates using Paper IV table-lookup distortion correction.

Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x 2 array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

Returns :

result : array

Returns the focal coordinates. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

ValueError :

Invalid coordinate transformation parameters.

rotateCD(theta) [edit on github][source]
wcs_pix2sky(*args, **kwargs) [edit on github][source]

Deprecated since version 0.0: Use wcs_pix2world instead.

get_axis_types() [edit on github][source]

Similar to self.wcsprm.axis_types but provides the information in a more Python-friendly format.

Returns :

result : list of dicts

Returns a list of dictionaries, one for each axis, each containing attributes about the type of that axis.

Each dictionary has the following keys:

  • ‘coordinate_type’:
    • None: Non-specific coordinate type.
    • ‘stokes’: Stokes coordinate.
    • ‘celestial’: Celestial coordinate (including CUBEFACE).
    • ‘spectral’: Spectral coordinate.
  • ‘scale’:
    • ‘linear’: Linear axis.
    • ‘quantized’: Quantized axis (STOKES, CUBEFACE).
    • ‘non-linear celestial’: Non-linear celestial axis.
    • ‘non-linear spectral’: Non-linear spectral axis.
    • ‘logarithmic’: Logarithmic axis.
    • ‘tabular’: Tabular axis.
  • ‘group’
    • Group number, e.g. lookup table number
  • ‘number’
    • For celestial axes:
      • 0: Longitude coordinate.
      • 1: Latitude coordinate.
      • 2: CUBEFACE number.
    • For lookup tables:
      • the axis number in a multidimensional table.

CTYPEia in "4-3" form with unrecognized algorithm code will generate an error.

det2im(*args) [edit on github][source]

Convert detector coordinates to image plane coordinates using Paper IV table-lookup distortion correction.

Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x 2 array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

Returns :

result : array

Returns the pixel coordinates. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

ValueError :

Invalid coordinate transformation parameters.

copy() [edit on github][source]

Return a shallow copy of the object.

Convenience method so user doesn’t have to import the copy stdlib module.

all_pix2world(*args, **kwargs) [edit on github][source]

Transforms pixel coordinates to world coordinates.

Performs all of the following in order:

  • Detector to image plane correction (optionally)
  • SIP distortion correction (optionally)
  • Paper IV table-lookup distortion correction (optionally)
  • wcslib WCS transformation
Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x naxis array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

For a transformation that is not two-dimensional, the two-argument form must be used.

ra_dec_order : bool, optional

When True will ensure that world coordinates are always given and returned in as (ra, dec) pairs, regardless of the order of the axes specified by the in the CTYPE keywords. Default is False.

Returns :

result : array

Returns the sky coordinates, in degrees. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

SingularMatrixError :

Linear transformation matrix is singular.

InconsistentAxisTypesError :

Inconsistent or unrecognized coordinate axis types.

ValueError :

Invalid parameter value.

ValueError :

Invalid coordinate transformation parameters.

ValueError :

x- and y-coordinate arrays are not the same size.

InvalidTransformError :

Invalid coordinate transformation parameters.

InvalidTransformError :

Ill-conditioned coordinate transformation parameters.

Notes

The order of the axes for the result is determined by the CTYPEia keywords in the FITS header, therefore it may not always be of the form (ra, dec). The lat, lng, lattyp and lngtyp members can be used to determine the order of the axes.

to_header(relax=False) [edit on github][source]

Generate an astropy.io.fits.Header object with the basic WCS and SIP information stored in this object. This should be logically identical to the input FITS file, but it will be normalized in a number of ways.

Warning

This function does not write out Paper IV distortion information, since that requires multiple FITS header data units. To get a full representation of everything in this object, use to_fits.

Parameters :

relax : bool or int, optional

Degree of permissiveness:

  • False (default): Write all extensions that are considered to be safe and recommended.
  • True: Write all recognized informal extensions of the WCS standard.
  • int: a bit field selecting specific extensions to write. See Header-writing relaxation constants for details.
Returns :

header : astropy.io.fits.Header

Notes

The output header will almost certainly differ from the input in a number of respects:

  1. The output header only contains WCS-related keywords. In particular, it does not contain syntactically-required keywords such as SIMPLE, NAXIS, BITPIX, or END.
  2. Deprecated (e.g. CROTAn) or non-standard usage will be translated to standard (this is partially dependent on whether fix was applied).
  3. Quantities will be converted to the units used internally, basically SI with the addition of degrees.
  4. Floating-point quantities may be given to a different decimal precision.
  5. Elements of the PCi_j matrix will be written if and only if they differ from the unit matrix. Thus, if the matrix is unity then no elements will be written.
  6. Additional keywords such as WCSAXES, CUNITia, LONPOLEa and LATPOLEa may appear.
  7. The original keycomments will be lost, although to_header tries hard to write meaningful comments.
  8. Keyword order may be changed.
sip_pix2foc(*args) [edit on github][source]

Convert pixel coordinates to focal plane coordinates using the SIP polynomial distortion convention.

Paper IV table lookup distortion correction is not applied, even if that information existed in the FITS file that initialized this WCS object. To correct for that, use pix2foc or p4_pix2foc.

Parameters :

args : flexible

There are two accepted forms for the positional arguments:

  • 2 arguments: An N x 2 array of coordinates, and an origin.
  • more than 2 arguments: An array for each axis, followed by an origin. These arrays must be broadcastable to one another.

Here, origin is the coordinate in the upper left corner of the image. In FITS and Fortran standards, this is 1. In Numpy and C standards this is 0.

Returns :

result : array

Returns the focal coordinates. If the input was a single array and origin, a single array is returned, otherwise a tuple of arrays is returned.

Raises :

MemoryError :

Memory allocation failed.

ValueError :

Invalid coordinate transformation parameters.

all_pix2sky(*args, **kwargs) [edit on github][source]

Deprecated since version 0.0: Use all_pix2world instead.

Page Contents