source: trunk/src/FitsIO/subsection.cc @ 521

Last change on this file since 521 was 495, checked in by MatthewWhiting, 16 years ago

Improving the data-reading, by using fits_read_subset_flt and directly writing blank pixels as BlankPixVal?. This saves having to record an additional array of chars. Also improved the subsection offset analysis.

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