[301] | 1 | // ----------------------------------------------------------------------- |
---|
| 2 | // subsection.cc: Make sure subsections are valid for a particular |
---|
| 3 | // FITS file. |
---|
| 4 | // ----------------------------------------------------------------------- |
---|
| 5 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
| 6 | // |
---|
| 7 | // This program is free software; you can redistribute it and/or modify it |
---|
| 8 | // under the terms of the GNU General Public License as published by the |
---|
| 9 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
| 10 | // option) any later version. |
---|
| 11 | // |
---|
| 12 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
| 13 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
| 15 | // for more details. |
---|
| 16 | // |
---|
| 17 | // You should have received a copy of the GNU General Public License |
---|
| 18 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
| 19 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | // |
---|
| 21 | // Correspondence concerning Duchamp may be directed to: |
---|
| 22 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
| 23 | // Postal address: Dr. Matthew Whiting |
---|
| 24 | // Australia Telescope National Facility, CSIRO |
---|
| 25 | // PO Box 76 |
---|
| 26 | // Epping NSW 1710 |
---|
| 27 | // AUSTRALIA |
---|
| 28 | // ----------------------------------------------------------------------- |
---|
[164] | 29 | #include <iostream> |
---|
| 30 | #include <sstream> |
---|
| 31 | #include <string> |
---|
[258] | 32 | #include <vector> |
---|
[164] | 33 | #include <math.h> |
---|
[394] | 34 | #include <wcslib/wcs.h> |
---|
[164] | 35 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
| 36 | // wtbarr (this is a problem when using gcc v.4+ |
---|
| 37 | #include <fitsio.h> |
---|
[393] | 38 | #include <duchamp/param.hh> |
---|
| 39 | #include <duchamp/duchamp.hh> |
---|
| 40 | #include <duchamp/Utils/Section.hh> |
---|
[164] | 41 | |
---|
[378] | 42 | namespace duchamp |
---|
[164] | 43 | { |
---|
| 44 | |
---|
[378] | 45 | void Param::setOffsets(struct wcsprm *wcs) |
---|
| 46 | { |
---|
[528] | 47 | /// If there is a subsection being used, set the offset values |
---|
| 48 | /// according to the correct dimensions given by the WCS struct. |
---|
| 49 | /// If not, set the offsets to zero. |
---|
| 50 | /// \param wcs The WCSLIB wcsprm struct that defines which axis is which. |
---|
| 51 | |
---|
[378] | 52 | if(this->flagSubsection){ // if so, then the offsets array is defined. |
---|
[495] | 53 | int specAxis = wcs->spec; |
---|
| 54 | if(specAxis>=wcs->naxis) specAxis = wcs->naxis-1; |
---|
[378] | 55 | this->xSubOffset = this->pixelSec.getStart(wcs->lng); |
---|
| 56 | this->ySubOffset = this->pixelSec.getStart(wcs->lat); |
---|
[1421] | 57 | if(specAxis<0) this->zSubOffset = 0; |
---|
| 58 | else this->zSubOffset = this->pixelSec.getStart(specAxis); |
---|
[378] | 59 | } |
---|
| 60 | else{// else they should be 0 |
---|
| 61 | this->xSubOffset = this->ySubOffset = this->zSubOffset = 0; |
---|
| 62 | } |
---|
[309] | 63 | } |
---|
| 64 | |
---|
[378] | 65 | void Param::setOffsets(struct wcsprm &wcs) |
---|
| 66 | { |
---|
[528] | 67 | /// If there is a subsection being used, set the offset values |
---|
| 68 | /// according to the correct dimensions given by the WCS struct. |
---|
| 69 | /// If not, set the offsets to zero. |
---|
| 70 | /// \param wcs The WCSLIB wcsprm struct that defines which axis is which. |
---|
| 71 | |
---|
[378] | 72 | if(this->flagSubsection){ // if so, then the offsets array is defined. |
---|
[495] | 73 | int specAxis = wcs.spec; |
---|
| 74 | if(specAxis>=wcs.naxis) specAxis = wcs.naxis-1; |
---|
[378] | 75 | this->xSubOffset = this->pixelSec.getStart(wcs.lng); |
---|
| 76 | this->ySubOffset = this->pixelSec.getStart(wcs.lat); |
---|
[1421] | 77 | if(specAxis<0) this->zSubOffset = 0; |
---|
| 78 | else this->zSubOffset = this->pixelSec.getStart(specAxis); |
---|
[378] | 79 | } |
---|
| 80 | else{// else they should be 0 |
---|
| 81 | this->xSubOffset = this->ySubOffset = this->zSubOffset = 0; |
---|
| 82 | } |
---|
[168] | 83 | } |
---|
[164] | 84 | |
---|
[698] | 85 | OUTCOME Param::verifySubsection() |
---|
[378] | 86 | { |
---|
[528] | 87 | /// Checks that the subsection strings (the pixel and stats |
---|
| 88 | /// subsections) are in the appropriate format, with the correct |
---|
| 89 | /// number of entries (one for each axis). |
---|
| 90 | /// |
---|
| 91 | /// This reads the dimensional information from the FITS file, and |
---|
| 92 | /// uses this with the Section::parse() function to make sure each |
---|
| 93 | /// section is OK. |
---|
| 94 | /// |
---|
| 95 | /// \return SUCCESS/FAILURE depending on outcome of the |
---|
| 96 | /// Section::parse() calls. Also FAILURE if something goes wrong with |
---|
| 97 | /// the FITS access. |
---|
[289] | 98 | |
---|
[378] | 99 | if(!this->flagSubsection && !this->flagStatSec){ |
---|
| 100 | // if we get here, we are using neither subsection |
---|
| 101 | return SUCCESS; |
---|
[289] | 102 | } |
---|
[378] | 103 | else{ |
---|
| 104 | // otherwise, at least one of the subsections is being used, and |
---|
| 105 | // so we need to check them |
---|
[289] | 106 | |
---|
[378] | 107 | // First open the requested FITS file and check its existence and |
---|
| 108 | // number of axes. |
---|
| 109 | int numAxes,status = 0; /* MUST initialize status */ |
---|
| 110 | fitsfile *fptr; |
---|
| 111 | |
---|
[910] | 112 | if(this->checkImageExists()==FAILURE) return FAILURE; |
---|
| 113 | |
---|
[378] | 114 | // Open the FITS file |
---|
| 115 | if( fits_open_file(&fptr,this->imageFile.c_str(),READONLY,&status) ){ |
---|
| 116 | fits_report_error(stderr, status); |
---|
| 117 | return FAILURE; |
---|
| 118 | } |
---|
| 119 | // Read the size information -- number of axes and their sizes |
---|
| 120 | status = 0; |
---|
| 121 | if(fits_get_img_dim(fptr, &numAxes, &status)){ |
---|
| 122 | fits_report_error(stderr, status); |
---|
| 123 | return FAILURE; |
---|
| 124 | } |
---|
| 125 | long *dimAxes = new long[numAxes]; |
---|
| 126 | for(int i=0;i<numAxes;i++) dimAxes[i]=1; |
---|
| 127 | status = 0; |
---|
| 128 | if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){ |
---|
| 129 | fits_report_error(stderr, status); |
---|
| 130 | return FAILURE; |
---|
| 131 | } |
---|
| 132 | // Close the FITS file. |
---|
| 133 | status = 0; |
---|
| 134 | fits_close_file(fptr, &status); |
---|
| 135 | if (status){ |
---|
[913] | 136 | DUCHAMPWARN("Cube Reader","Error closing file: "); |
---|
[378] | 137 | fits_report_error(stderr, status); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /////////////////// |
---|
| 141 | // Now parse the subsection and make sure all that works. |
---|
[258] | 142 | |
---|
[1263] | 143 | std::vector<size_t> dim(dimAxes,dimAxes+numAxes); |
---|
| 144 | // for(int i=0;i<numAxes;i++) dim[i] = dimAxes[i]; |
---|
[378] | 145 | delete [] dimAxes; |
---|
[289] | 146 | |
---|
[818] | 147 | return this->parseSubsections(dim); |
---|
| 148 | |
---|
[378] | 149 | } |
---|
| 150 | |
---|
[289] | 151 | } |
---|
| 152 | |
---|
[1263] | 153 | OUTCOME Param::parseSubsections(size_t *dim, int size) |
---|
[822] | 154 | { |
---|
[1263] | 155 | std::vector<size_t> vecDim(size); |
---|
[822] | 156 | for(int i=0;i<size;i++) vecDim[i] = dim[i]; |
---|
| 157 | return this->parseSubsections(vecDim); |
---|
| 158 | } |
---|
[818] | 159 | |
---|
[1263] | 160 | OUTCOME Param::parseSubsections(std::vector<long> &dim) |
---|
| 161 | { |
---|
| 162 | std::vector<size_t> vecDim(dim.size()); |
---|
| 163 | for(size_t i=0;i<dim.size();i++) vecDim[i] = size_t(dim[i]); |
---|
| 164 | return this->parseSubsections(vecDim); |
---|
| 165 | } |
---|
| 166 | |
---|
[822] | 167 | OUTCOME Param::parseSubsections(std::vector<int> &dim) |
---|
| 168 | { |
---|
[1263] | 169 | std::vector<size_t> vecDim(dim.size()); |
---|
| 170 | for(size_t i=0;i<dim.size();i++) vecDim[i] = size_t(dim[i]); |
---|
[822] | 171 | return this->parseSubsections(vecDim); |
---|
| 172 | } |
---|
| 173 | |
---|
[1263] | 174 | OUTCOME Param::parseSubsections(std::vector<size_t> &dim) |
---|
[818] | 175 | { |
---|
| 176 | |
---|
[827] | 177 | if(this->pixelSec.parse(dim)==FAILURE) return FAILURE; |
---|
[828] | 178 | this->pixelSec = this->pixelSec.intersect(dim); |
---|
[833] | 179 | if(this->pixelSec.parse(dim)==FAILURE) return FAILURE; |
---|
[829] | 180 | if(!this->pixelSec.isValid()){ |
---|
[1191] | 181 | DUCHAMPERROR("parseSubsections","pixel section is not valid"); |
---|
[829] | 182 | return FAILURE; |
---|
| 183 | } |
---|
[818] | 184 | |
---|
| 185 | if(this->flagStatSec){ |
---|
[833] | 186 | if(this->statSec.parse(dim)==FAILURE) return FAILURE; |
---|
[830] | 187 | this->statSec = this->statSec.intersect(dim); |
---|
[833] | 188 | if(this->statSec.parse(dim)==FAILURE) return FAILURE; |
---|
[830] | 189 | if(!this->statSec.isValid()){ |
---|
[1191] | 190 | DUCHAMPERROR("parseSubsections","StatSec is not valid"); |
---|
[830] | 191 | return FAILURE; |
---|
| 192 | } |
---|
[818] | 193 | this->statSec = this->statSec.intersect(this->pixelSec); |
---|
[833] | 194 | if(this->statSec.parse(dim)==FAILURE) return FAILURE; |
---|
[827] | 195 | if(!this->statSec.isValid()){ |
---|
[913] | 196 | DUCHAMPERROR("parseSubsections","StatSec does not intersect with pixel subsection"); |
---|
[827] | 197 | return FAILURE; |
---|
| 198 | } |
---|
[833] | 199 | if(this->statSec.parse(dim)==FAILURE) return FAILURE; |
---|
[818] | 200 | } |
---|
| 201 | |
---|
| 202 | return SUCCESS; |
---|
| 203 | |
---|
| 204 | } |
---|
| 205 | |
---|
[164] | 206 | } |
---|