Unidata / UDUNITS-2

API and utility for arithmetic manipulation of units of physical quantities
http://www.unidata.ucar.edu/software/udunits
Other
62 stars 36 forks source link

Prefix confusion #58

Closed rhardih closed 6 years ago

rhardih commented 6 years ago

In the documentation, it says:

If you use ut_read_xml(), then you should not normally need to add any new prefixes to a unit-system.

I can't seem to figure out something as simple as conversion from kilometer to meter. I get a NULL ut_type * return when asking for kilometer, although I can prefix kilo to e.g. gram and get a valid type, so I'm assuming the prefixes have loaded correctly. Color me confused.

Please see this example:

#include <stdio.h>

#include "udunits2.h"

int main(int argc, char const *argv[])
{
  ut_system *system = ut_read_xml(NULL);
  ut_unit *from, * to;
  cv_converter *converter;
  double result;

  // kilograms -> grams
  from = ut_get_unit_by_name(system, "kilogram");
  to = ut_get_unit_by_name(system, "gram");
  converter = ut_get_converter(from, to);
  printf("from: %p, to: %p\n", from, to);
  result = cv_convert_double(converter, 12.34);
  printf("kg -> g: %f\n", result);
  cv_free(converter);

  // kilometer -> meter
  from = ut_get_unit_by_name(system, "kilometer");
  to = ut_get_unit_by_name(system, "meter");
  printf("from: %p, to: %p\n", from, to);
  converter = ut_get_converter(from, to);
  result = cv_convert_double(converter, 12.34);
  printf("km -> m: %f\n", result);
  cv_free(converter);

  ut_free_system(system);

  return 0;
}

Which produces the following output:

$ UDUNITS2_XML_PATH=`pwd`/udunits-2-build/share/doc/udunits/udunits2.xml DYLD_LIBRARY_PATH=`pwd`/udunits-2-build/lib ./a.out
Definition of "au" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-accepted.xml", line 123, overrides prefixed-unit "1.6605402e-45 kilogram"
Definition of "kt" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 105, overrides prefixed-unit "1000000 kilogram"
Definition of "microns" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 411, overrides prefixed-unit "1e-15 second"
Definition of "ft" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 522, overrides prefixed-unit "1e-12 kilogram"
Definition of "yd" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 531, overrides prefixed-unit "8.64e-20 second"
Definition of "pt" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 785, overrides prefixed-unit "1e-09 kilogram"
Definition of "at" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 1250, overrides prefixed-unit "1e-15 kilogram"
Definition of "ph" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 1880, overrides prefixed-unit "3.6e-09 second"
Definition of "nt" in "/Users/rene/Code/UDUNITS-2/udunits-2-build/share/doc/udunits/udunits2-common.xml", line 1889, overrides prefixed-unit "1e-06 kilogram"
from: 0x7fb36f800600, to: 0x7fb36f800b80
kg -> g: 12340.000000
from: 0x0, to: 0x7fb36f800c70
ut_get_converter(): NULL unit argument
Segmentation fault: 11

I'm assuming this is not a bug, so what am I doing wrong?

rhardih commented 6 years ago

One thing that might be a bug though, is that when ut_get_unit_by_name returns NULL, ut_get_status still returns UT_SUCCESS, indicating that the "name doesn't map to a unit of system". Is that expected?

I've tried with the alternate spelling kilometre as well, but yielding the same outcome.

semmerson commented 6 years ago

kilometer isn't the name of a unit: meter is. Similarly for kilogram and gram.

Instead of using ut_get_unit_by_name() to parse unit strings, use ut_parse().

rhardih commented 6 years ago

Ah, my mistake. Thanks for clearing that up!

Out of curiosity, how come kilogram does return a valid unit, when only gram should?

semmerson commented 6 years ago

I misspoke. Because it's a base unit, kilogram is the name of a unit (even though it has a prefix). The same isn't true for kilometer.

rhardih commented 6 years ago

Duly noted. Thanks again.