next up previous contents FITSIO Home
Next: 2.5 SELECTROWS - copy Up: 2. Example FITSIO Programs Previous: 2.3 WRITEBINTABLE - write   Contents

2.4 COPYHDU - copy HDUs to a new file

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.

  1. The STATUS parameter must always be initialized. See the WRITEIMAGE program for a more detailed discussion.

  2. The DELETEFILE routine (described later in the Utilities section of the Cookbook) simply deletes the output FITS file if it already exists so that we can then regenerate it.
  3. FTGIOU allocates a logical unit number for use when opening a file. See the WRITEIMAGE program for a more detailed discussion.
  4. The input FITS file is opened with READONLY access by specifying READWRITE = 0.
  5. FTINIT creates a new empty output FITS file.
  6. FTCOPY copies the current HDU from the input FITS file to the output file. The MOREKEY parameter allows one to reserve space for additional header keywords when the HDU is created. FITSIO will automatically insert more header space if required, so programmers do not have to reserve space ahead of time, although it is more efficient to do so if it is known that more keywords will be appended to the header.

  7. FTCRHD creates a new empty FITS extension in the output file following the current extension and moves to it.
  8. FTMAHD moves the internal FITSIO pointer to the 3rd HDU (as specified here by the second parameter) which in this case is the binary table extension created by the previous WRITEBINARY routine.
  9. FTCOPY now copies the binary table from the input FITS file to the output file.
  10. The FITS file must always be closed before exiting the program.
  11. Any unit numbers allocated with FTGIOU must be freed with FTFIOU. Giving -1 for the value of the first argument causes all previously allocated unit numbers to be released.
  12. PRINTERROR is a general routine to print out error messages and is described later in the Utilities section of this cookbook.

      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

next up previous contents FITSIO Home
Next: 2.5 SELECTROWS - copy Up: 2. Example FITSIO Programs Previous: 2.3 WRITEBINTABLE - write   Contents