source: trunk/src/FitsIO/headerIO.cc @ 201

Last change on this file since 201 was 192, checked in by Matthew Whiting, 18 years ago

A large commit based around a few changes:

  • Implemented the new SNRpeak column, defining it in columns.cc and printing it out in outputDetection.cc.
  • Corrected a bug in the subsection parsing that miscalculated the pixel offset when "*" was given as an axis range.
  • setupFDR now calculates the flux threshold so this can be reported.
  • The object flags now distinguish between spatial edge and spectral edge locations.
  • Other minor changes for clarity's sake.
File size: 6.7 KB
RevLine 
[159]1#include <iostream>
2#include <string>
3#include <string.h>
4#include <wcsunits.h>
5#define WCSLIB_GETWCSTAB // define this so that we don't try and redefine
6                         //  wtbarr (this is a problem when using gcc v.4+
7#include <fitsio.h>
[160]8#include <longnam.h>
[159]9#include <math.h>
10#include <duchamp.hh>
11#include <Cubes/cubes.hh>
12
13int FitsHeader::readHeaderInfo(string fname, Param &par)
14{
[161]15  /**
16   * FitsHeader::readHeaderInfo
17   *  A simple front-end function to the three header access
18   *   functions defined below.
19   *
20   */
[159]21
[161]22  int returnValue = SUCCESS;
23
[192]24  if(this->readBUNIT(fname)==FAILURE) returnValue=FAILURE;
[159]25 
[192]26  if(this->readBLANKinfo(fname, par)==FAILURE) returnValue=FAILURE;
[159]27 
[192]28  if(this->readBeamInfo(fname, par)==FAILURE) returnValue=FAILURE;
[159]29
[161]30  return returnValue;
[159]31}
32
33
34//////////////////////////////////////////////////
35
[192]36int FitsHeader::readBUNIT(string fname)
[159]37{
38  /**
[192]39   *  FitsHeader::readBUNIT(string fname)
[159]40   *   Read the BUNIT header keyword, to store the units
41   *    of brightness (flux).
42   */
43  fitsfile *fptr;         
[160]44  char *comment = new char[FLEN_COMMENT];
45  char *unit = new char[FLEN_VALUE];
[159]46  int returnStatus = 0, status = 0;
[161]47
48  // Open the FITS file
[159]49  status = 0;
50  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
51    fits_report_error(stderr, status);
52    return FAILURE;
53  }
[161]54
55  // Read the BUNIT keyword, and translate to standard unit format if needs be
[159]56  fits_read_key(fptr, TSTRING, "BUNIT", unit, comment, &returnStatus);
57  if (returnStatus){
[192]58    duchampWarning("readBUNIT","Error reading BUNIT keyword: ");
[159]59    fits_report_error(stderr, returnStatus);
60  }
61  else{
62    wcsutrn(0,unit);
63    this->fluxUnits = unit;
64  }
[161]65
66  // Close the FITS file
[159]67  status = 0;
68  fits_close_file(fptr, &status);
69  if (status){
[192]70    duchampWarning("readBUNIT","Error closing file: ");
[159]71    fits_report_error(stderr, status);
72  }
73
74  delete [] comment, unit;
75
76  return returnStatus;
77}
78
79//////////////////////////////////////////////////
80
[192]81int FitsHeader::readBLANKinfo(string fname, Param &par)
[159]82{
83  /**
[192]84   *   FitsHeader::readBLANKinfo(string fname, Param &par)
[159]85   *    Reading in the Blank pixel value keywords.
86   *    If the BLANK keyword is in the header, use that and store the relevant
[172]87   *     values. Also copy them into the parameter set.
[159]88   *    If not, use the default value (either the default from param.cc or
89   *     from the param file) and assume simple values for the keywords
90   *        --> the scale keyword is the same as the blank value,
91   *            the blank keyword (which is an int) is 1 and
92   *            the bzero (offset) is 0.
93   */
94  int returnStatus = 0, status = 0;
[161]95  if(par.getFlagBlankPix()){  // Only do this if we want the blank pix value
96
[160]97    fitsfile *fptr;         
98    char *comment = new char[FLEN_COMMENT];
99    int blank;
100    float bscale, bzero;
[161]101   
102    // Open the FITS file.
[160]103    if( fits_open_file(&fptr,fname.c_str(),READONLY,&status) ){
104      fits_report_error(stderr, status);
105      return FAILURE;
106    }
[161]107
108    // Read the BLANK keyword.
109    //  If this isn't present, then report the error, and set to
110    //     the nominal values given in Param
111    //  If it is, read the other two necessary keywords, and then set
112    //     the values accordingly.
[160]113    if(fits_read_key(fptr, TINT, "BLANK", &blank, comment, &returnStatus)){
[192]114      duchampWarning("readBLANKinfo","Error reading BLANK keyword: ");
[160]115      fits_report_error(stderr, returnStatus);
116      std::stringstream errmsg;
117      errmsg << "Using default BLANK value ("
118             << par.getBlankPixVal() << ").\n";
[192]119      duchampWarning("readBLANKinfo", errmsg.str());
[172]120      this->blankKeyword  = 1;
121      this->bscaleKeyword = par.getBlankPixVal();
122      this->bzeroKeyword  = 0;
123      par.setBlankKeyword(1);
124      par.setBzeroKeyword(0);
[160]125      par.setFlagUsingBlank(true);
126    }
127    else{
128      status = 0;
129      fits_read_key(fptr, TFLOAT, "BZERO", &bzero, comment, &status);
130      status = 0;
131      fits_read_key(fptr, TFLOAT, "BSCALE", &bscale, NULL, &status);
[172]132      this->blankKeyword  = blank;
133      this->bscaleKeyword = bscale;
134      this->bzeroKeyword  = bzero;
135      par.setBlankKeyword(blank);
136      par.setBscaleKeyword(bscale);
137      par.setBzeroKeyword(bzero);
138      par.setBlankPixVal( blank*bscale + bzero );
[160]139    }
[159]140 
[161]141    // Close the FITS file.
[160]142    status = 0;
143    fits_close_file(fptr, &status);
144    if (status){
[192]145      duchampWarning("readBLANKinfo","Error closing file: ");
[160]146      fits_report_error(stderr, status);
147    }
148 
149    delete [] comment;
150
[159]151  }
[160]152
[159]153  return returnStatus;
154
155}
156
157//////////////////////////////////////////////////
158
[192]159int FitsHeader::readBeamInfo(string fname, Param &par)
[159]160{
161  /**
[192]162   *  FitsHeader::readBeamInfo(string fname, Param &par)
[159]163   *   Reading in the beam parameters from the header.
164   *   Use these, plus the basic WCS parameters to calculate the size of
[172]165   *    the beam in pixels. Copy the beam size into the parameter set.
166   *   If information not present in FITS header, use the parameter
167   *    set to define the beam size.
[159]168   */
169  char *comment = new char[80];
[181]170  string keyword[4]={"BMAJ","BMIN","CDELT1","CDELT2"};
[159]171  float bmaj,bmin,cdelt1,cdelt2;
[181]172  int status[6];
[159]173  fitsfile *fptr;         
[161]174
[181]175  for(int i=0;i<6;i++) status[i] = 0;
176
[161]177  // Open the FITS file
[181]178  if( fits_open_file(&fptr,fname.c_str(),READONLY,&status[0]) ){
179    fits_report_error(stderr, status[0]);
[159]180    return FAILURE;
181  }
[161]182
183  // Read the Keywords -- first look for BMAJ. If it is present, read the
184  //   others, and calculate the beam size.
185  // If it is not, give warning and set beam size to nominal value.
[181]186  fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj,
187                comment, &status[1]);
188  fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin,
189                comment, &status[2]);
190  fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &cdelt1,
191                comment, &status[3]);
192  fits_read_key(fptr, TFLOAT, (char *)keyword[3].c_str(), &cdelt2,
193                comment, &status[4]);
194
195  if(status[1]||status[2]||status[3]||status[4]){ // error
196    stringstream errmsg;
197    errmsg << "Header keywords not present: ";
198    for(int i=0;i<4;i++) if(status[i+1]) errmsg<<keyword[i]<<" ";
199    errmsg << "\nUsing parameter beamSize to determine size of beam.\n";
[192]200    duchampWarning("readBeamInfo",errmsg.str());
[181]201    this->setBeamSize(par.getBeamSize());
202    par.setFlagUsingBeam(true);
203  }
204  else{ // all keywords present
[159]205    this->setBeamSize( M_PI * (bmaj/2.) * (bmin/2.) / fabs(cdelt1*cdelt2) );
206    this->setBmajKeyword(bmaj);
207    this->setBminKeyword(bmin);
[172]208    par.setBeamSize(this->beamSize);
[159]209  }
[181]210   
[161]211  // Close the FITS file.
[181]212  fits_close_file(fptr, &status[5]);
213  if (status[5]){
[192]214    duchampWarning("readBeamInfo","Error closing file: ");
[181]215    fits_report_error(stderr, status[5]);
[159]216  }
217
218  delete [] comment;
219
[181]220  int returnStatus = status[0];
221  for(int i=1;i<6;i++) if(status[i]>returnStatus) returnStatus=status[i];
222
[159]223  return returnStatus;
224}
Note: See TracBrowser for help on using the repository browser.