UnitVal.h

Classes

UnitVal -- describes any valid unit as a factor and a dimenion of SI units (full description)
Global Functions -- Global output function (full description)
UnitVal_static_initializer -- Static initialisation of UnitVal constants (full description)

class UnitVal

Interface

friend UnitVal operator*(const UnitVal &in, const UnitVal &other)
friend UnitVal operator/(const UnitVal &in, const UnitVal &other)
friend ostream& operator<<(ostream &os, const UnitVal &ku)
Public Members
UnitVal()
UnitVal(const UnitVal &other)
UnitVal(Double factor)
UnitVal(Double factor, const String &s)
UnitVal(Double factor, Int pos)
~UnitVal()
UnitVal &operator=(const UnitVal &other)
UnitVal &operator*=(const UnitVal &other)
UnitVal &operator/=(const UnitVal &other)
Bool operator==(const UnitVal &other) const
Bool operator!=(const UnitVal &other) const
UnitVal pow(Int p)
UnitVal root(Int p) const
UnitVal sqrt() const
const UnitDim &getDim() const
Double getFac() const
static Bool check(const String &s)
static Bool check(const String &s, UnitVal &loc)
Protected Members
void init(Double factor)
void init(Double factor, Int pos)
Private Members
static Bool create(const String &s, UnitVal &res)
static Bool create(MUString &str, UnitVal &res)
static Int psign(MUString &str)
static Int power(MUString &str)
static Bool field(MUString &str, UnitVal &res)

Description

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25
Programs:
Tests:

Prerequisite

Etymology

The class name derives from Units and gives a Value for a unit string

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 Unit 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 Quantum 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.

Constructing UnitVal values

UnitVal has the following constructors:

Manipulating unit values

The UnitVal can be manipulated by the following operators and functions:

To aid in checking the dimensionality of units, the following constants are available:
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

Checking for valid unit strings

The validity of a unit string can be checked by:

    // 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") ) {...}
    

Example

An observation contains values in Janskys and in Westerbork Units. The data can be combined by the following code:
 // 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;

Motivation

To separate the actual manipulation of unit values from the related quantity

To Do

Member Description

friend UnitVal operator*(const UnitVal &in, const UnitVal &other)

Multiply

friend UnitVal operator/(const UnitVal &in, const UnitVal &other)

Divide

friend ostream& operator<<(ostream &os, const UnitVal &ku)

Output a unit as a value and a string of SI defining units

UnitVal()

Construct an non-dimensioned value of 1

UnitVal(const UnitVal &other)

Copy constructor

UnitVal(Double factor)

Construct an non-dimensioned value

UnitVal(Double factor, const String &s)

Construct a fully dimensioned value

Thrown Exceptions

UnitVal(Double factor, Int pos)

Construct a value with a single unit at position specified

~UnitVal()

Destructor

UnitVal &operator=(const UnitVal &other)

Assignment (copy semantics)

UnitVal &operator*=(const UnitVal &other)

Manipulate units

Multiply different units

UnitVal &operator/=(const UnitVal &other)

Manipulate units

Divide different units

Bool operator==(const UnitVal &other) const

Manipulate units

Compare the dimensionality of different units

Bool operator!=(const UnitVal &other) const

Manipulate units

UnitVal pow(Int p)

Raise a unit to an integer power

UnitVal root(Int p) const
UnitVal sqrt() const

Take integer root

Thrown Exceptions

Double getFac() const

Get the data parts of the unit value definition

Get the factor of the unit (as compared to pure SI units)

const UnitDim &getDim() const

Get the data parts of the unit value definition

static Bool check(const String &s)

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, UnitVal &loc)

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

void init(Double factor)

alternate initialization

void init(Double factor, Int pos)

static Bool create(const String &s, UnitVal &res)
static Bool create(MUString &str, UnitVal &res)

Convert (and check) a unit string to an SI value representation

static Int psign(MUString &str)

Determine sign of unit power (i.e. if '.' or '/')

static Int power(MUString &str)

Determine exponent of unit symbol

static Bool field(MUString &str, UnitVal &res)

Determine symbol name in unit string


Global output function (source)

Interface

ostream& operator<<(ostream &os, const UnitVal &ku)

Description

Member Description

ostream& operator<<(ostream &os, const UnitVal &ku)

Output

static class UnitVal_static_initializer

Interface

Public Members
UnitVal_static_initializer( )

Description

Member Description

UnitVal_static_initializer( )