PageRenderTime 5317ms CodeModel.GetById 2ms RepoModel.GetById 0ms app.codeStats 0ms

/casa/Quanta/UnitMap.cc

http://casacore.googlecode.com/
C++ | 424 lines | 344 code | 47 blank | 33 comment | 47 complexity | 325723087324174c8981508d341cccdb MD5 | raw file
Possible License(s): GPL-2.0
  1. //# UnitMap.cc: 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: aips2-request@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. //# $Id: UnitMap.cc 20551 2009-03-25 00:11:33Z Malte.Marquarding $
  27. //# Includes
  28. #include <casa/Quanta/UnitMap.h>
  29. #include <casa/Utilities/MUString.h>
  30. #include <casa/Utilities/Regex.h>
  31. #include <casa/iostream.h>
  32. namespace casa { //# NAMESPACE CASA - BEGIN
  33. void UnitMap::initUM() {
  34. static Bool needInit = True;
  35. if (!needInit) return;
  36. needInit = False;
  37. // Initialise lists
  38. UnitMap::mapPref =
  39. new map<String, UnitName>;
  40. UnitMap::mapDef =
  41. new map<String, UnitName>;
  42. UnitMap::mapSI =
  43. new map<String, UnitName>;
  44. UnitMap::mapCust =
  45. new map<String, UnitName>;
  46. UnitMap::mapUser =
  47. new map<String, UnitName>;
  48. UnitMap::mapCache =
  49. new map<String, UnitVal>;
  50. UnitMap::doneFITS = False;
  51. // Define the map
  52. // Known prefixes
  53. UnitMap::initUMPrefix();
  54. // Defining SI units
  55. UnitMap::initUMSI1();
  56. UnitMap::initUMSI2();
  57. // non-SI customary units
  58. UnitMap::initUMCust1();
  59. UnitMap::initUMCust2();
  60. UnitMap::initUMCust3();
  61. //# Start with clean cache
  62. UnitMap::mapCache->clear();
  63. }
  64. UnitMap::UnitMap() {}
  65. UnitMap::~UnitMap() {
  66. releaseUM();
  67. }
  68. void UnitMap::releaseUM() {
  69. delete UnitMap::mapPref; mapPref = 0;
  70. delete UnitMap::mapDef; mapDef = 0;
  71. delete UnitMap::mapSI; mapSI = 0;
  72. delete UnitMap::mapCust; mapCust = 0;
  73. delete UnitMap::mapUser; mapUser = 0;
  74. delete UnitMap::mapCache; mapCache = 0;
  75. }
  76. Bool UnitMap::getCache(const String& s, UnitVal &val) {
  77. UnitMap::initUM();
  78. map<String, UnitVal>::iterator pos = mapCache->find(s);
  79. if (pos == mapCache->end()) {
  80. val = UnitVal();
  81. return False;
  82. }
  83. val = pos->second;
  84. return True;
  85. }
  86. Bool UnitMap::getPref(const String& s, UnitName &name) {
  87. UnitMap::initUM();
  88. map<String, UnitName>::iterator pos = mapPref->find(s);
  89. if (pos == mapPref->end()) {
  90. name = UnitName();
  91. return False;
  92. }
  93. name = pos->second;
  94. return True;
  95. }
  96. Bool UnitMap::getUnit(const String& s, UnitName &name) {
  97. UnitMap::initUM();
  98. map<String, UnitName>::iterator pos;
  99. if ((pos = mapUser->find(s)) != mapUser->end() ||
  100. (pos = mapCust->find(s)) != mapCust->end() ||
  101. (pos = mapSI->find(s)) != mapSI->end()) {
  102. } else {
  103. name = UnitName();
  104. return False;
  105. }
  106. name = pos->second;
  107. return True;
  108. }
  109. void UnitMap::putCache(const String& s, const UnitVal& val) {
  110. UnitMap::initUM();
  111. if (! s.empty()) {
  112. if (mapCache->size() > 200) clearCache();
  113. mapCache->insert(map<String, UnitVal>::value_type(s,val));
  114. }
  115. }
  116. void UnitMap::putUser(const String& s, const UnitVal& val) {
  117. const String empty("");
  118. UnitMap::putUser(s, val, empty);
  119. }
  120. void UnitMap::putUser(const String& s, const UnitVal& val,
  121. const String& name) {
  122. UnitName loc(s,val,name);
  123. UnitMap::putUser(loc);
  124. }
  125. void UnitMap::putUser(const UnitName& name) {
  126. UnitMap::initUM();
  127. map<String, UnitName>::iterator pos;
  128. if ((pos = mapUser->find(name.getName())) != mapUser->end() ||
  129. (pos = mapCust->find(name.getName())) != mapCust->end() ||
  130. (pos = mapSI->find(name.getName())) != mapSI->end()) clearCache();
  131. mapUser->insert(map<String, UnitName>::value_type(name.getName(), name));
  132. }
  133. void UnitMap::removeUser(const String& s) {
  134. UnitMap::initUM();
  135. map<String, UnitName>::iterator pos = mapUser->find(s);
  136. if (pos != mapUser->end()) {
  137. mapUser->erase(pos);
  138. clearCache();
  139. }
  140. }
  141. void UnitMap::removeUser(const UnitName& name) {
  142. UnitMap::removeUser(name.getName());
  143. }
  144. const String &UnitMap::getStringFITS(uInt which) {
  145. static String FITSstring[N_FITS] = {
  146. "beam",
  147. "d",
  148. "deg",
  149. "deg",
  150. "Hz",
  151. "Jy",
  152. "K",
  153. "K",
  154. "km",
  155. "m",
  156. "m",
  157. "Pa",
  158. "pixel",
  159. "s",
  160. "s",
  161. "s",
  162. "V",
  163. "a",
  164. "a"
  165. };
  166. return FITSstring[which];
  167. }
  168. Bool UnitMap::getNameFITS(UnitName *&name, uInt which) {
  169. static UnitName FITSunit[N_FITS] = {
  170. UnitName("BEAM", UnitVal(1.0, getStringFITS(0)), "dimensionless beam"),
  171. UnitName("DAYS", UnitVal(1.0, getStringFITS(1)), "day"),
  172. UnitName("DEGREES", UnitVal(1.0, getStringFITS(2)), "degree"),
  173. UnitName("DEG", UnitVal(1.0, getStringFITS(3)), "degree"),
  174. UnitName("HZ", UnitVal(1.0, getStringFITS(4)), "hertz"),
  175. UnitName("JY", UnitVal(1.0, getStringFITS(5)), "jansky"),
  176. UnitName("KELVINS", UnitVal(1.0, getStringFITS(6)), "kelvin"),
  177. UnitName("KELVIN", UnitVal(1.0, getStringFITS(7)), "kelvin"),
  178. UnitName("KM", UnitVal(1.0, getStringFITS(8)), "km"),
  179. UnitName("METERS", UnitVal(1.0, getStringFITS(9)), "meter"),
  180. UnitName("M", UnitVal(1.0, getStringFITS(10)),"meter"),
  181. UnitName("PASCAL", UnitVal(1.0, getStringFITS(11)),"pascal"),
  182. UnitName("PIXEL", UnitVal(1.0, getStringFITS(12)),"dimensionless pixel"),
  183. UnitName("SECONDS", UnitVal(1.0, getStringFITS(13)),"second"),
  184. UnitName("SEC", UnitVal(1.0, getStringFITS(14)),"second"),
  185. UnitName("S", UnitVal(1.0, getStringFITS(15)),"second"),
  186. UnitName("VOLTS", UnitVal(1.0, getStringFITS(16)),"volt"),
  187. UnitName("YEARS", UnitVal(1.0, getStringFITS(17)),"year"),
  188. UnitName("YEAR", UnitVal(1.0, getStringFITS(18)),"year")
  189. };
  190. if (which >= N_FITS) {
  191. return False;
  192. }
  193. name = &FITSunit[which];
  194. return True;
  195. }
  196. void UnitMap::addFITS() {
  197. UnitMap::initUM();
  198. if (! UnitMap::doneFITS) {
  199. uInt cnt = 0;
  200. UnitName *Fname;
  201. while (UnitMap::getNameFITS(Fname, cnt)) {
  202. UnitMap::putUser(*Fname);
  203. cnt++;
  204. }
  205. UnitMap::doneFITS = True;
  206. }
  207. }
  208. void UnitMap::clearFITS() {
  209. UnitMap::initUM();
  210. if (UnitMap::doneFITS) {
  211. uInt cnt = 0;
  212. UnitName *Fname;
  213. while (UnitMap::getNameFITS(Fname, cnt)) {
  214. UnitMap::removeUser(*Fname);
  215. cnt++;
  216. }
  217. UnitMap::doneFITS = False;
  218. }
  219. }
  220. Unit UnitMap::fromFITS(const Unit &un) {
  221. static Regex sepa("[^a-zA-Z]");
  222. MUString mus(un.getName());
  223. String y;
  224. String z;
  225. UnitName *nam;
  226. while (!mus.eos()) {
  227. if (mus.testChar(sepa)) y += String(mus.getChar());
  228. else {
  229. z = mus.getAlpha();
  230. for (uInt i=0; i<N_FITS; i++) {
  231. getNameFITS(nam, i);
  232. if (z == nam->getName()) {
  233. z = getStringFITS(i);
  234. break;
  235. }
  236. }
  237. y += z;
  238. }
  239. }
  240. return Unit(y);
  241. }
  242. Unit UnitMap::toFITS(const Unit &un) {
  243. static Regex sepa("[^a-zA-Z]");
  244. MUString mus(un.getName());
  245. String y;
  246. String z;
  247. UnitName *nam;
  248. while (!mus.eos()) {
  249. if (mus.testChar(sepa)) y += String(mus.getChar());
  250. else {
  251. z = mus.getAlpha();
  252. for (Int i=N_FITS-1; i>= 0; i--) {
  253. if (z == getStringFITS(i)) {
  254. getNameFITS(nam, i);
  255. z = nam->getName();
  256. break;
  257. }
  258. }
  259. y += z;
  260. }
  261. }
  262. return Unit(y);
  263. }
  264. void UnitMap::clearCache() {
  265. UnitMap::initUM();
  266. mapCache->clear();
  267. }
  268. void UnitMap::listPref() {
  269. listPref(cout);
  270. }
  271. void UnitMap::listPref(ostream &os) {
  272. UnitMap::initUM();
  273. for (map<String, UnitName>::iterator i=mapPref->begin();
  274. i != mapPref->end(); ++i) {
  275. os << " " << i->second << endl;
  276. }
  277. }
  278. void UnitMap::listDef() {
  279. listDef(cout);
  280. }
  281. void UnitMap::listDef(ostream &os) {
  282. UnitMap::initUM();
  283. for (map<String, UnitName>::iterator i=mapDef->begin();
  284. i != mapDef->end(); ++i) {
  285. os << " " << i->second << endl;
  286. }
  287. }
  288. void UnitMap::listSI() {
  289. listSI(cout);
  290. }
  291. void UnitMap::listSI(ostream &os) {
  292. UnitMap::initUM();
  293. for (map<String, UnitName>::iterator i=mapSI->begin();
  294. i != mapSI->end(); ++i) {
  295. os << " " << i->second << endl;
  296. }
  297. }
  298. void UnitMap::listCust() {
  299. listCust(cout);
  300. }
  301. void UnitMap::listCust(ostream &os) {
  302. UnitMap::initUM();
  303. for (map<String, UnitName>::iterator i=mapCust->begin();
  304. i != mapCust->end(); ++i) {
  305. os << " " << i->second << endl;
  306. }
  307. }
  308. void UnitMap::listUser() {
  309. listUser(cout);
  310. }
  311. void UnitMap::listUser(ostream &os) {
  312. UnitMap::initUM();
  313. for (map<String, UnitName>::iterator i=mapUser->begin();
  314. i != mapUser->end(); ++i) {
  315. os << " " << i->second << endl;
  316. }
  317. }
  318. void UnitMap::list() {
  319. list(cout);
  320. }
  321. void UnitMap::list(ostream &os) {
  322. UnitMap::initUM();
  323. os << "Prefix table (" << mapPref->size() << "):" << endl;
  324. listPref(os);
  325. os << "Defining unit table (" << mapDef->size() << "):" << endl;
  326. listDef(os);
  327. os << "SI unit table (" << mapSI->size() << "):" << endl;
  328. listSI(os);
  329. os << "Customary unit table (" << mapCust->size() << "):" << endl;
  330. listCust(os);
  331. os << "User unit table (" << mapUser->size() << "):" << endl;
  332. listUser(os);
  333. }
  334. void UnitMap::listCache() {
  335. listCache(cout);
  336. }
  337. void UnitMap::listCache(ostream &os) {
  338. UnitMap::initUM();
  339. os << "Cached unit table (" << mapCache->size() << "):" << endl;
  340. for (map<String, UnitVal>::iterator i=mapCache->begin();
  341. i != mapCache->end(); ++i) {
  342. os << " " <<
  343. UnitName(i->first, i->second) << endl;
  344. }
  345. }
  346. const map<String, UnitName> &UnitMap::givePref() {
  347. UnitMap::initUM();
  348. return *mapPref;
  349. }
  350. const map<String, UnitName> &UnitMap::giveDef() {
  351. UnitMap::initUM();
  352. return *mapDef;
  353. }
  354. const map<String, UnitName> &UnitMap::giveSI() {
  355. UnitMap::initUM();
  356. return *mapSI;
  357. }
  358. const map<String, UnitName> &UnitMap::giveCust() {
  359. UnitMap::initUM();
  360. return *mapCust;
  361. }
  362. const map<String, UnitName> &UnitMap::giveUser() {
  363. UnitMap::initUM();
  364. return *mapUser;
  365. }
  366. const map<String, UnitVal> &UnitMap::giveCache() {
  367. UnitMap::initUM();
  368. return *mapCache;
  369. }
  370. map<String, UnitName> *UnitMap::mapPref;
  371. map<String, UnitName> *UnitMap::mapDef;
  372. map<String, UnitName> *UnitMap::mapSI;
  373. map<String, UnitName> *UnitMap::mapCust;
  374. map<String, UnitName> *UnitMap::mapUser;
  375. map<String, UnitVal> *UnitMap::mapCache;
  376. Bool UnitMap::doneFITS;
  377. } //# NAMESPACE CASA - END