<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Image Registration Exercise
Written by Gautham Narayan (gnarayan@stsci.edu) for LSST DSFP #5
In this directory, you should be able to find two fits file from one of the projects I worked on
Step1:  While the images have been de-trended, they still have the original WCS from the telescope. They aren't aligned. You could use ds9 to check this trivially, but lets do it with astropy instead.
Step2:  Use the astrometry.net client (solve-field) to determine an accurate WCS solution for this field.
Step3:  Options you might want to look at
Step4:  or
Step5:  Use the above info to solve for the WCS for both images.
The problem with the distortion polynomial that astronometry.net uses is that routines like ds9 ignore it. Look at astrometry.net's wcs-to-tan routine and convert the solved WCS to the tangent projection.
Step6:  The commands I used are in Register_images_solution.txt and the output I generated in the out/ subdirectory.
<ASSISTANT_TASK:>
Python Code:
!ls *fits
Explanation: Image Registration Exercise
Written by Gautham Narayan (gnarayan@stsci.edu) for LSST DSFP #5
In this directory, you should be able to find two fits file from one of the projects I worked on
End of explanation
import astropy.io.fits as afits
from astropy.wcs import WCS
from astropy.visualization import ZScaleInterval
import matplotlib
%matplotlib notebook
%pylab
f1 = afits.open('wdd7.040920_0452.051_6.fits')
f2 = afits.open('wdd7.080104_0214.1025_6.fits')
f1wcs = WCS(f1[0].header)
f2wcs = WCS(f2[0].header)
zscaler = ZScaleInterval(nsamples=1000, contrast=0.25)
f1d = zscaler(f1[0].data)
f2d = zscaler(f2[0].data)
fig = figure(figsize=(10,4))
ax = fig.add_subplot(1,1,1)
ax.imshow(f1d.T, cmap='Reds')
ax.imshow(f2d.T, cmap='Blues', alpha=0.5)
tight_layout()
xlabel('x')
ylabel('y')
savefig('out/original_misalignment.pdf')
Explanation: While the images have been de-trended, they still have the original WCS from the telescope. They aren't aligned. You could use ds9 to check this trivially, but lets do it with astropy instead.
End of explanation
!solve-field -h
Explanation: Use the astrometry.net client (solve-field) to determine an accurate WCS solution for this field.
End of explanation
print(WCS(f1[0].header))
Explanation: Options you might want to look at:
--ra, --dec and --radius: Restrict the solution to some radius around RA and Dec. The regular telescope WCS should be plenty for an initial guess.
--scale-units, --scale-low and --scale-high: You might not know the exact pixel scale (and it's a function of where you are on the detector in any case, but you also set limits from this based on the existing CD1_2, CD2_1
-D, -N: Write to an output directory and write out a new FITS file with the solved WCS.
<u>Don't use out/ as the output directory.</u>
--parity: You can usually set this and get a speedup of 2x
To get info from the header, you can use astropy, or just use imhead from the WCSTools package at the command line
End of explanation
!imhead wdd7.040920_0452.051_6.fits
Explanation: or
End of explanation
!wcs-to-tan -h
Explanation: Use the above info to solve for the WCS for both images.
The problem with the distortion polynomial that astronometry.net uses is that routines like ds9 ignore it. Look at astrometry.net's wcs-to-tan routine and convert the solved WCS to the tangent projection.
End of explanation
!cat Register_images_solutions_commands.txt
Explanation: The commands I used are in Register_images_solution.txt and the output I generated in the out/ subdirectory.
End of explanation
<END_TASK>