casacore
Loading...
Searching...
No Matches
UnitVal.h
Go to the documentation of this file.
1// # UnitVal.h: defines the class describing a unit as a value and a dimension
2// # Copyright (C) 1994-1999,2000,2001,2004
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_UNITVAL_H
27#define CASA_UNITVAL_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Quanta/UnitDim.h>
32#include <casacore/casa/iosfwd.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations
37class String;
38class MUString;
39class UnitMap;
40class UMaps;
41
42//
43// <summary>
44// describes any valid unit as a factor and a dimenion of SI units
45// </summary>
46
47// <use visibility=export>
48
49// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tUnit">
50//
51// <prerequisite>
52// You should have at least a preliminary understanding of these classes:
53// <li> <linkto class=Unit>Unit</linkto>
54// </prerequisite>
55//
56// <etymology>
57// The class name derives from Units and gives a Value for a unit string
58// </etymology>
59//
60// <synopsis>
61// Physical units are strings consisting of one or more names of known
62// basic units, separated by '.' or ' ' (for multiplication) or '/' (for
63// division). Each name can optionally be preceded by a standard decimal
64// prefix, and/or followed by an (optionally signed) exponent.
65// Example:
66// km/s/(Mpc.s)2 is identical to km.s-1.Mpc-2.s-2
67//
68// See the <linkto class="Unit">Unit</linkto> class for more details.
69//
70// The UnitVal class maps a Unit string to a factor and a dimension of SI
71// defining units. E.g 'km/s' will be 1000 m.s-1 .
72// This class is only of interest if the manipulation of units is of
73// direct interest. Normally units will be used as Quantities and Quantums
74// (see the <linkto class=Quantum>Quantum</linkto> class) only,
75// i.e. as a physical quantity having a value and unit.
76// The class can also be used to check the validity of a unit string.
77//
78// <h3> Constructing UnitVal values </h3>
79//
80// UnitVal has the following constructors:
81// <ul>
82// <li> UnitVal() creates an (non-dimensioned) value 1.
83// <li> UnitVal(Double f) creates an (non-dimensioned) value f.
84// <li> UnitVal(Double f, String s) creates value f with unit s
85// <li> UnitVal(Double f, Int i) (private) creates value f with unit
86// at position i in dimension vector
87// </ul>
88//
89//
90// <h3> Manipulating unit values </h3>
91//
92// The UnitVal can be manipulated by the following operators and functions:
93// <ul>
94// <li> *, / generates combined UnitVal (e.g. 1 yd * 1 m = 0.9 m2)
95// <li> pow(Int) UnitVal(2,"km")->pow(2) = 4000000 m2
96// <li> root(Int) UnitVal(4000000,"m2")->root(2) = 2 km
97// <li> ==, != compares dimensions only: 1 yd == 5 ly: True
98// <li> getFac() will return the factor (Double)
99// <li> getDim() will return the dimensions (as UnitDim)
100// <li> << will output formatted unit (factor and dimension)
101// </ul>
102// To aid in checking the dimensionality of units, the following constants
103// are available:
104// <ul>
105// <li> UnitVal::NODIM
106// <li> UnitVal::UNDIM
107// <li> UnitVal::LENGTH
108// <li> UnitVal::MASS
109// <li> UnitVal::ANGLE
110// <li> UnitVal::SOLIDANGLE
111// <li> UnitVal::MOLAR
112// <li> UnitVal::CURRENT
113// <li> UnitVal::TIME
114// <li> UnitVal::TEMPERATURE
115// <li> UnitVal::INTENSITY
116// </ul>
117// <note role=tip>
118// Any other dimension can be checked by a combination. To check e.g. if
119// a unit is an acceleration, use: UnitVal::LENGTH/UnitVal::TIME/UnitVal::TIME
120// </note>
121//
122// <h3> Checking for valid unit strings </h3>
123//
124// The validity of a unit string can be checked by:
125// <srcblock>
126// // Check if the given String is a valid unit representation. The String
127// // will be cached in the unit maps for later reference if True
128// if ( UnitVal::check( "km/s/Mpc") ) {...}
129// </srcblock>
130//
131// </synopsis>
132//
133// <example>
134// An observation contains values in Janskys and in Westerbork Units. The
135// data can be combined by the following code:
136// <srcblock>
137// // The Fits tape gave JY, we check if defined, else we define them
138// if ( !UnitVal::check( "JY")) {
139// UnitMap::putUser("JY", UnitVal(1.,"Jy"), "FITS way to write Jy");
140// }
141// // The Fits tape gave WU (which are defined):
142// // We check if JY and WU are of the same dimension:
143// if (UnitVal(1.,"JY") != UnitVal(1.,"WU")) {
144// cerr << "Wrong dimension for either JY ( " <<
145// UnitVal(1.,"JY")->getDim() <<
146// ") or WU ( " <<
147// UnitVal(1.,"WU")->getDim() << ")" << endl;
148// }
149// // And output the relation between WU and JY, and the WU value:
150// cout << "1 WU = " << ( UnitVal(1.,"WU")/UnitVal(1.,"Jy") )->getVal() <<
151// " JY with 1 WU = " << UnitVal(1.,"WU") << endl;
152// </srcblock>
153// </example>
154
155// <motivation>
156// To separate the actual manipulation of unit values from the related
157// quantity
158// </motivation>
159//
160// <todo asof="941110">
161// <li> Some inlining (did not work first go)
162// </todo>
163
164class UnitVal {
165 // # Friends
166 // Multiply
167 friend UnitVal operator*(const UnitVal &in, const UnitVal &other);
168 // Divide
169 friend UnitVal operator/(const UnitVal &in, const UnitVal &other);
170 // Output a unit as a value and a string of SI defining units
171 friend ostream &operator<<(ostream &os, const UnitVal &ku);
172 // ensure that statics are initialized
174
175 public:
176 // # Constructors
177 // Construct an non-dimensioned value of 1
179 // Copy constructor
180 UnitVal(const UnitVal &other);
181
182 // Construct an non-dimensioned value
184
185 // Construct a fully dimensioned value
186 // <thrown>
187 // <li> AipsError
188 // </thrown>
189 UnitVal(Double factor, const String &s, UMaps * = 0);
190
191 // Construct a value with a single unit at position specified
193
194 // Destructor
196
197 // # Operators
198 // Assignment (copy semantics)
199 UnitVal &operator=(const UnitVal &other);
200
201 // Manipulate units
202 // <group name="manipulate">
203 // Multiply different units
204 UnitVal &operator*=(const UnitVal &other);
206 // Divide different units
207 UnitVal &operator/=(const UnitVal &other);
208
209 // Compare the dimensionality of different units
210 Bool operator==(const UnitVal &other) const;
211 Bool operator!=(const UnitVal &other) const;
212 // </group>
213
214 // # General member functions
215
216 // Raise a unit to an integer power
218
219 // Take integer root
220 // <thrown>
221 // <li> AipsError if power equals zero
222 // <li> AipsError if unit dimensions not multiple of power
223 // </thrown>
224 // <group>
225 UnitVal root(Int p) const;
226 UnitVal sqrt() const;
227 // </group>
228
229 // Get the data parts of the unit value definition
230 // <group name="get data">
231 // Get the dimensions in the defining SI units
232 const UnitDim &getDim() const;
234 // Get the factor of the unit (as compared to pure SI units)
235 Double getFac() const;
236 // </group>
237
238 // # Helper functions
239 // Convert a unit string to a proper unit value and cache the result. The
240 // function will return False if invalid string specified
241 static Bool check(const String &s);
242
243 // Convert a unit string to a proper unit value, cache the result and compare
244 // the dimension with the specified unit value. False if any of the steps fails
245 static Bool check(const String &s, UnitVal &loc);
246
247 // # Data members
248 // Some constants to check type of units
249 // <group name="unit kinds">
250 static UnitVal NODIM;
253 static UnitVal MASS;
254 static UnitVal TIME;
261 // </group>
262
263 protected:
264 // alternate initialization
266 void init(Double factor, Int pos);
267
268 private:
269 // # Data members
270 // The factor necessary to express the specified unit in the defining SI units
272
273 // The dimensions of the unit in the defining SI units
275
276 // Convert (and check) a unit string to an SI value representation
277 // <group>
278 static Bool create(const String &s, UnitVal &res, UMaps * = 0);
279 static Bool create(MUString &str, UnitVal &res, UMaps * = 0);
280 // </group>
281
282 // Determine sign of unit power (i.e. if '.' or '/')
283 static Int psign(MUString &str);
284
285 // Determine exponent of unit symbol
286 static Int power(MUString &str);
287
288 // Determine symbol name in unit string
289 static Bool field(MUString &str, UnitVal &res, UMaps *);
290};
291
292// # Inline Implementations
293
294// # Global functions
295// <summary> Global output function </summary>
296// <group name=output>
297// Output
298ostream &operator<<(ostream &os, const UnitVal &ku);
299// </group>
301// <summary> Static initialisation of UnitVal constants </summary>
302static class UnitVal_static_initializer {
303 public:
325} // namespace casacore
326
327#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
Define a struct containing the static data members.
Definition UnitMap.h:47
Static initialisation of UnitVal constants.
Definition UnitVal.h:304
friend UnitVal operator/(const UnitVal &in, const UnitVal &other)
Divide.
UnitVal sqrt() const
void init(Double factor, Int pos)
static UnitVal ANGLE
Definition UnitVal.h:260
static UnitVal TEMPERATURE
Definition UnitVal.h:257
static UnitVal UNDIM
Definition UnitVal.h:252
void init(Double factor)
alternate initialization
static UnitVal TIME
Definition UnitVal.h:255
const UnitDim & getDim() const
Get the data parts of the unit value definition .
UnitDim kindDim
The dimensions of the unit in the defining SI units.
Definition UnitVal.h:274
UnitVal(Double factor)
Construct an non-dimensioned value.
Definition UnitVal.h:183
static UnitVal NODIM
Some constants to check type of units .
Definition UnitVal.h:251
UnitVal(Double factor, const String &s, UMaps *=0)
Construct a fully dimensioned value.
friend UnitVal operator*(const UnitVal &in, const UnitVal &other)
Multiply.
friend ostream & operator<<(ostream &os, const UnitVal &ku)
Output a unit as a value and a string of SI defining units.
static UnitVal CURRENT
Definition UnitVal.h:256
UnitVal pow(Int p)
Raise a unit to an integer power.
static UnitVal MASS
Definition UnitVal.h:254
static Int power(MUString &str)
Determine exponent of unit symbol.
UnitVal()
Construct an non-dimensioned value of 1.
static UnitVal INTENSITY
Definition UnitVal.h:258
static UnitVal SOLIDANGLE
Definition UnitVal.h:261
Double getFac() const
Get the factor of the unit (as compared to pure SI units).
static Int psign(MUString &str)
Determine sign of unit power (i.e.
~UnitVal()
Destructor.
UnitVal & operator=(const UnitVal &other)
Assignment (copy semantics).
Double kindFactor
The factor necessary to express the specified unit in the defining SI units.
Definition UnitVal.h:271
friend class UnitVal_static_initializer
ensure that statics are initialized
Definition UnitVal.h:173
Bool operator==(const UnitVal &other) const
Compare the dimensionality of different units.
static UnitVal MOLAR
Definition UnitVal.h:259
UnitVal & operator*=(const UnitVal &other)
Manipulate units .
static Bool create(MUString &str, UnitVal &res, UMaps *=0)
UnitVal(const UnitVal &other)
Copy constructor.
static Bool check(const String &s)
Convert a unit string to a proper unit value and cache the result.
Bool operator!=(const UnitVal &other) const
UnitVal & operator/=(const UnitVal &other)
Divide different units.
static Bool check(const String &s, UnitVal &loc)
Convert a unit string to a proper unit value, cache the result and compare the dimension with the spe...
UnitVal root(Int p) const
Take integer root.
UnitVal(Double factor, Int pos)
Construct a value with a single unit at position specified.
Definition UnitVal.h:192
static UnitVal LENGTH
Definition UnitVal.h:253
static Bool create(const String &s, UnitVal &res, UMaps *=0)
Convert (and check) a unit string to an SI value representation.
static Bool field(MUString &str, UnitVal &res, UMaps *)
Determine symbol name in unit string.
static class casacore::UnitVal_static_initializer unitval_static_initializer
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
int * factor
Definition hdu.h:521
void init()
Other internal helper function(s).
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53
ostream & operator<<(ostream &os, const UnitVal &ku)
Output.