mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
directPhaseReconstructorD.hpp
1 #ifndef directPhaseReconstructor_hpp
2 #define directPhaseReconstructor_hpp
3 
4 #pragma GCC system_header
5 #include <Eigen/Dense>
6 
7 #include <mx/signalWindows.hpp>
8 
9 #ifdef DEBUG
10 #define BREAD_CRUMB std::cout << "DEBUG: " << __FILE__ << " " << __LINE__ << "\n";
11 #else
12 #define BREAD_CRUMB
13 #endif
14 
15 namespace mx
16 {
17 
18 
19 
20 namespace AO
21 {
22 
23 namespace sim
24 {
25 
26 struct slopeReconstructorSpec
27 {
28  std::string dmName;
29  std::string basisName;
30 
31  std::string rMatId;
32 };
33 
34 ///A Pyramid Wavefront Sensor slope reconstructor.
35 /** Calculates slopes, normalized by total flux in the image.
36  */
37 template<typename realT>
38 class directPhaseReconstructor
39 {
40 public:
41 
42  ///The type of the measurement (i.e. the slope vector)
43  //typedef Eigen::Array<realT,-1,-1> measurementT;
44 
45  ///The type of the WFS image
46  typedef Eigen::Array<realT, -1, -1> imageT;
47 
48  ///The type of the response matrix
49  typedef Eigen::Array<realT, -1, -1> rmatT;
50 
51  typedef slopeReconstructorSpec specT;
52 
53 protected:
54  Eigen::Array<realT,-1,-1> _recon; ///< The reconstructor matrix.
55 
56  imageT _rMat; ///<The response matrix
57  eigenCube<realT> _rImages;
58 
59  int _nModes {0}; ///<The number of modes to be reconstructed
60 
61  int _detRows {0}; ///<The size of the WFS image, in rows
62  int _detCols {0};///<The size of the WFS image, in columns
63 
64  int _measurementSize {0}; ///<The number of values in the measurement
65 
66 
67  //The mirror modes
68  mx::eigenCube<realT> *_modes {nullptr};
69 
70  imageT * _pupil {nullptr};
71 
72 
73  imageT _mask;
74 
75  realT norm {0};
76 
77  ds9_interface ds9i;
78 
79 public:
80  mx::eigenImaged _spectrum;
81 
82  imageT * _gains {nullptr};
83 
84 public:
85  ///Default c'tor
87 
88  template<typename AOSysT>
89  void initialize(AOSysT & AOSys, specT & spec)
90  {
91 
92  _modes = &AOSys.dm._infF;
93 
94  _nModes = _modes->planes();
95  _detRows = _modes->rows();
96  _detCols = _modes->cols();
97 
99 
100  _pupil = &AOSys._pupil;
101  _measurementSize = _pupil->sum();
102 
103  _mask.resize(_pupil->rows(), _pupil->cols());
104 
105 // tukey2d(realT *filt, int dim, realT N, realT alpha, realT xc, realT yc)
106  //mx::tukey2d(_mask.data(), _mask.rows(), (realT) _mask.rows(), (realT) 0.0, (realT) 0.5*(_mask.rows()-1), (realT) 0.5*(_mask.cols()-1));
107  _mask = *_pupil;
108 
109  mx::fitsFile<realT> ff;
110  ff.write("dprMask.fits", _mask);
111 
112  std::string recMatrix = mx::AO::path::sys::cal::iMat( AOSys._sysName,
113  spec.dmName,
114  AOSys._wfsName,
115  AOSys._pupilName,
116  spec.basisName,
117  spec.rMatId) ;
118  loadRecon(recMatrix);
119 
120  }
121 
122  template<typename AOSysT>
123  void linkSystem(AOSysT & AOSys);
124 
125  ///Get the calibration amplitude used in response matrix acquisition (_calAmp)
126  realT calAmp();
127 
128 
129  ///Set the calibration amplitude used in response matrix acquisition (_calAmp)
130  /**
131  * \param ca [in] the new calibration amplitude
132  */
133  void calAmp(realT ca);
134 
135  ///Get the number of modes (_nModes)
136  int nModes();
137 
138  ///Get the number of detector rows (_detRows)
139  int detRows();
140 
141  ///Set the number of detector rows (_detRows)
142  void detRows(int dr);
143 
144  ///Get the number of detector columns (_detCols)
145  int detCols();
146 
147  ///Set the number of detector columns (_detCols)
148  void detCols(int dc);
149 
150  ///Load the reconstrutor from the specified FITS file
151  /**
152  * \param fname is the name of the FITS file, including path
153  */
154  void loadRecon(std::string fname);
155 
156  ///Return the size of the unbinned measurement
158 
159 
160  ///Calculate the slope measurement
161  /**
162  * \param slopes [out] a (_measurementSize X 2) array of slopes
163  * \param wfsImage [in] the WFS image from which to measure the slopes
164  */
165  template<typename measurementT, typename wfsImageT>
166  void calcMeasurement(measurementT & slopes, wfsImageT & wfsImage);
167 
168  ///Reconstruct the wavefront from the input image, producing the modal amplitude vector
169  template<typename measurementT, typename wfsImageT>
170  void reconstruct(measurementT & commandVect, wfsImageT & wfsImage);
171 
172  ///Initialize the response matrix for acquisition
173  /**
174  * \param nmodes the number of modes
175  * \param calamp the calibration amplitude
176  * \param detrows the number of detector rows
177  * \param detcols the number of detector columns
178  */
179  void initializeRMat(int nmodes, realT calamp, int detrows,int detcols);
180 
181  ///Accumalte the next measurement in the response matrix
182  /**
183  * \param i the measurement index
184  * \param measureVec is the i-th measurement vector
185  */
186  template<typename measurementT>
187  void accumulateRMat(int i, measurementT &measureVec);
188 
189  template<typename measurementT, typename wfsImageT>
190  void accumulateRMat(int i, measurementT &measureVec, wfsImageT & wfsImage);
191 
192  ///Write the accumulated response matrix to disk
193  /**
194  * \param fname the name, including path, of the response matrix
195  */
196  void saveRMat(std::string fname);
197 
198  void saveRImages(std::string fname);
199 
200 };
201 
202 
203 template<typename realT>
205 {
206 
207 }
208 
209 template<typename realT>
210 template<typename AOSysT>
211 void directPhaseReconstructor<realT>::linkSystem(AOSysT & AOSys)
212 {
213  _modes = &AOSys.dm._modes;
214 
215  _nModes = _modes->planes();
216  _detRows = _modes->rows();
217  _detCols = _modes->cols();
218 
219  _measurementSize = _detRows*_detCols;
220 
221  _pupil = &AOSys._pupil;
222  _measurementSize = _pupil->sum();
223 
224  _mask.resize(_pupil->rows(), _pupil->cols());
225  mx::tukey2d(_mask.data(), _mask.rows(), (realT) _mask.rows(), 0.0, 0.5*(_mask.rows()-1), 0.5*(_mask.cols()-1));
226 
227  mx::fitsFile<realT> ff;
228  ff.write("dprMask.fits", _mask);
229 
230 }
231 
232 template<typename realT>
234 {
235  return 0.5*780.0e-9/two_pi<realT>();
236 }
237 
238 template<typename realT>
240 {
241  return;
242 }
243 
244 template<typename realT>
246 {
247  return _nModes;
248 }
249 
250 template<typename realT>
252 {
253  return _detRows;
254 }
255 
256 template<typename realT>
258 {
259  return _detCols;
260 }
261 
262 template<typename realT>
264 {
265  mx::fitsFile<realT> ff;
266  mx::fitsHeader head;
267 
268  ff.read(fname, _recon, head);
269 
270 
271 }
272 
273 template<typename realT>
275 {
276  return _measurementSize;
277 }
278 
279 template<typename realT>
280 template<typename measurementT, typename wfsImageT>
281 void directPhaseReconstructor<realT>::calcMeasurement(measurementT & slopes, wfsImageT & wfsImage)
282 {
283 
284  slopes.measurement.resize(1, _measurementSize );
285 
286  int k = 0;
287 
288  for(int i=0; i< wfsImage.image.rows(); ++i)
289  {
290  for(int j=0; j < wfsImage.image.cols(); ++j)
291  {
292  if( (*_pupil)(i,j) == 0) continue;
293 
294  slopes.measurement(0, k) = wfsImage.image(i,j);
295  ++k;
296  }
297  }
298 
299 }
300 
301 template<typename realT>
302 template<typename measurementT, typename wfsImageT>
303 void directPhaseReconstructor<realT>::reconstruct(measurementT & commandVect, wfsImageT & wfsImage)
304 {
305 
306  BREAD_CRUMB;
307 
308  measurementT slopes;
309 
310  calcMeasurement(slopes, wfsImage);
311 
312  BREAD_CRUMB;
313  commandVect.measurement = slopes.measurement.matrix()*_recon.matrix();
314 
315  BREAD_CRUMB;
316  commandVect.iterNo = wfsImage.iterNo;
317 }
318 
319 template<typename realT>
320 void directPhaseReconstructor<realT>::initializeRMat(int nModes, realT calamp, int detRows, int detCols)
321 {
322  _nModes = nModes;
323 
324  _detRows = detRows;
325  _detCols = detCols;
326 
327  _rMat.resize(measurementSize(), nModes);
328  _rMat.setZero();
329 
330  _rImages.resize(_detRows, _detCols, _nModes);
331 }
332 
333 template<typename realT>
334 template<typename measurementT>
335 void directPhaseReconstructor<realT>::accumulateRMat(int i, measurementT &measureVec)
336 {
337  _rMat.col(i) = measureVec.measurement.row(0);
338 }
339 
340 template<typename realT>
341 template<typename measurementT, typename wfsImageT>
342 void directPhaseReconstructor<realT>::accumulateRMat(int i, measurementT &measureVec, wfsImageT & wfsImage)
343 {
344  BREAD_CRUMB;
345  accumulateRMat(i, measureVec);
346 
347  BREAD_CRUMB;
348 
349  _rImages.image(i) = wfsImage.image;
350 }
351 
352 template<typename realT>
353 void directPhaseReconstructor<realT>::saveRMat(std::string fname)
354 {
355  mx::fitsFile<realT> ff;
356  mx::fitsHeader head;
357 
358  ff.write(fname, _rMat);
359 
360 }
361 
362 template<typename realT>
363 void directPhaseReconstructor<realT>::saveRImages(std::string fname)
364 {
365  mx::fitsFile<realT> ff;
366 
367  ff.write(fname, _rImages);
368 }
369 
370 
371 } //namespace sim
372 } //namespace AO
373 } //namespace mx
374 
375 #endif //directPhaseReconstructor_hpp
376 
void saveRMat(std::string fname)
Write the accumulated response matrix to disk.
int detRows()
Get the number of detector rows (_detRows)
void loadRecon(const std::string &fname)
Load the reconstrutor from the specified FITS file.
int _nModes
The number of modes to be reconstructed.
void detCols(int dc)
Set the number of detector columns (_detCols)
Eigen::Array< realT, -1, -1 > imageT
The type of the WFS image.
int measurementSize()
Return the size of the unbinned measurement.
void calcMeasurement(measurementT &slopes, wfsImageT &wfsImage)
Calculate the slope measurement.
int _detRows
The size of the WFS image, in rows.
void initializeRMat(int nmodes, realT calamp, int detrows, int detcols)
Initialize the response matrix for acquisition.
realT calAmp()
Get the calibration amplitude used in response matrix acquisition (_calAmp)
directPhaseReconstructorSpec specT
The specificaion type.
void accumulateRMat(int i, measurementT &measureVec)
Accumalte the next measurement in the response matrix.
void reconstruct(measurementT &commandVect, wfsImageT &wfsImage)
Reconstruct the wavefront from the input image, producing the modal amplitude vector.
int _measurementSize
The number of values in the measurement.
int _detCols
The size of the WFS image, in columns.
Eigen::Array< realT,-1,-1 > _recon
The reconstructor matrix.
Eigen::Array< realT, -1, -1 > rmatT
The type of the response matrix.
int detCols()
Get the number of detector columns (_detCols)
void calAmp(realT ca)
Set the calibration amplitude used in response matrix acquisition (_calAmp)
int nModes()
Get the number of modes (_nModes)
void detRows(int dr)
Set the number of detector rows (_detRows)
constexpr units::realT k()
Boltzmann Constant.
Definition: constants.hpp:71
void tukey2d(realT *filt, int rows, int cols, realT D, realT alpha, realT xc, realT yc)
Create a 2-D Tukey window.
The mxlib c++ namespace.
Definition: mxError.hpp:107