[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]; |
---|
[224] | 80 | |
---|
| 81 | /* |
---|
| 82 | //////////// |
---|
| 83 | // MAY NOT NEED THIS -- SHOULD BE ABLE TO SET IT WITH THE BLANKPIXVALUE |
---|
| 84 | // INPUT PARAMETER. |
---|
| 85 | //////////// |
---|
| 86 | // set the BLANK pixel scaling if there is no BLANK keyword present |
---|
| 87 | if(this->par.getFlagUsingBlank()){ |
---|
| 88 | long nullval = long((this->par.getBlankPixVal()-this->par.getBzeroKeyword()) |
---|
| 89 | / this->getBscaleKeyword()); |
---|
| 90 | status=0; |
---|
| 91 | if(fits_set_imgnull(fptr, nullval, &status)){ |
---|
| 92 | duchampWarning("getFITSdata","There was an error setting the null pixel value:"); |
---|
| 93 | fits_report_error(stderr,status); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | */ |
---|
[160] | 97 | |
---|
[161] | 98 | int colnum = 0; // want the first dataset in the FITS file |
---|
| 99 | |
---|
[204] | 100 | // prepare the Cube's pixel array -- so that we don't have to use saveArray; |
---|
| 101 | // if(this->numPixels>0) delete [] array; |
---|
| 102 | // this->array = new float[npix]; |
---|
| 103 | // this->numPixels = npix; |
---|
[161] | 104 | // read the relevant subset, defined by the first & last pixel ranges |
---|
[160] | 105 | status = 0; |
---|
| 106 | if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes, |
---|
| 107 | fpixel, lpixel, inc, |
---|
[204] | 108 | // this->array, nullarray, &anynul, &status)){ |
---|
| 109 | pixarray, nullarray, &anynul, &status)){ |
---|
[160] | 110 | duchampError("getFITSdata", |
---|
| 111 | "There was an error reading in the data array:"); |
---|
| 112 | fits_report_error(stderr, status); |
---|
| 113 | return FAILURE; |
---|
| 114 | } |
---|
| 115 | |
---|
[205] | 116 | delete [] fpixel; |
---|
| 117 | delete [] lpixel; |
---|
| 118 | delete [] inc; |
---|
[204] | 119 | |
---|
[160] | 120 | if(anynul==0){ |
---|
| 121 | // no blank pixels, so don't bother with any trimming or checking... |
---|
| 122 | if(this->par.getFlagBlankPix()) { |
---|
| 123 | // if user requested fixing, inform them of change. |
---|
| 124 | duchampWarning("getFITSdata", |
---|
| 125 | "No blank pixels, so setting flagBlankPix to false.\n"); |
---|
| 126 | } |
---|
| 127 | this->par.setFlagBlankPix(false); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | this->initialiseCube(dimAxes); |
---|
[204] | 131 | this->saveArray(pixarray,npix); |
---|
[205] | 132 | delete [] pixarray; |
---|
| 133 | delete [] dimAxes; |
---|
[164] | 134 | this->par.setOffsets(this->head.getWCS()); |
---|
[160] | 135 | //------------------------------------------------------------- |
---|
| 136 | // Once the array is saved, change the value of the blank pixels from |
---|
| 137 | // 0 (as they are set by fits_read_pixnull) to the correct blank value |
---|
| 138 | // as determined by the above code. |
---|
| 139 | for(int i=0; i<npix;i++){ |
---|
| 140 | // if(nullarray[i]==1) this->array[i] = blank*bscale + bzero; |
---|
[187] | 141 | // if(isnan(array[i])) this->array[i] = par.getBlankPixVal(); |
---|
[160] | 142 | if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); |
---|
| 143 | } |
---|
| 144 | |
---|
[204] | 145 | delete [] nullarray; |
---|
[205] | 146 | // Close the FITS file -- not needed any more in this function. |
---|
| 147 | status = 0; |
---|
| 148 | fits_close_file(fptr, &status); |
---|
| 149 | if (status){ |
---|
| 150 | duchampWarning("defineWCS","Error closing file: "); |
---|
| 151 | fits_report_error(stderr, status); |
---|
| 152 | } |
---|
[204] | 153 | |
---|
[160] | 154 | return SUCCESS; |
---|
| 155 | |
---|
| 156 | } |
---|