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