int vips__fits_isfits( const char *filename ) { fitsfile *fptr; int status; VIPS_DEBUG_MSG( "isfits: testing \"%s\"\n", filename ); status = 0; if( fits_open_image( &fptr, filename, READONLY, &status ) ) { VIPS_DEBUG_MSG( "isfits: error reading \"%s\"\n", filename ); #ifdef VIPS_DEBUG vips_fits_error( status ); #endif /*VIPS_DEBUG*/ return( 0 ); } fits_close_file( fptr, &status ); return( 1 ); }
int main(int argc, char *argv[]) { fitsfile *fptr = 0; /* FITS file pointer */ int status = 0; /* CFITSIO status value MUST be initialized to zero! */ int hdutype = 0, naxis = 0, ii = 0; long naxes[2], totpix = 0, fpixel[2]; double *pix, sum = 0., meanval = 0., minval = 1.E33, maxval = -1.E33; if (argc != 2) { fprintf(stderr, "Usage: %s array \n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "Compute statistics of pixels in the input array\n"); fprintf(stderr, "\n"); fprintf(stderr, "Examples: \n"); fprintf(stderr, " imarith array.fits - the whole array\n"); fprintf(stderr, " imarith 'array.fits[200:210,300:310]' - array section\n"); fprintf(stderr, " imarith 'table.fits+1[bin (X,Y) = 4]' - array constructed\n"); fprintf(stderr, " from X and Y columns of a table, with 4-pixel bin size\n"); return (0); } if (!fits_open_image(&fptr, argv[1], READONLY, &status)) { if (fits_get_hdu_type(fptr, &hdutype, &status) || hdutype != IMAGE_HDU) { fprintf(stderr, "Error: this program only works on arrays, not tables\n"); return (1); } fits_get_img_dim(fptr, &naxis, &status); fits_get_img_size(fptr, 2, naxes, &status); if (status || naxis != 2) { fprintf(stderr, "Error: NAXIS = %d. Only 2-D arrays are supported.\n", naxis); return (1); } pix = (double *) malloc(naxes[0] * sizeof(double)); /* memory for 1 row */ if (pix == NULL) { fprintf(stderr, "Memory allocation error\n"); return (1); } totpix = naxes[0] * naxes[1]; fpixel[0] = 1; /* read starting with first pixel in each row */ /* process array one row at a time; increment row # in each loop */ for (fpixel[1] = 1; fpixel[1] <= naxes[1]; fpixel[1]++) { /* give starting pixel coordinate and number of pixels to read */ if (fits_read_pix(fptr, TDOUBLE, fpixel, naxes[0],0, pix,0, &status)) { break; /* jump out of loop on error */ } for (ii = 0; ii < naxes[0]; ii++) { sum += pix[ii]; /* accumlate sum */ if (pix[ii] < minval) { minval = pix[ii]; /* find min and */ } if (pix[ii] > maxval) { maxval = pix[ii]; /* max values */ } } } free(pix); fits_close_file(fptr, &status); } if (status) { fits_report_error(stderr, status); /* print any error message */ } else { if (totpix > 0) { meanval = sum / totpix; } printf("Statistics of %ld x %ld array\n", naxes[0], naxes[1]); printf(" sum of pixels = %g\n", sum); printf(" mean value = %g\n", meanval); printf(" minimum value = %g\n", minval); printf(" maximum value = %g\n", maxval); } return (status); }
bool FITSImage::loadFITS ( const QString &inFilename, QProgressDialog *progress ) { int status=0, nulval=0, anynull=0; long fpixel[2], nelements, naxes[2]; char error_status[512]; qDeleteAll(starCenters); starCenters.clear(); if (mode == FITS_NORMAL && progress) { progress->setLabelText(i18n("Please hold while loading FITS file...")); progress->setWindowTitle(i18n("Loading FITS")); } if (mode == FITS_NORMAL && progress) progress->setValue(30); if (fptr) { fits_close_file(fptr, &status); if (tempFile) QFile::remove(filename); } filename = inFilename; if (filename.contains("/tmp/")) tempFile = true; else tempFile = false; filename.remove("file://"); if (fits_open_image(&fptr, filename.toAscii(), READONLY, &status)) { fits_report_error(stderr, status); fits_get_errstatus(status, error_status); if (progress) KMessageBox::error(0, i18n("Could not open file %1 (fits_get_img_param). Error %2", filename, QString::fromUtf8(error_status)), i18n("FITS Open")); return false; } if (mode == FITS_NORMAL && progress) if (progress->wasCanceled()) return false; if (mode == FITS_NORMAL && progress) progress->setValue(40); if (fits_get_img_param(fptr, 2, &(stats.bitpix), &(stats.ndim), naxes, &status)) { fits_report_error(stderr, status); fits_get_errstatus(status, error_status); if (progress) KMessageBox::error(0, i18n("FITS file open error (fits_get_img_param): %1", QString::fromUtf8(error_status)), i18n("FITS Open")); return false; } if (stats.ndim < 2) { if (progress) KMessageBox::error(0, i18n("1D FITS images are not supported in KStars."), i18n("FITS Open")); return false; } if (fits_get_img_type(fptr, &data_type, &status)) { fits_report_error(stderr, status); fits_get_errstatus(status, error_status); if (progress) KMessageBox::error(0, i18n("FITS file open error (fits_get_img_type): %1", QString::fromUtf8(error_status)), i18n("FITS Open")); return false; } if (mode == FITS_NORMAL && progress) if (progress->wasCanceled()) return false; if (mode == FITS_NORMAL && progress) progress->setValue(60); stats.dim[0] = naxes[0]; stats.dim[1] = naxes[1]; delete (image_buffer); image_buffer = NULL; image_buffer = new float[stats.dim[0] * stats.dim[1]]; if (image_buffer == NULL) { qDebug() << "Not enough memory for image_buffer"; return false; } if (mode == FITS_NORMAL && progress) { if (progress->wasCanceled()) { delete (image_buffer); return false; } } if (mode == FITS_NORMAL && progress) progress->setValue(70); nelements = stats.dim[0] * stats.dim[1]; fpixel[0] = 1; fpixel[1] = 1; qApp->processEvents(); if (fits_read_2d_flt(fptr, 0, nulval, naxes[0], naxes[0], naxes[1], image_buffer, &anynull, &status)) { fprintf(stderr, "fits_read_pix error\n"); fits_report_error(stderr, status); return false; } if (mode == FITS_NORMAL && progress) { if (progress->wasCanceled()) { delete (image_buffer); return false; } } calculateStats(); if (mode == FITS_NORMAL && progress) progress->setValue(80); //currentWidth = stats.dim[0]; // currentHeight = stats.dim[1]; qApp->processEvents(); if (mode == FITS_NORMAL) { checkWCS(); if (progress) progress->setValue(90); } if (mode == FITS_NORMAL && progress) { if (progress->wasCanceled()) { delete (image_buffer); return false; } } if (mode == FITS_NORMAL && progress) progress->setValue(100); starsSearched = false; return true; }
void read_fits(const char* filename, int datatype, size_t* width, size_t* height, void** image) { int status = 0; // the FITS file fitsfile* fptr; // metadata int bitpix; int naxis; long naxes[2]; // total number of pixels long npix; // reading offset long fpixel[2] = { 1, 1 }; // size of output image pixels size_t spix = 0; // open FITS file fits_open_image(&fptr, filename, READONLY, &status); if(status) fits_error(filename, status); // get metadata fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status); if(status) fits_error(filename, status); // check dimension of image if(naxis != 2) errorf(filename, 0, "file has %d axes (should be 2)", naxis); // set dimensions *width = naxes[0]; *height = naxes[1]; // size of output pixels switch(datatype) { case TINT: spix = sizeof(int); break; case TFLOAT: spix = sizeof(float); break; case TDOUBLE: spix = sizeof(double); break; default: error("read_fits(): datatype %d not implemented", datatype); } // create array for pixels npix = naxes[0]*naxes[1]; *image = malloc(npix*spix); if(!*image) errori(NULL); // read pixels into array fits_read_pix(fptr, datatype, fpixel, npix, NULL, *image, NULL, &status); if(status) fits_error(filename, status); // close file again fits_close_file(fptr, &status); if(status) fits_error(filename, status); }
std::string read_image_3D(std::string pathname_3D, float *&array_2D_real, float *&array_2D_imag, int &naxis_2D, long *&naxes_2D) { TRACE_ENTER(); // open FITS image file in READONLY mode fitsfile *fptr; int status = 0; fits_open_image(&fptr, pathname_3D.c_str(), READONLY, &status); if (status != 0) { return FORMAT_STATUS(status); } // read number of HDUs in the file int nhdus = 0; fits_get_num_hdus(fptr, &nhdus, &status); if (status != 0) { return FORMAT_STATUS(status); } // we expect only one HDU in the file if (nhdus != 1) { std::ostringstream exit_oss; exit_oss << "nhdus is " << nhdus << ", not 1"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // read the type of the HDU int hdutype = 0; fits_movabs_hdu(fptr, nhdus, &hdutype, &status); if (status != 0) { return FORMAT_STATUS(status); } // we expect the HDU to be an image if (hdutype != IMAGE_HDU) { std::ostringstream exit_oss; exit_oss << "hdutype is " << hdutype << ", not IMAGE_HDU"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // read the type of data in the HDU int bitpix = 0; fits_get_img_type (fptr, &bitpix, &status); if (status != 0) { return FORMAT_STATUS(status); } // we expect the data to be floats if (bitpix != FLOAT_IMG) { std::ostringstream exit_oss; exit_oss << "bitpix is " << bitpix << ", not FLOAT_IMG"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // get the number of dimensions in the image int naxis_3D = 0; fits_get_img_dim (fptr, &naxis_3D, &status); if (status != 0) { return FORMAT_STATUS(status); } naxis_2D = naxis_3D-1; // we expect 3 dimensions in the image if (naxis_3D != 3) { std::ostringstream exit_oss; exit_oss << "naxis_3D is " << naxis_3D << ", not 3"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // get the size of each dimension in the image long *naxes_3D = new long[naxis_3D]; int maxdim = naxis_3D; fits_get_img_size(fptr, maxdim, naxes_3D, &status); if (status != 0) { return FORMAT_STATUS(status); } naxes_2D = new long[naxis_2D]; for (int i = 0; i < naxis_2D; i++) { naxes_2D[i] = naxes_3D[i]; } // we expect two sub-images in the third dimension if (naxes_3D[2] != 2) { std::ostringstream exit_oss; exit_oss << "naxes_2D[2] is " << naxes_2D[2] << ", not 2"; TRACE_ERROR (exit_oss.str()); return exit_oss.str(); } // fits_read_subset long *fpixel = new long[naxis_3D]; for (int i = 0; i < naxis_3D; i++) { fpixel[i] = 1; } long *lpixel = new long[naxis_3D]; for (int i = 0; i < naxis_3D; i++) { lpixel[i] = naxes_3D[i]; } long *inc = new long[naxis_3D]; for (int i = 0; i < naxis_3D; i++) { inc[i] = 1; } float nulval = 0; long nelements_2D = 1; for (int i = 0; i < naxis_2D; i++) { nelements_2D = nelements_2D*naxes_2D[i]; } int anynul = 0; // read array_2D_real array_2D_real = new float[nelements_2D]; fpixel[2] = 1; lpixel[2] = 1; fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, &nulval, array_2D_real, &anynul, &status); // read array_2D_imag array_2D_imag = new float[nelements_2D]; fpixel[2] = 2; lpixel[2] = 2; fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, &nulval, array_2D_imag, &anynul, &status); // free arrays delete [] inc; delete [] lpixel; delete [] fpixel; // test read result if (status != 0) { return FORMAT_STATUS(status); } // close the FITS image file fits_close_file(fptr, &status); if (status != 0) { return FORMAT_STATUS(status); } return "READ_OK"; }
std::string read_image(std::string pathname, float *&array, int &naxis, long *&naxes) { TRACE_ENTER(); // open FITS image file in READONLY mode fitsfile *fptr; int status = 0; fits_open_image(&fptr, pathname.c_str(), READONLY, &status); if (status != 0) { return FORMAT_STATUS(status); } // read number of HDUs in the file int nhdus = 0; fits_get_num_hdus(fptr, &nhdus, &status); if (status != 0) { return FORMAT_STATUS(status); } // we expect only one HDU in the file if (nhdus != 1) { std::ostringstream exit_oss; exit_oss << "nhdus is " << nhdus << ", not 1"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // read the type of the HDU int hdutype = 0; fits_movabs_hdu(fptr, nhdus, &hdutype, &status); if (status != 0) { return FORMAT_STATUS(status); } // we expect the HDU to be an image if (hdutype != IMAGE_HDU) { std::ostringstream exit_oss; exit_oss << "hdutype is " << hdutype << ", not IMAGE_HDU"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // read the type of data in the HDU int bitpix = 0; fits_get_img_type (fptr, &bitpix, &status); if (status != 0) { return FORMAT_STATUS(status); } // we expect the data to be floats if (bitpix != FLOAT_IMG) { std::ostringstream exit_oss; exit_oss << "bitpix is " << bitpix << ", not FLOAT_IMG"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // get the number of dimensions in the image naxis = 0; fits_get_img_dim (fptr, &naxis, &status); if (status != 0) { return FORMAT_STATUS(status); } // we expect 2 or 3 dimensions in the image if ((naxis < 2) || (naxis > 3)) { std::ostringstream exit_oss; exit_oss << "naxis is " << naxis << ", neither 2 nor 3"; TRACE_ERROR(exit_oss.str()); return exit_oss.str(); } // get the size of each dimension in the image naxes = new long[naxis]; int maxdim = naxis; fits_get_img_size(fptr, maxdim, naxes, &status); if (status != 0) { return FORMAT_STATUS(status); } // fits_read_subset long *fpixel = new long[naxis]; for (int i = 0; i < naxis; i++) { fpixel[i] = 1; } long *lpixel = new long[naxis]; for (int i = 0; i < naxis; i++) { lpixel[i] = naxes[i]; } long *inc = new long[naxis]; for (int i = 0; i < naxis; i++) { inc[i] = 1; } float nulval = 0; long nelements = 1; for (int i = 0; i < naxis; i++) { nelements = nelements * naxes[i]; } array = new float[nelements]; int anynul = 0; fits_read_subset(fptr, TFLOAT, fpixel, lpixel, inc, &nulval, array, &anynul, &status); delete [] inc; delete [] lpixel; delete [] fpixel; if (status != 0) { return FORMAT_STATUS(status); } // close the FITS image file fits_close_file(fptr, &status); if (status != 0) { return FORMAT_STATUS(status); } return "READ_OK"; }
static GwyContainer* fits_load(const gchar *filename, G_GNUC_UNUSED GwyRunType mode, GError **error) { GwyContainer *container = NULL; fitsfile *fptr = NULL; GwyDataField *field = NULL, *mask; gint status = 0; /* Must be initialised to zero! */ gint hdutype, naxis, anynull, nkeys, k; glong res[3]; /* First index is the fast looping one. */ char strvalue[FLEN_VALUE]; gchar *invalid = NULL; gdouble real, off; if (fits_open_image(&fptr, filename, READONLY, &status)) { err_FITS(error, status); return NULL; } if (fits_get_hdu_type(fptr, &hdutype, &status)) { err_FITS(error, status); goto fail; } gwy_debug("hdutype %d", hdutype); if (hdutype != IMAGE_HDU) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Only two-dimensional images are supported.")); goto fail; } if (fits_get_img_dim(fptr, &naxis, &status)) { err_FITS(error, status); goto fail; } gwy_debug("naxis %d", naxis); if (naxis != 2 && naxis != 3) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Only two-dimensional images are supported.")); goto fail; } if (fits_get_img_size(fptr, naxis, res, &status)) { err_FITS(error, status); goto fail; } if (naxis == 3 && res[2] != 1) { g_set_error(error, GWY_MODULE_FILE_ERROR, GWY_MODULE_FILE_ERROR_DATA, _("Only two-dimensional images are supported.")); goto fail; } gwy_debug("xres %ld, yres %ld", res[0], res[1]); if (err_DIMENSION(error, res[0]) || err_DIMENSION(error, res[1])) goto fail; field = gwy_data_field_new(res[0], res[1], res[0], res[1], FALSE); invalid = g_new(gchar, res[0]*res[1]); if (fits_read_imgnull(fptr, TDOUBLE, 1, res[0]*res[1], field->data, invalid, &anynull, &status)) { err_FITS(error, status); goto fail; } container = gwy_container_new(); gwy_container_set_object_by_name(container, "/0/data", field); /* Failures here are non-fatal. We already have an image. */ if (fits_get_hdrspace(fptr, &nkeys, NULL, &status)) { g_warning("Cannot get the first hdrspace."); goto fail; } if (!fits_read_key(fptr, TSTRING, "BUINT ", strvalue, NULL, &status)) { gint power10; gwy_debug("BUINT = <%s>", strvalue); gwy_si_unit_set_from_string_parse(gwy_data_field_get_si_unit_z(field), strvalue, &power10); if (power10) gwy_data_field_multiply(field, pow10(power10)); } status = 0; if (get_real_and_offset(fptr, 1, res[0], &real, &off)) { if (real < 0.0) { off += real; real = -real; gwy_data_field_invert(field, FALSE, TRUE, FALSE); } gwy_data_field_set_xreal(field, real); gwy_data_field_set_xoffset(field, off); } if (get_real_and_offset(fptr, 2, res[1], &real, &off)) { if (real < 0.0) { off += real; real = -real; gwy_data_field_invert(field, TRUE, FALSE, FALSE); } gwy_data_field_set_yreal(field, real); gwy_data_field_set_yoffset(field, off); } /* Create a mask of invalid data. */ for (k = 0; k < field->xres*field->yres; k++) { if (invalid[k]) field->data[k] = NAN; } if ((mask = gwy_app_channel_mask_of_nans(field, TRUE))) { gwy_container_set_object_by_name(container, "/0/mask", mask); g_object_unref(mask); } fail: fits_close_file(fptr, &status); gwy_object_unref(field); g_free(invalid); return container; }