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