ReFRACtor
array_with_unit.h
Go to the documentation of this file.
1 #ifndef ARRAY_WITH_UNIT_H
2 #define ARRAY_WITH_UNIT_H
3 #include "printable.h"
4 #include "unit.h"
5 #include "double_with_unit.h"
6 #include <blitz/array.h>
7 
8 namespace FullPhysics {
9 /****************************************************************/
14 template<class T, int D>
15 class ArrayWithUnit : public Printable<ArrayWithUnit<T, D> >,
16  boost::field_operators<ArrayWithUnit<T, D> > {
17 public:
20  : value(V.value.copy()), units(V.units) { }
21  ArrayWithUnit(const blitz::Array<T, D>& Value, const Unit& Value_units)
22  : value(Value.copy()), units(Value_units) { }
23  ArrayWithUnit(const blitz::Array<T, D>& Value, const std::string& Value_units_name)
24  : value(Value.copy()), units(Value_units_name) { }
25 
26  blitz::Array<T, D> value;
28 
29 //-----------------------------------------------------------------------
31 //-----------------------------------------------------------------------
33  { value.reference(V.value.copy()); units = V.units; return *this;}
34 
35 //-----------------------------------------------------------------------
37 //-----------------------------------------------------------------------
39  { value *= V.value; units *= V.units; return *this;}
41  { value /= V.value; units /= V.units; return *this;}
43  { value += V.value * FullPhysics::conversion(V.units, units); return *this;}
45  { value -= V.value * FullPhysics::conversion(V.units, units); return *this;}
46 
47  DoubleWithUnit operator()(int i1) const
48  {return DoubleWithUnit(value(i1), units);}
49  DoubleWithUnit operator()(int i1, int i2) const
50  {return DoubleWithUnit(value(i1, i2), units);}
51  DoubleWithUnit operator()(int i1, int i2, int i3) const
52  {return DoubleWithUnit(value(i1, i2, i3), units);}
53  DoubleWithUnit operator()(int i1, int i2, int i3, int i4) const
54  {return DoubleWithUnit(value(i1, i2, i3, i4), units);}
55 
56  // Convenience wrappers so we don't need to enumerate
57  // all possible instances where say an int and Range
58  // are interchanged
59  template<class I>
61  {return ArrayWithUnit<T, D>(value(i1), units);}
62 
63  template<class I>
64  ArrayWithUnit<T, D> operator()(I i1, I i2) const
65  {return ArrayWithUnit<T, D>(value(i1, i2), units);}
66  template<class I>
67  ArrayWithUnit<T, D> operator()(I i1, I i2, I i3) const
68  {return ArrayWithUnit<T, D>(value(i1, i2, i3), units);}
69  template<class I>
70  ArrayWithUnit<T, D> operator()(I i1, I i2, I i3, I i4) const
71  {return ArrayWithUnit<T, D>(value(i1, i2, i3, i4), units);}
72 
73  template<class I, class J>
74  ArrayWithUnit<T, D-1> operator()(I i1, J i2) const
75  {return ArrayWithUnit<T, D-1>(value(i1, i2), units);}
76 
77  template<class I, class J>
78  ArrayWithUnit<T, D-1> operator()(J i1, I i2, I i3) const
79  {return ArrayWithUnit<T, D-1>(value(i1, i2, i3), units);}
80  template<class I, class J>
81  ArrayWithUnit<T, D-1> operator()(I i1, J i2, I i3) const
82  {return ArrayWithUnit<T, D-1>(value(i1, i2, i3), units);}
83  template<class I, class J>
84  ArrayWithUnit<T, D-1> operator()(I i1, I i2, J i3) const
85  {return ArrayWithUnit<T, D-1>(value(i1, i2, i3), units);}
86 
87  template<class I, class J>
88  ArrayWithUnit<T, D-2> operator()(I i1, J i2, J i3) const
89  {return ArrayWithUnit<T, D-1>(value(i1, i2, i3), units);}
90  template<class I, class J>
91  ArrayWithUnit<T, D-2> operator()(J i1, I i2, J i3) const
92  {return ArrayWithUnit<T, D-1>(value(i1, i2, i3), units);}
93  template<class I, class J>
94  ArrayWithUnit<T, D-2> operator()(J i1, J i2, I i3) const
95  {return ArrayWithUnit<T, D-1>(value(i1, i2, i3), units);}
96 
97 //-----------------------------------------------------------------------
99 //-----------------------------------------------------------------------
100 
102  {
103  ArrayWithUnit<T,D> res;
104  res.value.reference(blitz::Array<T, D>(value * FullPhysics::conversion(units, R)));
105  res.units = R;
106  return res;
107  }
108 
109 //-----------------------------------------------------------------------
115 //-----------------------------------------------------------------------
116 
118  {
119  ArrayWithUnit<T,D> res;
120  res.units = R;
121  if(units.is_commensurate(R))
122  res.value.reference(blitz::Array<T, D>(FullPhysics::conversion(units, R) * value));
123  else
124  res.value.reference(blitz::Array<T, D>(FullPhysics::conversion(1 / units, R) / value));
125  return res;
126  }
127 
128  int rows() const {return value.rows();}
129  int cols() const {return value.cols();}
130  int depth() const {return value.depth();}
131 
132  void print(std::ostream& Os) const {
133  Os << "ArrayWithUnit:\n"
134  << "Value: " << value << "\n"
135  << units << "\n";
136  }
137 
138 };
139 }
140 #endif
ArrayWithUnit< T, D > & operator*=(const ArrayWithUnit< T, D > &V)
Basic math operators for class.
ArrayWithUnit(const blitz::Array< T, D > &Value, const std::string &Value_units_name)
DoubleWithUnit operator()(int i1, int i2, int i3, int i4) const
DoubleWithUnit operator()(int i1) const
We frequently have a array of numbers with units associated with them.
ArrayWithUnit< T, D-1 > operator()(J i1, I i2, I i3) const
ArrayWithUnit< T, D > operator()(I i1, I i2) const
ArrayWithUnit< T, D-2 > operator()(J i1, I i2, J i3) const
double conversion(const Unit &Dunit_from, const Unit &Dunit_to)
Return conversion factor to go from one unit to another.
Definition: unit.cc:180
const Unit J("J", N *m)
ArrayWithUnit< T, D-1 > operator()(I i1, J i2) const
This is a Mixin for classes that can be printed.
Definition: printable.h:24
blitz::Array< T, D > value
ArrayWithUnit< T, D > operator()(I i1, I i2, I i3) const
ArrayWithUnit< T, D > & operator+=(const ArrayWithUnit< T, D > &V)
ArrayWithUnit< T, D > & operator-=(const ArrayWithUnit< T, D > &V)
ArrayWithUnit< T, D > operator()(I i1, I i2, I i3, I i4) const
DoubleWithUnit operator()(int i1, int i2) const
ArrayWithUnit< T, D-2 > operator()(I i1, J i2, J i3) const
We frequently have a double with units associated with it.
ArrayWithUnit< T, D-2 > operator()(J i1, J i2, I i3) const
DoubleWithUnit operator()(int i1, int i2, int i3) const
ArrayWithUnit< T, D > & operator/=(const ArrayWithUnit< T, D > &V)
ArrayWithUnit< T, D > operator()(I i1) const
ArrayWithUnit< T, D-1 > operator()(I i1, J i2, I i3) const
ArrayWithUnit< T, D > & operator=(const ArrayWithUnit< T, D > &V)
Assignment operator so internals are correctly set.
Libraries such as boost::units allow unit handling where we know the units at compile time...
Definition: unit.h:25
bool is_commensurate(const Unit &Units) const
Test if this set of units is commensurate with another set.
Definition: unit.h:68
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
void print(std::ostream &Os) const
ArrayWithUnit< T, D-1 > operator()(I i1, I i2, J i3) const
ArrayWithUnit(const blitz::Array< T, D > &Value, const Unit &Value_units)
ArrayWithUnit< T, D > convert_wave(const Unit &R) const
We often need to handle conversion from wavenumber to/from wavelength.
ArrayWithUnit< T, D > convert(const Unit &R) const
Convert to the given units.
ArrayWithUnit(const ArrayWithUnit< T, D > &V)

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