source: tags/release-1.1.12/src/FitsIO/subsection.cc

Last change on this file was 833, checked in by MatthewWhiting, 13 years ago

Making sure we always parse the subsection after it changes (ie. through intersection with another).

File size: 7.1 KB
Line 
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// -----------------------------------------------------------------------
29#include <iostream>
30#include <sstream>
31#include <string>
32#include <vector>
33#include <math.h>
34#include <wcslib/wcs.h>
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>
38#include <duchamp/param.hh>
39#include <duchamp/duchamp.hh>
40#include <duchamp/Utils/Section.hh>
41
42namespace duchamp
43{
44
45  void Param::setOffsets(struct wcsprm *wcs)
46  {
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
52    if(this->flagSubsection){ // if so, then the offsets array is defined.
53      int specAxis = wcs->spec;
54      if(specAxis<0) specAxis=2;
55      if(specAxis>=wcs->naxis) specAxis = wcs->naxis-1;
56      this->xSubOffset = this->pixelSec.getStart(wcs->lng);
57      this->ySubOffset = this->pixelSec.getStart(wcs->lat);
58      this->zSubOffset = this->pixelSec.getStart(specAxis);
59    }
60    else{// else they should be 0
61      this->xSubOffset = this->ySubOffset = this->zSubOffset = 0;
62    }
63  }
64
65  void Param::setOffsets(struct wcsprm &wcs)
66  {
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   
72    if(this->flagSubsection){ // if so, then the offsets array is defined.
73      int specAxis = wcs.spec;
74      if(specAxis<0) specAxis=2;
75      if(specAxis>=wcs.naxis) specAxis = wcs.naxis-1;
76      this->xSubOffset = this->pixelSec.getStart(wcs.lng);
77      this->ySubOffset = this->pixelSec.getStart(wcs.lat);
78      this->zSubOffset = this->pixelSec.getStart(specAxis);
79    }
80    else{// else they should be 0
81      this->xSubOffset = this->ySubOffset = this->zSubOffset = 0;
82    }
83  }
84
85  OUTCOME Param::verifySubsection()
86  {
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.
98
99    if(!this->flagSubsection && !this->flagStatSec){
100      // if we get here, we are using neither subsection
101      return SUCCESS;
102    }
103    else{
104      // otherwise, at least one of the subsections is being used, and
105      // so we need to check them
106
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
112      // Make sure the FITS file exists
113      int exists;
114      fits_file_exists(this->imageFile.c_str(),&exists,&status);
115      if(exists<=0){
116        fits_report_error(stderr, status);
117        duchampWarning("Cube Reader", "Requested image does not exist!\n");
118        return FAILURE;
119      }
120      // Open the FITS file
121      if( fits_open_file(&fptr,this->imageFile.c_str(),READONLY,&status) ){
122        fits_report_error(stderr, status);
123        return FAILURE;
124      }
125      // Read the size information -- number of axes and their sizes
126      status = 0;
127      if(fits_get_img_dim(fptr, &numAxes, &status)){
128        fits_report_error(stderr, status);
129        return FAILURE;
130      }
131      long *dimAxes = new long[numAxes];
132      for(int i=0;i<numAxes;i++) dimAxes[i]=1;
133      status = 0;
134      if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
135        fits_report_error(stderr, status);
136        return FAILURE;
137      }
138      // Close the FITS file.
139      status = 0;
140      fits_close_file(fptr, &status);
141      if (status){
142        duchampWarning("Cube Reader","Error closing file: ");
143        fits_report_error(stderr, status);
144      }
145
146      ///////////////////
147      // Now parse the subsection and make sure all that works.
148 
149      std::vector<long> dim(numAxes);
150      for(int i=0;i<numAxes;i++) dim[i] = dimAxes[i];
151      delete [] dimAxes;
152
153      return this->parseSubsections(dim);
154
155    }
156
157  }
158
159  OUTCOME Param::parseSubsections(long *dim, int size)
160  {
161    std::vector<long> vecDim(size);
162    for(int i=0;i<size;i++) vecDim[i] = dim[i];
163    return this->parseSubsections(vecDim);
164  }
165
166  OUTCOME Param::parseSubsections(std::vector<int> &dim)
167  {
168    std::vector<long> vecDim(dim.size());
169    for(size_t i=0;i<dim.size();i++) vecDim[i] = long(dim[i]);
170    return this->parseSubsections(vecDim);
171  }
172
173  OUTCOME Param::parseSubsections(std::vector<long> &dim)
174  {
175
176    if(this->pixelSec.parse(dim)==FAILURE) return FAILURE;
177    this->pixelSec = this->pixelSec.intersect(dim);
178    if(this->pixelSec.parse(dim)==FAILURE) return FAILURE;
179    if(!this->pixelSec.isValid()){
180      duchampError("parseSubsections","pixel section does not include any pixels\n");
181      return FAILURE;
182    }
183   
184    if(this->flagStatSec){
185      if(this->statSec.parse(dim)==FAILURE) return FAILURE;
186      this->statSec = this->statSec.intersect(dim);
187      if(this->statSec.parse(dim)==FAILURE) return FAILURE;
188      if(!this->statSec.isValid()){
189        duchampError("parseSubsections","StatSec does not include any pixels\n");
190        return FAILURE;
191      }
192      this->statSec = this->statSec.intersect(this->pixelSec);
193      if(this->statSec.parse(dim)==FAILURE) return FAILURE;
194      if(!this->statSec.isValid()){
195        duchampError("parseSubsections","StatSec does not intersect with pixel subsection\n");
196        return FAILURE;
197      }
198      if(this->statSec.parse(dim)==FAILURE) return FAILURE;
199    }
200   
201    return SUCCESS;
202
203  }
204
205}
Note: See TracBrowser for help on using the repository browser.