Loading...
Searching...
No Matches
UnitVal.h
Go to the documentation of this file.
//# UnitVal.h: defines the class describing a unit as a value and a dimension
//# Copyright (C) 1994-1999,2000,2001,2004
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//# Internet email: casa-feedback@nrao.edu.
//# Postal address: AIPS++ Project Office
//# National Radio Astronomy Observatory
//# 520 Edgemont Road
//# Charlottesville, VA 22903-2475 USA
#ifndef CASA_UNITVAL_H
#define CASA_UNITVAL_H
//# Includes
#include <casacore/casa/aips.h>
#include <casacore/casa/Quanta/UnitDim.h>
#include <casacore/casa/iosfwd.h>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
//# Forward Declarations
class String;
class MUString;
class UnitMap;
class UMaps;
//
// <summary>
// describes any valid unit as a factor and a dimenion of SI units
// </summary>
// <use visibility=export>
// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tUnit">
//
// <prerequisite>
// You should have at least a preliminary understanding of these classes:
// <li> <linkto class=Unit>Unit</linkto>
// </prerequisite>
//
// <etymology>
// The class name derives from Units and gives a Value for a unit string
// </etymology>
//
// <synopsis>
// Physical units are strings consisting of one or more names of known
// basic units, separated by '.' or ' ' (for multiplication) or '/' (for
// division). Each name can optionally be preceded by a standard decimal
// prefix, and/or followed by an (optionally signed) exponent.
// Example:
// km/s/(Mpc.s)2 is identical to km.s-1.Mpc-2.s-2
//
// See the <linkto class="Unit">Unit</linkto> class for more details.
//
// The UnitVal class maps a Unit string to a factor and a dimension of SI
// defining units. E.g 'km/s' will be 1000 m.s-1 .
// This class is only of interest if the manipulation of units is of
// direct interest. Normally units will be used as Quantities and Quantums
// (see the <linkto class=Quantum>Quantum</linkto> class) only,
// i.e. as a physical quantity having a value and unit.
// The class can also be used to check the validity of a unit string.
//
// <h3> Constructing UnitVal values </h3>
//
// UnitVal has the following constructors:
// <ul>
// <li> UnitVal() creates an (non-dimensioned) value 1.
// <li> UnitVal(Double f) creates an (non-dimensioned) value f.
// <li> UnitVal(Double f, String s) creates value f with unit s
// <li> UnitVal(Double f, Int i) (private) creates value f with unit
// at position i in dimension vector
// </ul>
//
//
// <h3> Manipulating unit values </h3>
//
// The UnitVal can be manipulated by the following operators and functions:
// <ul>
// <li> *, / generates combined UnitVal (e.g. 1 yd * 1 m = 0.9 m2)
// <li> pow(Int) UnitVal(2,"km")->pow(2) = 4000000 m2
// <li> root(Int) UnitVal(4000000,"m2")->root(2) = 2 km
// <li> ==, != compares dimensions only: 1 yd == 5 ly: True
// <li> getFac() will return the factor (Double)
// <li> getDim() will return the dimensions (as UnitDim)
// <li> << will output formatted unit (factor and dimension)
// </ul>
// To aid in checking the dimensionality of units, the following constants
// are available:
// <ul>
// <li> UnitVal::NODIM
// <li> UnitVal::UNDIM
// <li> UnitVal::LENGTH
// <li> UnitVal::MASS
// <li> UnitVal::ANGLE
// <li> UnitVal::SOLIDANGLE
// <li> UnitVal::MOLAR
// <li> UnitVal::CURRENT
// <li> UnitVal::TIME
// <li> UnitVal::TEMPERATURE
// <li> UnitVal::INTENSITY
// </ul>
// <note role=tip>
// Any other dimension can be checked by a combination. To check e.g. if
// a unit is an acceleration, use: UnitVal::LENGTH/UnitVal::TIME/UnitVal::TIME
// </note>
//
// <h3> Checking for valid unit strings </h3>
//
// The validity of a unit string can be checked by:
// <srcblock>
// // Check if the given String is a valid unit representation. The String
// // will be cached in the unit maps for later reference if True
// if ( UnitVal::check( "km/s/Mpc") ) {...}
// </srcblock>
//
// </synopsis>
//
// <example>
// An observation contains values in Janskys and in Westerbork Units. The
// data can be combined by the following code:
// <srcblock>
// // The Fits tape gave JY, we check if defined, else we define them
// if ( !UnitVal::check( "JY")) {
// UnitMap::putUser("JY", UnitVal(1.,"Jy"), "FITS way to write Jy");
// }
// // The Fits tape gave WU (which are defined):
// // We check if JY and WU are of the same dimension:
// if (UnitVal(1.,"JY") != UnitVal(1.,"WU")) {
// cerr << "Wrong dimension for either JY ( " <<
// UnitVal(1.,"JY")->getDim() <<
// ") or WU ( " <<
// UnitVal(1.,"WU")->getDim() << ")" << endl;
// }
// // And output the relation between WU and JY, and the WU value:
// cout << "1 WU = " << ( UnitVal(1.,"WU")/UnitVal(1.,"Jy") )->getVal() <<
// " JY with 1 WU = " << UnitVal(1.,"WU") << endl;
// </srcblock>
// </example>
// <motivation>
// To separate the actual manipulation of unit values from the related
// quantity
// </motivation>
//
// <todo asof="941110">
// <li> Some inlining (did not work first go)
// </todo>
class UnitVal {
//# Friends
// Multiply
friend UnitVal operator*(const UnitVal &in, const UnitVal &other);
// Divide
friend UnitVal operator/(const UnitVal &in, const UnitVal &other);
// Output a unit as a value and a string of SI defining units
friend ostream& operator<<(ostream &os, const UnitVal &ku);
// ensure that statics are initialized
friend class UnitVal_static_initializer;
public:
//# Constructors
// Construct an non-dimensioned value of 1
UnitVal();
// Copy constructor
UnitVal(const UnitVal &other);
// Construct an non-dimensioned value
UnitVal(Double factor) { init(factor); }
// Construct a fully dimensioned value
// <thrown>
// <li> AipsError
// </thrown>
UnitVal(Double factor, const String &s, UMaps* = 0);
// Construct a value with a single unit at position specified
UnitVal(Double factor, Int pos) { init(factor, pos); }
// Destructor
~UnitVal();
//# Operators
// Assignment (copy semantics)
UnitVal &operator=(const UnitVal &other);
// Manipulate units
// <group name="manipulate">
// Multiply different units
UnitVal &operator*=(const UnitVal &other);
// Divide different units
UnitVal &operator/=(const UnitVal &other);
// Compare the dimensionality of different units
Bool operator==(const UnitVal &other) const;
Bool operator!=(const UnitVal &other) const;
// </group>
//# General member functions
// Raise a unit to an integer power
UnitVal pow(Int p);
// Take integer root
// <thrown>
// <li> AipsError if power equals zero
// <li> AipsError if unit dimensions not multiple of power
// </thrown>
// <group>
UnitVal root(Int p) const;
UnitVal sqrt() const;
// </group>
// Get the data parts of the unit value definition
// <group name="get data">
// Get the dimensions in the defining SI units
const UnitDim &getDim() const;
// Get the factor of the unit (as compared to pure SI units)
Double getFac() const;
// </group>
//# Helper functions
// Convert a unit string to a proper unit value and cache the result. The
// function will return False if invalid string specified
static Bool check(const String &s);
// Convert a unit string to a proper unit value, cache the result and compare
// the dimension with the specified unit value. False if any of the steps fails
static Bool check(const String &s, UnitVal &loc);
//# Data members
// Some constants to check type of units
// <group name="unit kinds">
static UnitVal NODIM;
static UnitVal UNDIM;
static UnitVal LENGTH;
static UnitVal MASS;
static UnitVal TIME;
static UnitVal CURRENT;
static UnitVal TEMPERATURE;
static UnitVal INTENSITY;
static UnitVal MOLAR;
static UnitVal ANGLE;
static UnitVal SOLIDANGLE;
// </group>
protected:
// alternate initialization
void init(Double factor);
void init(Double factor, Int pos);
private:
//# Data members
// The factor necessary to express the specified unit in the defining SI units
Double kindFactor;
// The dimensions of the unit in the defining SI units
UnitDim kindDim;
// Convert (and check) a unit string to an SI value representation
// <group>
static Bool create(const String &s, UnitVal &res, UMaps* = 0);
static Bool create(MUString &str, UnitVal &res, UMaps* = 0);
// </group>
// Determine sign of unit power (i.e. if '.' or '/')
static Int psign(MUString &str);
// Determine exponent of unit symbol
static Int power(MUString &str);
// Determine symbol name in unit string
static Bool field(MUString &str, UnitVal &res, UMaps*);
};
//# Inline Implementations
//# Global functions
// <summary> Global output function </summary>
// <group name=output>
// Output
ostream& operator<<(ostream &os, const UnitVal &ku);
// </group>
// <summary> Static initialisation of UnitVal constants </summary>
static class UnitVal_static_initializer {
public:
UnitVal_static_initializer( ) {
if ( ! initialized ) {
UnitVal::NODIM.init( 1.);
UnitVal::UNDIM.init( 1., UnitDim::Dnon);
UnitVal::LENGTH.init( 1., UnitDim::Dm);
UnitVal::MASS.init( 1., UnitDim::Dkg);
UnitVal::TIME.init( 1., UnitDim::Ds);
UnitVal::CURRENT.init( 1., UnitDim::DA);
UnitVal::TEMPERATURE.init( 1., UnitDim::DK);
UnitVal::INTENSITY.init( 1., UnitDim::Dcd);
UnitVal::MOLAR.init( 1., UnitDim::Dmol);
UnitVal::ANGLE.init( 1., UnitDim::Drad);
UnitVal::SOLIDANGLE.init( 1., UnitDim::Dsr);
initialized = 1;
}
}
private:
static int initialized;
} unitval_static_initializer;
} //# NAMESPACE CASACORE - END
#endif
casacore::MUString
Definition MUString.h:228
casacore::String
String: the storage and methods of handling collections of characters.
Definition String.h:223
casacore::UMaps
Define a struct containing the static data members.
Definition UnitMap.h:48
casacore::UnitDim
Definition UnitDim.h:85
casacore::UnitDim::Drad
@ Drad
Definition UnitDim.h:97
casacore::UnitDim::DK
@ DK
Definition UnitDim.h:97
casacore::UnitDim::Dmol
@ Dmol
Definition UnitDim.h:97
casacore::UnitDim::Dcd
@ Dcd
Definition UnitDim.h:97
casacore::UnitDim::Ds
@ Ds
Definition UnitDim.h:97
casacore::UnitDim::Dkg
@ Dkg
Definition UnitDim.h:97
casacore::UnitDim::DA
@ DA
Definition UnitDim.h:97
casacore::UnitDim::Dm
@ Dm
Definition UnitDim.h:97
casacore::UnitDim::Dnon
@ Dnon
Definition UnitDim.h:97
casacore::UnitDim::Dsr
@ Dsr
Definition UnitDim.h:97
casacore::UnitMap
Definition UnitMap.h:210
casacore::UnitVal_static_initializer
Static initialisation of UnitVal constants.
Definition UnitVal.h:307
casacore::UnitVal_static_initializer::UnitVal_static_initializer
UnitVal_static_initializer()
Definition UnitVal.h:309
casacore::UnitVal_static_initializer::initialized
static int initialized
Definition UnitVal.h:326
casacore::UnitVal
Definition UnitVal.h:165
casacore::UnitVal::operator/
friend UnitVal operator/(const UnitVal &in, const UnitVal &other)
Divide.
casacore::UnitVal::sqrt
UnitVal sqrt() const
casacore::UnitVal::init
void init(Double factor, Int pos)
casacore::UnitVal::ANGLE
static UnitVal ANGLE
Definition UnitVal.h:262
casacore::UnitVal::TEMPERATURE
static UnitVal TEMPERATURE
Definition UnitVal.h:259
casacore::UnitVal::UNDIM
static UnitVal UNDIM
Definition UnitVal.h:254
casacore::UnitVal::init
void init(Double factor)
alternate initialization
casacore::UnitVal::TIME
static UnitVal TIME
Definition UnitVal.h:257
casacore::UnitVal::getDim
const UnitDim & getDim() const
Get the data parts of the unit value definition .
casacore::UnitVal::kindDim
UnitDim kindDim
The dimensions of the unit in the defining SI units.
Definition UnitVal.h:276
casacore::UnitVal::UnitVal
UnitVal(Double factor)
Construct an non-dimensioned value.
Definition UnitVal.h:184
casacore::UnitVal::NODIM
static UnitVal NODIM
Some constants to check type of units .
Definition UnitVal.h:253
casacore::UnitVal::UnitVal
UnitVal(Double factor, const String &s, UMaps *=0)
Construct a fully dimensioned value.
casacore::UnitVal::operator*
friend UnitVal operator*(const UnitVal &in, const UnitVal &other)
Multiply.
casacore::UnitVal::operator<<
friend ostream & operator<<(ostream &os, const UnitVal &ku)
Output a unit as a value and a string of SI defining units.
casacore::UnitVal::CURRENT
static UnitVal CURRENT
Definition UnitVal.h:258
casacore::UnitVal::pow
UnitVal pow(Int p)
Raise a unit to an integer power.
casacore::UnitVal::MASS
static UnitVal MASS
Definition UnitVal.h:256
casacore::UnitVal::power
static Int power(MUString &str)
Determine exponent of unit symbol.
casacore::UnitVal::UnitVal
UnitVal()
Construct an non-dimensioned value of 1.
casacore::UnitVal::INTENSITY
static UnitVal INTENSITY
Definition UnitVal.h:260
casacore::UnitVal::SOLIDANGLE
static UnitVal SOLIDANGLE
Definition UnitVal.h:263
casacore::UnitVal::getFac
Double getFac() const
Get the factor of the unit (as compared to pure SI units).
casacore::UnitVal::psign
static Int psign(MUString &str)
Determine sign of unit power (i.e.
casacore::UnitVal::~UnitVal
~UnitVal()
Destructor.
casacore::UnitVal::operator=
UnitVal & operator=(const UnitVal &other)
Assignment (copy semantics).
casacore::UnitVal::kindFactor
Double kindFactor
The factor necessary to express the specified unit in the defining SI units.
Definition UnitVal.h:273
casacore::UnitVal::UnitVal_static_initializer
friend class UnitVal_static_initializer
ensure that statics are initialized
Definition UnitVal.h:174
casacore::UnitVal::operator==
Bool operator==(const UnitVal &other) const
Compare the dimensionality of different units.
casacore::UnitVal::MOLAR
static UnitVal MOLAR
Definition UnitVal.h:261
casacore::UnitVal::operator*=
UnitVal & operator*=(const UnitVal &other)
Manipulate units .
casacore::UnitVal::create
static Bool create(MUString &str, UnitVal &res, UMaps *=0)
casacore::UnitVal::UnitVal
UnitVal(const UnitVal &other)
Copy constructor.
casacore::UnitVal::check
static Bool check(const String &s)
Convert a unit string to a proper unit value and cache the result.
casacore::UnitVal::operator!=
Bool operator!=(const UnitVal &other) const
casacore::UnitVal::operator/=
UnitVal & operator/=(const UnitVal &other)
Divide different units.
casacore::UnitVal::check
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...
casacore::UnitVal::root
UnitVal root(Int p) const
Take integer root.
casacore::UnitVal::UnitVal
UnitVal(Double factor, Int pos)
Construct a value with a single unit at position specified.
Definition UnitVal.h:193
casacore::UnitVal::LENGTH
static UnitVal LENGTH
Definition UnitVal.h:255
casacore::UnitVal::create
static Bool create(const String &s, UnitVal &res, UMaps *=0)
Convert (and check) a unit string to an SI value representation.
casacore::UnitVal::field
static Bool field(MUString &str, UnitVal &res, UMaps *)
Determine symbol name in unit string.
casacore::unitval_static_initializer
static class casacore::UnitVal_static_initializer unitval_static_initializer
casacore
this file contains all the compiler specific defines
Definition mainpage.dox:28
casacore::operator<<
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
casacore::factor
int * factor
Definition hdu.h:478
casacore::init
void init()
Other internal helper function(s).
casacore::Int
int Int
Definition aipstype.h:48
casacore::Bool
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
casacore::Double
double Double
Definition aipstype.h:53
casacore::UnitVal_global_functions_output
Global output function .
Definition UnitVal.h:301
casacore::UnitVal_global_functions_output::operator<<
ostream & operator<<(ostream &os, const UnitVal &ku)
Output.