Use udunits package for units conversions

Change the Variable Server to use udunits.

refs #231
This commit is contained in:
Alex Lin 2016-05-16 17:14:50 -05:00
parent 9d3a51625f
commit 7e218a0472
4 changed files with 46 additions and 23 deletions

View File

@ -7,7 +7,8 @@
#define VARIABLESERVERREFERENCE_HH
#include "trick/reference.h"
#include "trick/UCFn.hh"
union cv_converter ;
#define MAX_ARRAY_LENGTH 4096
@ -25,7 +26,7 @@ namespace Trick {
/** Pointer to trick variable reference structure.\n */
REF2 * ref ;
UCFn * conversion_factor ;
cv_converter * conversion_factor ; // ** udunits conversion factor
void * buffer_in ;
void * buffer_out ;
void * address ; // -- address of data copied to buffer

View File

@ -1,6 +1,7 @@
#include <stdlib.h>
#include <iostream>
#include <udunits2.h>
#include "trick/VariableServer.hh"
#include "trick/memorymanager_c_intf.h"
#include "trick/wcs_ext.h"
@ -13,7 +14,8 @@ Trick::VariableReference::VariableReference(REF2 * in_ref ) {
int k ;
// VariableReference copy setup: set address & size to copy into buffer
conversion_factor = new UCFn(in_ref->attr->units, in_ref->attr->units, 1.0 , 0.0) ;
conversion_factor = cv_get_trivial() ;
ref = in_ref ;
address = ref->address ;
size = ref->attr->size ;

View File

@ -2,6 +2,7 @@
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <udunits2.h>
#include "trick/VariableServer.hh"
#include "trick/variable_server_message_types.h"
#include "trick/memorymanager_c_intf.h"
@ -13,6 +14,8 @@
#include "trick/message_type.h"
#include "trick/TrickConstant.hh"
#include "trick/sie_c_intf.h"
#include "trick/UdUnits.hh"
#include "trick/map_trick_units_to_udunits.hh"
int Trick::VariableServerThread::bad_ref_int = 0 ;
int Trick::VariableServerThread::do_not_resolve_bad_ref_int = 0 ;
@ -115,15 +118,31 @@ int Trick::VariableServerThread::var_units(std::string var_name, std::string uni
vars[ii]->ref->units = strdup(vars[ii]->ref->attr->units);
}
else {
Unit orig_units(vars[ii]->ref->attr->units) ;
try {
vars[ii]->conversion_factor = orig_units.Conversion_to(units_name.c_str()) ;
vars[ii]->ref->units = strdup(units_name.c_str());
std::string new_units = map_trick_units_to_udunits(units_name) ;
if ( units_name.compare(new_units) ) {
std::cout << "\033[33mUnits converted from [" << units_name << "] to [" << new_units
<< "] in Variable Server for " << var_name << "\033[0m" << std::endl ;
}
catch (Unit::CONVERSION_ERROR & ce_err ) {
message_publish(MSG_ERROR, "Variable Server Error: var_units Units conversion error for \"%s\".\n",var_name.c_str());
return(-1) ;
ut_unit * from = ut_parse(Trick::UdUnits::get_u_system(), vars[ii]->ref->attr->units, UT_ASCII) ;
if ( !from ) {
message_publish(MSG_ERROR, "Variable Server Error: var_units Units conversion error for \"%s\".\n",
var_name.c_str());
return -1 ;
}
ut_unit * to = ut_parse(Trick::UdUnits::get_u_system(), new_units.c_str(), UT_ASCII) ;
if ( !to ) {
message_publish(MSG_ERROR, "Variable Server Error: var_units Units conversion error for \"%s\".\n",
var_name.c_str());
return -1 ;
}
cv_converter * conversion_factor = ut_get_converter(from,to) ;
if ( conversion_factor != NULL ) {
// only assign conversion_factor if it is not NULL, otherwise leave previous value.
vars[ii]->conversion_factor = conversion_factor ;
}
vars[ii]->ref->units = strdup(units_name.c_str());
ut_free(from) ;
ut_free(to) ;
}
}
}

View File

@ -8,6 +8,7 @@ PROGRAMMERS: (((Keith Vetter) (LinCom) (September 2001) (--)))
#include <string.h>
#include <ctype.h>
#include <limits>
#include <udunits2.h>
#include "trick/parameter_types.h"
#include "trick/attributes.h"
@ -40,7 +41,7 @@ int vs_format_ascii(Trick::VariableReference * var, char *value) {
case TRICK_CHARACTER:
if (ref->attr->num_index == ref->num_index) {
sprintf(value, "%s%d", value,(char)var->conversion_factor->eval(*(char *)buf_ptr));
sprintf(value, "%s%d", value,(char)cv_convert_double(var->conversion_factor, *(char *)buf_ptr));
} else {
/* All but last dim specified, leaves a char array */
escape_str((char *) buf_ptr, value);
@ -49,7 +50,7 @@ int vs_format_ascii(Trick::VariableReference * var, char *value) {
break;
case TRICK_UNSIGNED_CHARACTER:
if (ref->attr->num_index == ref->num_index) {
sprintf(value, "%s%u", value,(unsigned char)var->conversion_factor->eval(*(unsigned char *)buf_ptr));
sprintf(value, "%s%u", value,(unsigned char)cv_convert_double(var->conversion_factor,*(unsigned char *)buf_ptr));
} else {
/* All but last dim specified, leaves a char array */
escape_str((char *) buf_ptr, value);
@ -97,16 +98,16 @@ int vs_format_ascii(Trick::VariableReference * var, char *value) {
#if ( __linux | __sgi )
case TRICK_BOOLEAN:
sprintf(value, "%s%d", value,(unsigned char)var->conversion_factor->eval(*(unsigned char *)buf_ptr));
sprintf(value, "%s%d", value,(unsigned char)cv_convert_double(var->conversion_factor,*(unsigned char *)buf_ptr));
break;
#endif
case TRICK_SHORT:
sprintf(value, "%s%d", value, (short)var->conversion_factor->eval(*(short *)buf_ptr));
sprintf(value, "%s%d", value, (short)cv_convert_double(var->conversion_factor,*(short *)buf_ptr));
break;
case TRICK_UNSIGNED_SHORT:
sprintf(value, "%s%u", value,(unsigned short)var->conversion_factor->eval(*(unsigned short *)buf_ptr));
sprintf(value, "%s%u", value,(unsigned short)cv_convert_double(var->conversion_factor,*(unsigned short *)buf_ptr));
break;
case TRICK_INTEGER:
@ -114,7 +115,7 @@ int vs_format_ascii(Trick::VariableReference * var, char *value) {
#if ( __sun | __APPLE__ )
case TRICK_BOOLEAN:
#endif
sprintf(value, "%s%d", value, (int)var->conversion_factor->eval(*(int *)buf_ptr));
sprintf(value, "%s%d", value, (int)cv_convert_double(var->conversion_factor,*(int *)buf_ptr));
break;
case TRICK_BITFIELD:
@ -125,23 +126,23 @@ int vs_format_ascii(Trick::VariableReference * var, char *value) {
sprintf(value, "%u", GET_UNSIGNED_BITFIELD(buf_ptr, ref->attr->size, ref->attr->index[0].start, ref->attr->index[0].size));
break;
case TRICK_UNSIGNED_INTEGER:
sprintf(value, "%s%u", value, (unsigned int)var->conversion_factor->eval(*(unsigned int *)buf_ptr));
sprintf(value, "%s%u", value, (unsigned int)cv_convert_double(var->conversion_factor,*(unsigned int *)buf_ptr));
break;
case TRICK_LONG:
sprintf(value, "%s%ld", value, (long)var->conversion_factor->eval(*(long *)buf_ptr));
sprintf(value, "%s%ld", value, (long)cv_convert_double(var->conversion_factor,*(long *)buf_ptr));
break;
case TRICK_UNSIGNED_LONG:
sprintf(value, "%s%lu", value, (unsigned long)var->conversion_factor->eval(*(unsigned long *)buf_ptr));
sprintf(value, "%s%lu", value, (unsigned long)cv_convert_double(var->conversion_factor,*(unsigned long *)buf_ptr));
break;
case TRICK_FLOAT:
sprintf(value, "%s%.8g", value, var->conversion_factor->eval(*(float *)buf_ptr));
sprintf(value, "%s%.8g", value, cv_convert_float(var->conversion_factor,*(float *)buf_ptr));
break;
case TRICK_DOUBLE:
sprintf(value, "%s%.16g", value, var->conversion_factor->eval(*(double *)buf_ptr));
sprintf(value, "%s%.16g", value, cv_convert_double(var->conversion_factor,*(double *)buf_ptr));
break;
case TRICK_LONG_LONG:
@ -152,12 +153,12 @@ int vs_format_ascii(Trick::VariableReference * var, char *value) {
if (!var_name.compare("trick_sys.sched.terminate_time")) {
sprintf(value, "%s%lld", value, *(long long *)buf_ptr);
} else {
sprintf(value, "%s%lld", value, (long long)var->conversion_factor->eval(*(long long *)buf_ptr));
sprintf(value, "%s%lld", value, (long long)cv_convert_double(var->conversion_factor,*(long long *)buf_ptr));
}
break;
case TRICK_UNSIGNED_LONG_LONG:
sprintf(value, "%s%llu", value,(unsigned long long)var->conversion_factor->eval(*(unsigned long long *)buf_ptr));
sprintf(value, "%s%llu", value,(unsigned long long)cv_convert_double(var->conversion_factor,*(unsigned long long *)buf_ptr));
break;
case TRICK_NUMBER_OF_TYPES: