source: tags/release-1.1.12/src/FitsIO/DuchampBeam.cc

Last change on this file was 795, checked in by MatthewWhiting, 13 years ago

Getting the integrated flux units right - needed to reorder the way we called certain functions, so that we didn't set the integrated flux units too early.

File size: 4.1 KB
Line 
1#include <iostream>
2#include <string.h>
3#include <duchamp/duchamp.hh>
4#include <duchamp/FitsIO/DuchampBeam.hh>
5#include <duchamp/FitsIO/Beam.hh>
6#include <duchamp/param.hh>
7#include <sstream>
8
9namespace duchamp
10{
11
12  DuchampBeam::DuchampBeam():
13    Beam()
14  {
15    this->itsOrigin = EMPTY;
16  }
17
18  DuchampBeam::DuchampBeam(float maj, float min, float pa):
19    Beam(maj,min,pa)
20  {
21    this->itsOrigin  = EMPTY;
22  }
23
24  DuchampBeam::DuchampBeam(const Beam &b):
25    Beam(b)
26  {
27    this->itsOrigin = EMPTY;
28  }
29
30  DuchampBeam::DuchampBeam(const DuchampBeam &b):
31    Beam(b)
32  {
33    operator=(b);
34  }
35
36  DuchampBeam& DuchampBeam::operator=(const DuchampBeam &b)
37  {
38    ((Beam &) *this) = b;
39    this->itsOrigin = b.itsOrigin;
40    return *this;
41  }
42   
43  void DuchampBeam::define(float maj, float min, float pa, BEAM_ORIGIN origin)
44  {
45    this->Beam::define(maj,min,pa);
46    this->itsOrigin = origin;
47  }
48 
49  void DuchampBeam::setFWHM(float fwhm, BEAM_ORIGIN origin)
50  {
51    this->Beam::setFWHM(fwhm);
52    this->itsOrigin = origin;
53  }
54
55  void DuchampBeam::setArea(float area, BEAM_ORIGIN origin)
56  {
57    this->Beam::setArea(area);
58    this->itsOrigin = origin;
59  }
60
61  void DuchampBeam::empty()
62  {
63    this->itsMaj = this->itsMin = this->itsPA = this->itsArea = 0.;
64    this->itsOrigin = EMPTY;
65  }
66
67  std::string DuchampBeam::originString()
68  {
69    std::string output;
70    switch(this->itsOrigin)
71      {
72      case HEADER:
73        output="HEADER";
74        break;
75      case PARAM:
76        output="PARAM";
77        break;
78      case EMPTY:
79        output="EMPTY";
80        break;
81      default:
82        output="ERROR";
83        break;
84      }
85    return output;
86
87  }
88
89  void DuchampBeam::define(Param &par, bool warn)
90  {
91    std::string paramName;
92    bool doWarning=warn;
93    if(par.getBeamFWHM()>0.){
94      this->setFWHM(par.getBeamFWHM(),PARAM);
95      par.setBeamSize(this->itsArea);
96      paramName = "beamFWHM";
97      par.setBeamAsUsed(*this);
98    }
99    else if(par.getBeamSize()>0.){
100      this->setArea(par.getBeamSize(),PARAM);
101      paramName = "beamArea";
102      par.setBeamAsUsed(*this);
103    }
104    else{
105      this->empty();
106      doWarning = false;
107    }
108
109    if(doWarning){
110      std::stringstream errmsg;
111      errmsg << "Header beam keywords not present. Using parameter "<< paramName <<" to determine size of beam.\n";
112      duchampWarning("Beam definition",errmsg.str());
113    }
114  }
115   
116
117  void DuchampBeam::readFromFITS(fitsfile *fptr, Param &par, float pixelScale) // read from file a la headerIO.cc
118  {
119    char *comment = new char[80];
120    std::string keyword[3]={"BMAJ","BMIN","BPA"};
121    float bmaj,bmin,bpa;
122
123    // Read the Keywords. If they are present, read the
124    //   others, and calculate the beam size.
125    // If it is not, give warning and set beam size to nominal value.
126    int bstatus[3]={0,0,0};
127    fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj, comment, &bstatus[0]);
128    fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin, comment, &bstatus[1]);
129    fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &bpa, comment,  &bstatus[2]);
130
131    if(bstatus[0]||bstatus[1]||bstatus[2]){ // error
132      this->define(par);
133    }
134    else{ // all keywords present
135      this->define(bmaj/pixelScale, bmin/pixelScale, bpa, HEADER);
136      par.setBeamAsUsed(*this);
137    }
138  }
139
140 
141  void DuchampBeam::writeToFITS(fitsfile *fptr) // write to file, but only if itsOrigin==HEADER. Use cfitsio commands directly.
142  {
143   
144    if(this->itsOrigin == HEADER){
145      char *keyword = new char[FLEN_KEYWORD];
146      int status = 0;
147      strcpy(keyword,"BMAJ");
148      if (fits_update_key(fptr, TFLOAT, keyword, &this->itsMaj, NULL, &status)){
149        duchampError("Writing beam","Error writing beam info:");
150        fits_report_error(stderr, status);
151      }
152      status = 0;
153      strcpy(keyword,"BMIN");
154      if (fits_update_key(fptr, TFLOAT, keyword, &this->itsMin, NULL, &status)){
155        duchampError("Writing beam","Error writing beam info:");
156        fits_report_error(stderr, status);
157      }
158      status = 0;
159      strcpy(keyword,"BPA");
160      if (fits_update_key(fptr, TFLOAT, keyword, &this->itsPA, NULL, &status)){
161        duchampError("Writing beam","Error writing beam info:");
162        fits_report_error(stderr, status);
163      }
164    }
165  }
166
167
168
169}
170
Note: See TracBrowser for help on using the repository browser.