source: trunk/src/FitsIO/DuchampBeam.cc @ 793

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

Header files to make sure we know about strcpy

File size: 3.8 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  void DuchampBeam::define(Param &par, bool warn)
68  {
69    std::string paramName;
70    bool doWarning=warn;
71    if(par.getBeamFWHM()>0.){
72      this->setFWHM(par.getBeamFWHM(),PARAM);
73      par.setBeamSize(this->itsArea);
74      paramName = "beamFWHM";
75      par.setBeamAsUsed(*this);
76    }
77    else if(par.getBeamSize()>0.){
78      this->setArea(par.getBeamSize(),PARAM);
79      paramName = "beamArea";
80      par.setBeamAsUsed(*this);
81    }
82    else{
83      this->empty();
84      doWarning = false;
85    }
86
87    if(doWarning){
88      std::stringstream errmsg;
89      errmsg << "Header beam keywords not present. Using parameter "<< paramName <<" to determine size of beam.\n";
90      duchampWarning("Beam definition",errmsg.str());
91    }
92  }
93   
94
95  void DuchampBeam::readFromFITS(fitsfile *fptr, Param &par, float pixelScale) // read from file a la headerIO.cc
96  {
97    char *comment = new char[80];
98    std::string keyword[3]={"BMAJ","BMIN","BPA"};
99    float bmaj,bmin,bpa;
100
101    // Read the Keywords. If they are present, read the
102    //   others, and calculate the beam size.
103    // If it is not, give warning and set beam size to nominal value.
104    int bstatus[3]={0,0,0};
105    fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj, comment, &bstatus[0]);
106    fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin, comment, &bstatus[1]);
107    fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &bpa, comment,  &bstatus[2]);
108
109    if(bstatus[0]||bstatus[1]||bstatus[2]){ // error
110      this->define(par);
111    }
112    else{ // all keywords present
113      this->define(bmaj/pixelScale, bmin/pixelScale, bpa, HEADER);
114      par.setBeamSize(this->itsArea);
115    }
116
117  }
118
119 
120  void DuchampBeam::writeToFITS(fitsfile *fptr) // write to file, but only if itsOrigin==HEADER. Use cfitsio commands directly.
121  {
122   
123    if(this->itsOrigin == HEADER){
124      char *keyword = new char[FLEN_KEYWORD];
125      int status = 0;
126      strcpy(keyword,"BMAJ");
127      if (fits_update_key(fptr, TFLOAT, keyword, &this->itsMaj, NULL, &status)){
128        duchampError("Writing beam","Error writing beam info:");
129        fits_report_error(stderr, status);
130      }
131      status = 0;
132      strcpy(keyword,"BMIN");
133      if (fits_update_key(fptr, TFLOAT, keyword, &this->itsMin, NULL, &status)){
134        duchampError("Writing beam","Error writing beam info:");
135        fits_report_error(stderr, status);
136      }
137      status = 0;
138      strcpy(keyword,"BPA");
139      if (fits_update_key(fptr, TFLOAT, keyword, &this->itsPA, NULL, &status)){
140        duchampError("Writing beam","Error writing beam info:");
141        fits_report_error(stderr, status);
142      }
143    }
144  }
145
146
147
148}
149
Note: See TracBrowser for help on using the repository browser.