ReFRACtor
output.h
Go to the documentation of this file.
1 #ifndef OUTPUT_H
2 #define OUTPUT_H
3 #include "printable.h"
4 #include <boost/function.hpp>
5 #include <blitz/array.h>
6 #include <boost/shared_ptr.hpp>
7 #include <boost/bind.hpp>
8 #include <boost/lambda/lambda.hpp>
9 #include <boost/any.hpp>
10 #include <map>
11 #include <string>
12 #include <stdint.h>
13 
14 namespace FullPhysics {
15 // Helper class to handle level padding.
16 // Don't have Doxygen document this class.
18 template<class T> class LevelPad {
19 public:
20  LevelPad(const boost::function<blitz::Array<T, 1> ()>& F, T Fill, int Fullsize)
21  : f(F), fill(Fill), fullsize(Fullsize) {}
22  blitz::Array<T, 1> operator()() const
23  {
24  blitz::Array<T, 1> res(fullsize);
25  res = fill;
26  blitz::Array<T, 1> t = f();
27  res(blitz::Range(0, t.rows() - 1)) = t;
28  return res;
29  }
30 private:
31  boost::function<blitz::Array<T, 1> ()> f;
32  T fill;
33  int fullsize;
34 };
35 
36 template<class T> class LevelPad2 {
37 public:
38  LevelPad2(const boost::function<blitz::Array<T, 2> ()>& F, T Fill, int Fullsize)
39  : f(F), fill(Fill), fullsize(Fullsize) {}
40  blitz::Array<T, 2> operator()() const
41  {
42  blitz::Array<T, 2> res(fullsize);
43  res = fill;
44  blitz::Array<T, 2> t = f();
45  res(blitz::Range(0, t.rows() - 1),
46  blitz::Range(0, t.rows() - 1)) = t;
47  return res;
48  }
49 private:
50  boost::function<blitz::Array<T, 2> ()> f;
51  T fill;
52  int fullsize;
53 };
55 
56 
57 
58 /****************************************************************/
102 class Output : public Printable<Output> {
103 public:
104  virtual ~Output() {};
105  virtual void print(std::ostream& Os) const { Os << "Output";}
106 
107 //-----------------------------------------------------------------------
111 //-----------------------------------------------------------------------
112 
113  template<class S, class T, int D> void
114  register_data_source(const std::string& Dataset_name,
115  blitz::Array<T, D> (S::*Pmf)() const,
116  const boost::shared_ptr<S>& Src)
117  {
118  boost::function<blitz::Array<T, D> ()> f = boost::bind(Pmf, Src);
119  register_data_source(Dataset_name, f);
120  }
121 
122 //-----------------------------------------------------------------------
127 //-----------------------------------------------------------------------
128 
129  template<class S, class T> void
130  register_data_source_pad(const std::string& Dataset_name,
131  blitz::Array<T, 1> (S::*Pmf)() const,
132  const boost::shared_ptr<S>& Src,
133  int Full_size,
134  T Fill_value)
135  {
136  boost::function<blitz::Array<T, 1> ()> f = boost::bind(Pmf, Src);
137  boost::function<blitz::Array<T, 1> ()> f2 =
138  LevelPad<T>(f, Fill_value, Full_size);
139  register_data_source(Dataset_name, f2);
140  }
141 
142  template<class S, class T> void
143  register_data_source_pad(const std::string& Dataset_name,
144  blitz::Array<T, 2> (S::*Pmf)() const,
145  const boost::shared_ptr<S>& Src,
146  int Full_size,
147  T Fill_value)
148  {
149  boost::function<blitz::Array<T, 2> ()> f = boost::bind(Pmf, Src);
150  boost::function<blitz::Array<T, 2> ()> f2 =
151  LevelPad2<T>(f, Fill_value, Full_size);
152  register_data_source(Dataset_name, f2);
153  }
154 
155 //-----------------------------------------------------------------------
159 //-----------------------------------------------------------------------
160 
161  template<class S, class T> void
162  register_data_source(const std::string& Dataset_name,
163  T (S::*Pmf)() const,
164  const boost::shared_ptr<S>& Src)
165  {
166  boost::function<T ()> f = boost::bind(Pmf, Src);
167  register_data_source(Dataset_name, f);
168  }
169 
170 //-----------------------------------------------------------------------
172 //-----------------------------------------------------------------------
173 
174  template<class T, int D> void
175  register_data_source(const std::string& Dataset_name,
176  const blitz::Array<T, D>& Val)
177  {
178  boost::function<blitz::Array<T, D> ()> f =
179  boost::lambda::constant(Val.copy());
180  register_data_source(Dataset_name, f);
181  }
182 
183 //-----------------------------------------------------------------------
185 //-----------------------------------------------------------------------
186 
187  template<class T> void
188  register_data_source(const std::string& Dataset_name,
189  const T& Val)
190  {
191  boost::function<T ()> f = boost::lambda::constant(Val);
192  register_data_source(Dataset_name, f);
193  }
194 
195 //-----------------------------------------------------------------------
197 //-----------------------------------------------------------------------
198 
199  template<class T>
200  void register_data_source(const std::string& Dataset_name,
201  boost::function<T> f)
202  { func[Dataset_name] = f;}
203  void write();
204  void write_best_attempt();
205 protected:
206 
207 //-----------------------------------------------------------------------
210 //-----------------------------------------------------------------------
211 
212  virtual void start_write() {};
213 
214 //-----------------------------------------------------------------------
217 //-----------------------------------------------------------------------
218 
219  virtual void end_write() {};
220 
221 //-----------------------------------------------------------------------
224 //-----------------------------------------------------------------------
225 
226  virtual void end_because_of_error() {}
227 
228 //-----------------------------------------------------------------------
230 //-----------------------------------------------------------------------
231  virtual void write_data(const std::string& Dataset_name, int Val) = 0;
232  virtual void write_data(const std::string& Dataset_name, int64_t Val) = 0;
233  virtual void write_data(const std::string& Dataset_name, double Val) = 0;
234  virtual void write_data(const std::string& Dataset_name,
235  const std::string& Val) = 0;
236  virtual void write_data(const std::string& Dataset_name,
237  const char* Val) = 0;
238  virtual void write_data(const std::string& Dataset_name,
239  const blitz::Array<int, 1>& Val) = 0;
240  virtual void write_data(const std::string& Dataset_name,
241  const blitz::Array<std::string, 1>& Val) = 0;
242  virtual void write_data(const std::string& Dataset_name,
243  const blitz::Array<const char*, 1>& Val) = 0;
244  virtual void write_data(const std::string& Dataset_name,
245  const blitz::Array<double, 1>& Val) = 0;
246  virtual void write_data(const std::string& Dataset_name,
247  const blitz::Array<int, 2>& Val) = 0;
248  virtual void write_data(const std::string& Dataset_name,
249  const blitz::Array<std::string, 2>& Val) = 0;
250  virtual void write_data(const std::string& Dataset_name,
251  const blitz::Array<const char*, 2>& Val) = 0;
252  virtual void write_data(const std::string& Dataset_name,
253  const blitz::Array<double, 2>& Val) = 0;
254  virtual void write_data(const std::string& Dataset_name,
255  const blitz::Array<int, 3>& Val) = 0;
256  virtual void write_data(const std::string& Dataset_name,
257  const blitz::Array<std::string, 3>& Val) = 0;
258  virtual void write_data(const std::string& Dataset_name,
259  const blitz::Array<const char*, 3>& Val) = 0;
260  virtual void write_data(const std::string& Dataset_name,
261  const blitz::Array<double, 3>& Val) = 0;
262 private:
263  template<class T>
264  void pass_to_write_t(const std::string& Dataset_name, boost::any* D);
265  void pass_to_write(const std::string& Dataset_name, boost::any* D);
266  std::map<std::string, boost::any> func;
267 };
268 
269 /****************************************************************/
276 template<class T> class OutputTemplate : public Output
277 {
278 public:
279  virtual ~OutputTemplate() {}
280 protected:
281  virtual void write_data(const std::string& Dataset_name, int Val)
282  { ((T*)this)->write_data_t(Dataset_name, Val); }
283  virtual void write_data(const std::string& Dataset_name, int64_t Val)
284  { ((T*)this)->write_data_t(Dataset_name, Val); }
285  virtual void write_data(const std::string& Dataset_name, double Val)
286  { ((T*)this)->write_data_t(Dataset_name, Val); }
287  virtual void write_data(const std::string& Dataset_name,
288  const std::string& Val)
289  { ((T*)this)->write_data_t(Dataset_name, Val); }
290  virtual void write_data(const std::string& Dataset_name,
291  const char* Val)
292  { ((T*)this)->write_data_t(Dataset_name, Val); }
293  virtual void write_data(const std::string& Dataset_name,
294  const blitz::Array<int, 1>& Val)
295  { ((T*)this)->write_data_t(Dataset_name, Val); }
296  virtual void write_data(const std::string& Dataset_name,
297  const blitz::Array<std::string, 1>& Val)
298  { ((T*)this)->write_data_t(Dataset_name, Val); }
299  virtual void write_data(const std::string& Dataset_name,
300  const blitz::Array<const char*, 1>& Val)
301  { ((T*)this)->write_data_t(Dataset_name, Val); }
302  virtual void write_data(const std::string& Dataset_name,
303  const blitz::Array<double, 1>& Val)
304  { ((T*)this)->write_data_t(Dataset_name, Val); }
305  virtual void write_data(const std::string& Dataset_name,
306  const blitz::Array<int, 2>& Val)
307  { ((T*)this)->write_data_t(Dataset_name, Val); }
308  virtual void write_data(const std::string& Dataset_name,
309  const blitz::Array<std::string, 2>& Val)
310  { ((T*)this)->write_data_t(Dataset_name, Val); }
311  virtual void write_data(const std::string& Dataset_name,
312  const blitz::Array<const char*, 2>& Val)
313  { ((T*)this)->write_data_t(Dataset_name, Val); }
314  virtual void write_data(const std::string& Dataset_name,
315  const blitz::Array<double, 2>& Val)
316  { ((T*)this)->write_data_t(Dataset_name, Val); }
317  virtual void write_data(const std::string& Dataset_name,
318  const blitz::Array<int, 3>& Val)
319  { ((T*)this)->write_data_t(Dataset_name, Val); }
320  virtual void write_data(const std::string& Dataset_name,
321  const blitz::Array<std::string, 3>& Val)
322  { ((T*)this)->write_data_t(Dataset_name, Val); }
323  virtual void write_data(const std::string& Dataset_name,
324  const blitz::Array<const char*, 3>& Val)
325  { ((T*)this)->write_data_t(Dataset_name, Val); }
326  virtual void write_data(const std::string& Dataset_name,
327  const blitz::Array<double, 3>& Val)
328  { ((T*)this)->write_data_t(Dataset_name, Val); }
329 };
330 
331 class OutputDouble : public GenericObject {
332 public:
334  virtual ~OutputDouble() {}
335  virtual double f() const = 0;
336 };
337 
338 class OutputBlitz1d : public GenericObject {
339 public:
341  virtual ~OutputBlitz1d() {}
342  virtual blitz::Array<double,1> f() const = 0;
343 };
344 
345 class OutputBlitz2d : public GenericObject {
346 public:
348  virtual ~OutputBlitz2d() {}
349  virtual blitz::Array<double,2> f() const = 0;
350 };
351 }
352 
353 #endif
void register_data_source(const std::string &Dataset_name, boost::function< T > f)
Most general way to enter a Data source.
Definition: output.h:200
void register_data_source(const std::string &Dataset_name, const T &Val)
Handling for when data is constant.
Definition: output.h:188
virtual void write_data(const std::string &Dataset_name, const blitz::Array< double, 3 > &Val)
Definition: output.h:326
virtual void write_data(const std::string &Dataset_name, const blitz::Array< double, 2 > &Val)
Definition: output.h:314
void register_data_source(const std::string &Dataset_name, T(S::*Pmf)() const, const boost::shared_ptr< S > &Src)
A common way to supply the metadata source is with a shared_ptr to an object, and a pointer to a memb...
Definition: output.h:162
void register_data_source_pad(const std::string &Dataset_name, blitz::Array< T, 1 >(S::*Pmf)() const, const boost::shared_ptr< S > &Src, int Full_size, T Fill_value)
There are several fields that are generated on the active levels only.
Definition: output.h:130
virtual void print(std::ostream &Os) const
Definition: output.h:105
virtual void write_data(const std::string &Dataset_name, int Val)
Various write functions that derived classes.
Definition: output.h:281
virtual void write_data(const std::string &Dataset_name, const blitz::Array< const char *, 3 > &Val)
Definition: output.h:323
This is a Mixin for classes that can be printed.
Definition: printable.h:24
virtual void write_data(const std::string &Dataset_name, const blitz::Array< const char *, 2 > &Val)
Definition: output.h:311
virtual void write_data(const std::string &Dataset_name, int64_t Val)
Definition: output.h:283
virtual void write_data(const std::string &Dataset_name, const blitz::Array< std::string, 3 > &Val)
Definition: output.h:320
void register_data_source(const std::string &Dataset_name, const blitz::Array< T, D > &Val)
Handling for when data is constant.
Definition: output.h:175
virtual ~OutputBlitz2d()
Definition: output.h:348
virtual void write_data(const std::string &Dataset_name, const blitz::Array< int, 3 > &Val)
Definition: output.h:317
virtual ~Output()
Definition: output.h:104
virtual ~OutputBlitz1d()
Definition: output.h:341
Most of the time the write_data needed by Output is best done through a template. ...
Definition: output.h:276
virtual void write_data(const std::string &Dataset_name, const char *Val)
Definition: output.h:290
virtual void write_data(const std::string &Dataset_name, const blitz::Array< const char *, 1 > &Val)
Definition: output.h:299
This is the base class for classes that write output for Level 2 Full Physics.
Definition: output.h:102
virtual void write_data(const std::string &Dataset_name, double Val)
Definition: output.h:285
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
virtual void write_data(const std::string &Dataset_name, const std::string &Val)
Definition: output.h:287
void register_data_source(const std::string &Dataset_name, blitz::Array< T, D >(S::*Pmf)() const, const boost::shared_ptr< S > &Src)
A common way to supply the metadata source is with a shared_ptr to an object, and a pointer to a memb...
Definition: output.h:114
virtual void end_because_of_error()
Notify when an error occurred.
Definition: output.h:226
For use with SWIG, it is useful to have a base class that everything can be cast to.
virtual void end_write()
Notify when we are done.
Definition: output.h:219
virtual void write_data(const std::string &Dataset_name, const blitz::Array< int, 2 > &Val)
Definition: output.h:305
virtual void start_write()
Notify derived class that we are starting to write data.
Definition: output.h:212
virtual void write_data(const std::string &Dataset_name, const blitz::Array< int, 1 > &Val)
Definition: output.h:293
virtual void write_data(const std::string &Dataset_name, const blitz::Array< double, 1 > &Val)
Definition: output.h:302
virtual ~OutputDouble()
Definition: output.h:334
void register_data_source_pad(const std::string &Dataset_name, blitz::Array< T, 2 >(S::*Pmf)() const, const boost::shared_ptr< S > &Src, int Full_size, T Fill_value)
Definition: output.h:143
virtual void write_data(const std::string &Dataset_name, const blitz::Array< std::string, 1 > &Val)
Definition: output.h:296
virtual void write_data(const std::string &Dataset_name, const blitz::Array< std::string, 2 > &Val)
Definition: output.h:308

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10