Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous
Next: Bibliography Up: No Title Previous: Access to Data

An Example Program

The following program creates a FITS file containing a primary image and an image extension. A brief commentary follows the program source code.

# include <aips/fits.h>
# include <aips/fitsio.h>
# include <aips/hdu.h>
# include <iostream.h>
# include <stdlib.h>

int main() {
    cout << "Create a primary array and an image extension\n";
    FitsOutput fout("test.dat",FITS::Disk);
    if (fout.err() == FitsIO::IOERR) {
        cout << "Could not open FITS output.\n";
        exit(0);
    }

    // We will create an array with 3 rows and 6 columns,
    // as one would normally do in C.
    const int row = 3;
    const int col = 6;
    long data[col][row];
    // And, we will populate it with some kind of data
    int i, j;
    for (i = 0; i < col; ++i)
      for(j = 0; j < row; ++j)
        data[i][j] = j * 10 + i;

    FitsKeywordList st; // Create the initial keyword list
    st.mk(FITS::SIMPLE,True,"Standard FITS format");
    st.mk(FITS::BITPIX,32,"Integer data");
    st.mk(FITS::NAXIS,2,"This is a primary array");
    st.mk(1,FITS::NAXIS,3);
    st.mk(2,FITS::NAXIS,6);
    st.mk(FITS::EXTEND,True,"Extension exists");
    st.spaces();
    st.comment("This is a test.");
    st.spaces();
    st.end();

    PrimaryArray<long> hdu1(st); // Create and write the initial HDU
    if (hdu1.err())
        exit(0);
    cout << "Initial HDU constructed\n";
    cout << hdu1; // Display the keyword list
    hdu1.write_hdr(fout);
    hdu1.store(&data[0][0],FITS::CtoF);
    hdu1.write(fout);

    FitsKeywordList kw; // Create a keyword list for an image extension
    kw.mk(FITS::XTENSION,"IMAGE   ","Image extension");
    kw.mk(FITS::BITPIX,32,"Integer data");
    kw.mk(FITS::NAXIS,2,"This is an image");
    kw.mk(1,FITS::NAXIS,3);
    kw.mk(2,FITS::NAXIS,6);
    kw.mk(FITS::PCOUNT,0);
    kw.mk(FITS::GCOUNT,1);
    kw.spaces();
    kw.comment("This is test 2.");
    kw.spaces();
    kw.end();

    ImageExtension<long> ie(kw);
    if (ie.err())
        exit(0);
    cout << "ImageExtension constructed\n";
    cout << ie; // Display the keyword list
    ie.write_hdr(fout);

    ie.set_next(row * col);        // setup to write the whole array
    for (i = 0; i < row; ++i)
      for(j = 0; j < col; ++j)
        ie.data(i,j) = i * 10 + j; // assign the data

    ie.write(fout);                // write the data

    return 0;
}

The `FitsOutput' statement creates a FITS disk file whose name is `test.dat'. The following section allocates a 3x6 array of integers and assigns some arbitrary data to the array. The section beginning `FitsKeywordList st' declares a keyword list, st. The function, st.mk(), creates a keyword entry and appends it to the end of the list. So, the following sequence of st functions, down to st.end(), creates a linked list of keywords sufficient to define a primary array with two dimensions, with 3 rows and 6 columns of integer data.

The statement, `PrimaryArray<long> hdu1(st);', creates a primary array header-data-unit, hdu1, using the st keyword list. The statement, `hdu1.write_hdr(fout);' translates the keyword list to a FITS header and writes it to the previously created FitsOutput file, fout. The `hdu1.store' function copies the C data array into the FITS buffer, while converting it from C-order to FORTRAN-order. Finally, the `hdu1.write(fout)' function writes the FITS data to disk, thus completing the writing of the primary array.

The next sequence of code creates another keyword list that defines an image extension. The statement `ImageExtension<long> ie(kw);' creates the image extension and `ie.write_hdr(fout)' writes the FITS header, as before. The following code uses the `set_next' function to allocate a buffer area and the `data' function to assign data to it. This sequence of operations is functionally equivalent to the `store' function used in the previous section. Finally, the `ie.write' function writes the data to disk.


next up previous
Next: Bibliography Up: No Title Previous: Access to Data
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15