source: tags/release-1.1.2/src/FitsIO/subsection.cc @ 1441

Last change on this file since 1441 was 394, checked in by MatthewWhiting, 17 years ago

Fixing wcslib include calls so that it is a bit more robust.

File size: 5.6 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    /**
48     * If there is a subsection being used, set the offset values
49     * according to the correct dimensions given by the WCS struct. 
50     * If not, set the offsets to zero.
51     * \param wcs The WCSLIB wcsprm struct that defines which axis is which.
52     */
53    if(this->flagSubsection){ // if so, then the offsets array is defined.
54      this->xSubOffset = this->pixelSec.getStart(wcs->lng);
55      this->ySubOffset = this->pixelSec.getStart(wcs->lat);
56      this->zSubOffset = this->pixelSec.getStart(wcs->spec);
57    }
58    else{// else they should be 0
59      this->xSubOffset = this->ySubOffset = this->zSubOffset = 0;
60    }
61  }
62
63  void Param::setOffsets(struct wcsprm &wcs)
64  {
65    /**
66     * If there is a subsection being used, set the offset values
67     * according to the correct dimensions given by the WCS struct. 
68     * If not, set the offsets to zero.
69     * \param wcs The WCSLIB wcsprm struct that defines which axis is which.
70     */
71    if(this->flagSubsection){ // if so, then the offsets array is defined.
72      this->xSubOffset = this->pixelSec.getStart(wcs.lng);
73      this->ySubOffset = this->pixelSec.getStart(wcs.lat);
74      this->zSubOffset = this->pixelSec.getStart(wcs.spec);
75    }
76    else{// else they should be 0
77      this->xSubOffset = this->ySubOffset = this->zSubOffset = 0;
78    }
79  }
80
81  int Param::verifySubsection()
82  {
83    /**
84     * Checks that the subsection strings (the pixel and stats
85     * subsections) are in the appropriate format, with the correct
86     * number of entries (one for each axis).
87     *
88     * This reads the dimensional information from the FITS file, and
89     * uses this with the Section::parse() function to make sure each
90     * section is OK.
91     *
92     * \return SUCCESS/FAILURE depending on outcome of the
93     * Section::parse() calls. Also FAILURE if something goes wrong with
94     * the FITS access.
95     */
96
97    if(!this->flagSubsection && !this->flagStatSec){
98      // if we get here, we are using neither subsection
99      return SUCCESS;
100    }
101    else{
102      // otherwise, at least one of the subsections is being used, and
103      // so we need to check them
104
105      // First open the requested FITS file and check its existence and
106      //  number of axes.
107      int numAxes,status = 0;  /* MUST initialize status */
108      fitsfile *fptr;         
109
110      // Make sure the FITS file exists
111      int exists;
112      fits_file_exists(this->imageFile.c_str(),&exists,&status);
113      if(exists<=0){
114        fits_report_error(stderr, status);
115        duchampWarning("Cube Reader", "Requested image does not exist!\n");
116        return FAILURE;
117      }
118      // Open the FITS file
119      if( fits_open_file(&fptr,this->imageFile.c_str(),READONLY,&status) ){
120        fits_report_error(stderr, status);
121        return FAILURE;
122      }
123      // Read the size information -- number of axes and their sizes
124      status = 0;
125      if(fits_get_img_dim(fptr, &numAxes, &status)){
126        fits_report_error(stderr, status);
127        return FAILURE;
128      }
129      long *dimAxes = new long[numAxes];
130      for(int i=0;i<numAxes;i++) dimAxes[i]=1;
131      status = 0;
132      if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
133        fits_report_error(stderr, status);
134        return FAILURE;
135      }
136      // Close the FITS file.
137      status = 0;
138      fits_close_file(fptr, &status);
139      if (status){
140        duchampWarning("Cube Reader","Error closing file: ");
141        fits_report_error(stderr, status);
142      }
143
144      ///////////////////
145      // Now parse the subsection and make sure all that works.
146 
147      std::vector<long> dim(numAxes);
148      for(int i=0;i<numAxes;i++) dim[i] = dimAxes[i];
149      delete [] dimAxes;
150 
151      if(this->flagSubsection)
152        if(this->pixelSec.parse(dim)==FAILURE) return FAILURE;
153
154      if(this->flagStatSec)
155        if(this->statSec.parse(dim)==FAILURE)  return FAILURE;
156 
157      return SUCCESS;
158    }
159
160  }
161
162}
Note: See TracBrowser for help on using the repository browser.