source: tags/release-1.1/src/Cubes/getImage.cc @ 1391

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

Adding distribution text at the start of each file...

File size: 4.9 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 <wcs.h>
32#include <wcshdr.h>
33#include <fitshdr.h>
34#include <wcsfix.h>
35#include <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.hh>
41#include <param.hh>
42#include <fitsHeader.hh>
43#include <Cubes/cubes.hh>
44
45std::string imageType[4] = {"point", "spectrum", "image", "cube"};
46
47int Cube::getCube(std::string fname)
48{
49  /**
50   * Read in a cube from the file fname (assumed to be in FITS format).
51   *  Function is a front end to the I/O functions in the FitsIO/ directory.
52   *  This function will check that the file exists, report the dimensions
53   *   and then get other functions to read the data, WCS, and necessary
54   *   header keywords.
55   *  \param fname A std::string with name of FITS file.
56   *  \return SUCCESS or FAILURE.
57   */
58
59  int numAxes, status = 0;  /* MUST initialize status */
60  fitsfile *fptr;         
61
62  int exists;
63  fits_file_exists(fname.c_str(),&exists,&status);
64  if(exists<=0){
65    fits_report_error(stderr, status);
66    std::stringstream errmsg;
67    errmsg << "Requested image (" << fname << ") does not exist!\n";
68    duchampError("Cube Reader", errmsg.str());
69    return FAILURE;
70  }
71
72  // Open the FITS file -- make sure it exists
73  status = 0;
74  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
75    fits_report_error(stderr, status);
76    if(((status==URL_PARSE_ERROR)||(status==BAD_NAXIS))
77       &&(this->pars().getFlagSubsection()))
78      duchampError("Cube Reader",
79                   "It may be that the subsection you've entered is invalid.\n\
80Either it has the wrong number of axes, or one axis has too large a range.\n");
81    return FAILURE;
82  }
83
84  // Read the size information -- number and lengths of all axes.
85  // Just use this for reporting purposes.
86  status = 0;
87  if(fits_get_img_dim(fptr, &numAxes, &status)){
88    fits_report_error(stderr, status);
89    return FAILURE;
90  }
91  long *dimAxes = new long[numAxes];
92  for(int i=0;i<numAxes;i++) dimAxes[i]=1;
93  status = 0;
94  if(fits_get_img_size(fptr, numAxes, dimAxes, &status)){
95    fits_report_error(stderr, status);
96    return FAILURE;
97  }
98 
99  // Close the FITS file.
100  status = 0;
101  fits_close_file(fptr, &status);
102  if (status){
103    duchampWarning("Cube Reader","Error closing file: ");
104    fits_report_error(stderr, status);
105  }
106
107  // Report the size of the FITS file
108  if(this->par.isVerbose()){
109    std::cout << "Dimensions of FITS file: ";
110    int dim = 0;
111    std::cout << dimAxes[dim];
112    while(dim+1<numAxes) std::cout << "x" << dimAxes[++dim];
113    std::cout << std::endl;
114  }
115
116  delete [] dimAxes;
117
118  // Get the WCS information
119  this->head.defineWCS(fname, this->par);
120
121  if(!this->head.isWCS())
122    duchampWarning("Cube Reader","WCS is not good enough to be used.\n");
123
124  // Get the data array from the FITS file.
125  // Report the dimensions of the data array that was read (this can be
126  //   different to the full FITS array).
127  if(this->par.isVerbose()) std::cout << "Reading data ... ";
128  this->getFITSdata(fname);
129  if(this->par.isVerbose()){
130    std::cout << "Done. Data array has dimensions: ";
131    std::cout << this->axisDim[0];
132    if(this->axisDim[1]>1) std::cout  <<"x"<< this->axisDim[1];
133    if(this->axisDim[2]>1) std::cout  <<"x"<< this->axisDim[2];
134    std::cout << "\n";
135  }   
136
137  if(this->axisDim[2] == 1){
138    this->par.setMinChannels(0);
139  }
140
141  // Read the necessary header information, and copy some of it into the Param.
142  this->head.readHeaderInfo(fname, this->par);
143
144  return SUCCESS;
145
146}
147
Note: See TracBrowser for help on using the repository browser.