mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
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 
22 namespace mx
23 {
24 namespace AO
25 {
26 namespace sim
27 {
28 
29 template<typename _realT>
30 struct 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 
106  realT wfPS()
107  {
108  return _wfPS;
109  }
110 
111  void wfPS(realT ps)
112  {
113  _wfPS = ps;
114  calcPixVal();
115  }
116 
117  realT F0Photons()
118  {
119  return _F0Photons;
120  }
121 
122  void F0Photons(realT f0)
123  {
124  _F0Photons = f0;
125  calcPixVal();
126  }
127 
128  realT starMag()
129  {
130  return _starMag;
131  }
132 
133  void starMag(realT sm)
134  {
135  _starMag = sm;
136  calcPixVal();
137  }
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 
152  /// Get the file names of the sequence assuming they are stored in a directory.
153  /**
154  * \param [in] dir is the directory to search for phase and amplitude files.
155  * \param [in] max [optional] specifies the maximum number of files to include the file name list.
156  *
157  * \retval 0 on success.
158  * \retval -1 on error.
159  */
160  int turbFnames(std::string dir, int max= 0);
161 
162  void openPhaseFrame(int fn);
163 
164  void nextPhase(wavefront<realT> &wf);
165 
166  void nextWF(wavefront<realT> & wf);
167 
168 
169 };
170 
171 template<typename realT>
172 int turbSequence<realT>::turbFnames(std::string dir, int max)
173 {
174  _phaseFnames = ioutils::getFileNames(dir, "", ".pha", ".fits");
175  _ampFnames = ioutils::getFileNames(dir, "", ".amp", ".fits");
176 
177 
178  if(_phaseFnames.size() == 0)
179  {
180  mxError("turbSequence", MXE_FILENOTFOUND, "No turbulent phase files found.");
181  return -1;
182  }
183 
184  if(_ampFnames.size() == 0)
185  {
186  std::cerr << "turbSequence: no turbulent amplitude files found.\n";
187 
188  _phaseOnly = true;
189  }
190 
191  if(max > 0)
192  {
193  _phaseFnames.erase(_phaseFnames.begin()+max, _phaseFnames.end());
194  if(_phaseOnly == false) _ampFnames.erase(_ampFnames.begin()+max, _ampFnames.end());
195  }
196 
197  _files = _phaseFnames.size();
198 
199  openPhaseFrame(0);
200 
201  _size = _currPhase.rows();
202  _nPerCube = _currPhase.planes();
203 
204  _frames = _nPerCube*_phaseFnames.size();
205 
206 }
207 
208 template<typename realT>
209 void turbSequence<realT>::openPhaseFrame(int fn)
210 {
211  fits::fitsFile<float> ff;
212 
213  std::cout << _phaseFnames[fn] << "\n";
214 
215  ff.read(_phaseFnames[fn], _currPhase);
216  ff.close();
217 
218  if(_phaseOnly == false)
219  {
220  ff.read(_ampFnames[fn], _currAmp);
221  ff.close();
222  }
223 
224  _currFileNo = fn;
225  _currFrameNo = 0;
226 }
227 
228 template<typename realT>
229 void turbSequence<realT>::nextPhase(wavefront<realT> &wf)
230 {
231 
232  int Npix = _pupil->sum();
233 
234  if(_currFrameNo > _nPerCube-1)
235  {
236  openPhaseFrame(_currFileNo + 1);
237  }
238 
239  wf.phase.resize(_pupil->rows(),_pupil->cols());
240  //mx::cutImage(wf.phase, _currPhase.image(_currFrameNo), _pupil->rows(), _pupil->cols());
241 
242  wf.phase = _currPhase.image(_currFrameNo).block( 0.5*( _currPhase.rows()-_pupil->rows()), 0.5*(_currPhase.cols() - _pupil->cols()), _pupil->rows(), _pupil->cols());
243 
244 
245  wf.phase = (wf.phase - (wf.phase* (*_pupil)).sum()/Npix)* (*_pupil);
246 
247  //wf.amplitude.Constant(_pixVal);
248 
249  wf.amplitude = (*_pupil)*_pixVal;
250 
251  ++_currFrameNo;
252 }
253 
254 template<typename realT>
255 void turbSequence<realT>::nextWF(wavefront<realT> & wf)
256 {
257 
258  int Npix = _pupil->sum();
259 
260 
261  if(_currFrameNo > _nPerCube-1)
262  {
263  openPhaseFrame(_currFileNo + 1);
264  }
265  wf.phase.resize(_pupil->rows(),_pupil->cols());
266  //mx::cutImage(wf.phase, _currPhase.image(_currFrameNo), _pupil->rows(), _pupil->cols());
267  wf.phase = _currPhase.image(_currFrameNo).block( 0.5*( _currPhase.rows()-_pupil->rows()), 0.5*(_currPhase.cols() - _pupil->cols()), _pupil->rows(), _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).block( 0.5*( _currAmp.rows()-_pupil->rows()), 0.5*(_currAmp.cols() - _pupil->cols()), _pupil->rows(), _pupil->cols());
280 
281  wf.amplitude *= _pixVal*(*_pupil);
282  }
283  _currFrameNo++;
284 }
285 
286 } //namespace sim
287 } //namespace AO
288 } //namespace mx
289 
290 #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,...
Definition: fileUtils.cpp:85
The mxlib c++ namespace.
Definition: mxError.hpp:107