[160] | 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 | { |
---|
[161] | 12 | /** |
---|
[220] | 13 | * This function retrieves the data array from the FITS file at the |
---|
[161] | 14 | * location given by the string argument. |
---|
| 15 | * Only the two spatial axes and the one spectral axis are stored in the |
---|
| 16 | * data array. These axes are given by the wcsprm variables wcs->lng, |
---|
| 17 | * wcs->lat and wcs->spec. |
---|
| 18 | * All other axes are just read by their first pixel, using the |
---|
| 19 | * fits_read_subsetnull_ functions |
---|
| 20 | */ |
---|
[160] | 21 | |
---|
| 22 | long nelements; |
---|
| 23 | int bitpix,numAxes; |
---|
| 24 | int status = 0, nkeys; /* MUST initialize status */ |
---|
| 25 | fitsfile *fptr; |
---|
| 26 | |
---|
[161] | 27 | // Open the FITS file |
---|
[160] | 28 | status = 0; |
---|
| 29 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 30 | fits_report_error(stderr, status); |
---|
| 31 | return FAILURE; |
---|
| 32 | } |
---|
[161] | 33 | |
---|
| 34 | // Read the size of the FITS file -- number and sizes of the axes |
---|
[160] | 35 | status = 0; |
---|
| 36 | if(fits_get_img_dim(fptr, &numAxes, &status)){ |
---|
| 37 | fits_report_error(stderr, status); |
---|
| 38 | return FAILURE; |
---|
| 39 | } |
---|
| 40 | long *dimAxes = new long[numAxes]; |
---|
| 41 | for(int i=0;i<numAxes;i++) dimAxes[i]=1; |
---|
| 42 | status = 0; |
---|
| 43 | if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){ |
---|
| 44 | fits_report_error(stderr, status); |
---|
| 45 | return FAILURE; |
---|
| 46 | } |
---|
| 47 | |
---|
[161] | 48 | // Identify which axes are the "interesting" ones |
---|
[186] | 49 | int lng,lat,spc; |
---|
| 50 | if(this->head.isWCS()){ |
---|
| 51 | lng = this->head.getWCS()->lng; |
---|
| 52 | lat = this->head.getWCS()->lat; |
---|
| 53 | spc = this->head.getWCS()->spec; |
---|
| 54 | } |
---|
| 55 | else{ |
---|
| 56 | lng = 0; |
---|
| 57 | lat = 1; |
---|
| 58 | spc = 2; |
---|
| 59 | } |
---|
| 60 | |
---|
[160] | 61 | |
---|
| 62 | int anynul; |
---|
| 63 | int npix = dimAxes[lng]*dimAxes[lat]*dimAxes[spc]; |
---|
[161] | 64 | |
---|
[204] | 65 | float *pixarray = new float[npix];// the array of pixel values |
---|
[161] | 66 | char *nullarray = new char[npix]; // the array of null pixels |
---|
| 67 | long *inc = new long[numAxes]; // the data-sampling increment |
---|
| 68 | |
---|
| 69 | // define the first and last pixels for each axis. |
---|
| 70 | // set both to 1 for the axes we don't want, and to the full |
---|
| 71 | // range for the ones we do. |
---|
[160] | 72 | long *fpixel = new long[numAxes]; |
---|
| 73 | long *lpixel = new long[numAxes]; |
---|
| 74 | for(int i=0;i<numAxes;i++){ |
---|
| 75 | inc[i] = fpixel[i] = lpixel[i] = 1; |
---|
| 76 | } |
---|
| 77 | lpixel[lng] = dimAxes[lng]; |
---|
| 78 | lpixel[lat] = dimAxes[lat]; |
---|
| 79 | lpixel[spc] = dimAxes[spc]; |
---|
| 80 | |
---|
[161] | 81 | int colnum = 0; // want the first dataset in the FITS file |
---|
| 82 | |
---|
[204] | 83 | // prepare the Cube's pixel array -- so that we don't have to use saveArray; |
---|
| 84 | // if(this->numPixels>0) delete [] array; |
---|
| 85 | // this->array = new float[npix]; |
---|
| 86 | // this->numPixels = npix; |
---|
[161] | 87 | // read the relevant subset, defined by the first & last pixel ranges |
---|
[160] | 88 | status = 0; |
---|
| 89 | if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes, |
---|
| 90 | fpixel, lpixel, inc, |
---|
[204] | 91 | // this->array, nullarray, &anynul, &status)){ |
---|
| 92 | pixarray, nullarray, &anynul, &status)){ |
---|
[160] | 93 | duchampError("getFITSdata", |
---|
| 94 | "There was an error reading in the data array:"); |
---|
| 95 | fits_report_error(stderr, status); |
---|
| 96 | return FAILURE; |
---|
| 97 | } |
---|
| 98 | |
---|
[205] | 99 | delete [] fpixel; |
---|
| 100 | delete [] lpixel; |
---|
| 101 | delete [] inc; |
---|
[204] | 102 | |
---|
[160] | 103 | if(anynul==0){ |
---|
| 104 | // no blank pixels, so don't bother with any trimming or checking... |
---|
| 105 | if(this->par.getFlagBlankPix()) { |
---|
| 106 | // if user requested fixing, inform them of change. |
---|
| 107 | duchampWarning("getFITSdata", |
---|
| 108 | "No blank pixels, so setting flagBlankPix to false.\n"); |
---|
| 109 | } |
---|
| 110 | this->par.setFlagBlankPix(false); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | this->initialiseCube(dimAxes); |
---|
[204] | 114 | this->saveArray(pixarray,npix); |
---|
[205] | 115 | delete [] pixarray; |
---|
| 116 | delete [] dimAxes; |
---|
[164] | 117 | this->par.setOffsets(this->head.getWCS()); |
---|
[160] | 118 | //------------------------------------------------------------- |
---|
| 119 | // Once the array is saved, change the value of the blank pixels from |
---|
| 120 | // 0 (as they are set by fits_read_pixnull) to the correct blank value |
---|
| 121 | // as determined by the above code. |
---|
| 122 | for(int i=0; i<npix;i++){ |
---|
| 123 | // if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; |
---|
[187] | 124 | // if(isnan(array[i])) this->array[i] = par.getBlankPixVal(); |
---|
[160] | 125 | if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); |
---|
| 126 | } |
---|
| 127 | |
---|
[204] | 128 | delete [] nullarray; |
---|
[205] | 129 | // Close the FITS file -- not needed any more in this function. |
---|
| 130 | status = 0; |
---|
| 131 | fits_close_file(fptr, &status); |
---|
| 132 | if (status){ |
---|
| 133 | duchampWarning("defineWCS","Error closing file: "); |
---|
| 134 | fits_report_error(stderr, status); |
---|
| 135 | } |
---|
[204] | 136 | |
---|
[160] | 137 | return SUCCESS; |
---|
| 138 | |
---|
| 139 | } |
---|