Changeset 68 for trunk/src/core
- Timestamp:
- Sep 26, 2005 6:55:39 PM (19 years ago)
- Location:
- trunk/src/core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpUnits.cc
r21 r68 540 540 } 541 541 542 // convert function so people can just send in two strings and 543 // we'll see if the units exists and do a conversion 544 // strVal = RpUnits::convert("300K","C",1); 545 std::string 546 RpUnits::convert ( std::string val, 547 std::string toUnitsName, 548 int showUnits, 549 int* result ) { 550 551 int valLen = 0; 552 RpUnits* toUnits = NULL; 553 RpUnits* fromUnits = NULL; 554 std::string tmpNumVal = ""; 555 std::string fromUnitsName = ""; 556 std::string convVal = ""; 557 double numVal = 0; 558 int idx = 0; 559 int convResult = 0; 560 561 562 // set default result flag/error code 563 if (result) { 564 *result = 0; 565 } 566 567 toUnits = find(toUnitsName); 568 569 // did we find the unit in the dictionary? 570 if (toUnits == NULL) { 571 // toUnitsName was not found in the dictionary 572 return val; 573 } 574 575 valLen = val.length(); 576 577 // search our string to see where the numeric part stops 578 // and the units part starts 579 // make sure not to stop searching if we encounter '.', '-', '+' 580 for (idx=0; idx < valLen; idx++) { 581 if ( !((val[idx] >= '0') && (val[idx] <= '9')) ) { 582 if ( (val[idx] != '.')&&(val[idx] != '-')&&(val[idx] != '+') ) { 583 break; 584 } 585 } 586 } 587 588 if ( (idx < valLen) && (idx > 0) ) { 589 tmpNumVal = val.substr(0,idx); 590 if (!tmpNumVal.empty()) { 591 numVal = atof(tmpNumVal.c_str()); 592 } 593 else { 594 // tmpNumVal was empty string? 595 // set error code here and return error 596 597 if (result) { 598 *result = 1; 599 } 600 } 601 fromUnitsName = val.substr(idx, valLen-idx+1); 602 } 603 else { 604 // return error, no units or no number in the val string? 605 if (result) { 606 *result = 1; 607 } 608 } 609 610 fromUnits = find(fromUnitsName); 611 612 // did we find the unit in the dictionary? 613 if (fromUnits == NULL) { 614 // fromUnitsName was not found in the dictionary 615 return val; 616 } 617 618 convVal = fromUnits->convert(toUnits, numVal, showUnits, &convResult); 619 620 if ( (result) && (*result == 0) ) { 621 *result = convResult; 622 } 623 624 return convVal; 625 626 } 542 627 543 628 std::string RpUnits::convert ( RpUnits* toUnits, … … 982 1067 } 983 1068 1069 // return codes: 0 success, anything else is error 1070 int 1071 RpUnits::addPresets (std::string group) { 1072 int retVal = 1; 1073 if (group.compare("all") == 0) { 1074 retVal = addPresetAll(); 1075 } 1076 else if (group.compare("energy") == 0) { 1077 retVal = addPresetEnergy(); 1078 } 1079 else if (group.compare("length") == 0) { 1080 retVal = addPresetLength(); 1081 } 1082 else if (group.compare("temp") == 0) { 1083 retVal = addPresetTemp(); 1084 } 1085 else if (group.compare("time") == 0) { 1086 retVal = addPresetTime(); 1087 } 1088 1089 return retVal; 1090 } 1091 1092 // return codes: 0 success, anything else is error 1093 int 1094 RpUnits::addPresetAll () { 1095 1096 // RpUnits* volt = RpUnits::define("V", NULL); 1097 1098 RpUnits::define("V", NULL); 1099 1100 addPresetTime(); 1101 addPresetTemp(); 1102 addPresetLength(); 1103 addPresetEnergy(); 1104 1105 return 0; 1106 } 1107 1108 // return codes: 0 success, anything else is error 1109 int 1110 RpUnits::addPresetTime () { 1111 1112 RpUnits* seconds = RpUnits::define("s", NULL); 1113 1114 RpUnits::makeMetric(seconds); 1115 1116 // add time definitions 1117 1118 return 0; 1119 } 1120 1121 // return codes: 0 success, anything else is error 1122 int 1123 RpUnits::addPresetTemp () { 1124 1125 RpUnits* fahrenheit = RpUnits::define("F", NULL); 1126 RpUnits* celcius = RpUnits::define("C", NULL); 1127 RpUnits* kelvin = RpUnits::define("K", NULL); 1128 RpUnits* rankine = RpUnits::define("R", NULL); 1129 1130 // add temperature definitions 1131 RpUnits::define(fahrenheit, celcius, fahrenheit2centigrade, centigrade2fahrenheit); 1132 RpUnits::define(celcius, kelvin, centigrade2kelvin, kelvin2centigrade); 1133 RpUnits::define(fahrenheit, kelvin, fahrenheit2kelvin, kelvin2fahrenheit); 1134 RpUnits::define(rankine, kelvin, rankine2kelvin, kelvin2rankine); 1135 1136 return 0; 1137 } 1138 1139 // return codes: 0 success, anything else is error 1140 int 1141 RpUnits::addPresetLength () { 1142 1143 RpUnits* meters = RpUnits::define("m", NULL); 1144 RpUnits* angstrom = RpUnits::define("A", NULL); 1145 RpUnits* inch = RpUnits::define("in", NULL); 1146 RpUnits* feet = RpUnits::define("ft", NULL); 1147 RpUnits* yard = RpUnits::define("yd", NULL); 1148 1149 RpUnits::makeMetric(meters); 1150 1151 // add length definitions 1152 RpUnits::define(angstrom, meters, angstrom2meter, meter2angstrom); 1153 RpUnits::define(inch, meters, inch2meter, meter2inch); 1154 RpUnits::define(feet, meters, feet2meter, meter2feet); 1155 RpUnits::define(yard, meters, yard2meter, meter2yard); 1156 1157 return 0; 1158 } 1159 1160 // return codes: 0 success, anything else is error 1161 int 1162 RpUnits::addPresetEnergy () { 1163 1164 RpUnits* electrVolt = RpUnits::define("eV", NULL); 1165 RpUnits* joule = RpUnits::define("J", NULL); 1166 1167 // add energy definitions 1168 RpUnits::define(electrVolt,joule,electronVolt2joule,joule2electronVolt); 1169 1170 return 0; 1171 } 984 1172 985 1173 // -------------------------------------------------------------------- // -
trunk/src/core/RpUnitsStd.cc
r20 r68 131 131 /**************************************** 132 132 * METRIC TO NON-METRIC CONVERSIONS 133 * LENGTH CONVERSIONS 134 * http://www.nodc.noaa.gov/dsdt/ucg/ 133 135 ****************************************/ 134 136 … … 143 145 } 144 146 147 double meter2inch (double m) 148 { 149 return (m*39.37008); 150 } 151 152 double inch2meter (double in) 153 { 154 return (in/39.37008); 155 } 156 157 double meter2feet (double m) 158 { 159 return (m*3.280840); 160 } 161 162 double feet2meter (double ft) 163 { 164 return (ft/3.280840); 165 } 166 167 double meter2yard (double m) 168 { 169 return (m*1.093613); 170 } 171 172 double yard2meter (double yd) 173 { 174 return (yd/1.093613); 175 } 176 145 177 /**************************************** 146 178 * TEMPERATURE CONVERSIONS … … 167 199 } 168 200 201 double rankine2kelvin (double R) 202 { 203 return ((9.0/5.0)*R); 204 } 205 206 double kelvin2rankine (double K) 207 { 208 return ((5.0/9.0)*K); 209 } 210 211 double fahrenheit2kelvin (double F) 212 { 213 return ((F+459.67)*(5.0/9.0)); 214 } 215 216 double kelvin2fahrenheit (double K) 217 { 218 return (((9.0/5.0)*K)-459.67); 219 } 220 221 /**************************************** 222 * ENERGY CONVERSIONS 223 ****************************************/ 224 225 double electronVolt2joule (double eV) 226 { 227 return (eV*1.602177e-19); 228 } 229 230 double joule2electronVolt (double J) 231 { 232 return (J*1.602177e19); 233 } 169 234 170 235 #ifdef __cplusplus
Note: See TracChangeset
for help on using the changeset viewer.