Changeset 68 for trunk/src/core


Ignore:
Timestamp:
Sep 26, 2005 6:55:39 PM (19 years ago)
Author:
dkearney
Message:

fixed the python rpunits setup script so now it really finds PyRpUnits?.cc

added more conversions to RpUnitsStd?.[h,cc]

added static convert function to RpUnits.[h,cc] so now users can ask for a
conversion as without having to make an RpUnits Object. instead, the
user specifies the value to be converted (value with attached units) as well
as the units we should convert to and if they want units in the returned
string. if the units exist, the conversion is done, if not, the original
string should be returned.

also added the ability to load preset units into the dictionary.
use the static function RpUnits::addPresets(...) in c++ this is done
automatically in python (although, maybe it shouldn't be).

Location:
trunk/src/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpUnits.cc

    r21 r68  
    540540}
    541541
     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);
     545std::string
     546RpUnits::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}
    542627
    543628std::string RpUnits::convert (   RpUnits* toUnits,
     
    9821067}
    9831068
     1069// return codes: 0 success, anything else is error
     1070int
     1071RpUnits::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
     1093int
     1094RpUnits::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
     1109int
     1110RpUnits::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
     1122int
     1123RpUnits::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
     1140int
     1141RpUnits::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
     1161int
     1162RpUnits::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}
    9841172
    9851173// -------------------------------------------------------------------- //
  • trunk/src/core/RpUnitsStd.cc

    r20 r68  
    131131/****************************************
    132132 * METRIC TO NON-METRIC CONVERSIONS
     133 * LENGTH CONVERSIONS
     134 * http://www.nodc.noaa.gov/dsdt/ucg/
    133135 ****************************************/
    134136
     
    143145}
    144146
     147double meter2inch (double m)
     148{
     149        return (m*39.37008);
     150}
     151
     152double inch2meter (double in)
     153{
     154        return (in/39.37008);
     155}
     156
     157double meter2feet (double m)
     158{
     159        return (m*3.280840);
     160}
     161
     162double feet2meter (double ft)
     163{
     164        return (ft/3.280840);
     165}
     166
     167double meter2yard (double m)
     168{
     169        return (m*1.093613);
     170}
     171
     172double yard2meter (double yd)
     173{
     174        return (yd/1.093613);
     175}
     176
    145177/****************************************
    146178 * TEMPERATURE CONVERSIONS
     
    167199}
    168200
     201double rankine2kelvin (double R)
     202{
     203        return ((9.0/5.0)*R);
     204}
     205
     206double kelvin2rankine (double K)
     207{
     208        return ((5.0/9.0)*K);
     209}
     210
     211double fahrenheit2kelvin (double F)
     212{
     213        return ((F+459.67)*(5.0/9.0));
     214}
     215
     216double kelvin2fahrenheit (double K)
     217{
     218        return (((9.0/5.0)*K)-459.67);
     219}
     220
     221/****************************************
     222 * ENERGY CONVERSIONS
     223 ****************************************/
     224
     225double electronVolt2joule (double eV)
     226{
     227        return (eV*1.602177e-19);
     228}
     229
     230double joule2electronVolt (double J)
     231{
     232        return (J*1.602177e19);
     233}
    169234
    170235#ifdef __cplusplus
Note: See TracChangeset for help on using the changeset viewer.