1 #ifndef __pywfsSlopeReconstructor_hpp__
2 #define __pywfsSlopeReconstructor_hpp__
4 #include <mx/improc/eigenCube.hpp>
14 struct pywfsSlopeReconstructorSpec
17 std::string basisName;
25 template<
typename _
floatT>
30 typedef _floatT floatT;
36 typedef Eigen::Array<floatT, -1, -1>
imageT;
39 typedef Eigen::Array<floatT, -1, -1>
rmatT;
41 typedef pywfsSlopeReconstructorSpec specT;
78 template<
typename AOSysT>
79 void initialize(AOSysT & AOSys, specT & spec)
81 std::string recMatrix = mx::AO::path::sys::cal::iMat( AOSys._sysName,
121 void maskFile(
const std::string & mf);
161 template<
typename measurementT,
typename wfsImageT>
165 template<
typename measurementT,
typename wfsImageT>
166 void reconstruct(measurementT & commandVect, wfsImageT & wfsImage);
175 void initializeRMat(
int nmodes, floatT calamp,
int detrows,
int detcols);
182 template<
typename measurementT>
185 template<
typename measurementT,
typename wfsImageT>
186 void accumulateRMat(
int i, measurementT &measureVec, wfsImageT & wfsImage);
194 void saveRImages(std::string fname);
201 template<
typename floatT>
205 _maskObscuration = 0;
212 template<
typename floatT>
218 template<
typename floatT>
227 template<
typename floatT>
230 return _maskObscuration;
233 template<
typename floatT>
236 _maskObscuration = mo;
241 template<
typename floatT>
247 template<
typename floatT>
255 template<
typename floatT>
261 template<
typename floatT>
267 template<
typename floatT>
273 template<
typename floatT>
279 template<
typename floatT>
286 template<
typename floatT>
292 template<
typename floatT>
302 template<
typename floatT>
305 improc::fitsFile<floatT> ff;
306 improc::fitsHeader head;
308 ff.read(fname, _recon, head);
310 _maskRadius = head[
"MASKRAD"].Value<floatT>();
311 _maskObscuration = head[
"MASKOBS"].Value<floatT>();
312 _maskFile = head[
"MASKFILE"].String();
314 if( _maskFile !=
"") _maskType = 1;
316 _calAmp = head[
"CALAMP"].Value<floatT>();
318 _nModes = head[
"NMODES"].Value<
int>();
320 _detRows = head[
"DETROWS"].Int();
321 _detCols = head[
"DETCOLS"].Int();
326 template<
typename floatT>
331 improc::fitsFile<floatT> ff;
333 std::cerr <<
"Loading Mask: " << _maskFile <<
"\n";
334 ff.read(_maskFile, _quadMask);
339 _quadMask.resize(0.5*_detRows/_binFact,0.5*_detCols/_binFact);
340 wfp::circularPupil( _quadMask, _maskObscuration,_maskRadius/_binFact);
343 _measurementSize = 2* _quadMask.sum();
347 improc::fitsFile<floatT> ff;
348 ff.write(
"quadMask.fits", _quadMask);
351 template<
typename floatT>
354 if(!_maskMade) calcMask();
355 return _measurementSize;
359 template<
typename floatT>
360 template<
typename measurementT,
typename wfsImageT>
363 if(!_maskMade) calcMask();
369 std::cout <<
"rebinning" <<
"\n";
370 _wfsImage.resize( wfsImage.image.rows()/_binFact, wfsImage.image.cols()/_binFact);
375 _wfsImage = wfsImage.image;
378 int nsz = _wfsImage.rows();
380 int nPix = _quadMask.sum();
382 std::vector<int> x(nPix), y(nPix);
385 for(
int i=0;i<_quadMask.rows(); ++i)
387 for(
int j=0;j<_quadMask.rows(); ++j)
389 if(_quadMask(i,j) == 1)
398 slopes.measurement.resize(1, 2.*nPix);
400 floatT I0, I1, I2, I3;
402 floatT norm = _wfsImage.sum();
404 for(
int i=0;i<nPix; ++i)
406 I0 = _wfsImage(x[i], y[i]);
407 I1 = _wfsImage(x[i] + 0.5*nsz, y[i]);
408 I2 = _wfsImage(x[i], y[i]+0.5*nsz);
409 I3 = _wfsImage(x[i]+0.5*nsz, y[i]+0.5*nsz);
413 slopes.measurement(0,i) =0;;
414 slopes.measurement(0,i+nPix) = 0;
418 slopes.measurement(0,i) = (I0 + I1 - I2 - I3)/norm;
419 slopes.measurement(0,i+nPix) = (I0 + I2 - I1 - I3)/norm;
435 template<
typename floatT>
436 template<
typename measurementT,
typename wfsImageT>
441 calcMeasurement(slopes, wfsImage);
443 commandVect.measurement = slopes.measurement.matrix()*_recon.matrix();
445 commandVect.iterNo = wfsImage.iterNo;
448 template<
typename floatT>
459 _rMat.resize(measurementSize(), nModes);
462 _rImages.resize(_detRows, _detCols, _nModes);
466 template<
typename floatT>
467 template<
typename measurementT>
470 _rMat.col(i) = measureVec.measurement.row(0);
473 template<
typename floatT>
474 template<
typename measurementT,
typename wfsImageT>
477 accumulateRMat(i, measureVec);
479 _rImages.image(i) = wfsImage.image;
482 template<
typename floatT>
485 improc::fitsFile<floatT> ff;
486 improc::fitsHeader head;
490 head.append(
"MASKFILE", maskFile(),
"Name of mask file");
494 head.append(
"MASKRAD", maskRadius(),
"Mask radius, in pixels");
495 head.append(
"MASKOBS", maskObscuration(),
"Mask fractional central obscuration");
498 head.append(
"DETROWS", _detRows,
"WFS detector rows");
499 head.append(
"DETCOLS", _detCols,
"WFS detector cols");
500 head.append(
"CALAMP", _calAmp,
"DM Calibration amplitude");
501 head.append(
"NMODES", _nModes,
"Number of modes included in the response matrix.");
504 ff.write(fname, _rMat, head);
507 template<
typename floatT>
510 improc::fitsFile<floatT> ff;
511 improc::fitsHeader head;
515 head.append(
"MASKFILE", maskFile(),
"Name of mask file");
519 head.append(
"MASKRAD", maskRadius(),
"Mask radius, in pixels");
520 head.append(
"MASKOBS", maskObscuration(),
"Mask fractional central obscuration");
523 head.append(
"DETROWS", _detRows,
"WFS detector rows");
524 head.append(
"DETCOLS", _detCols,
"WFS detector cols");
525 head.append(
"CALAMP", _calAmp,
"DM Calibration amplitude");
526 head.append(
"NMODES", _nModes,
"Number of modes included in the response matrix.");
529 ff.write(fname, _rImages, head);
A Pyramid Wavefront Sensor slope reconstructor.
floatT calAmp()
Get the calibration amplitude used in response matrix acquisition (_calAmp)
int _detCols
The size of the WFS image, in columns.
void reconstruct(measurementT &commandVect, wfsImageT &wfsImage)
Reconstruct the wavefront from the input image, producing the modal amplitude vector.
int _detRows
The size of the WFS image, in rows.
int nModes()
Get the number of modes (_nModes)
floatT _maskRadius
0 is centrally obscured circle, 1 is supplied by fits files
std::string _maskFile
The name of the quadrant mask file.
void saveRMat(std::string fname)
Write the accumulated response matrix to disk.
floatT maskObscuration()
Get the quadrant mask central obscuration ratio (_maskObscuration)
Eigen::Array< floatT,-1,-1 > _recon
The reconstructor matrix.
Eigen::Array< floatT, -1,-1 > _quadMask
The quadrant mask.
int _nModes
The number of modes to be reconstructed.
int detCols()
Get the number of detector columns (_detCols)
int _measurementSize
The number of slopes in the measurement.
Eigen::Array< floatT, -1, -1 > imageT
The type of the measurement (i.e. the slope vector)
int measurementSize()
Return the size of the unbinned measurement.
bool _maskMade
Whether or not the mask has been made.
int _binFact
The binning to apply before reconstructing.
floatT _calAmp
The calibration amplitude used for response matrix acquisition.
void calcMeasurement(measurementT &slopes, wfsImageT &wfsImage)
Calculate the slope measurement.
void accumulateRMat(int i, measurementT &measureVec)
Accumalte the next measurement in the response matrix.
Eigen::Array< floatT, -1, -1 > rmatT
The type of the response matrix.
floatT _maskObscuration
The central obscuration of the quadrant mask.
int detRows()
Get the number of detector rows (_detRows)
void loadRecon(std::string fname)
Load the reconstrutor from the specified FITS file.
imageT _rMat
The response matrix.
floatT maskRadius()
Get the quadrant mask radius (_maskRadius)
std::string maskFile()
Get the quadrant mask file name (_maskFile)
void initializeRMat(int nmodes, floatT calamp, int detrows, int detcols)
Initialize the response matrix for acquisition.
pywfsSlopeReconstructor()
Default c'tor.
void calcMask()
Calculates the quadrant mask.
constexpr units::realT k()
Boltzmann Constant.