casacore
Loading...
Searching...
No Matches
UnitMap.h
Go to the documentation of this file.
1//# UnitMap.h: defines the UnitMap class containing standard unit definitions
2//# Copyright (C) 1994-2002,2007
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_UNITMAP_H
27#define CASA_UNITMAP_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/BasicSL/Constants.h>
33#include <casacore/casa/stdmap.h>
34#include <casacore/casa/BasicSL/String.h>
35#include <casacore/casa/Quanta/UnitDim.h>
36#include <casacore/casa/Quanta/UnitVal.h>
37#include <casacore/casa/Quanta/UnitName.h>
38
39#include <mutex>
40
41namespace casacore { //# NAMESPACE CASACORE - BEGIN
42
43//# Forward Declarations
44
45// Define a struct containing the static data members.
46// The static struct object is created in function getMaps
47// to ensure proper static initialization order.
48class UMaps {
49public:
50 UMaps() {init();}
51 // Decimal prefix list
52 map<String, UnitName> mapPref;
53 // Defining SI unit list
54 map<String, UnitName> mapDef;
55 // SI unit list
56 map<String, UnitName> mapSI;
57 // Customary list
58 map<String, UnitName> mapCust;
59 // User defined unit list
60 map<String, UnitName> mapUser;
61 // FITS unit list inclusion
63private:
64 void init();
65};
66
67
68
69//* Constants
70// IAU definition of Gaussian grav. constant for calculating IAU units
71const Double IAU_k=0.01720209895;
72// Number of FITS units recognised (change the FITSstring and FITSunit lists
73// in the UnitMap.cc when changing this number.
74const uInt N_FITS = 19;
75
76
77// <summary>
78// contains all simple known physical units
79// </summary>
80
81// <use visibility=export>
82
83// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tUnit">
84//
85// <prerequisite>
86// You should have at least a preliminary understanding of these classes:
87// <li> <linkto class=Unit>Unit</linkto>
88// </prerequisite>
89//
90// <etymology>
91// Based on Units and the Casacore container classes called 'Map'
92// </etymology>
93//
94// <synopsis>
95// Physical units are strings consisting of one or more names of known
96// basic units, separated by '.' or ' ' (for multiplication) or '/' (for
97// division). Each name can optionally be preceded by a standard decimal
98// prefix, and/or followed by an (optionally signed) exponent.
99// Example:
100// km/s/(Mpc.s)2 is identical to km.s-1.Mpc-2.s-2
101//
102// See the <linkto class="Unit">Unit</linkto> class for more details.
103//
104// The UnitMap class contains the known standard basic units, and any
105// other basic unit defined by the user of the Unit related classes.
106// The known units are divided into 5 different groups:
107// <ol>
108// <li> Defining units: m, kg, s, A, K, cd, mol, rad, sr, _
109// <li> SI units: including a.o. Jy, AU etc)
110// <li> Customary units: e.g. lb, hp, ly etc
111// <li> User defined units: defined by user (e.g. Beam, KPH, KM)
112// <li> Cached units: cached unit strings for speed in operations
113// </ol>
114// The full list of known units can be viewed by running the tUnit test
115// program.
116// <note role=caution>
117// There is a difference between units without a dimension (non-dimensioned
118// I will call them), and undimensioned units. Non-dimensioned examples are
119// "", "%"; undimensioned examples: "beam", "pixel".
120// </note>
121//
122// Information about the contents of the unit maps can be obtained by
123// the Bool functions (False if not present):
124// <ul>
125// <li> UnitMap::getPref("string", UnitName &) prefix
126// <li> UnitMap::getUnit("string", UnitName &) search user,
127// customary, SI (in that order)
128// <li> UnitMap::getCache("string", UnitVal &) search cache
129// </ul>
130//
131// The standard units can be viewed by the following commands, which
132// output to cout:
133// <ul>
134// <li> UnitMap::list() all prefixes and SI, Cust and User units
135// <li> UnitMap::listCache() current cache contents
136// <li> UnitMap::listPref() all prefixes
137// <li> UnitMap::listDef() all defining units
138// <li> UnitMap::listSI() all SI Units
139// <li> UnitMap::listCust() all customary units
140// <li> UnitMap::listUser() all user defined units
141// </ul>
142//
143// Units can be defined in the user list by:
144// <note role=tip> The cache will be cleared if a user defined unit is overwritten,
145// to make sure no old value will be used. </note>
146// <srcblock>
147// UnitMap::putUser("tag", UnitVal(factor,"unit"), "full name (optional)");
148// or:
149// UnitMap::putUser(UnitName);
150// </srcblock>
151// <note role=caution>
152// If using an explicit Unit variable (e.g. <src>Unit a("5Bolton/beam")</src>),
153// the check on the legality of the given string, and the conversion to the
154// cached canonical value in the variable 'a', is only done at creation time. This
155// means that if the user changes the value of a unit involved by the
156// <linkto class=UnitMap>putUser()</linkto> method, the unit using it should be
157// re-created (<src> a = Unit("5Bolton/beam");</src>).
158// </note>
159// A special set of 'units' used in FITS datasets can be added by the command
160// <srcblock>
161// UnitMap::addFITS();
162// </srcblock>
163// This set can be cleared from the user table by:
164// <srcblock>
165// UnitMap::clearFITS();
166// </srcblock>
167// Note that Unitmap keeps track of the inclusion of the FITS inclusion,
168// making multiple calls inexpensive. The list of current FITS units can
169// be viewed by running the tUnit program, or looking at the FITSunit
170// table.
171//
172// Once the UnitMap::addFITS() has been run, the FITS units can be used as
173// any other unit. In addition, a FITS unit can be translated to standard
174// SI units by a call to <em>Unit UnitMap::fromFITS(const Unit)</em>. Any
175// unit that is defined as a standard FITS unit will be translated. Unknown
176// ones will not be translated, making the way clear for having standard
177// units in a FITS units string. A comparable <em>toFITS()</em> translates in
178// the same way in the reversed direction.
179//
180// The cache can be cleared by:
181// <srcblock>
182// UnitMap::clearCache();
183// </srcblock>
184// </synopsis>
185//
186// <example>
187// Check for legal prefix:
188// <srcblock>
189// UnitName myUnit;
190// if (UnitMap::getPref("k", myUnit)) { cout << "k has value " << myUnit;}
191// </srcblock>
192// Define a value for the unit 'beam':
193// <srcblock>
194// UnitMap::putUser("beam",UnitVal(C::pi * 0.1, "\"_2"),"telescope beam");
195// </srcblock>
196// List current cache:
197// <srcblock>
198// UnitMap::listCache();
199// </srcblock>
200// </example>
201//
202// <motivation>
203// Standard list available to try to enhance use of SI and related units
204// </motivation>
205//
206// <todo asof="941110">
207// <li> Some inlining (did not work first go)
208// </todo>
209
210class UnitMap {
211public:
212 friend class UMaps;
213
214 //# Constructors
215// Default constructor of maps
217
218// Destructor
220
221//# General member functions
222 // Check if a unit name is known, and return its value if True
223 // <group name="find">
224 // Get a prefix definition from key
225 static Bool getPref(const String &s, UnitName &name, UMaps* maps=0);
227 // Get a standard unit definition (search order: User, Customary, SI)
228 static Bool getUnit(const String &s, UnitName &name, UMaps* maps=0);
229
230 // Get a cached definition
231 static Bool getCache(const String &s, UnitVal &val);
232
233 // </group>
234 // Save a definition of a full unit name in the cache (the cache will be
235 // cleared if getting too large (200 entries)
236 static void putCache(const String &s, const UnitVal &val);
237
238 // Define a user defined standard unit. If the unit is being redefined, and it
239 // has already been used in a user's <src>Unit</src> variable, the value
240 // cached in that variable will not change.
241 // <group name="define">
242 static void putUser(const String &s, const UnitVal &val);
243 static void putUser(const String &s, const UnitVal &val,
244 const String &name);
245 static void putUser(const UnitName &name);
246 // </group>
247// Remove a user unit
248// <group>
249 static void removeUser(const String &name);
250 static void removeUser(const UnitName &name);
251// </group>
252
253// Clear out the cache
254 static void clearCache();
255
256// Define FITS related unit names
257 static void addFITS();
258
259// Clear FITS related units from user list
260 static void clearFITS();
261
262// Translate a FITS unit to the proper units. Note that this is a translation
263// of the string only, no conversion. Unknown FITS units are not translated.
264// Hence any new definition of the FITS units will work ok
265 static Unit fromFITS(const Unit &un);
266
267// Translate to a FITS unit
268 static Unit toFITS(const Unit &un);
269
270// List some part of the standard unit lists on cout or stream
271// <group name="list">
272// List all known unit symbols
273// <group>
274 static void list(ostream &os);
275 static void list();
276 // </group>
277
278// List all units in cache
279 // <group>
280 static void listCache(ostream &os);
281 static void listCache();
282 // </group>
283
284// List all prefixes
285 // <group>
286 static void listPref(ostream &os);
287 static void listPref();
288 // </group>
289
290// List all defining units
291 // <group>
292 static void listDef(ostream &os);
293 static void listDef();
294 // </group>
295
296// List all SI units
297 // <group>
298 static void listSI(ostream &os);
299 static void listSI();
300 // </group>
301
302// List all customary units
303 // <group>
304 static void listCust(ostream &os);
305 static void listCust();
306 // </group>
307
308// List all user defined units
309 // <group>
310 static void listUser(ostream &os);
311 static void listUser();
312 // </group>
313// </group>
314
315 // Return the different maps
316 // <group>
317 static const map<String, UnitName> &givePref();
318 static const map<String, UnitName> &giveDef();
319 static const map<String, UnitName> &giveSI();
320 static const map<String, UnitName> &giveCust();
321 static const map<String, UnitName> &giveUser();
322 static const map<String, UnitVal> &giveCache();
323 // </group>
324
325 private:
326 //# Constructors
327 // Copy constructor (not implemented)
328 UnitMap(const UnitMap &other);
329
330 //# Operators
331 // Copy assignment (not implemented)
332 UnitMap &operator=(const UnitMap &other);
333
334 static std::mutex fitsMutex;
335
336 //# member functions
337 // Get the static UMaps struct.
338 static UMaps& getMaps();
339 // Get the static mapCache object.
340 // This cannot be part of the UMaps struct, because the UnitVal ctor
341 // is called in the initialization of UMaps, but uses mapCache resulting
342 // in a recursive call.
343 static map<String, UnitVal>& getMapCache();
344 // Get the name of a FITS unit
345 static Bool getNameFITS(const UnitName *&name, uInt which);
346 // Get the belonging unit to a FITS unit
347 static const String &getStringFITS(uInt which);
348
349 static void initUM();
350 // Bits and pieces of initUM() to get compilation speed improved
351 // <group>
352 static void initUMPrefix (UMaps&);
353 static void initUMSI1 (UMaps&);
354 static void initUMSI2 (UMaps&);
355 static void initUMCust1 (UMaps&);
356 static void initUMCust2 (UMaps&);
357 static void initUMCust3 (UMaps&);
358 // </group>
359
360};
361
362} //# NAMESPACE CASACORE - END
363
364#endif
String: the storage and methods of handling collections of characters.
Definition String.h:223
Define a struct containing the static data members.
Definition UnitMap.h:48
map< String, UnitName > mapPref
Decimal prefix list.
Definition UnitMap.h:52
Bool doneFITS
FITS unit list inclusion.
Definition UnitMap.h:62
map< String, UnitName > mapDef
Defining SI unit list.
Definition UnitMap.h:54
map< String, UnitName > mapCust
Customary list.
Definition UnitMap.h:58
map< String, UnitName > mapUser
User defined unit list.
Definition UnitMap.h:60
map< String, UnitName > mapSI
SI unit list.
Definition UnitMap.h:56
~UnitMap()
Destructor.
static void initUMSI1(UMaps &)
static void initUMSI2(UMaps &)
static void listCache(ostream &os)
List all units in cache.
static const map< String, UnitVal > & giveCache()
static void list()
static const String & getStringFITS(uInt which)
Get the belonging unit to a FITS unit.
static void putUser(const String &s, const UnitVal &val)
Define a user defined standard unit.
UnitMap(const UnitMap &other)
Copy constructor (not implemented)
static const map< String, UnitName > & givePref()
Return the different maps.
static const map< String, UnitName > & giveCust()
static void list(ostream &os)
List some part of the standard unit lists on cout or stream
static void listSI(ostream &os)
List all SI units.
static void listPref(ostream &os)
List all prefixes.
static Unit toFITS(const Unit &un)
Translate to a FITS unit.
UnitMap & operator=(const UnitMap &other)
Copy assignment (not implemented)
static Bool getNameFITS(const UnitName *&name, uInt which)
Get the name of a FITS unit.
static Bool getPref(const String &s, UnitName &name, UMaps *maps=0)
Check if a unit name is known, and return its value if True
static void initUM()
static Unit fromFITS(const Unit &un)
Translate a FITS unit to the proper units.
static void listUser(ostream &os)
List all user defined units.
static UMaps & getMaps()
Get the static UMaps struct.
UnitMap()
Default constructor of maps.
static const map< String, UnitName > & giveDef()
static Bool getCache(const String &s, UnitVal &val)
Get a cached definition.
static void initUMCust1(UMaps &)
static void initUMCust2(UMaps &)
static void putUser(const String &s, const UnitVal &val, const String &name)
static void listPref()
static void listDef(ostream &os)
List all defining units.
static void listUser()
static void listCache()
static void listCust()
static void clearFITS()
Clear FITS related units from user list.
static void removeUser(const String &name)
Remove a user unit.
static void putUser(const UnitName &name)
static void listDef()
static void listCust(ostream &os)
List all customary units.
static void listSI()
static Bool getUnit(const String &s, UnitName &name, UMaps *maps=0)
Get a standard unit definition (search order: User, Customary, SI)
static void initUMPrefix(UMaps &)
Bits and pieces of initUM() to get compilation speed improved.
static void putCache(const String &s, const UnitVal &val)
Save a definition of a full unit name in the cache (the cache will be cleared if getting too large (2...
static const map< String, UnitName > & giveSI()
static void clearCache()
Clear out the cache.
static void addFITS()
Define FITS related unit names.
static map< String, UnitVal > & getMapCache()
Get the static mapCache object.
static std::mutex fitsMutex
Definition UnitMap.h:334
static void initUMCust3(UMaps &)
static const map< String, UnitName > & giveUser()
static void removeUser(const UnitName &name)
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
const Double IAU_k
Definition UnitMap.h:71
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const uInt N_FITS
Number of FITS units recognised (change the FITSstring and FITSunit lists in the UnitMap....
Definition UnitMap.h:74
double Double
Definition aipstype.h:53