trick/trick_source/trick_swig/swig_convert_units.cpp
Alex Lin a95e8e086b Use udunits package for units conversions
Copied the units_conv.c from 15 that includes the new units to master.

Swept through the code removing includes to Unit.hh and UCF.h where they
are no longer needed.  Remade makefile dependencies.

refs #231
2016-05-17 09:01:37 -05:00

73 lines
2.5 KiB
C++

#include <Python.h>
#include <iostream>
#include <udunits2.h>
#include "trick/UdUnits.hh"
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 ;
}
cv_converter * converter = ut_get_converter(from,to) ;
if ( converter ) {
*val = (long long)cv_convert_double(converter, (double)*val ) ;
cv_free(converter) ;
} else {
PyErr_SetString(PyExc_AttributeError,"Units conversion Error");
return -1 ;
}
ut_free(from) ;
ut_free(to) ;
}
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 ;
}
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 ;
}
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) ;
}