trick/trick_source/trick_swig/swig_convert_units.cpp

73 lines
2.5 KiB
C++
Raw Normal View History

2015-02-26 15:02:31 +00:00
#include <Python.h>
#include <iostream>
#include <udunits2.h>
2015-02-26 15:02:31 +00:00
#include "trick/UdUnits.hh"
2015-02-26 15:02:31 +00:00
int convert_united_value( std::string & to_units , std::string & from_units , long long * val ) {
if ( from_units.compare("1") ) {
ut_unit * from = ut_parse(Trick::UdUnits::get_u_system(), from_units.c_str(), UT_ASCII) ;
if ( !from ) {
PyErr_SetString(PyExc_AttributeError,(std::string("could not covert from units "+from_units).c_str()));
return -1 ;
}
ut_unit * to = ut_parse(Trick::UdUnits::get_u_system(), to_units.c_str(), UT_ASCII) ;
if ( !to ) {
PyErr_SetString(PyExc_AttributeError,(std::string("could not covert to units "+to_units).c_str()));
return -1 ;
2015-02-26 15:02:31 +00:00
}
cv_converter * converter = ut_get_converter(from,to) ;
if ( converter ) {
*val = (long long)cv_convert_double(converter, (double)*val ) ;
cv_free(converter) ;
} else {
2015-02-26 15:02:31 +00:00
PyErr_SetString(PyExc_AttributeError,"Units conversion Error");
return -1 ;
}
ut_free(from) ;
ut_free(to) ;
2015-02-26 15:02:31 +00:00
}
return 0 ;
}
int convert_united_value( std::string & to_units , std::string & from_units , double * val ) {
if ( from_units.compare("1") ) {
ut_unit * from = ut_parse(Trick::UdUnits::get_u_system(), from_units.c_str(), UT_ASCII) ;
if ( !from ) {
PyErr_SetString(PyExc_AttributeError,(std::string("could not covert from units "+from_units).c_str()));
return -1 ;
2015-02-26 15:02:31 +00:00
}
ut_unit * to = ut_parse(Trick::UdUnits::get_u_system(), to_units.c_str(), UT_ASCII) ;
if ( !to ) {
PyErr_SetString(PyExc_AttributeError,(std::string("could not covert to units "+to_units).c_str()));
2015-02-26 15:02:31 +00:00
return -1 ;
}
cv_converter * converter = ut_get_converter(from,to) ;
if ( converter ) {
*val = cv_convert_double(converter, *val ) ;
cv_free(converter) ;
} else {
PyErr_SetString(PyExc_AttributeError,"Units conversion Error");
return -1 ;
}
ut_free(from) ;
ut_free(to) ;
}
return 0 ;
}
int scale_united_value( std::string & to_units , std::string & from_units , long long * val ) {
return convert_united_value(to_units, from_units, val) ;
}
int scale_united_value( std::string & to_units , std::string & from_units , double * val ) {
return convert_united_value(to_units, from_units, val) ;
}