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

Last change on this file since 326 was 309, checked in by Matthew Whiting, 17 years ago

Mostly changes to fix some memory allocation/deallocation issues raised by valgrind. Minor changes to the Guide.

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