source: tags/release-1.1.4/src/Cubes/getImage.cc @ 1441

Last change on this file since 1441 was 400, checked in by MatthewWhiting, 16 years ago

Fixed references to fitshdr.h so that the wcslib path is included.

File size: 5.2 KB
Line 
1// -----------------------------------------------------------------------
2// getImage.cc: Read in a FITS file to a Cube object.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <string>
30#include <string.h>
31#include <wcslib/wcs.h>
32#include <wcslib/wcshdr.h>
33#include <wcslib/fitshdr.h>
34#include <wcslib/wcsfix.h>
35#include <wcslib/wcsunits.h>
36#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
37                         //  wtbarr (this is a problem when using gcc v.4+)
38#include <fitsio.h>
39#include <math.h>
40#include <duchamp/duchamp.hh>
41#include <duchamp/param.hh>
42#include <duchamp/fitsHeader.hh>
43#include <duchamp/Cubes/cubes.hh>
44
45namespace duchamp
46{
47
48  std::string imageType[4] = {"point", "spectrum", "image", "cube"};
49
50  int Cube::getCube(std::string fname)
51  {
52    /**
53     * Read in a cube from the file fname (assumed to be in FITS format).
54     *  Function is a front end to the I/O functions in the FitsIO/ directory.
55     *  This function will check that the file exists, report the dimensions
56     *   and then get other functions to read the data, WCS, and necessary
57     *   header keywords.
58     *  \param fname A std::string with name of FITS file.
59     *  \return SUCCESS or FAILURE.
60     */
61
62    int numAxes, status = 0;  /* MUST initialize status */
63    fitsfile *fptr;         
64
65    int exists;
66    fits_file_exists(fname.c_str(),&exists,&status);
67    if(exists<=0){
68      fits_report_error(stderr, status);
69      std::stringstream errmsg;
70      errmsg << "Requested image (" << fname << ") does not exist!\n";
71      duchampError("Cube Reader", errmsg.str());
72      return FAILURE;
73    }
74
75    // Open the FITS file -- make sure it exists
76    status = 0;
77    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
78      fits_report_error(stderr, status);
79      if(((status==URL_PARSE_ERROR)||(status==BAD_NAXIS))
80         &&(this->pars().getFlagSubsection()))
81        duchampError("Cube Reader",
82                     "It may be that the subsection you've entered is invalid.\n\
83Either it has the wrong number of axes, or one axis has too large a range.\n");
84      return FAILURE;
85    }
86
87    // Read the size information -- number and lengths of all axes.
88    // Just use this for reporting purposes.
89    status = 0;
90    if(fits_get_img_dim(fptr, &numAxes, &status)){
91      fits_report_error(stderr, status);
92      return FAILURE;
93    }
94    long *dimAxes = new long[numAxes];
95    for(int i=0;i<numAxes;i++) dimAxes[i]=1;
96    status = 0;
97    if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
98      fits_report_error(stderr, status);
99      return FAILURE;
100    }
101 
102    // Close the FITS file.
103    status = 0;
104    fits_close_file(fptr, &status);
105    if (status){
106      duchampWarning("Cube Reader","Error closing file: ");
107      fits_report_error(stderr, status);
108    }
109
110    // Report the size of the FITS file
111    if(this->par.isVerbose()){
112      std::cout << "Dimensions of FITS file: ";
113      int dim = 0;
114      std::cout << dimAxes[dim];
115      while(dim+1<numAxes) std::cout << "x" << dimAxes[++dim];
116      std::cout << std::endl;
117    }
118
119    delete [] dimAxes;
120
121    // Get the WCS information
122    this->head.defineWCS(fname, this->par);
123
124    if(!this->head.isWCS())
125      duchampWarning("Cube Reader","WCS is not good enough to be used.\n");
126
127    // Get the data array from the FITS file.
128    // Report the dimensions of the data array that was read (this can be
129    //   different to the full FITS array).
130    if(this->par.isVerbose()) std::cout << "Reading data ... "<<std::flush;
131    if(this->getFITSdata(fname)) return FAILURE;
132    if(this->par.isVerbose()){
133      std::cout << "Done. Data array has dimensions: ";
134      std::cout << this->axisDim[0];
135      if(this->axisDim[1]>1) std::cout  <<"x"<< this->axisDim[1];
136      if(this->axisDim[2]>1) std::cout  <<"x"<< this->axisDim[2];
137      std::cout << "\n";
138    }   
139
140    if(this->axisDim[2] == 1){
141      this->par.setMinChannels(0);
142    }
143
144    // Read the necessary header information, and copy some of it into the Param.
145    this->head.readHeaderInfo(fname, this->par);
146
147    return SUCCESS;
148
149  }
150
151}
Note: See TracBrowser for help on using the repository browser.