<jupyter_start><jupyter_text>Handling image coordinatesThe `astropy.wcs` package implements the FITS World Coordinate System (WCS) standard, and some commonly-used distortion conventions used in imaging data.This tutorial will show how to extract from FITS files, and how to use it to transform coordinates.<jupyter_code>import os
import numpy as np
%matplotlib inline
from matplotlib import pyplot as plt
from astropy.utils.data import get_pkg_data_filename
from astropy.io import fits
from astropy import wcs<jupyter_output><empty_output><jupyter_text>Creating a WCS object from the header of a FITS file Open a FITS image with `astropy.fits` and examine the contents.<jupyter_code>image_file = fits.open('data/w5.fits')

image_file.info()<jupyter_output><empty_output><jupyter_text>To create a WCS object pass the header with the WCS keywords to astropy.wcs.WCS. In this case it is the primary header.<jupyter_code>w = wcs.WCS(image_file[0].header)
print(w)<jupyter_output><empty_output><jupyter_text>For this case, `fits.getheader` may be used to save a step.<jupyter_code>w = wcs.WCS(fits.getheader('data/w5.fits'))<jupyter_output><empty_output><jupyter_text>Examine the physical types of the world coordinate axes, to show they are RA and Dec.<jupyter_code>w.world_axis_physical_types<jupyter_output><empty_output><jupyter_text>Print the number of pixel and world axes, and the shape of the data<jupyter_code>print(w.pixel_n_dim, w.world_n_dim, w.array_shape)<jupyter_output><empty_output><jupyter_text>Transforming between pixel coordinates and sky coordinates To determine the sky coordinate associated with a position on the detector, pass x-pixel and y-pixel values to the `pixel_to_world` method.The inputs can be numbers, numpy arrays or array-like objects. See the [Pixel Conventions and Definitions](https://docs.astropy.org/en/stable/wcs/index.htmlpixel-conventions) for an explanation of the pixel numbering convention.The output is a SkyCoord object.<jupyter_code>coord = w.pixel_to_world([10, 100], [24, 500.5])
print(coord)<jupyter_output><empty_output><jupyter_text>Perfom the inverse transformation - from sky to detector coordinates.<jupyter_code>w.world_to_pixel(coord)<jupyter_output><empty_output><jupyter_text>Creating a WCS programatically A WCS object can be created programatically. Here is a concise example with 1 arcsecond pixels that is aligned with "North up, East to the left".<jupyter_code>my_wcs = wcs.WCS(naxis=2)
my_wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN']
my_wcs.wcs.crpix = [512, 512]
my_wcs.wcs.crval = [70., 20.]
my_wcs.wcs.cdelt = [-1/3600, 1/3600]
my_wcs.array_shape = [1024, 1024] # NAXIS2, NAXIS1
my_wcs<jupyter_output><empty_output><jupyter_text>Exercise: compute the coordinates of image cornersFor the WCS `w` we created from the `'data/w5.fits'` image, compute and print the RA and Dec coordinates of the centers of the corner pixels of the image, which in pixel units are (0, 0), (0, 999), (999, 0), (999, 999).Print the output of the `w.calc_footprint()` method for comparison. If you prefer to load the solution, uncomment the line below and run the cell.<jupyter_code># %load wcs_corners_solution.py<jupyter_output><empty_output>