/** * Saves the image to a FITS file using the cfitsio library. * * @param output_filename string, where to save the FITS file * @param sx, integer, horizontal image size * @param sy, integer, vertical image size * @param data, rows of a two dimensional integer array, image data * @return Zero on success, non-zero on failure. */ int camera_save_fits_image(char * output_filename, long sx, long sy, unsigned short * data) { fitsfile *fptr; int status = 0; long fpixel, nelements; float gain; int bitpix = USHORT_IMG; long naxis = 2; long naxes[2] = {sx, sy}; /* Delete old file if it already exists */ remove(output_filename); /* create new FITS file */ if (fits_create_file(&fptr, output_filename, &status)) { if (status) fits_report_error(stderr, status); return status; } /* the setting of bitpix = USHORT_IMG implies that BITPIX = 16, BSCALE = 1.0 and BZERO = 32768, and the latter two keywords are added automatically. */ if (fits_create_img(fptr, bitpix, naxis, naxes, &status)) { if (status) fits_report_error(stderr, status); return status; } /* first pixel to write */ fpixel = 1; /* number of pixels to write */ nelements = naxes[0] * naxes[1]; /* write the array of unsigned integers to the FITS file */ if (fits_write_img(fptr, TUSHORT, fpixel, nelements, data, &status)) { if (status) fits_report_error(stderr, status); return status; } /* write the gain parameter of the camera to the header */ gain = ML16803_CAMERA_GAIN; if (fits_update_key(fptr, TFLOAT, "GAIN", &gain, "Camera gain of ML16803", &status)) { if (status) fits_report_error(stderr, status); return status; } /* close the file */ if (fits_close_file(fptr, &status)) { if (status) fits_report_error(stderr, status); return status; } return 0; }
void writeFITS(const cv::Mat& cvImage, const std::string& filename) { if(cvImage.channels() != 1) { CustomException("writeFITS: Only able to write FITS from single channel images."); } fitsfile *fptr; //pointer to the FITS file defined in fitsioh int status; char err_text[100]; cv::Mat floatImg = cv::Mat_<float>(cvImage); long fpixel = 1, naxis = 2, nelements; long naxes[2] = {cvImage.cols, cvImage.rows}; float array[cvImage.cols][cvImage.rows]; for(int i = 0;i < floatImg.rows ;i++) { for(int j = 0;j < floatImg.cols ;j++) { array[j][i] = floatImg.at<float>(j,i); } } status=0; fits_create_file(&fptr, filename.c_str(), &status); if (status) { fits_report_error(stdout, status); fits_get_errstatus(status,err_text); fptr = nullptr; throw CustomException("writeFITS: Cannot create fits file."); } fits_create_img(fptr, FLOAT_IMG, naxis, naxes, &status); if (status) { fits_report_error(stdout, status); fits_get_errstatus(status,err_text); fptr = nullptr; throw CustomException("writeFITS: Cannot create image file."); } nelements = naxes[0] * naxes[1]; fits_write_img(fptr, TFLOAT, fpixel, nelements, array[0], &status); if (status) { fits_report_error(stdout, status); fits_get_errstatus(status,err_text); fptr = nullptr; throw CustomException("writeFITS: Cannot write image file."); } fits_close_file(fptr, &status); fits_report_error(stderr, status); }
int main(int argc, char *argv[]) { fitsfile *infptr, *outfptr; int status = 0, hdutype, ii; char infile[FLEN_FILENAME],outfile[FLEN_FILENAME]; if (argc != 3) { printf("Usage: fitscopy inputfile outputfile\n"); return(1); } strcpy(infile, argv[1] ); strcpy(outfile, argv[2] ); if ( fits_open_file(&infptr, infile, READONLY, &status) ) { fits_report_error(stderr, status); return(status); } if ( fits_create_file(&outfptr, outfile, &status) ) { fits_close_file(infptr, &status); fits_report_error(stderr, status); return(status); } /* attempt to move to next HDU, until we get an EOF error */ for (ii = 1; !(fits_movabs_hdu(infptr, ii, &hdutype, &status) ); ii++) { if ( fits_copy_hdu(infptr, outfptr, 0, &status) ) { fits_report_error(stderr, status); break; } } if (status == END_OF_FILE) status = 0; /* got the expected EOF error; reset = 0 */ fits_close_file(infptr, &status); fits_close_file(outfptr, &status); if (status) fits_report_error(stderr, status); return(0); }
void save_data_in_fits_file(const char* file_name, double* data, size_t num_of_elements) { fitsfile* file; int status = 0; char* ttype[] = { "SAMPLE" }; char* tform[] = { "1D" }; char* tunit[] = { "" }; if (fits_create_file(&file, file_name, &status) != 0) goto error; if (fits_create_tbl(file, BINARY_TBL, 0, 1, ttype, tform, tunit, "DECOMPR", &status) != 0) goto error; if (fits_write_col(file, TDOUBLE, 1, 1, 1, num_of_elements, data, &status) != 0) goto error; fits_close_file(file, &status); return; error: fits_report_error(stderr, status); if (file != NULL) fits_close_file(file, &status); exit(1); }
void save_2d_image_potential(SimulationData &sim_data, double *potential, const char * fits_file_name) { double *save_data; save_data = (double*)mkl_malloc(sim_data.get_num_x() * sim_data.get_num_y() * sizeof(double), 64); fitsfile *fptr; int status = 0; long fpixel = 1, naxis = 2, nelements; long naxes[2] = {sim_data.get_num_y(), sim_data.get_num_x()}; for (int i = 0; i < sim_data.get_num_x(); ++i) { for (int j = 0; j < sim_data.get_num_y(); ++j) { save_data[i * sim_data.get_num_y() + j] = 0; for (int k = 0; k < sim_data.get_num_z(); ++k) { save_data[i * sim_data.get_num_y() + j] += potential[i * sim_data.get_num_y() * sim_data.get_num_z() + j * sim_data.get_num_z() + k]; } } } fits_create_file(&fptr, fits_file_name, &status); fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status); nelements = naxes[0] * naxes[1]; fits_write_img(fptr, TDOUBLE, fpixel, nelements, save_data, &status); fits_close_file(fptr, &status); fits_report_error(stderr, status); mkl_free(save_data); }
image_type load_file( const std::string& filename ) { fitsfile* f; int status = 0, naxis; fits_open_file( &f, filename.c_str(), READONLY, &status ); fits_get_img_dim( f, &naxis, &status ); if( naxis != 2 && naxis != 3 ) { std::cout << "Could not read image" << std::endl; fits_close_file( f, &status ); return image_type(); } long dim[3]; dim[0] = dim[1] = dim[2] = 0; fits_get_img_size( f, 3, dim, &status ); image_type data( image_type::extents_type( dim[0], dim[1] ) ); typedef CFitsIOTypeMap< image_type::element > map; int any_null; unsigned short null = 0; fits_read_img( f, map::type, 1, data.num_elements(), &null, data.ptr(), &any_null, &status ); fits_close_file( f, &status ); if( status ) { fits_report_error( stderr, status ); return image_type(); } return data; }
int append_profile(const char *fname, int ispec, struct profile_phase *pp) { int status=0; fitsfile *fptr; fits_open_file(&fptr, fname, READWRITE, &status); fits_movnam_hdu(fptr, IMAGE_HDU, "PROFILE", 0, &status); int naxis; long naxes[2]; fits_get_img_dim(fptr, &naxis, &status); if (naxis!=2) { fits_close_file(fptr, &status); return(-1); } fits_get_img_size(fptr, 2, naxes, &status); if (naxes[0]!=pp->nphase || naxes[1]<ispec) { fits_close_file(fptr, &status); return(-2); } naxes[0] = 1; naxes[1] = ispec; fits_write_pix(fptr, TFLOAT, naxes, pp->nphase, pp->data, &status); fits_close_file(fptr, &status); if (status) { fprintf(stderr, "Error in append_profile:\n"); fits_report_error(stderr, status); return(-1); } return(0); }
//---------------------------------------------------------- int write_primary_header(etfits_t * etf) { //---------------------------------------------------------- int * status_p = &(etf->status); *status_p = 0; int itmp; char ctmp[40]; //fprintf(stderr, "writing primary header\n"); if(! *status_p) fits_get_system_time(ctmp, &itmp, status_p); // date the file was written if(! *status_p) fits_movabs_hdu(etf->fptr, 1, NULL, status_p); // go to primary HDU if(! *status_p) fits_update_key(etf->fptr, TSTRING, "DATE", ctmp, NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TSTRING, "TELESCOP", &(etf->primary_hdr.receiver), NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TINT, "NSUBBAND", &(etf->primary_hdr.n_subband), NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TINT, "NCHAN", &(etf->primary_hdr.n_chan), NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TINT, "NINPUTS", &(etf->primary_hdr.n_inputs), NULL, status_p); // TODO not yet implemented //if(! *status_p) fits_update_key(etf->fptr, TINT, "BANDWID", &(etf->primary_hdr.bandwidth), NULL, status_p); //if(! *status_p) fits_update_key(etf->fptr, TINT, "CHAN_BW", &(etf->primary_hdr.chan_bandwidth), NULL, status_p); //if(! *status_p) its_update_key(etf->fptr, TINT, "FREQRES", &(etf->primary_hdr.freq_res), NULL, status_p); // redundant w/ CHAN_BW? //if(! *status_p) fits_flush_file(etf->fptr, status_p); if (*status_p) { hashpipe_error(__FUNCTION__, "Error updating primary header"); //fprintf(stderr, "Error updating primary header.\n"); fits_report_error(stderr, *status_p); } return *status_p; }
int fitscopy ( char *infile, char *outfile) { fitsfile *infptr, *outfptr; /* FITS file pointers defined in fitsio.h */ int status = 0, ii = 1; /* status must always be initialized = 0 */ /* Open the input file */ if ( !fits_open_file(&infptr, infile, READONLY, &status) ) { /* Create the output file */ if ( !fits_create_file(&outfptr, outfile, &status) ) { /* Copy every HDU until we get an error */ while( !fits_movabs_hdu(infptr, ii++, NULL, &status) ) fits_copy_hdu(infptr, outfptr, 0, &status); /* Reset status after normal error */ if (status == END_OF_FILE) status = 0; fits_close_file(outfptr, &status); } fits_close_file(infptr, &status); } /* if error occured, print out error message */ if (status) fits_report_error(stderr, status); return(status); }
/* Function to catch errors thrown by CFITSIO */ void fitswrap_catcherror(int *status){ fits_report_error(stderr,*status); /* Add error-catching code here */ switch(*status){ case FILE_NOT_OPENED : printf("We encountered this one!\n"); break; case WRITE_ERROR : printf("This is a write error!\n"); break; case FITSWRAP_NOFILE_EXIT : printf("This is using a new error code!\n"); break; default : printf("We hit the default... exiting.\n"); exit(1); } printf("We need to add some error-catching code here...\n"); exit(1); return; }
int fits_has_error(struct fitsparams *p, int actioncode, char *string, int status) { char *action=NULL; int r=EXIT_SUCCESS; switch(actioncode) { case FITS_ACTION_DELETE: action="deleted"; break; case FITS_ACTION_RENAME: action="renamed"; break; case FITS_ACTION_UPDATE: action="updated"; break; case FITS_ACTION_WRITE: action="written"; break; case FITS_ACTION_COPY: action="copied"; break; case FITS_ACTION_REMOVE: action="renoved"; break; default: error(EXIT_FAILURE, 0, "%s: a bug! Please contact us at `%s' so we " "can fix this problem. The value of `actioncode' must not be %d", __func__, PACKAGE_BUGREPORT, actioncode); } if(p->quitonerror) { fits_report_error(stderr, status); error(EXIT_FAILURE, 0, "%s: not %s: %s\n", __func__, action, string); } else { fprintf(stderr, "Not %s: %s\n", action, string); r=EXIT_FAILURE; } return r; }
//---------------------------------------------------------- int write_hits_header(etfits_t * etf, int beampol, size_t nhits, size_t missed_pkts) { //---------------------------------------------------------- #define TFIELDS 4 int * status_p = &(etf->status); *status_p = 0; int tbltype = BINARY_TBL; long long naxis2 = 0; //const int tfields = 3; // TODO check chan types! const char *ttype[TFIELDS] = {"DETPOW ", "MEANPOW ", "COARCHAN", "FINECHAN"}; const char *tform[TFIELDS] = {"1E", "1E", "1U", "1V"}; // cfitsio format codes // 32-bit floats 16-bit unint 32-bit uint if(etf->integration_cnt == 0 && etf->beampol_cnt == 0) { // at start of file go to the template created HDU for this set of beampols if(! *status_p) fits_movnam_hdu(etf->fptr, BINARY_TBL, (char *)"ETHITS", 0, status_p); } else { // otherwise create new HDU for this set of beampols if(! *status_p) fits_create_tbl(etf->fptr, BINARY_TBL, 0, TFIELDS, (char **)&ttype, (char **)&tform, NULL, (char *)"ETHITS", status_p); } if(! *status_p) fits_update_key(etf->fptr, TINT, "TIME", &(etf->hits_hdr[beampol].time), NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TDOUBLE, "RA", &(etf->hits_hdr[beampol].ra), NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TDOUBLE, "DEC", &(etf->hits_hdr[beampol].dec), NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TINT, "BEAMPOL", &(etf->hits_hdr[beampol].beampol), NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TINT, "NHITS", &nhits, NULL, status_p); if(! *status_p) fits_update_key(etf->fptr, TINT, "MISSEDPK",&missed_pkts, NULL, status_p); if (*status_p) { hashpipe_error(__FUNCTION__, "Error writing hits header"); fits_report_error(stderr, *status_p); } }
void ClienteQtFits::printerror(int status) { if (status) { fits_report_error(stderr, status); /* print error report */ exit( status ); /* terminate the program, returning error status */ } }
void printerror (int status) { if (status) { fits_report_error (stderr, status); /* print error report */ exit (status); } return; }
void show_cfitsio_error(int status) { if (status) { fits_report_error(stderr, status); } return; }
//打印错误信息 void printerror(int status) { if (status) { fits_report_error(stderr, status); /* print error report */ exit( status ); /* stop the program, returning error status */ } return; }
/* Read only part (N "spectra" in time) of the DATA column from the * current subint from the set of files described by the psrfits * struct. Put the data into the buffer "buffer". It is assumed that * all files form a consistent set. Read automatically goes to the * next file when one ends. Arrays should be allocated outside this * routine. Counters are _not_ updated as they are in * psrfits_read_subint(). */ int psrfits_read_part_DATA(struct psrfits *pf, int N, int numunsigned, float *fbuffer) { struct hdrinfo *hdr = &(pf->hdr); int colnum = 0, *status = &(pf->status); // See if we need to move to next file if (pf->rownum > pf->rows_per_file) { printf("Closing file '%s'\n", pf->filename); fits_close_file(pf->fptr, status); pf->filenum++; psrfits_open(pf); if (*status==FILE_NOT_OPENED) { printf("Finished with all input files.\n"); pf->filenum--; *status = 1; return *status; } } int nivals = hdr->nchan * hdr->npol; long long numdata = (long long) nivals * (long long) N; long long bytes_to_read = (hdr->nbits * numdata) / 8L; float *offsets = (float *)malloc(sizeof(float) * nivals); float *scales = (float *)malloc(sizeof(float) * nivals); unsigned char *buffer = (unsigned char *)malloc(numdata); unsigned char *rawbuffer = buffer; if (hdr->nbits==4) { rawbuffer = (unsigned char *)malloc(bytes_to_read); } // Now read the data fits_get_colnum(pf->fptr, 0, "DAT_OFFS", &colnum, status); fits_read_col(pf->fptr, TFLOAT, colnum, pf->rownum, 1, nivals, NULL, offsets, NULL, status); fits_get_colnum(pf->fptr, 0, "DAT_SCL", &colnum, status); fits_read_col(pf->fptr, TFLOAT, colnum, pf->rownum, 1, nivals, NULL, scales, NULL, status); fits_get_colnum(pf->fptr, 0, "DATA", &colnum, status); fits_read_col(pf->fptr, TBYTE, colnum, pf->rownum, 1, bytes_to_read, NULL, rawbuffer, NULL, status); if (hdr->nbits==4) { convert_4bit_to_8bit(rawbuffer, (unsigned char *)buffer, numdata); free(rawbuffer); } // Now convert the 8-bit data to floats using the scales and offsets apply_scales_and_offsets(hdr->nchan, hdr->npol, N, numunsigned, scales, offsets, buffer, fbuffer); free(offsets); free(scales); free(buffer); // Complain on error fits_report_error(stderr, *status); return *status; }
int write2Dfits(char *fname, double *f, int pwidth, int pheight) { /* write out image as a fits file */ fitsfile *afptr; int status = 0; int anaxis; long anaxes[2], fpixel[2]={1,1}; int bitpix; int size; bitpix = -32; anaxis = 2; anaxes[0] = pwidth; anaxes[1] = pheight; size = anaxes[0]*anaxes[1]; fits_create_file(&afptr, fname, &status); fits_create_img(afptr, bitpix, anaxis, anaxes, &status); if (status) { fits_report_error(stderr, status); /* print error message */ return(status); } /* write all data into image array */ if (fits_write_pix(afptr, TDOUBLE, fpixel, size, f, &status) ) { printf(" error reading pixel data \n"); exit (2); } /* close the fits file */ fits_close_file(afptr, &status); if (status) { fits_report_error(stderr, status); /* print error message */ exit(2); } return 0; }
static void cfits_report_error (int status) { if (Cfits_Verbose) { fits_report_error (stderr, status); fflush (stderr); } else fits_clear_errmsg (); }
int main(int argc, char *argv[]) { fitsfile *infptr, *outfptr; /* FITS file pointers defined in fitsio.h */ int status = 0; /* status must always be initialized = 0 */ if (argc != 3) { printf("Usage: fitscopy inputfile outputfile\n"); printf("\n"); printf("Copy an input file to an output file, optionally filtering\n"); printf("the file in the process. This seemingly simple program can\n"); printf("apply powerful filters which transform the input file as\n"); printf("it is being copied. Filters may be used to extract a\n"); printf("subimage from a larger image, select rows from a table,\n"); printf("filter a table with a GTI time extension or a SAO region file,\n"); printf("create or delete columns in a table, create an image by\n"); printf("binning (histogramming) 2 table columns, and convert IRAF\n"); printf("format *.imh or raw binary data files into FITS images.\n"); printf("See the CFITSIO User's Guide for a complete description of\n"); printf("the Extended File Name filtering syntax.\n"); printf("\n"); printf("Examples:\n"); printf("\n"); printf("fitscopy in.fit out.fit (simple file copy)\n"); printf("fitscopy - - (stdin to stdout)\n"); printf("fitscopy in.fit[11:50,21:60] out.fit (copy a subimage)\n"); printf("fitscopy iniraf.imh out.fit (IRAF image to FITS)\n"); printf("fitscopy in.dat[i512,512] out.fit (raw array to FITS)\n"); printf("fitscopy in.fit[events][pi>35] out.fit (copy rows with pi>35)\n"); printf("fitscopy in.fit[events][bin X,Y] out.fit (bin an image) \n"); printf("fitscopy in.fit[events][col x=.9*y] out.fit (new x column)\n"); printf("fitscopy in.fit[events][gtifilter()] out.fit (time filter)\n"); printf("fitscopy in.fit[2][regfilter(\"pow.reg\")] out.fit (spatial filter)\n"); printf("\n"); printf("Note that it may be necessary to enclose the input file name\n"); printf("in single quote characters on the Unix command line.\n"); return(0); } /* Open the input file */ if ( !fits_open_file(&infptr, argv[1], READONLY, &status) ) { /* Create the output file */ if ( !fits_create_file(&outfptr, argv[2], &status) ) { /* copy the previous, current, and following HDUs */ fits_copy_file(infptr, outfptr, 1, 1, 1, &status); fits_close_file(outfptr, &status); } fits_close_file(infptr, &status); } /* if error occured, print out error message */ if (status) fits_report_error(stderr, status); return(status); }
//================================================== //================================================== 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; }
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; }
int addkey(char *outfile, char *keyword, char *keyvalue) { fitsfile *outfptr; /* FITS file pointer, defined in fitsio.h */ char card[FLEN_CARD], newcard[FLEN_CARD]; char oldvalue[FLEN_VALUE], comment[FLEN_COMMENT]; int status = 0; /* CFITSIO status value MUST be initialized to zero! */ int keytype; if (!fits_open_file(&outfptr, outfile, READWRITE, &status)) { if (fits_read_card(outfptr, keyword, card, &status)) { printf("Keyword does not exist\n"); card[0] = '\0'; comment[0] = '\0'; status = 0; /* reset status after error */ } else printf("%s\n",card); /* check if this is a protected keyword that must not be changed */ if (*card && fits_get_keyclass(card) == TYP_STRUC_KEY) { printf("Protected keyword cannot be modified.\n"); } else { /* get the comment string */ if (*card)fits_parse_value(card, oldvalue, comment, &status); /* construct template for new keyword */ strcpy(newcard, keyword); /* copy keyword name */ strcat(newcard, " = "); /* '=' value delimiter */ strcat(newcard, keyvalue); /* new value */ if (*comment) { strcat(newcard, " / "); /* comment delimiter */ strcat(newcard, comment); /* append the comment */ } /* reformat the keyword string to conform to FITS rules */ fits_parse_template(newcard, card, &keytype, &status); /* overwrite the keyword with the new value */ fits_update_card(outfptr, keyword, card, &status); printf("Keyword has been changed to:\n"); printf("%s\n",card); } fits_close_file(outfptr, &status); } /* open_file */ /* if error occured, print out error message */ if (status) fits_report_error(stderr, status); return(status); }
int main(int argc, char *argv[]) { fitsfile *fptr = 0; /* FITS file pointer, defined in fitsio.h */ char card[FLEN_CARD], newcard[FLEN_CARD]; char oldvalue[FLEN_VALUE], comment[FLEN_COMMENT]; int status = 0; /* CFITSIO status value MUST be initialized to zero! */ int keytype = 0; if (argc != 4) { fprintf(stderr, "Usage: %s filename[ext] keyword newvalue\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "Modify the value of a header keyword.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Examples: \n"); fprintf(stderr, " %s file.fits dec 30.0 - set DEC = 30.0 \n", argv[0]); return (0); } if (!fits_open_file(&fptr, argv[1], READWRITE, &status)) { if (fits_read_card(fptr,argv[2], card, &status)) { fprintf(stderr, "Keyword does not exist\n"); return (1); } if (fits_get_keyclass(card) == TYP_STRUC_KEY) { fprintf(stderr, "Protected keyword cannot be modified\n"); return (1); } fits_parse_value(card, oldvalue, comment, &status); /* construct template for new keyword */ strcpy(newcard, argv[2]); /* copy keyword name */ strcat(newcard, " = "); /* '=' value delimiter */ strcat(newcard, argv[3]); /* new value */ if (*comment) { strcat(newcard, " / "); /* comment delimiter */ strcat(newcard, comment); /* append the comment */ } /* reformat the keyword string to conform to FITS rules */ fits_parse_template(newcard, card, &keytype, &status); /* overwrite the keyword with the new value */ fits_update_card(fptr, argv[2], card, &status); fits_close_file(fptr, &status); } /* open_file */ /* if error occured, print out error message */ if (status) { fits_report_error(stderr, status); } return (status); }
void closeFitsFile(fitsfile *fp) { int status=0; fits_close_file(fp,&status); fits_report_error(stderr,status); if (status) { printf("Error closing file\n"); exit(1); } }
fitsfile * openFitsFile(char *fname) { fitsfile *fp; int status=0; fits_open_file(&fp,fname,READONLY,&status); fits_report_error(stderr,status); if (status) { printf("Error opening file >%s<\n",fname); exit(1); } return fp; }
int read_data(const char *filename,const char *extname, int colnum, long firstelem, long nelements, double *data) { firstelem++; //from 0 to 1 based int status = 0, anynul; double doublenull = 0.; fitsfile *fptr; fits_open_file(&fptr, filename, READONLY, &status); fits_movnam_hdu(fptr, BINARY_TBL, extname, 0, &status); fits_read_col(fptr, TDOUBLE, colnum, firstelem, 1, nelements, &doublenull, data, &anynul, &status); fits_close_file(fptr, &status); if (status) /* print any error messages */ fits_report_error(stderr, status); return(status); }
int nelem(const char* filename, long *length) { int status = 0; fitsfile *fptr; fits_open_file(&fptr, filename, READONLY, &status); fprintf(stderr, "%s :reading.\n", filename); //fits_movnam_hdu(fptr, BINARY_TBL, "DATA", 0, &status); fits_movabs_hdu(fptr, 2, NULL, &status); fits_read_key(fptr, TLONG, "NAXIS2", length , NULL, &status); fits_close_file(fptr, &status); if (status) /* print any error messages */ fits_report_error(stderr, status); return(status); }
double get_average(std::string filename) { fitsfile *fptr; /* FITS file pointer, defined in fitsio.h */ int status = 0; /* CFITSIO status value MUST be initialized to zero! */ int bitpix, naxis, ii; long naxes[2] = {1,1}, fpixel[2] = {1,1}; double *pixels; std::vector<double> values; //stores the values of all pixels in image if (!fits_open_file(&fptr, filename.c_str(), READONLY, &status)) { if (!fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status) ) { if (naxis > 2 || naxis == 0) printf("Error: only 1D or 2D images are supported\n"); else { /* get memory for 1 row */ pixels = (double *) malloc(naxes[0] * sizeof(double)); if (pixels == NULL) { printf("Memory allocation error\n"); return(1); } /* loop over all the rows in the image, top to bottom */ for (fpixel[1] = naxes[1]; fpixel[1] >= 1; fpixel[1]--) { if (fits_read_pix(fptr, TDOUBLE, fpixel, naxes[0], NULL, pixels, NULL, &status) ) /* read row of pixels */ break; /* jump out of loop on error */ for (ii = 0; ii < naxes[0]; ii++) { values.push_back(pixels[ii]); } } free(pixels); } } fits_close_file(fptr, &status); } if (status) fits_report_error(stderr, status); /* print any error message */ //report average of values double sum = 0; for (int i=0; i < values.size(); i++) sum += values[i]; return sum/values.size(); }
void save_3d_image(SimulationData &sim_data, WaveFunction &psi, const char * fits_file_name) { fitsfile *fptr; int status = 0; long fpixel = 1, naxis = 3, nelements; long naxes[3] = {sim_data.get_num_x(), sim_data.get_num_y(), sim_data.get_num_z()}; fits_create_file(&fptr, fits_file_name, &status); fits_create_img(fptr, DOUBLE_IMG, naxis, naxes, &status); nelements = naxes[0] * naxes[1] * naxes[2]; fits_write_img(fptr, TDOUBLE, fpixel, nelements, psi.abs_psi, &status); fits_close_file(fptr, &status); fits_report_error(stderr, status); }