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