[3] | 1 | #include <iostream> |
---|
| 2 | #include <string> |
---|
[142] | 3 | #include <string.h> |
---|
[3] | 4 | #include <wcs.h> |
---|
| 5 | #include <wcshdr.h> |
---|
| 6 | #include <fitshdr.h> |
---|
| 7 | #include <wcsfix.h> |
---|
[103] | 8 | #include <wcsunits.h> |
---|
[142] | 9 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
| 10 | // wtbarr (this is a problem when using gcc v.4+ |
---|
[66] | 11 | #include <fitsio.h> |
---|
[69] | 12 | #include <math.h> |
---|
[71] | 13 | #include <duchamp.hh> |
---|
[3] | 14 | #include <Cubes/cubes.hh> |
---|
| 15 | |
---|
[71] | 16 | string imageType[4] = {"point", "spectrum", "image", "cube"}; |
---|
[3] | 17 | |
---|
| 18 | int Cube::getCube(string fname) |
---|
| 19 | { |
---|
| 20 | /** |
---|
| 21 | * Cube::getCube(string ) |
---|
| 22 | * Read in a cube from the file fname (assumed to be in FITS format). |
---|
[161] | 23 | * Function is a front end to the I/O functions in the FitsIO/ directory. |
---|
| 24 | * This function will check that the file exists, report the dimensions |
---|
| 25 | * and then get other functions to read the data, WCS, and necessary |
---|
| 26 | * header keywords. |
---|
| 27 | * Returns SUCCESS/FAILURE. |
---|
[3] | 28 | */ |
---|
| 29 | |
---|
| 30 | long nelements; |
---|
[71] | 31 | int bitpix,numAxes; |
---|
[3] | 32 | int status = 0, nkeys; /* MUST initialize status */ |
---|
| 33 | fitsfile *fptr; |
---|
| 34 | |
---|
[168] | 35 | int exists; |
---|
| 36 | fits_file_exists(fname.c_str(),&exists,&status); |
---|
| 37 | if(exists<=0){ |
---|
| 38 | fits_report_error(stderr, status); |
---|
| 39 | duchampWarning("getCube", "Requested image does not exist!\n"); |
---|
| 40 | return FAILURE; |
---|
| 41 | } |
---|
| 42 | |
---|
[161] | 43 | // Open the FITS file -- make sure it exists |
---|
[159] | 44 | status = 0; |
---|
[3] | 45 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 46 | fits_report_error(stderr, status); |
---|
[168] | 47 | if(((status==URL_PARSE_ERROR)||(status==BAD_NAXIS)) |
---|
| 48 | &&(this->pars().getFlagSubsection())) |
---|
[164] | 49 | duchampError("getCube", |
---|
| 50 | "It may be that the subsection you've entered is invalid.\n\ |
---|
| 51 | Either it has the wrong number of axes, or one axis has too large a range.\n"); |
---|
[3] | 52 | return FAILURE; |
---|
| 53 | } |
---|
| 54 | |
---|
[161] | 55 | // Read the size information -- number and lengths of all axes. |
---|
| 56 | // Just use this for reporting purposes. |
---|
[3] | 57 | status = 0; |
---|
[159] | 58 | if(fits_get_img_dim(fptr, &numAxes, &status)){ |
---|
[3] | 59 | fits_report_error(stderr, status); |
---|
| 60 | return FAILURE; |
---|
| 61 | } |
---|
[159] | 62 | long *dimAxes = new long[numAxes]; |
---|
| 63 | for(int i=0;i<numAxes;i++) dimAxes[i]=1; |
---|
| 64 | status = 0; |
---|
| 65 | if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){ |
---|
| 66 | fits_report_error(stderr, status); |
---|
| 67 | return FAILURE; |
---|
| 68 | } |
---|
[161] | 69 | |
---|
| 70 | // Close the FITS file. |
---|
[71] | 71 | status = 0; |
---|
[159] | 72 | fits_close_file(fptr, &status); |
---|
[3] | 73 | if (status){ |
---|
[159] | 74 | duchampWarning("getCube","Error closing file: "); |
---|
[3] | 75 | fits_report_error(stderr, status); |
---|
| 76 | } |
---|
| 77 | |
---|
[161] | 78 | // Report the size of the FITS file |
---|
[164] | 79 | std::cout << "Dimensions of FITS file: "; |
---|
[160] | 80 | int dim = 0; |
---|
| 81 | std::cout << dimAxes[dim]; |
---|
| 82 | while(dim+1<numAxes) std::cout << "x" << dimAxes[++dim]; |
---|
| 83 | std::cout << std::endl; |
---|
[103] | 84 | |
---|
[160] | 85 | delete [] dimAxes; |
---|
[3] | 86 | |
---|
[161] | 87 | // Get the WCS information |
---|
[160] | 88 | this->head.defineWCS(fname, this->par); |
---|
[103] | 89 | |
---|
[160] | 90 | if(!this->head.isWCS()) |
---|
| 91 | duchampWarning("getCube","WCS is not good enough to be used.\n"); |
---|
[3] | 92 | |
---|
[161] | 93 | // Get the data array from the FITS file. |
---|
| 94 | // Report the dimensions of the data array that was read (this can be |
---|
| 95 | // different to the full FITS array). |
---|
[160] | 96 | std::cerr << "Reading data ... "; |
---|
| 97 | this->getFITSdata(fname); |
---|
| 98 | std::cerr << "Done. Data array has dimensions: "; |
---|
| 99 | std::cerr << this->axisDim[0] <<"x" |
---|
| 100 | << this->axisDim[1] <<"x" |
---|
| 101 | << this->axisDim[2] << std::endl; |
---|
[46] | 102 | |
---|
[161] | 103 | // Read the necessary header information, and copy some of it into the Param. |
---|
[160] | 104 | this->head.readHeaderInfo(fname, this->par); |
---|
[3] | 105 | |
---|
[103] | 106 | return SUCCESS; |
---|
[3] | 107 | |
---|
| 108 | } |
---|
| 109 | |
---|