CCfits  2.6
PrimaryHDU.h
1 // Astrophysics Science Division,
2 // NASA/ Goddard Space Flight Center
3 // HEASARC
4 // http://heasarc.gsfc.nasa.gov
5 // e-mail: ccfits@legacy.gsfc.nasa.gov
6 //
7 // Original author: Ben Dorman
8 
9 #ifndef PRIMARYHDU_H
10 #define PRIMARYHDU_H 1
11 
12 // valarray
13 #include <valarray>
14 // PHDU
15 #include "PHDU.h"
16 // HDUCreator
17 #include "HDUCreator.h"
18 // Image
19 #include "Image.h"
20 // FITS
21 #include "FITS.h"
22 #include "CCfits.h"
23 #include <functional>
24 #include <numeric>
25 #include <memory>
26 
27 
28 namespace CCfits {
29 
30 
31 
32  template <typename T>
33  class PrimaryHDU : public PHDU //## Inherits: <unnamed>%394E6F870338
34  {
35 
36  public:
37  virtual PrimaryHDU<T> * clone (FITS* p) const;
38  // Read data reads the image if readFlag is true and
39  // optional keywords if supplied. Thus, with no arguments,
40  // readData() does nothing.
41  virtual void readData (bool readFlag = false, const std::vector<String>& keys = std::vector<String>());
42  const std::valarray<T>& image () const;
43  const std::valarray<T>& readImage (long first, long nElements, T* nullValue);
44  const std::valarray<T>& readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride,T* nullValue);
45  void writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue = 0);
46  void writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData);
47  virtual void zero (double value);
48  virtual void scale (double value);
49  virtual void suppressScaling(bool toggle = true);
50  virtual void resetImageRead ();
51 
52  // Additional Public Declarations
53 
54  protected:
55  // Constructor for new FITS objects, takes as arguments
56  // the required keywords for a primary HDU.
57  PrimaryHDU (FITS* p, const int bitpix, const int naxis, const std::vector<long>& naxes, const std::valarray<T>& data = std::valarray<T>());
58  // Custom constructor. Allows specification of data to be read and whether to read data at
59  // construction or wait until the image data are requested. The default is 'lazy initialization:'
60  // wait until asked.
61  PrimaryHDU (FITS* p, bool readFlag = false, const std::vector<String>& keys = std::vector<String>());
62 
63  // Additional Protected Declarations
64 
65  private:
66  PrimaryHDU(const PrimaryHDU< T > &right);
67  PrimaryHDU< T > & operator=(const PrimaryHDU< T > &right);
68 
69  virtual std::ostream & put (std::ostream &s) const;
70  const Image<T>& data () const;
71 
72  // Additional Private Declarations
73 
74  private: //## implementation
75  // Data Members for Associations
76  Image<T> m_data;
77 
78  // Additional Implementation Declarations
79  friend class HDUCreator;
80  friend class PHDU;
81  };
82 
83  // Parameterized Class CCfits::PrimaryHDU
84 
85  template <typename T>
86  inline std::ostream & PrimaryHDU<T>::put (std::ostream &s) const
87  {
88  s << "PrimaryHDU:: Simple? " << simple() << " Extend?: " << extend() <<
89  " Bitpix: " << bitpix() << " naxis = " << axes() << "\n";
90  s << "Axis Lengths: \n";
91 
92 
93 
94  for (int i=0; i < axes(); i++)
95  s << " axis[" << i << "] " << axis(i) << "\n";
96 
97  s << "\nNumber of keywords read: " << keyWord().size() << "\n";
98 
99  for (std::map<String,Keyword*>::const_iterator ki = keyWord().begin();
100  ki != keyWord().end(); ki++)
101  {
102  s << *((*ki).second) << std::endl;
103  }
104 
105 
106  s << " HISTORY: " << history() << '\n';
107  s << " COMMENTS: " <<comment() << '\n';
108  return s;
109  }
110 
111  template <typename T>
112  inline const Image<T>& PrimaryHDU<T>::data () const
113  {
114  return m_data;
115  }
116 
117  // Parameterized Class CCfits::PrimaryHDU
118 
119  template <typename T>
120  PrimaryHDU<T>::PrimaryHDU(const PrimaryHDU<T> &right)
121  : PHDU(right), m_data(right.m_data)
122  {
123  }
124 
125  template <typename T>
126  PrimaryHDU<T>::PrimaryHDU (FITS* p, const int bitpix, const int naxis, const std::vector<long>& naxes, const std::valarray<T>& data)
127  : PHDU(p,bitpix,naxis,naxes),m_data(data)
128  {
129  }
130 
131  template <typename T>
132  PrimaryHDU<T>::PrimaryHDU (FITS* p, bool readFlag, const std::vector<String>& keys)
133  : PHDU(p), m_data()
134  {
135  initRead();
136 
137  if (readFlag || keys.size()) readData(readFlag,keys);
138 
139  }
140 
141 
142  template <typename T>
143  PrimaryHDU<T> * PrimaryHDU<T>::clone (FITS* p) const
144  {
145  PrimaryHDU<T>* cloned = new PrimaryHDU<T>(*this);
146  cloned->parent() = p;
147  return cloned;
148  }
149 
150  template <typename T>
151  void PrimaryHDU<T>::readData (bool readFlag, const std::vector<String>& keys)
152  {
153 
154  // Default reading mode. Read everything if readFlag is true.
155  makeThisCurrent();
156 
157  if ( keys.size() > 0)
158  {
159  std::list<String> keyList(keys.size());
160  // keys is converted to a list so that any keys not in the header
161  // can be easily erased. internally an exception will be thrown,
162  // on a missing key, and its catch clause will print a message.
163  std::copy(keys.begin(),keys.end(),keyList.begin());
164  readKeywords(keyList);
165  }
166  // read the entire image, setting null values to the
167  // return value from FitsNullValue<T>. It would be easy to make the null value
168  // a user defined input, but that's not implemented yet.
169  if ( readFlag && (naxis() > 0) )
170  {
171  FITSUtil::FitsNullValue<T> null;
172  long init(1);
173  T nulValue(null());
174  long nelements(std::accumulate(naxes().begin(),naxes().end(),init,std::multiplies<long>() ));
175  readImage(1,nelements,&nulValue);
176 
177  }
178  }
179 
180  template <typename T>
181  const std::valarray<T>& PrimaryHDU<T>::image () const
182  {
183 
184  return m_data.image();
185  }
186 
187 
188  template <typename T>
189  const std::valarray<T>& PrimaryHDU<T>::readImage (long first, long nElements, T* nullValue)
190  {
191  makeThisCurrent();
192  return m_data.readImage(fitsPointer(),first,nElements,nullValue,naxes(),anynul());
193  }
194 
195  template <typename T>
196  const std::valarray<T>& PrimaryHDU<T>::readImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride,T* nullValue)
197  {
198  makeThisCurrent();
199  return m_data.readImage(fitsPointer(),firstVertex,lastVertex,stride,nullValue,naxes(),anynul());
200  }
201 
202  template <typename T>
203  void PrimaryHDU<T>::writeImage (long first, long nElements, const std::valarray<T>& inData, T* nullValue)
204  {
205  long newNaxisN=0;
206  m_data.writeImage(fitsPointer(),first,nElements,inData,naxes(),newNaxisN,nullValue);
207  if (newNaxisN)
208  naxes(naxes().size()-1,newNaxisN);
209  }
210 
211  template <typename T>
212  void PrimaryHDU<T>::writeImage (const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData)
213  {
214  long newNaxisN=0;
215  m_data.writeImage(fitsPointer(),firstVertex,lastVertex,stride,inData,naxes(),newNaxisN);
216  if (newNaxisN)
217  naxes(naxes().size()-1,newNaxisN);
218  }
219 
220  template <typename T>
221  void PrimaryHDU<T>::scale (double value)
222  {
223  PHDU::scale(value);
224  m_data.scalingHasChanged();
225  }
226 
227  template <typename T>
228  void PrimaryHDU<T>::zero (double value)
229  {
230  PHDU::zero(value);
231  m_data.scalingHasChanged();
232  }
233 
234  template <typename T>
235  void PrimaryHDU<T>::suppressScaling (bool toggle)
236  {
237  HDU::suppressScaling(toggle);
238  m_data.scalingHasChanged();
239  }
240 
241  template <typename T>
242  void PrimaryHDU<T>::resetImageRead()
243  {
244  m_data.resetRead();
245  }
246 
247 
248  // Additional Declarations
249 
250 } // namespace CCfits
251 
252 
253 #endif
virtual double zero() const
return the BZERO keyword value
Definition: PHDU.cxx:160
virtual double scale() const
return the BSCALE keyword value
Definition: PHDU.cxx:166
PHDU(const PHDU &right)
copy constructor
Definition: PHDU.cxx:20
Namespace enclosing all CCfits classes and globals definitions.
Definition: AsciiTable.cxx:26