This routine copies selected HDUs from one FITS file to another. In this example the input FITS file has 3 HDUs (the Primary array, a TABLE extension, and a BINTABLE extension). The 1st and 3rd HDU are copied into a new output FITS file.
subroutine copyhdu
C copy the 1st and 3rd HDUs from the input file to a new FITS file
integer status,inunit,outunit,readwrite,blocksize,morekeys,hdutype
character infilename*40,outfilename*40
1 status=0
C Name of the FITS files:
infilename='ATESTFILEZ.FITS'
outfilename='BTESTFILEZ.FITS'
C Delete the file if it already exists, so we can then recreate it
2 call deletefile(outfilename,status)
C Get unused Logical Unit Numbers to use to open the FITS files
3 call ftgiou(inunit,status)
call ftgiou(outunit,status)
C open the input FITS file, with readonly access
readwrite=0
4 call ftopen(inunit,infilename,readwrite,blocksize,status)
C create the new empty FITS file with the standard block size
blocksize=1
5 call ftinit(outunit,outfilename,blocksize,status)
C copy the primary array from the input file to the output file
morekeys=0
6 call ftcopy(inunit,outunit,morekeys,status)
C append/create a new empty extension on the end of the output file
7 call ftcrhd(outunit,status)
C skip to the 3rd extension in the input file
8 call ftmahd(inunit,3,hdutype,status)
C copy this extension from the input file to the output file
9 call ftcopy(inunit,outunit,morekeys,status)
C close the FITS file and free the unit numbers
10 call ftclos(inunit, status)
call ftclos(outunit, status)
11 call ftfiou(-1, status)
C check for any error, and if so print out error messages
12 if (status .gt. 0)call printerror(status)
end