[301] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // dataIO.cc: Read the data array from a FITS file into a Cube object. |
---|
| 3 | // ----------------------------------------------------------------------- |
---|
| 4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 5 | // |
---|
| 6 | // This program is free software; you can redistribute it and/or modify it |
---|
| 7 | // under the terms of the GNU General Public License as published by the |
---|
| 8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 9 | // option) any later version. |
---|
| 10 | // |
---|
| 11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 14 | // for more details. |
---|
| 15 | // |
---|
| 16 | // You should have received a copy of the GNU General Public License |
---|
| 17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 19 | // |
---|
| 20 | // Correspondence concerning Duchamp may be directed to: |
---|
| 21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 22 | // Postal address: Dr. Matthew Whiting |
---|
| 23 | // Australia Telescope National Facility, CSIRO |
---|
| 24 | // PO Box 76 |
---|
| 25 | // Epping NSW 1710 |
---|
| 26 | // AUSTRALIA |
---|
| 27 | // ----------------------------------------------------------------------- |
---|
[160] | 28 | #include <iostream> |
---|
| 29 | #include <string> |
---|
| 30 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
| 31 | // wtbarr (this is a problem when using gcc v.4+ |
---|
| 32 | #include <fitsio.h> |
---|
| 33 | #include <math.h> |
---|
[393] | 34 | #include <duchamp/duchamp.hh> |
---|
| 35 | #include <duchamp/param.hh> |
---|
| 36 | #include <duchamp/fitsHeader.hh> |
---|
| 37 | #include <duchamp/Cubes/cubes.hh> |
---|
[160] | 38 | |
---|
[378] | 39 | namespace duchamp |
---|
[160] | 40 | { |
---|
| 41 | |
---|
[378] | 42 | int Cube::getFITSdata(std::string fname) |
---|
| 43 | { |
---|
| 44 | /** |
---|
| 45 | * This function retrieves the data array from the FITS file at the |
---|
| 46 | * location given by the string argument. |
---|
| 47 | * Only the two spatial axes and the one spectral axis are stored in the |
---|
| 48 | * data array. These axes are given by the wcsprm variables wcs->lng, |
---|
| 49 | * wcs->lat and wcs->spec. |
---|
| 50 | * All other axes are just read by their first pixel, using the |
---|
| 51 | * fits_read_subsetnull_flt function |
---|
| 52 | */ |
---|
[160] | 53 | |
---|
[378] | 54 | int numAxes, status = 0; /* MUST initialize status */ |
---|
| 55 | fitsfile *fptr; |
---|
[161] | 56 | |
---|
[378] | 57 | // Open the FITS file |
---|
| 58 | status = 0; |
---|
| 59 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 60 | fits_report_error(stderr, status); |
---|
| 61 | return FAILURE; |
---|
| 62 | } |
---|
[271] | 63 | |
---|
[378] | 64 | // Read the size of the FITS file -- number and sizes of the axes |
---|
| 65 | status = 0; |
---|
| 66 | if(fits_get_img_dim(fptr, &numAxes, &status)){ |
---|
| 67 | fits_report_error(stderr, status); |
---|
| 68 | return FAILURE; |
---|
| 69 | } |
---|
[160] | 70 | |
---|
[378] | 71 | long *dimAxes = new long[numAxes]; |
---|
| 72 | for(int i=0;i<numAxes;i++) dimAxes[i]=1; |
---|
| 73 | status = 0; |
---|
| 74 | if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){ |
---|
| 75 | fits_report_error(stderr, status); |
---|
| 76 | return FAILURE; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | // Identify which axes are the "interesting" ones |
---|
| 80 | int lng,lat,spc; |
---|
| 81 | if(this->head.isWCS()){ |
---|
| 82 | lng = this->head.WCS().lng; |
---|
| 83 | lat = this->head.WCS().lat; |
---|
| 84 | spc = this->head.WCS().spec; |
---|
| 85 | } |
---|
| 86 | else{ |
---|
| 87 | lng = 0; |
---|
| 88 | lat = 1; |
---|
| 89 | spc = 2; |
---|
| 90 | } |
---|
[186] | 91 | |
---|
[160] | 92 | |
---|
[378] | 93 | int anynul; |
---|
| 94 | int npix = dimAxes[lng]; |
---|
| 95 | if(numAxes>1) npix *= dimAxes[lat]; |
---|
| 96 | if(spc>=0) npix *= dimAxes[spc]; |
---|
[161] | 97 | |
---|
[378] | 98 | float *pixarray = new float[npix];// the array of pixel values |
---|
| 99 | char *nullarray = new char[npix]; // the array of null pixels |
---|
| 100 | long *inc = new long[numAxes]; // the data-sampling increment |
---|
[161] | 101 | |
---|
[378] | 102 | // define the first and last pixels for each axis. |
---|
| 103 | // set both to 1 for the axes we don't want, and to the full |
---|
| 104 | // range for the ones we do. |
---|
| 105 | long *fpixel = new long[numAxes]; |
---|
| 106 | long *lpixel = new long[numAxes]; |
---|
| 107 | for(int i=0;i<numAxes;i++){ |
---|
| 108 | inc[i] = fpixel[i] = lpixel[i] = 1; |
---|
| 109 | } |
---|
| 110 | lpixel[lng] = dimAxes[lng]; |
---|
| 111 | if(numAxes>1) lpixel[lat] = dimAxes[lat]; |
---|
| 112 | if(spc>=0) lpixel[spc] = dimAxes[spc]; |
---|
[224] | 113 | |
---|
[160] | 114 | |
---|
[378] | 115 | int colnum = 0; // want the first dataset in the FITS file |
---|
[161] | 116 | |
---|
[378] | 117 | // read the relevant subset, defined by the first & last pixel ranges |
---|
| 118 | status = 0; |
---|
| 119 | if(fits_read_subsetnull_flt(fptr, colnum, numAxes, dimAxes, |
---|
| 120 | fpixel, lpixel, inc, |
---|
| 121 | pixarray, nullarray, &anynul, &status)){ |
---|
| 122 | duchampError("Cube Reader", |
---|
| 123 | "There was an error reading in the data array:"); |
---|
| 124 | fits_report_error(stderr, status); |
---|
| 125 | return FAILURE; |
---|
| 126 | } |
---|
[160] | 127 | |
---|
[378] | 128 | delete [] fpixel; |
---|
| 129 | delete [] lpixel; |
---|
| 130 | delete [] inc; |
---|
[204] | 131 | |
---|
[378] | 132 | if(anynul==0){ |
---|
| 133 | // no blank pixels, so don't bother with any trimming or checking... |
---|
| 134 | if(this->par.getFlagTrim()) { |
---|
| 135 | // if user requested fixing, inform them of change. |
---|
| 136 | duchampWarning("Cube Reader", |
---|
| 137 | "No blank pixels, so setting flagTrim to false.\n"); |
---|
| 138 | } |
---|
| 139 | this->par.setFlagBlankPix(false); |
---|
| 140 | this->par.setFlagTrim(false); |
---|
[160] | 141 | } |
---|
| 142 | |
---|
[378] | 143 | this->initialiseCube(dimAxes); |
---|
| 144 | this->saveArray(pixarray,npix); |
---|
| 145 | delete [] pixarray; |
---|
| 146 | delete [] dimAxes; |
---|
| 147 | this->par.setOffsets(this->head.WCS()); |
---|
[365] | 148 | |
---|
[378] | 149 | //------------------------------------------------------------- |
---|
| 150 | // Once the array is saved, change the value of the blank pixels |
---|
| 151 | // from 0 (as they are set by fits_read_subsetnull_flt) to the |
---|
| 152 | // correct blank value as determined by the above code. |
---|
| 153 | for(int i=0; i<npix;i++){ |
---|
| 154 | if(nullarray[i]==1) this->array[i] = this->par.getBlankPixVal(); |
---|
| 155 | } |
---|
[160] | 156 | |
---|
[378] | 157 | delete [] nullarray; |
---|
| 158 | // Close the FITS file -- not needed any more in this function. |
---|
| 159 | status = 0; |
---|
| 160 | fits_close_file(fptr, &status); |
---|
| 161 | if (status){ |
---|
| 162 | duchampWarning("Cube Reader","Error closing file: "); |
---|
| 163 | fits_report_error(stderr, status); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | return SUCCESS; |
---|
| 167 | |
---|
[205] | 168 | } |
---|
[204] | 169 | |
---|
[160] | 170 | } |
---|