1 | #include <iostream> |
---|
2 | #include <string> |
---|
3 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
4 | // wtbarr (this is a problem when using gcc v.4+ |
---|
5 | #include <fitsio.h> |
---|
6 | #include <math.h> |
---|
7 | #include <duchamp.hh> |
---|
8 | #include <Cubes/cubes.hh> |
---|
9 | |
---|
10 | int Cube::getFITSdata(string fname) |
---|
11 | { |
---|
12 | /** |
---|
13 | * Cube::getFITSdata(string fname) |
---|
14 | * |
---|
15 | * This function retrieves the data array from the FITS file at the |
---|
16 | * location given by the string argument. |
---|
17 | * Only the two spatial axes and the one spectral axis are stored in the |
---|
18 | * data array. These axes are given by the wcsprm variables wcs->lng, |
---|
19 | * wcs->lat and wcs->spec. |
---|
20 | * All other axes are just read by their first pixel, using the |
---|
21 | * fits_read_subsetnull_ functions |
---|
22 | */ |
---|
23 | |
---|
24 | long nelements; |
---|
25 | int bitpix,numAxes; |
---|
26 | int status = 0, nkeys; /* MUST initialize status */ |
---|
27 | fitsfile *fptr; |
---|
28 | |
---|
29 | // Open the FITS file |
---|
30 | status = 0; |
---|
31 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
32 | fits_report_error(stderr, status); |
---|
33 | return FAILURE; |
---|
34 | } |
---|
35 | |
---|
36 | // Read the size of the FITS file -- number and sizes of the axes |
---|
37 | status = 0; |
---|
38 | if(fits_get_img_dim(fptr, &numAxes, &status)){ |
---|
39 | fits_report_error(stderr, status); |
---|
40 | return FAILURE; |
---|
41 | } |
---|
42 | long *dimAxes = new long[numAxes]; |
---|
43 | for(int i=0;i<numAxes;i++) dimAxes[i]=1; |
---|
44 | status = 0; |
---|
45 | if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){ |
---|
46 | fits_report_error(stderr, status); |
---|
47 | return FAILURE; |
---|
48 | } |
---|
49 | |
---|
50 | // Identify which axes are the "interesting" ones |
---|
51 | int lng = this->head.getWCS()->lng; |
---|
52 | int lat = this->head.getWCS()->lat; |
---|
53 | int spc = this->head.getWCS()->spec; |
---|
54 | |
---|
55 | int anynul; |
---|
56 | int npix = dimAxes[lng]*dimAxes[lat]*dimAxes[spc]; |
---|
57 | |
---|
58 | float *array = new float[npix]; // the array of data values |
---|
59 | char *nullarray = new char[npix]; // the array of null pixels |
---|
60 | long *inc = new long[numAxes]; // the data-sampling increment |
---|
61 | |
---|
62 | // define the first and last pixels for each axis. |
---|
63 | // set both to 1 for the axes we don't want, and to the full |
---|
64 | // range for the ones we do. |
---|
65 | long *fpixel = new long[numAxes]; |
---|
66 | long *lpixel = new long[numAxes]; |
---|
67 | for(int i=0;i<numAxes;i++){ |
---|
68 | inc[i] = fpixel[i] = lpixel[i] = 1; |
---|
69 | } |
---|
70 | lpixel[lng] = dimAxes[lng]; |
---|
71 | lpixel[lat] = dimAxes[lat]; |
---|
72 | lpixel[spc] = dimAxes[spc]; |
---|
73 | |
---|
74 | int colnum = 0; // want the first dataset in the FITS file |
---|
75 | |
---|
76 | // read the relevant subset, defined by the first & last pixel ranges |
---|
77 | status = 0; |
---|
78 | if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes, |
---|
79 | fpixel, lpixel, inc, |
---|
80 | array, nullarray, &anynul, &status)){ |
---|
81 | duchampError("getFITSdata", |
---|
82 | "There was an error reading in the data array:"); |
---|
83 | fits_report_error(stderr, status); |
---|
84 | return FAILURE; |
---|
85 | } |
---|
86 | |
---|
87 | if(anynul==0){ |
---|
88 | // no blank pixels, so don't bother with any trimming or checking... |
---|
89 | if(this->par.getFlagBlankPix()) { |
---|
90 | // if user requested fixing, inform them of change. |
---|
91 | duchampWarning("getFITSdata", |
---|
92 | "No blank pixels, so setting flagBlankPix to false.\n"); |
---|
93 | } |
---|
94 | this->par.setFlagBlankPix(false); |
---|
95 | } |
---|
96 | |
---|
97 | this->initialiseCube(dimAxes); |
---|
98 | this->saveArray(array,npix); |
---|
99 | this->par.setOffsets(this->head.getWCS()); |
---|
100 | //------------------------------------------------------------- |
---|
101 | // Once the array is saved, change the value of the blank pixels from |
---|
102 | // 0 (as they are set by fits_read_pixnull) to the correct blank value |
---|
103 | // as determined by the above code. |
---|
104 | for(int i=0; i<npix;i++){ |
---|
105 | // if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; |
---|
106 | if(isnan(array[i])) this->array[i] = par.getBlankPixVal(); |
---|
107 | if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); |
---|
108 | } |
---|
109 | |
---|
110 | return SUCCESS; |
---|
111 | |
---|
112 | } |
---|