casacore
Loading...
Searching...
No Matches
Unit.h
Go to the documentation of this file.
1// # Unit.h: defines the Unit class
2// # Copyright (C) 1994-1996,1998-2000,2008
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_UNIT_H
27#define CASA_UNIT_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32#include <casacore/casa/Quanta/UnitVal.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward Declarations
37
38// <summary>
39// defines physical units
40// </summary>
41
42// <use visibility=export>
43
44// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tUnit">
45// </reviewed>
46//
47// # // <prerequisite>
48// # // </prerequisite>
49//
50// # // <etymology>
51// # // </etymology>
52//
53// <synopsis>
54// Physical units are basically used as quantities (see the
55// <linkto class=Quantum>Quantum</linkto> class), i.e.
56// a value and a dimension. The Unit class, or one of its subsidaries, will
57// in general not be called separately. The only reason to make use of these
58// classes is to generate additional 'tagged' units, i.e. units with a
59// special name, e.g. 'beam' for a telescope beam, or 'JY', a non-SI name
60// for Jy.
61// <h3> Units </h3>
62// A Unit is a String, and can be defined as either a Unit or a String
63// everywhere where a Unit is required.<br>
64// If defined as a Unit, the format of the string will be checked for a
65// legal definition and its value will be stored. If defined as a String,
66// the checking and determination of the value will be done each time
67// the string is encountered when a Unit is expected.<br>
68// <note role=tip> The use of a separate Unit variable will give a tremendous
69// speed increase, if compared to using the String representation in
70// e.g. <linkto class=Quantum>Quantity(5,"deg")</linkto> </note>
71// <note role=caution>
72// If using an explicit Unit variable (e.g. <src>Unit a("5Bolton/beam")</src>),
73// the check on the legality of the given string, and the conversion to the
74// cached canonical value in the variable 'a', is only done at creation time. This
75// means that if the user changes the value of a unit involved by the
76// <linkto class=UnitMap>putUser()</linkto> method, the unit using it should be
77// re-created (<src> a = Unit("5Bolton/beam");</src>).
78// </note>
79// A unit is a string of one or more fields separated
80// by 'space' or '.' or '*' (FITS option)
81// (to indicate multiply) or '/' (to indicate divide).
82// Multiple separators are acted upon (i.e. m//s == m.s).
83// Separators are acted upon left-to-right (i.e. m/s/A == (m/s)/A; use
84// () to indicate otherwise (e.g. m/(s/A))).
85//
86// A field is a name, or a unit enclosed in (), optionally followed by an,
87// optionally signed, decimal constant.
88// The decimal constant may be proceeded by '**' or '^' (FITS option)
89//
90// E.g. m.(m/s)-2 == m-1.s2)
91// <note role=tip>
92// A 'space' or '.' before an opening '(' can be omitted.
93// </note>
94// A name can consist of case-sensitive letters, '_', ''', ':', '"' and '0'
95// ('0' not as first character). Digits 1-9 are allowed if preceded with
96// an '_'.
97//
98// Possible legal names are e.g. <src>Jy, R0, R_1, "_2</src>.
99// <note role=tip>
100// <ul>
101// <li> <src>'</src> is used for arcmin
102// <li> <src>''</src> or <src>"</src> for arcsec
103// <li> : :: and ::: are used for h, min, s respectively
104// <li> _ is used for an undimensioned value (like beam or pixel)
105// </ul>
106// </note>
107// <note role=caution> The standard naming conventions for SI units are that they are
108// all in lowercase, unless derived from a person's name, when they start
109// with a capital letter. Notable exceptions are some of the astronomical
110// SI related units (e.g. AU).
111// </note>
112// A name can be preceded by a (standard) decimal prefix.
113//
114// A name must be defined in a Unit map before it can be used.
115//
116// All SI units and some customary units are part of the classes. User
117// defined names can be added by the UnitMap::putUser() function (see
118// the <linkto class=UnitMap>UnitMap</linkto> class).
119//
120// Example:
121// km/s/(Mpc.s)2 is identical to km.s-1.Mpc-2.s-2
122//
123// There are 5 name lists in the UnitMap, which are searched in reverse order:
124// <ol>
125// <li> Defining units: m, kg, s, A, K, cd, mol, rad, sr, _
126// <li> SI units: including a.o. g, Jy, AU
127// <li> Customary units: e.g. lb, hp, ly
128// <li> User defined units: defined by user (e.g. beam, KPH, KM)
129// <li> Cached units: for speed in operations
130// </ol>
131// All known names can be viewed by running the tUnit test program, or
132// using the MapUnit::list() routine.
133// They are also (at least the 1999/09/15 values) available in the
134// <linkto module="Quanta">Quanta module documentation</linkto>.
135// <note role=caution>
136// There is a difference between units without a dimension (non-dimensioned
137// I will call them), and undimensioned units. Non-dimensioned examples are
138// "", "%"; undimensioned examples: "beam", "pixel".
139// </note>
140//
141// <h3> Unit class </h3>
142// The Unit class is not directly based on the String class, but Strings and
143// Units are interchangeable in all Unit and Quantum related calls.
144// (But notice the earlier note on speed if using explicit Strings often.)
145//
146// To calculate with Units (or Strings representing units), use the
147// <linkto class=UnitVal>UnitVal</linkto> class. To use dimensioned values,
148// use the <linkto class=Quantum>Quantum</linkto> (cq Quantity) class.
149//
150// Using Unit i.s.o. String will give an immediate check of the legality
151// of the unit string.
152// In addition the UnitVal class contains a check facility to determine the
153// legality of a unit string:
154// <srcblock>
155// Bool UnitVal::check("string");
156// </srcblock>
157//
158// </synopsis>
159//
160// <example>
161// <srcblock>
162// #include <casacore/casa/Quanta.h>
163// // check if a string is a valid unit
164// if ( !UnitVal::check("Km") ) { cout << "Invalid unit string " << "Km" << endl; }
165// // define some units
166// String unit1="km/Mpc";
167// Unit unit2="uJy/Mpc";
168// // define your own unit name
169// UnitMap::putUser("my_univ", UnitVal( C::pi, unit2), "My universe param");
170// // use the units in model calculations
171// Quantity observed( 8.97, "Mmy_univ/a");
172// Quantity theory (3.8e-9, "mmy_univ/s");
173// if ( ( observed / theory) < 1.) { cout << "Eureka" << endl; }
174// </srcblock>
175// </example>
176//
177// <motivation>
178// Make basis for all dimensioned values the SI system of units
179// </motivation>
180//
181// <todo asof="941110">
182// <li> Some inlining (did not work first go)
183// <li> Look into possiblity of conversion routine from rad2 to sr
184// </todo>
185
186class Unit {
187 public:
188 // # Constructors
189 // Default empty string constructor
191 // Copy constructor
192 Unit(const Unit &other);
193 // String based constructors.
194 // <thrown>
195 // <li> AipsError if illegal unit string
196 // </thrown>
197 // <group name="constructor">
198 Unit(const std::string &other);
199 Unit(const Char *other);
200 explicit Unit(Char other);
201 Unit(const Char *other, Int len);
202 // </group>
203 // Destructor
204 ~Unit();
206 //* Operators
207 // Copy assignment
208 Unit &operator=(const Unit &other);
209 // Comparisons. Comparisons are done on the basis of the inherent units. I.e.
210 // <src>m/s</src> are identical to <src>AU/cy</src>.
211 // <group>
212 Bool operator==(const Unit &other) const;
213 Bool operator!=(const Unit &other) const;
214 // Fast check for "" units
215 Bool empty() const;
216 // </group>
217 // # Member functions
218 // Get the unit value
219 const UnitVal &getValue() const;
220 // Get the unit name
221 const String &getName() const;
222 // Set the unit value
223 void setValue(const UnitVal &in);
224 // Set the unit name
225 void setName(const String &in);
226
227 private:
228 // # Data
231
232 // # Member functions
233 // Check format of unit string
234 // <thrown>
235 // <li> AipsError
236 // </thrown>
237 void check();
238};
239
240// # Inline Implementations
241
242} // namespace casacore
243
244#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
Unit()
Default empty string constructor.
void check()
Check format of unit string.
Unit(const Char *other)
Unit(const Unit &other)
Copy constructor.
Unit(const std::string &other)
String based constructors.
Bool empty() const
Fast check for "" units.
~Unit()
Destructor.
Bool operator==(const Unit &other) const
Comparisons.
void setName(const String &in)
Set the unit name.
Bool operator!=(const Unit &other) const
Unit(const Char *other, Int len)
const UnitVal & getValue() const
Get the unit value.
UnitVal uVal
Definition Unit.h:230
Unit & operator=(const Unit &other)
Unit(Char other)
String uName
Definition Unit.h:229
void setValue(const UnitVal &in)
Set the unit value.
const String & getName() const
Get the unit name.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
char Char
Definition aipstype.h:44