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