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

Last change on this file was 1069, checked in by MatthewWhiting, 12 years ago

Adding the licensing text to files that didn't have it.

File size: 5.8 KB
Line 
1// -----------------------------------------------------------------------
2// DuchampBeam.cc: Handling of a beam in the Duchamp context, with Param and
3//                 FITS I/O interfaces
4// -----------------------------------------------------------------------
5// Copyright (C) 2006, Matthew Whiting, ATNF
6//
7// This program is free software; you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// Duchamp is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15// for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Duchamp; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
20//
21// Correspondence concerning Duchamp may be directed to:
22//    Internet email: Matthew.Whiting [at] atnf.csiro.au
23//    Postal address: Dr. Matthew Whiting
24//                    Australia Telescope National Facility, CSIRO
25//                    PO Box 76
26//                    Epping NSW 1710
27//                    AUSTRALIA
28// -----------------------------------------------------------------------
29#include <iostream>
30#include <string.h>
31#include <duchamp/duchamp.hh>
32#include <duchamp/FitsIO/DuchampBeam.hh>
33#include <duchamp/FitsIO/Beam.hh>
34#include <duchamp/param.hh>
35#include <sstream>
36
37namespace duchamp
38{
39
40  DuchampBeam::DuchampBeam():
41    Beam()
42  {
43    this->itsOrigin = EMPTY;
44    this->itsPixelScale = 1.;
45  }
46
47  DuchampBeam::DuchampBeam(float maj, float min, float pa):
48    Beam(maj,min,pa)
49  {
50    this->itsOrigin  = EMPTY;
51    this->itsPixelScale = 1.;
52  }
53
54  DuchampBeam::DuchampBeam(const Beam &b):
55    Beam(b)
56  {
57    this->itsOrigin = EMPTY;
58    this->itsPixelScale = 1.;
59  }
60
61  DuchampBeam::DuchampBeam(const DuchampBeam &b):
62    Beam(b)
63  {
64    operator=(b);
65  }
66
67  DuchampBeam& DuchampBeam::operator=(const DuchampBeam &b)
68  {
69    ((Beam &) *this) = b;
70    this->itsOrigin = b.itsOrigin;             
71    this->itsPixelScale = b.itsPixelScale;
72    return *this;
73  }
74   
75  void DuchampBeam::define(float maj, float min, float pa, BEAM_ORIGIN origin)
76  {
77    this->Beam::define(maj,min,pa);
78    this->itsOrigin = origin;
79    this->itsPixelScale = 1.;
80  }
81 
82  void DuchampBeam::setFWHM(float fwhm, BEAM_ORIGIN origin)
83  {
84    this->Beam::setFWHM(fwhm);
85    this->itsOrigin = origin;
86    this->itsPixelScale = 1.;
87  }
88
89  void DuchampBeam::setArea(float area, BEAM_ORIGIN origin)
90  {
91    this->Beam::setArea(area);
92    this->itsOrigin = origin;
93  }
94
95  void DuchampBeam::empty()
96  {
97    this->itsMaj = this->itsMin = this->itsPA = this->itsArea = 0.;
98    this->itsOrigin = EMPTY;
99  }
100
101  std::string DuchampBeam::originString()
102  {
103    std::string output;
104    switch(this->itsOrigin)
105      {
106      case HEADER:
107        output="HEADER";
108        break;
109      case PARAM:
110        output="PARAM";
111        break;
112      case EMPTY:
113        output="EMPTY";
114        break;
115      default:
116        output="ERROR";
117        break;
118      }
119    return output;
120
121  }
122
123  void DuchampBeam::define(Param &par, bool warn)
124  {
125    std::string paramName;
126    bool doWarning=warn;
127    if(par.getBeamFWHM()>0.){
128      this->setFWHM(par.getBeamFWHM(),PARAM);
129      par.setBeamSize(this->itsArea);
130      paramName = "beamFWHM";
131      par.setBeamAsUsed(*this);
132    }
133    else if(par.getBeamSize()>0.){
134      this->setArea(par.getBeamSize(),PARAM);
135      paramName = "beamArea";
136      par.setBeamAsUsed(*this);
137    }
138    else{
139      this->empty();
140      doWarning = false;
141    }
142
143    if(doWarning){
144      DUCHAMPWARN("Beam definition","Header beam keywords not present. Using parameter "<< paramName <<" to determine size of beam.");
145    }
146  }
147   
148
149  void DuchampBeam::readFromFITS(fitsfile *fptr, Param &par, float pixelScale) // read from file a la headerIO.cc
150  {
151    char *comment = new char[80];
152    std::string keyword[3]={"BMAJ","BMIN","BPA"};
153    float bmaj,bmin,bpa;
154
155    // Read the Keywords. If they are present, read the
156    //   others, and calculate the beam size.
157    // If it is not, give warning and set beam size to nominal value.
158    int bstatus[3]={0,0,0};
159    fits_read_key(fptr, TFLOAT, (char *)keyword[0].c_str(), &bmaj, comment, &bstatus[0]);
160    fits_read_key(fptr, TFLOAT, (char *)keyword[1].c_str(), &bmin, comment, &bstatus[1]);
161    fits_read_key(fptr, TFLOAT, (char *)keyword[2].c_str(), &bpa, comment,  &bstatus[2]);
162
163    if(bstatus[0]||bstatus[1]||bstatus[2]){ // error
164      this->define(par);
165    }
166    else{ // all keywords present
167      this->define(bmaj/pixelScale, bmin/pixelScale, bpa, HEADER);
168      par.setBeamAsUsed(*this);
169      this->itsPixelScale = pixelScale;
170    }
171    delete [] comment;
172  }
173
174 
175  void DuchampBeam::writeToFITS(fitsfile *fptr) // write to file, but only if itsOrigin==HEADER. Use cfitsio commands directly.
176  {
177   
178    if(this->itsOrigin == HEADER){
179      char *keyword = new char[FLEN_KEYWORD];
180      int status = 0;
181      strcpy(keyword,"BMAJ");
182      float val=this->itsMaj*this->itsPixelScale;
183      if (fits_update_key(fptr, TFLOAT, keyword, &val, NULL, &status)){
184        DUCHAMPERROR("Writing beam","Error writing beam info:");
185        fits_report_error(stderr, status);
186      }
187      status = 0;
188      strcpy(keyword,"BMIN");
189      val=this->itsMin*this->itsPixelScale;
190      if (fits_update_key(fptr, TFLOAT, keyword, &val, NULL, &status)){
191        DUCHAMPERROR("Writing beam","Error writing beam info:");
192        fits_report_error(stderr, status);
193      }
194      status = 0;
195      strcpy(keyword,"BPA");
196      val=this->itsPA;
197      if (fits_update_key(fptr, TFLOAT, keyword, &val, NULL, &status)){
198        DUCHAMPERROR("Writing beam","Error writing beam info:");
199        fits_report_error(stderr, status);
200      }
201    }
202  }
203
204
205
206}
207
Note: See TracBrowser for help on using the repository browser.