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