mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
aoWFS.hpp
Go to the documentation of this file.
1 /** \file aoWFS.hpp
2  * \author Jared R. Males (jaredmales@gmail.com)
3  * \brief Definitions of various analytic wavefront sensors
4  * \ingroup mxAO_files
5  *
6  */
7 
8 #ifndef aoWFS_hpp
9 #define aoWFS_hpp
10 
11 #include "../../math/constants.hpp"
12 #include "../../mxError.hpp"
13 #include "../../mxException.hpp"
14 #include "../../improc/eigenImage.hpp"
15 #include "../../ioutils/fits/fitsFile.hpp"
16 
17 namespace mx
18 {
19 
20 namespace AO
21 {
22 
23 namespace analysis
24 {
25 
26 ///The ideal wavefront sensor sensitivity function.
27 /** Provides the \f$ \beta_p \f$ parameter of Guyon, 2005 \cite guyon_2005
28  * for the ideal WFS.
29  *
30  * This is the base class for all WFS.
31  *
32  * \tparam realT is the floating point type used for calculations
33  * \tparam iosT is an output stream type with operator << defined (default is std::ostream)
34  *
35  * \ingroup mxAOAnalytic
36  */
37 template<typename realT, typename iosT = std::ostream>
38 struct wfs
39 {
40  std::string _id;
41 
42  ///Constructor
43  /** Only sets the value of _id.
44  */
45  wfs()
46  {
47  _id = "Ideal WFS";
48  }
49 
50  ///Destructor
51  /** Declared virtual so more complicated derived types can be created.
52  */
53  virtual ~wfs()
54  {
55  return;
56  }
57 
58  ///Get the photon noise sensitivity at a spatial frequency.
59  /** The sensitivity of the ideal WFS is 1 at all k \cite guyon_2005.
60  *
61  * \returns the sensitivity to photon noise parameter
62  */
63  virtual realT beta_p( int m, ///< [in] the spatial frequency index for u (not used by this WFS)
64  int n, ///< [in] the spatial frequency index for v (not used by this WFS)
65  realT D, ///< [in] the telescope diameter (not used by this WFS)
66  realT d, ///< [in] the sub-ap spacing (not used by this WFS)
67  realT r0 ///< [in] Fried's parameter (not used by this WFS)
68  )
69  {
70  //Stuff a sock in the compiler's mouth:
71  static_cast<void>(m);
72  static_cast<void>(n);
73  static_cast<void>(D);
74  static_cast<void>(d);
75  static_cast<void>(r0);
76 
77  return static_cast<realT>(1);
78  }
79 
80  ///Get the read noise sensitivity at a spatial frequency.
81  /** Here we assume beta_r is the same as beta_p.
82  *
83  * \returns the sensitivity to read noise parameter
84  */
85  virtual realT beta_r( int m, ///< [in] the spatial frequency index for u (not used by this WFS)
86  int n, ///< [in] the spatial frequency index for v (not used by this WFS)
87  realT D, ///< [in] the telescope diameter (not used by this WFS)
88  realT d, ///< [in] the sub-ap spacing (not used by this WFS)
89  realT r0 ///< [in] Fried's parameter (not used by this WFS)
90  )
91  {
92  return beta_p(m,n,D,d,r0);
93  }
94 
95  ///Dump the details of the WFS to an io stream.
96  /** Is virtual so that derived types can add parameters.
97  */
98  virtual iosT & dumpWFS(iosT & ios)
99  {
100  ios << "# WFS Parameters:\n";
101  ios << "# ID = " << _id << '\n';
102 
103  return ios;
104  }
105 };
106 
107 ///The unmodulated pyramid wavefront sensor sensitivity function.
108 /** Provides the \f$ \beta_p \f$ parameter of Guyon, 2005 \cite guyon_2005
109  * for the unmodulated PyWFS.
110  *
111  * \tparam realT is the floating point type used for calculations
112  * \tparam iosT is an output stream type with operator << defined (default is std::ostream)
113  *
114  * \ingroup mxAOAnalytic
115  */
116 template<typename realT, typename iosT = std::ostream>
117 struct pywfsUnmod : public wfs<realT, iosT>
118 {
119  pywfsUnmod()
120  {
121  this->_id = "Unmodulated Pyramid";
122  }
123 
124  ///Get the photon noise sensitivity at a spatial frequency.
125  /** The sensitivity of the unmodulated PyWFS is \f$ \sqrt{2} \f$ at all k.
126  *
127  * \returns the sensitivity to photon noise parameter
128  */
129  virtual realT beta_p( int m, ///< [in] the spatial frequency index for u (not used by this WFS)
130  int n, ///< [in] the spatial frequency index for v (not used by this WFS)
131  realT D, ///< [in] the telescope diameter (not used by this WFS)
132  realT d, ///< [in] the sub-ap spacing (not used by this WFS)
133  realT r0 ///< [in] Fried's parameter (not used by this WFS)
134  )
135  {
136  //Stuff a sock in the compiler's mouth:
137  static_cast<void>(m);
138  static_cast<void>(n);
139  static_cast<void>(D);
140  static_cast<void>(d);
141  static_cast<void>(r0);
142 
143  return math::root_two<realT>();
144  }
145 
146  ///Get the read noise sensitivity at a spatial frequency.
147  /** Here we assume that beta_r is the same as beta_p.
148  *
149  * \returns the sensitivity to read noise parameter
150  */
151  virtual realT beta_r( int m, ///< [in] the spatial frequency index for u (not used by this WFS)
152  int n, ///< [in] the spatial frequency index for v (not used by this WFS)
153  realT D, ///< [in] the telescope diameter (not used by this WFS)
154  realT d, ///< [in] the sub-ap spacing (not used by this WFS)
155  realT r0 ///< [in] Fried's parameter (not used by this WFS)
156  )
157  {
158  return beta_p(m,n,D,d,r0);
159  }
160 
161 };
162 
163 
164 ///The asymptotic modulated pyramid wavefront sensor sensitivity function.
165 /** Provides the \f$ \beta_p \f$ parameter of Guyon, 2005 \cite guyon_2005
166  * for the modulated PyWFS in the asymptotic limit.
167  *
168  * \tparam realT is the floating point type used for calculations
169  * \tparam iosT is an output stream type with operator << defined (default is std::ostream)
170  *
171  * \ingroup mxAOAnalytic
172  */
173 template<typename realT, typename iosT = std::ostream>
174 struct pywfsModAsymptotic : public wfs<realT, iosT>
175 {
177  {
178  this->_id = "Asymptotic Modulated Pyramid";
179  }
180 
181  ///Get the photon sensitivity at a spatial frequency.
182  /** The photon noise sensitivity of the asymptotic modulated PyWFS is \f$ 2 \sqrt{2} \f$ at all k.
183  *
184  * \returns the sensitivity to photon noise parameter
185  */
186  virtual realT beta_p( int m, ///< [in] the spatial frequency index for u (not used by this WFS)
187  int n, ///< [in] the spatial frequency index for v (not used by this WFS)
188  realT D, ///< [in] the telescope diameter (not used by this WFS)
189  realT d, ///< [in] the sub-ap spacing (not used by this WFS)
190  realT r0 ///< [in] Fried's parameter (not used by this WFS)
191  )
192  {
193  //Stuff a sock in the compiler's mouth:
194  static_cast<void>(m);
195  static_cast<void>(n);
196  static_cast<void>(D);
197  static_cast<void>(d);
198  static_cast<void>(r0);
199 
200  return static_cast<realT>(2) * math::root_two<realT>();
201  }
202 
203  ///Get the read noise sensitivity at a spatial frequency.
204  /** Here we assume beta_r is the same as beta_p
205  *
206  * \returns the sensitivity to read noise parameter
207  */
208  virtual realT beta_r( int m, ///< [in] the spatial frequency index for u (not used by this WFS)
209  int n, ///< [in] the spatial frequency index for v (not used by this WFS)
210  realT D, ///< [in] the telescope diameter (not used by this WFS)
211  realT d, ///< [in] the sub-ap spacing (not used by this WFS)
212  realT r0 ///< [in] Fried's parameter (not used by this WFS)
213  )
214  {
215  return beta_p(m,m,D,d,r0);
216  }
217 
218 };
219 
220 ///The shack hartmann wavefront sensor sensitivity function.
221 /** Provides the \f$ \beta_p \f$ parameter of Guyon, 2005 \cite guyon_2005
222  * for the shack hartmann WFS.
223  *
224  * \tparam realT is the floating point type used for calculations
225  * \tparam iosT is an output stream type with operator << defined (default is std::ostream)
226  *
227  * \ingroup mxAOAnalytic
228  */
229 template<typename realT, typename iosT = std::ostream>
230 struct shwfs : public wfs<realT, iosT>
231 {
232  shwfs()
233  {
234  this->_id = "Shack Hartmann";
235  }
236 
237  ///Get the photon noise sensitivity at a spatial frequency.
238  /** The photon noise sensitivity of the shack hartmann WFS
239  *
240  * \returns the sensitivity to photon noise parameter
241  */
242  virtual realT beta_p( int m, ///< [in] the spatial frequency index for u
243  int n, ///< [in] the spatial frequency index for v
244  realT D, ///< [in] the telescope diameter
245  realT d, ///< [in] the sub-ap spacing
246  realT r0 ///< [in] Fried's parameter
247  )
248  {
249  realT k = sqrt(m*m + n*n)/D;
250 
251  return 1.48/(k*d)*sqrt(1 + pow(d/r0,2));
252 
253  }
254 
255  ///Get the read noise sensitivity at a spatial frequency.
256  /** Here we assume beta_r = beta_p
257  *
258  * \returns the sensitivity to read noise parameter
259  */
260  virtual realT beta_r( int m, ///< [in] the spatial frequency index for u
261  int n, ///< [in] the spatial frequency index for v
262  realT D, ///< [in] the telescope diameter
263  realT d, ///< [in] the sub-ap spacing
264  realT r0 ///< [in] Fried's parameter
265  )
266  {
267  return beta_p(m,n,D,d,r0);
268  }
269 
270 };
271 
272 ///The calculated WFS uses sensitivities provided by FITS files
273 /** Provides the \f$ \beta_p \f$ and \f$ \beta_r \f$ parameters
274  * from FITS files.
275  *
276  * \tparam realT is the floating point type used for calculations
277  * \tparam iosT is an output stream type with operator << defined (default is std::ostream)
278  *
279  * \ingroup mxAOAnalytic
280  */
281 template<typename realT, typename iosT = std::ostream>
282 struct calculatedWFS : public wfs<realT, iosT>
283 {
284  std::string m_beta_p_file;
285  std::string m_beta_r_file;
286  bool m_sensitivity {false};
287 
288  improc::eigenImage<realT> m_beta_p;
289  improc::eigenImage<realT> m_beta_r;
290 
291  calculatedWFS()
292  {
293  this->_id = "Calculated WFS";
294  }
295 
296  ///Get the photon noise sensitivity at a spatial frequency.
297  /** The photon noise sensitivity from the FITS file is returned.
298  *
299  * \returns the sensitivity to photon noise parameter
300  */
301  virtual realT beta_p( int m, ///< [in] the spatial frequency index for u
302  int n, ///< [in] the spatial frequency index for v
303  realT D, ///< [in] the telescope diameter
304  realT d, ///< [in] the sub-ap spacing
305  realT r0 ///< [in] Fried's parameter
306  )
307  {
308  if(m_beta_p.rows() == 0)
309  {
311  ff.read(m_beta_p, m_beta_p_file);
312 
313  if(m_sensitivity) m_beta_p = 1.0/m_beta_p;
314  }
315 
316  int midx = 0.5*(m_beta_p.rows()-1.0) + m;
317  int nidx = 0.5*(m_beta_p.cols()-1.0) + n;
318 
319  if(midx > m_beta_p.rows()-1 || midx < 0)
320  {
321  mxThrowException(err::sizeerr, "calculatedWFS::beta_p", "m index out of range");
322  }
323 
324  if(nidx > m_beta_p.cols()-1 || nidx < 0)
325  {
326  mxThrowException(err::sizeerr, "calculatedWFS::beta_p", "n index out of range");
327  }
328 
329  return m_beta_p(midx,nidx);
330 
331  }
332 
333  ///Get the read noise sensitivity at a spatial frequency.
334  /** The read noise sensitivity from the FITS file is returned.
335  *
336  * \returns the sensitivity to read noise parameter
337  */
338  virtual realT beta_r( int m, ///< [in] the spatial frequency index for u
339  int n, ///< [in] the spatial frequency index for v
340  realT D, ///< [in] the telescope diameter
341  realT d, ///< [in] the sub-ap spacing
342  realT r0 ///< [in] Fried's parameter
343  )
344  {
345  if(m_beta_r.rows() == 0)
346  {
348  ff.read(m_beta_r, m_beta_r_file);
349 
350  if(m_sensitivity) m_beta_r = 1.0/m_beta_r;
351  }
352 
353  int midx = 0.5*(m_beta_r.rows()-1.0) + m;
354  int nidx = 0.5*(m_beta_r.cols()-1.0) + n;
355 
356  if(midx > m_beta_r.rows()-1 || midx < 0)
357  {
358  mxThrowException(err::sizeerr, "calculatedWFS::beta_r", "m index out of range");
359  }
360 
361  if(nidx > m_beta_r.cols()-1 || nidx < 0)
362  {
363  mxThrowException(err::sizeerr, "calculatedWFS::beta_r", "n index out of range");
364  }
365 
366  return m_beta_r(midx,nidx);
367 
368  }
369 
370  ///Dump the details of the WFS to an io stream.
371  /** Is virtual so that derived types can add parameters.
372  */
373  virtual iosT & dumpWFS(iosT & ios)
374  {
376 
377  ios << "# beta_p = " << m_beta_p_file << '\n';
378  ios << "# beta_r = " << m_beta_r_file << '\n';
379  ios << "# sensitivity = " << std::boolalpha << m_sensitivity << "\n";
380 
381  return ios;
382  }
383 };
384 } //namespace analysis
385 } //namespace AO
386 } //namespace mx
387 
388 #endif // aoWFS_hpp
389 
mxException for a size error
Class to manage interactions with a FITS file.
Definition: fitsFile.hpp:54
int read(dataT *data)
Read the contents of the FITS file into an array.
Definition: fitsFile.hpp:828
constexpr units::realT k()
Boltzmann Constant.
Definition: constants.hpp:71
Eigen::Array< scalarT, -1, -1 > eigenImage
Definition of the eigenImage type, which is an alias for Eigen::Array.
Definition: eigenImage.hpp:44
The mxlib c++ namespace.
Definition: mxError.hpp:107
The calculated WFS uses sensitivities provided by FITS files.
Definition: aoWFS.hpp:283
virtual realT beta_p(int m, int n, realT D, realT d, realT r0)
Get the photon noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:301
virtual iosT & dumpWFS(iosT &ios)
Dump the details of the WFS to an io stream.
Definition: aoWFS.hpp:373
virtual realT beta_r(int m, int n, realT D, realT d, realT r0)
Get the read noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:338
The asymptotic modulated pyramid wavefront sensor sensitivity function.
Definition: aoWFS.hpp:175
virtual realT beta_p(int m, int n, realT D, realT d, realT r0)
Get the photon sensitivity at a spatial frequency.
Definition: aoWFS.hpp:186
virtual realT beta_r(int m, int n, realT D, realT d, realT r0)
Get the read noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:208
The unmodulated pyramid wavefront sensor sensitivity function.
Definition: aoWFS.hpp:118
virtual realT beta_p(int m, int n, realT D, realT d, realT r0)
Get the photon noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:129
virtual realT beta_r(int m, int n, realT D, realT d, realT r0)
Get the read noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:151
The shack hartmann wavefront sensor sensitivity function.
Definition: aoWFS.hpp:231
virtual realT beta_p(int m, int n, realT D, realT d, realT r0)
Get the photon noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:242
virtual realT beta_r(int m, int n, realT D, realT d, realT r0)
Get the read noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:260
The ideal wavefront sensor sensitivity function.
Definition: aoWFS.hpp:39
virtual realT beta_r(int m, int n, realT D, realT d, realT r0)
Get the read noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:85
virtual realT beta_p(int m, int n, realT D, realT d, realT r0)
Get the photon noise sensitivity at a spatial frequency.
Definition: aoWFS.hpp:63
virtual ~wfs()
Destructor.
Definition: aoWFS.hpp:53
wfs()
Constructor.
Definition: aoWFS.hpp:45
virtual iosT & dumpWFS(iosT &ios)
Dump the details of the WFS to an io stream.
Definition: aoWFS.hpp:98