#include #include #include /* Every program which uses the CFITSIO interface must include the the fitsio.h header file. This contains the prototypes for all the routines and defines the error status values and other symbolic constants used in the interface. */ #include "fitsio.h" int main( void ); void writeimage( void ); void writeascii( void ); void writebintable( void ); void copyhdu( void ); void selectrows( void ); void readheader( void ); void readimage( void ); void readtable( void ); void printerror( int status); int main() { /************************************************************************* This is a simple main program that calls the following routines: writeimage - write a FITS primary array image writeascii - write a FITS ASCII table extension writebintable - write a FITS binary table extension copyhdu - copy a header/data unit from one FITS file to another selectrows - copy selected row from one HDU to another readheader - read and print the header keywords in every extension readimage - read a FITS image and compute the min and max value readtable - read columns of data from ASCII and binary tables **************************************************************************/ writeimage(); writeascii(); writebintable(); copyhdu(); selectrows(); readheader(); readimage(); readtable(); printf("\nAll the cfitsio cookbook routines ran successfully.\n"); return(0); } /*--------------------------------------------------------------------------*/ void writeimage( void ) /******************************************************/ /* Create a FITS primary array containing a 2-D image */ /******************************************************/ { fitsfile *fptr; /* pointer to the FITS file, defined in fitsio.h */ int status, ii, jj; long fpixel, nelements, exposure; unsigned short *array[200]; /* initialize FITS image parameters */ char filename[] = "atestfil.fit"; /* name for new FITS file */ int bitpix = USHORT_IMG; /* 16-bit unsigned short pixel values */ long naxis = 2; /* 2-dimensional image */ long naxes[2] = { 300, 200 }; /* image is 300 pixels wide by 200 rows */ /* allocate memory for the whole image */ array[0] = (unsigned short *)malloc( naxes[0] * naxes[1] * sizeof( unsigned short ) ); /* initialize pointers to the start of each row of the image */ for( ii=1; ii 0) { nbuffer = npixels; if (npixels > buffsize) nbuffer = buffsize; /* read as many pixels as will fit in buffer */ /* Note that even though the FITS images contains unsigned integer */ /* pixel values (or more accurately, signed integer pixels with */ /* a bias of 32768), this routine is reading the values into a */ /* float array. Cfitsio automatically performs the datatype */ /* conversion in cases like this. */ if ( fits_read_img(fptr, TFLOAT, fpixel, nbuffer, &nullval, buffer, &anynull, &status) ) printerror( status ); for (ii = 0; ii < nbuffer; ii++) { if ( buffer[ii] < datamin ) datamin = buffer[ii]; if ( buffer[ii] > datamax ) datamax = buffer[ii]; } npixels -= nbuffer; /* increment remaining number of pixels */ fpixel += nbuffer; /* next pixel to be read in image */ } printf("\nMin and max image pixels = %.0f, %.0f\n", datamin, datamax); if ( fits_close_file(fptr, &status) ) printerror( status ); return; } /*--------------------------------------------------------------------------*/ void readtable( void ) /************************************************************/ /* read and print data values from an ASCII or binary table */ /************************************************************/ { fitsfile *fptr; /* pointer to the FITS file, defined in fitsio.h */ int status, hdunum, hdutype, nfound, anynull, ii; long frow, felem, nelem, longnull, dia[6]; float floatnull, den[6]; char strnull[10], *name[6], *ttype[3]; char filename[] = "atestfil.fit"; /* name of existing FITS file */ status = 0; if ( fits_open_file(&fptr, filename, READONLY, &status) ) printerror( status ); for (ii = 0; ii < 3; ii++) /* allocate space for the column labels */ ttype[ii] = (char *) malloc(FLEN_VALUE); /* max label length = 69 */ for (ii = 0; ii < 6; ii++) /* allocate space for string column value */ name[ii] = (char *) malloc(10); for (hdunum = 2; hdunum <= 3; hdunum++) /*read ASCII, then binary table */ { /* move to the HDU */ if ( fits_movabs_hdu(fptr, hdunum, &hdutype, &status) ) printerror( status ); if (hdutype == ASCII_TBL) printf("\nReading ASCII table in HDU %d:\n", hdunum); else if (hdutype == BINARY_TBL) printf("\nReading binary table in HDU %d:\n", hdunum); else { printf("Error: this HDU is not an ASCII or binary table\n"); printerror( status ); } /* read the column names from the TTYPEn keywords */ fits_read_keys_str(fptr, "TTYPE", 1, 3, ttype, &nfound, &status); printf(" Row %10s %10s %10s\n", ttype[0], ttype[1], ttype[2]); frow = 1; felem = 1; nelem = 6; strcpy(strnull, " "); longnull = 0; floatnull = 0.; /* read the columns */ fits_read_col(fptr, TSTRING, 1, frow, felem, nelem, strnull, name, &anynull, &status); fits_read_col(fptr, TLONG, 2, frow, felem, nelem, &longnull, dia, &anynull, &status); fits_read_col(fptr, TFLOAT, 3, frow, felem, nelem, &floatnull, den, &anynull, &status); for (ii = 0; ii < 6; ii++) printf("%5d %10s %10ld %10.2f\n", ii + 1, name[ii], dia[ii], den[ii]); } for (ii = 0; ii < 3; ii++) /* free the memory for the column labels */ free( ttype[ii] ); for (ii = 0; ii < 6; ii++) /* free the memory for the string column */ free( name[ii] ); if ( fits_close_file(fptr, &status) ) printerror( status ); return; } /*--------------------------------------------------------------------------*/ void printerror( int status) { /*****************************************************/ /* Print out cfitsio error messages and exit program */ /*****************************************************/ if (status) { fits_report_error(stderr, status); /* print error report */ exit( status ); /* terminate the program, returning error status */ } return; }