mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
turbSequence.hpp
Go to the documentation of this file.
1/** \file turbSequence.hpp
2 * \author Jared R. Males (jaredmales@gmail.com)
3 * \brief Manage a turbulence sequence saved as FITS files.
4 * \ingroup mxAO_sim_files
5 *
6 */
7
8#ifndef turbSequence_hpp
9#define turbSequence_hpp
10
11#include <vector>
12#include <string>
13#include <iostream>
14
15#include <Eigen/Dense>
16
21
22#include "wavefront.hpp"
23
24namespace mx
25{
26namespace AO
27{
28namespace sim
29{
30
31template <typename _realT>
32struct turbSequence
33{
34 typedef _realT realT;
35 typedef Eigen::Array<realT, Eigen::Dynamic, Eigen::Dynamic> imageT;
36
37 std::vector<std::string> _phaseFnames;
38 std::vector<std::string> _ampFnames;
39
40 int _size{ 0 };
41
42 int _nPerCube{ 0 };
43
44 int _files{ 0 };
45
46 int _frames{ 0 };
47
48 int _currFileNo{ 0 };
49 int _currFrameNo{ 0 };
50
51 realT _wfPS{ 1 };
52 realT _F0Photons{ 2e11 };
53
54 realT _starMag{ 0 };
55
56 realT _pixVal;
57
58 imageT *_pupil;
59
60 bool _loopClosed;
61
62 improc::eigenCube<realT> _currPhase;
63 improc::eigenCube<realT> _currAmp;
64
65 bool _phaseOnly{ false };
66
67 turbSequence()
68 {
69 }
70
71 /// Return the size of the turbulence screens
72 /**
73 * \returns _size
74 */
75 int size()
76 {
77 return _size;
78 }
79
80 /// Return the number of frames per cube
81 /**
82 * \returns _nPerCube
83 */
84 int nPerCube()
85 {
86 return _nPerCube;
87 }
88
89 /// Return the number of files
90 /**
91 * \returns _files
92 */
93 int files()
94 {
95 return _files;
96 }
97
98 /// Return the number of frames in each file
99 /**
100 * \returns _frames
101 */
102 int frames()
103 {
104 return _frames;
105 }
106
107 realT wfPS()
108 {
109 return _wfPS;
110 }
111
112 void wfPS( realT ps )
113 {
114 _wfPS = ps;
115 calcPixVal();
116 }
117
118 realT F0Photons()
119 {
120 return _F0Photons;
121 }
122
123 void F0Photons( realT f0 )
124 {
125 _F0Photons = f0;
126 calcPixVal();
127 }
128
129 realT starMag()
130 {
131 return _starMag;
132 }
133
134 void starMag( realT sm )
135 {
136 _starMag = sm;
137 calcPixVal();
138 }
139
140 realT pixVal()
141 {
142 return _pixVal;
143 }
144
145 void calcPixVal()
146 {
147 // This is the square root of photons/pixel
148 _pixVal = sqrt( _F0Photons ) * pow( 10., -0.2 * _starMag ) * _wfPS; // pow(_wfPS,2);
149 }
150
151 /// Get the file names of the sequence assuming they are stored in a directory.
152 /**
153 * \param [in] dir is the directory to search for phase and amplitude files.
154 * \param [in] max [optional] specifies the maximum number of files to include the file name list.
155 *
156 * \retval 0 on success.
157 * \retval -1 on error.
158 */
159 int turbFnames( std::string dir, int max = 0 );
160
161 void openPhaseFrame( int fn );
162
163 void nextPhase( wavefront<realT> &wf );
164
165 void nextWF( wavefront<realT> &wf );
166};
167
168template <typename realT>
169int turbSequence<realT>::turbFnames( std::string dir, int max )
170{
171 ioutils::getFileNames( _phaseFnames, dir, "", ".pha", ".fits" );
172 ioutils::getFileNames( _ampFnames, dir, "", ".amp", ".fits" );
173
174 if( _phaseFnames.size() == 0 )
175 {
176 mxError( "turbSequence", MXE_FILENOTFOUND, "No turbulent phase files found." );
177 return -1;
178 }
179
180 if( _ampFnames.size() == 0 )
181 {
182 std::cerr << "turbSequence: no turbulent amplitude files found.\n";
183
184 _phaseOnly = true;
185 }
186
187 if( max > 0 )
188 {
189 const size_t maxFiles = static_cast<size_t>( max );
190
191 if( _phaseFnames.size() > maxFiles )
192 _phaseFnames.erase( _phaseFnames.begin() + maxFiles, _phaseFnames.end() );
193
194 if( _phaseOnly == false && _ampFnames.size() > maxFiles )
195 _ampFnames.erase( _ampFnames.begin() + maxFiles, _ampFnames.end() );
196 }
197
198 _files = _phaseFnames.size();
199
200 openPhaseFrame( 0 );
201
202 _size = _currPhase.rows();
203 _nPerCube = _currPhase.planes();
204
205 _frames = _nPerCube * _phaseFnames.size();
206
207 return 0;
208}
209
210template <typename realT>
211void turbSequence<realT>::openPhaseFrame( int fn )
212{
213 fits::fitsFile<float> ff;
214
215 std::cout << _phaseFnames[fn] << "\n";
216
217 ff.read( _currPhase, _phaseFnames[fn] );
218 ff.close();
219
220 if( _phaseOnly == false )
221 {
222 ff.read( _currAmp, _ampFnames[fn] );
223 ff.close();
224 }
225
226 _currFileNo = fn;
227 _currFrameNo = 0;
228}
229
230template <typename realT>
231void turbSequence<realT>::nextPhase( wavefront<realT> &wf )
232{
233
234 int Npix = _pupil->sum();
235
236 if( _currFrameNo > _nPerCube - 1 )
237 {
238 openPhaseFrame( _currFileNo + 1 );
239 }
240
241 wf.phase.resize( _pupil->rows(), _pupil->cols() );
242 // mx::cutImage(wf.phase, _currPhase.image(_currFrameNo), _pupil->rows(), _pupil->cols());
243
244 wf.phase = _currPhase.image( _currFrameNo )
245 .block( 0.5 * ( _currPhase.rows() - _pupil->rows() ),
246 0.5 * ( _currPhase.cols() - _pupil->cols() ),
247 _pupil->rows(),
248 _pupil->cols() );
249
250 wf.phase = ( wf.phase - ( wf.phase * ( *_pupil ) ).sum() / Npix ) * ( *_pupil );
251
252 // wf.amplitude.Constant(_pixVal);
253
254 wf.amplitude = ( *_pupil ) * _pixVal;
255
256 ++_currFrameNo;
257}
258
259template <typename realT>
260void turbSequence<realT>::nextWF( wavefront<realT> &wf )
261{
262
263 int Npix = _pupil->sum();
264
265 if( _currFrameNo > _nPerCube - 1 )
266 {
267 openPhaseFrame( _currFileNo + 1 );
268 }
269 wf.phase.resize( _pupil->rows(), _pupil->cols() );
270 // mx::cutImage(wf.phase, _currPhase.image(_currFrameNo), _pupil->rows(), _pupil->cols());
271 wf.phase = _currPhase.image( _currFrameNo )
272 .block( 0.5 * ( _currPhase.rows() - _pupil->rows() ),
273 0.5 * ( _currPhase.cols() - _pupil->cols() ),
274 _pupil->rows(),
275 _pupil->cols() );
276
277 wf.phase = ( wf.phase - ( wf.phase * ( *_pupil ) ).sum() / Npix ) * ( *_pupil );
278
279 if( _phaseOnly )
280 {
281 wf.amplitude = _pixVal * ( *_pupil );
282 }
283 else
284 {
285 wf.amplitude.resize( _pupil->rows(), _pupil->cols() );
286 // mx::cutImage(wf.amplitude, _currAmp.image(_currFrameNo), _pupil->rows(), _pupil->cols());
287 wf.amplitude = _currAmp.image( _currFrameNo )
288 .block( 0.5 * ( _currAmp.rows() - _pupil->rows() ),
289 0.5 * ( _currAmp.cols() - _pupil->cols() ),
290 _pupil->rows(),
291 _pupil->cols() );
292
293 wf.amplitude *= _pixVal * ( *_pupil );
294 }
295 _currFrameNo++;
296}
297
298} // namespace sim
299} // namespace AO
300} // namespace mx
301
302#endif // turbSequence_hpp
An image cube with an Eigen API.
Declarations of utilities for working with files.
Declares and defines a class to work with a FITS file.
#define mxError(esrc, ecode, expl)
This reports an mxlib specific error.
error_t getFileNames(std::vector< std::string > &fileNames, const std::string &directory, const std::string &prefix, const std::string &substr, const std::string &extension)
Get a list of file names from the specified directory, specifying a prefix, a substring to match,...
#define MXE_FILENOTFOUND
The file was not found.
Old version. Deprecated. Declares and defines the mxlib error reporting system.
The mxlib c++ namespace.
Definition mxlib.hpp:37
Structure containing the phase and amplitude of a wavefront.
Definition wavefront.hpp:24