This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Chad,
> I'm currently building a Fortran 2003 interface for Udunits 2.
Cool! Can I get it from you when you're done?
> I'm having
> some trouble testing it though. I need UDUNITS to convert "days since ..."
> to YYYY/MM. In my testing, I became convinced that the interface didn't work
> properly. But I then did some testing in C and found that I'm probably not
> calling UDUNITS properly in either language. Below is code to illustrate
> what my issue is. I've used Udunits 2 in the context of using David Pierce's
> calcalcs extensions of udunits 2's functionality. This involved calling
> ut_read_xml, ut_parse and then utCalendar2_cal. utCalendar2_cal recieves the
> ut_unit type variable passed from ut_parse so it knows what the referene
> time is.
The "ut_unit" type is intended to be opaque: clients should neither know about 
nor care about the details of its implementation. The "calcalcs" package is 
taking a risk if it uses those details.
> In using udunits 2 only, I call ut_read_xml, ut_parse and then
> ut_decode_time. The result however is relative to January 1, 2001 00:00:00.
> How do I make udunits2 aware of my preferred time origin?
I don't understand why you would want to set the time-origin of the UDUNITS-2 
package. What's the use-case or user-story that you're trying to implement?
> Also, is it possible to suppress the "... overrides prefixed-unit ..."
> warnings upon calling ut_read_xml?
To eliminate the warning messages, precede the call to "ut_read_xml" with a 
call to "ut_set_error_message_handler":
    ut_set_error_message_handler(ut_ignore);
    unitSystem = ut_read_xml(_xmlPath);
> Chad
> 
> 
> example code:
> 
> /*
> gcc -Wall -Wextra -pedantic -std=c89 udunits2_demo.c -ludunits2 && ./a.out
> */
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <udunits2.h>
> 
> int main(void)
> {
> const char *units    = "days since 1800-1-1 00:00:0.0";
> 
> ut_system *u_system;
> ut_unit *u_unit;
> 
> double time;
> int year, month, day, hour, minute;
> double second, resolution;
> 
> if( (u_system = ut_read_xml(NULL)) == NULL ){
> fprintf(stderr, "Unable to initialize the udunits2 library!\n");
> exit(EXIT_FAILURE);
> }
> 
> if( (u_unit = ut_parse(u_system, units, UT_ASCII)) == NULL ){
> fprintf(stderr, "Unable to the unit string %s!\n", units);
> exit(EXIT_FAILURE);
> }
> 
> time = 18277.5;
> 
> ut_decode_time(time, &year, &month, &day, &hour, &minute, &second,
> &resolution);
> 
> fprintf(stdout, "%4d %2d %2d %2d %2d %3.1f\n", year, month, day, hour,
> minute, second);
> 
> /*
> output is: 2001  1  1  5  4 37.5
> should be: 1850  1 16 12  0  0.0
> */
> 
> return(EXIT_SUCCESS);
> }
Regards,
Steve Emmerson
Ticket Details
===================
Ticket ID: SJT-401280
Department: Support UDUNITS
Priority: Normal
Status: Closed