GNU Astronomy Utilities

Next: , Previous: , Up: ConvertType   [Contents][Index]

5.2.3 Aligning images with small WCS offsets

In order to have nice color images, it is important that the images be properly aligned. This is usually the case in many scenarios, but it some times happens that the images have a small WCS offset, even though they have the same size. In such cases you can use the script below to align the images into approximately the same pixel grid (to within about 0.5 pixels which is sufficient in many color-image usage scenarios).

The script below does the job using Gnuastro’s Warp and Crop programs. Simply copy the lines below into a plain-text file with your favorite text editor and save it as Don’t forget to set the variables of the first three lines to specify the file names (without the .fits suffix) and the HDUs of your inputs. These four lines are all you need to edit, leave the rest unchanged. Also, if you are copy/pasting the script from a PDF, be careful that the single-quotes used in AWK may need to be corrected.


# Set the input names (without the '.fits' suffix),
# and their HDUs.

# To stop the script if there is a crash
set -e

# Align all the images to the celestial poles.
astwarp $r.fits --align -h$rhdu -o $r-aligned.fits
astwarp $g.fits --align -h$ghdu -o $g-aligned.fits
astwarp $b.fits --align -h$bhdu -o $b-aligned.fits

# Calculate the final WCS-based center and image-based width based on
# the G-band (in RGB) image.
centerwcs=$(astfits $g-aligned.fits --skycoverage --quiet \
                    | awk 'NR==1{printf "%g %g", $1,$2}')
widthpix=$(astfits $g-aligned.fits -h1 --quiet \
                   --keyvalue=NAXIS1,NAXIS2 \
               | awk '{printf "%d,%d", $1, $2}')

# Crop all the images around the desired center and width.
for f in $r $g $b; do
  centerpix=$(echo $centerwcs \
                   | asttable -c'arith $1 $2 wcs-to-img' \
                              --wcsfile=$f-aligned.fits \
                   | awk '{printf "%g,%g", $1, $2}')
  astcrop $f-aligned.fits --mode=img --width=$widthpix \
          --center=$centerpix -o$f-use.fits
  rm $f-aligned.fits

Once you have saved the file and come back to your command-line you can run the script like this:

$ chmod +x
$ ./

Of course, feel free to hack it and modify it to fit your datasets, like the rest of Gnuastro, this script is released under GNU GPLv.3 and above, see Your rights.

Next: Annotations for figure in paper, Previous: Color, Up: ConvertType   [Contents][Index]