CCfits  2.4
Copying an Extension between Files

Copying extensions from one fits file to another is very straightforward. A complication arises, however, because CCfits requires every FITS object to correspond to a conforming FITS file once constructed. Thus we provide a custom constructor which copies the primary HDU of a ``source" FITS file into a new file. Subsequent extensions can be copied by name or extension number as illustrated below.

Note that the simple call

FITS::FITS(const std::string& filename)

Reads the headers for all of the extensions in the file, so that after the FITS object corresponding to infile in the following code is instantiated, all extensions are recognized [read calls are also provided to read only specific HDUs - see below].

In the example code below, the file outFile is written straight to disk. Since the code never requests that the HDUs being written to that file are read, the user needs to add statements to do this after the copy is complete.

int copyHDU()
{
    //******************************************************************
    // copy the 1st and 3rd HDUs from the input file to a new FITS file 
    //******************************************************************


    const string inFileName("atestfil.fit");  
    const string outFileName("btestfil.fit");  

    int status(0);

    status = 0;

    remove(outFileName.c_str());       // Delete old file if it already exists

    // open the existing FITS file
    FITS inFile(inFileName);
    
    // custom constructor FITS::FITS(const string&, const FITS&) for
    // this particular task.
    
    FITS outFile(outFileName,inFile);

    // copy extension by number...
    outFile.copy(inFile.extension(2));
    
    // copy extension by name...
    outFile.copy(inFile.extension("TABLE_BINARY"));
    
   return 0;

 }