« CASA leap seconds issue | Main | Observing last night - Jupiter + Dumbbell »

Creating FITS files in c++

A simple example, very similar to that given in the CFITSIO guidebook, on how to create a FITS file using CFITSIO. In this case I'm also building against some casacore libraries, but these aren't going to be used in this little code snippet but the idea is to use casacore todo further analysis. I'm hoping to post more here over time. Anyway the code (this can also be found as a filebuild_fits.cpp:

/* Create a FITS file, using cfitsio and some casacore libraries by Samuel George 21-11-2011 Compile: g++ build_fits.cpp -o build_fits -lcasa_casa -lcfitsio */ #include // STL iostream #include #include

extern "C"{
#include
}

using std::cerr;
using std::cout;
using std::endl;
using std::string;

int main()
{
cout << "Create a FITS file" << endl;
int lenTime(10), status (0), lenFreq(20);
long naxis(2), naxes[2] = {lenTime,lenFreq};
long nelements (lenTime*lenFreq);
long fpixel (1), exposure (1500);
char comment[] ="Total Exposure Time";
cout << comment << endl;
float pixels[lenFreq][lenTime];
// create an array of pixels
try {
for (int ii(0); ii < lenTime; ii++){
for (int jj(0); jj pixels[jj][ii] = 10.0*(ii+jj);
}
}
} catch (string message) {
cerr << "Error creating pixel array" << message << endl;
}

try { // write the image to a fits file...
fitsfile *fptr;
fits_create_file (&fptr, "!output.fits", &status);
fits_create_img (fptr, FLOAT_IMG,naxis,naxes,&status);
/* Write a keyword - its the address you pass */
fits_update_key(fptr,TLONG,"EXPOSURE",&exposure,comment,&status);
//write an array to the image
fits_write_img(fptr, TFLOAT, fpixel, nelements, pixels[0],&status);
fits_close_file(fptr,&status);
status = 0 ;
} catch (std::string message) {
cerr << message << endl;
}
}

Save the code and compile like so:

g++ build_fits.cpp -o build_fits -lcasa_casa -lcfitsio

and then run with ./build_fits

TrackBack

TrackBack URL for this entry:
http://krioma.net/cgi-bin/MT-3.38/mt-tb.cgi/584

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on December 18, 2011 10:41 AM.

The previous post in this blog was CASA leap seconds issue.

The next post in this blog is Observing last night - Jupiter + Dumbbell.

Many more can be found on the main index page or by looking through the archives.

Get Firefox! Valid XHTML 1.0! Valid CSS! RSS Feed BlogUniverse - listed Powered by Apache Creative Commons License ringsofsaturnrock's Most Interesting Photos on Flickriver

Powered by
Movable Type 3.38