[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 | |
---|
[698] | 42 | OUTCOME Cube::getFITSdata(std::string fname) |
---|
[378] | 43 | { |
---|
[971] | 44 | OUTCOME returnValue = SUCCESS; |
---|
| 45 | |
---|
| 46 | int status = 0; /* MUST initialize status */ |
---|
| 47 | fitsfile *fptr; |
---|
| 48 | // Open the FITS file |
---|
| 49 | status = 0; |
---|
| 50 | if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){ |
---|
| 51 | fits_report_error(stderr, status); |
---|
| 52 | returnValue = FAILURE; |
---|
| 53 | } |
---|
| 54 | else { |
---|
| 55 | |
---|
| 56 | returnValue = this->getFITSdata(fptr); |
---|
| 57 | |
---|
| 58 | // Close the FITS file -- not needed any more in this function. |
---|
| 59 | status = 0; |
---|
| 60 | fits_close_file(fptr, &status); |
---|
| 61 | if (status){ |
---|
| 62 | DUCHAMPWARN("Cube Reader","Error closing file: "); |
---|
| 63 | fits_report_error(stderr, status); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | return returnValue; |
---|
| 69 | |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | OUTCOME Cube::getFITSdata() |
---|
| 73 | { |
---|
| 74 | |
---|
| 75 | OUTCOME returnValue = SUCCESS; |
---|
| 76 | if(this->head.FPTR() == 0) { |
---|
| 77 | returnValue = FAILURE; |
---|
| 78 | DUCHAMPERROR("Cube Reader", "FITS file not opened."); |
---|
| 79 | } |
---|
| 80 | else { |
---|
| 81 | returnValue = this->getFITSdata(this->head.FPTR()); |
---|
| 82 | } |
---|
| 83 | return returnValue; |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | |
---|
| 89 | OUTCOME Cube::getFITSdata(fitsfile *fptr) |
---|
| 90 | { |
---|
[528] | 91 | /// This function retrieves the data array from the FITS file at the |
---|
| 92 | /// location given by the string argument. |
---|
| 93 | /// Only the two spatial axes and the one spectral axis are stored in the |
---|
| 94 | /// data array. These axes are given by the wcsprm variables wcs->lng, |
---|
| 95 | /// wcs->lat and wcs->spec. |
---|
| 96 | /// All other axes are just read by their first pixel, using the |
---|
| 97 | /// fits_read_subsetnull_flt function |
---|
[160] | 98 | |
---|
[378] | 99 | int numAxes, status = 0; /* MUST initialize status */ |
---|
[161] | 100 | |
---|
[378] | 101 | // Read the size of the FITS file -- number and sizes of the axes |
---|
| 102 | status = 0; |
---|
| 103 | if(fits_get_img_dim(fptr, &numAxes, &status)){ |
---|
| 104 | fits_report_error(stderr, status); |
---|
| 105 | return FAILURE; |
---|
| 106 | } |
---|
[160] | 107 | |
---|
[944] | 108 | this->head.setNumAxes(numAxes); |
---|
| 109 | |
---|
[378] | 110 | long *dimAxes = new long[numAxes]; |
---|
| 111 | for(int i=0;i<numAxes;i++) dimAxes[i]=1; |
---|
| 112 | status = 0; |
---|
| 113 | if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){ |
---|
| 114 | fits_report_error(stderr, status); |
---|
| 115 | return FAILURE; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | // Identify which axes are the "interesting" ones |
---|
| 119 | int lng,lat,spc; |
---|
[477] | 120 | if(this->head.isWCS() && (this->head.WCS().spec>=0)){ |
---|
[378] | 121 | lng = this->head.WCS().lng; |
---|
| 122 | lat = this->head.WCS().lat; |
---|
| 123 | spc = this->head.WCS().spec; |
---|
| 124 | } |
---|
| 125 | else{ |
---|
| 126 | lng = 0; |
---|
| 127 | lat = 1; |
---|
| 128 | spc = 2; |
---|
| 129 | } |
---|
[186] | 130 | |
---|
[378] | 131 | int anynul; |
---|
| 132 | int npix = dimAxes[lng]; |
---|
| 133 | if(numAxes>1) npix *= dimAxes[lat]; |
---|
[467] | 134 | if((spc>=0)&&(numAxes>spc)) npix *= dimAxes[spc]; |
---|
[161] | 135 | |
---|
| 136 | |
---|
[378] | 137 | // define the first and last pixels for each axis. |
---|
| 138 | // set both to 1 for the axes we don't want, and to the full |
---|
| 139 | // range for the ones we do. |
---|
| 140 | long *fpixel = new long[numAxes]; |
---|
| 141 | long *lpixel = new long[numAxes]; |
---|
[740] | 142 | long *inc = new long[numAxes]; // the data-sampling increment |
---|
[378] | 143 | for(int i=0;i<numAxes;i++){ |
---|
| 144 | inc[i] = fpixel[i] = lpixel[i] = 1; |
---|
| 145 | } |
---|
| 146 | lpixel[lng] = dimAxes[lng]; |
---|
| 147 | if(numAxes>1) lpixel[lat] = dimAxes[lat]; |
---|
[467] | 148 | if((spc>=0)&&(numAxes>spc)) lpixel[spc] = dimAxes[spc]; |
---|
[224] | 149 | |
---|
[378] | 150 | int colnum = 0; // want the first dataset in the FITS file |
---|
[161] | 151 | |
---|
[378] | 152 | // read the relevant subset, defined by the first & last pixel ranges |
---|
[740] | 153 | if(this->initialiseCube(dimAxes) == FAILURE) return FAILURE; |
---|
[378] | 154 | status = 0; |
---|
[495] | 155 | if(fits_read_subset_flt(fptr, colnum, numAxes, dimAxes, |
---|
| 156 | fpixel, lpixel, inc, |
---|
[740] | 157 | this->par.getBlankPixVal(), this->array, &anynul, &status)){ |
---|
[913] | 158 | DUCHAMPERROR("Cube Reader", "There was an error reading in the data array:"); |
---|
[378] | 159 | fits_report_error(stderr, status); |
---|
| 160 | return FAILURE; |
---|
| 161 | } |
---|
[160] | 162 | |
---|
[378] | 163 | delete [] fpixel; |
---|
| 164 | delete [] lpixel; |
---|
| 165 | delete [] inc; |
---|
[204] | 166 | |
---|
[378] | 167 | if(anynul==0){ |
---|
| 168 | // no blank pixels, so don't bother with any trimming or checking... |
---|
| 169 | if(this->par.getFlagTrim()) { |
---|
| 170 | // if user requested fixing, inform them of change. |
---|
[913] | 171 | DUCHAMPWARN("Cube Reader", "No blank pixels, so setting flagTrim to false."); |
---|
[378] | 172 | } |
---|
| 173 | this->par.setFlagBlankPix(false); |
---|
| 174 | this->par.setFlagTrim(false); |
---|
[160] | 175 | } |
---|
| 176 | |
---|
[378] | 177 | delete [] dimAxes; |
---|
[365] | 178 | |
---|
[378] | 179 | return SUCCESS; |
---|
| 180 | |
---|
[205] | 181 | } |
---|
[204] | 182 | |
---|
[160] | 183 | } |
---|