static ut_status isTimeVisitProduct( const ut_unit* unit, int count, const ut_unit* const* basicUnits, const int* powers, void* arg) { int isTime; if (!ut_are_convertible(unit, second)) { isTime = 0; } else { int i; isTime = 0; for (i = 0; i < count; i++) { if (ut_are_convertible(basicUnits[i], second) && powers[i] == 1) { isTime = 1; break; } } } return (ut_status)isTime; }
void R_ut_are_convertible(char * const *ustring1, char * const *ustring2, int *convertible) { ut_unit *u1, *u2; int one = 1; if (sys == NULL) { R_ut_init(&one); } ut_trim(*ustring1, enc); ut_trim(*ustring2, enc); u1 = ut_parse(sys, *ustring1, enc); u2 = ut_parse(sys, *ustring2, enc); if (!(u1 && u2)) { handle_error("R_ut_are_convertible"); } if (ut_are_convertible(u1, u2) == 0) { *convertible = 0; } else { *convertible = 1; } ut_free(u1); ut_free(u2); return; }
static ut_status isTimeVisitBasic( const ut_unit* unit, void* arg) { return (ut_status)ut_are_convertible(unit, second); }
int udu_utistime( char *dimname, char *unitstr ) { int ierr; static int have_initted=0; ut_unit *unit; static ut_unit *time_unit_with_origin; if( (unitstr == NULL) || (!valid_udunits_pkg)) return(0); if( (unit = ut_parse( unitsys, unitstr, UT_ASCII)) == NULL ) { /* can't parse unit spec */ if( is_unique(unitstr) ) fprintf( stderr, "Note: udunits: unknown units for %s: \"%s\"\n", dimname, unitstr ); return( 0 ); } if(!have_initted) { if( (time_unit_with_origin = ut_parse( unitsys, "seconds since 1901-01-01", UT_ASCII)) == NULL ) { fprintf( stderr, "Internal error: could not ut_parse time origin test string\n" ); valid_udunits_pkg = 0; return(0); } have_initted = 1; } ierr = ut_are_convertible( unit, time_unit_with_origin ); ut_free( unit ); return( ierr != 0 ); /* Oddly, returns a non-zero number if ARE convertible */ }
value ml_ut_are_convertible( value from, value to ) { CAMLparam2( from, to ); int result; result = ut_are_convertible( UD_ut_unit_val( from ), UD_ut_unit_val( to ) ); CAMLreturn( Val_bool( result ) ); }
int harp_unit_converter_new(const char *from_unit, const char *to_unit, harp_unit_converter **new_unit_converter) { harp_unit_converter *unit_converter; ut_unit *from_udunit; ut_unit *to_udunit; if (parse_unit(from_unit, &from_udunit) != 0) { return -1; } if (parse_unit(to_unit, &to_udunit) != 0) { ut_free(from_udunit); return -1; } if (!ut_are_convertible(from_udunit, to_udunit)) { harp_set_error(HARP_ERROR_UNIT_CONVERSION, "unit '%s' cannot be converted to unit '%s'", from_unit, to_unit); ut_free(to_udunit); ut_free(from_udunit); return -1; } unit_converter = (harp_unit_converter *)malloc(sizeof(harp_unit_converter)); if (unit_converter == NULL) { harp_set_error(HARP_ERROR_OUT_OF_MEMORY, "out of memory (could not allocate %lu bytes) (%s:%u)", sizeof(harp_unit_converter), __FILE__, __LINE__); ut_free(to_udunit); ut_free(from_udunit); return -1; } unit_converter->converter = ut_get_converter(from_udunit, to_udunit); if (unit_converter->converter == NULL) { handle_udunits_error(); harp_unit_converter_delete(unit_converter); ut_free(to_udunit); ut_free(from_udunit); return -1; } ut_free(to_udunit); ut_free(from_udunit); *new_unit_converter = unit_converter; return 0; }
int udu_calc_tgran( int fileid, NCVar *v, int dimid ) { ut_unit *unit, *seconds; int ii, retval; int verbose, has_bounds; double tval0_user, tval0_sec, tval1_user, tval1_sec, delta_sec, bound_min, bound_max; char cval0[1024], cval1[1024]; nc_type rettype; size_t cursor_place[MAX_NC_DIMS]; cv_converter *convert_units_to_sec; NCDim *d; d = *(v->dim+dimid); /* Not enough data to analyze */ if( d->size < 3 ) return(TGRAN_SEC); verbose = 0; if( ! valid_udunits_pkg ) return( 0 ); if( (unit = ut_parse( unitsys, d->units, UT_ASCII )) == NULL ) { fprintf( stderr, "internal error: udu_calc_tgran with invalid unit string: %s\n", d->units ); exit( -1 ); } if( (seconds = ut_parse( unitsys, "seconds", UT_ASCII )) == NULL ) { fprintf( stderr, "internal error: udu_calc_tgran can't parse seconds unit string!\n" ); exit( -1 ); } /* Get converter to convert from "units" to "seconds" */ if( ut_are_convertible( unit, seconds ) == 0 ) { /* Units are not convertible */ return( TGRAN_SEC ); } if( (convert_units_to_sec = ut_get_converter( unit, seconds )) == NULL ) { /* This shouldn't happen */ return( TGRAN_SEC ); } /* Get a delta time to analyze */ for( ii=0L; ii<v->n_dims; ii++ ) cursor_place[ii] = (int)((*(v->size+ii))/2.0); rettype = fi_dim_value( v, dimid, 1L, &tval0_user, cval0, &has_bounds, &bound_min, &bound_max, cursor_place ); rettype = fi_dim_value( v, dimid, 2L, &tval1_user, cval1, &has_bounds, &bound_min, &bound_max, cursor_place ); /* Convert time vals from user units to seconds */ tval0_sec = cv_convert_double( convert_units_to_sec, tval0_user ); tval1_sec = cv_convert_double( convert_units_to_sec, tval1_user ); /* Free our units converter storage */ cv_free( convert_units_to_sec ); delta_sec = fabs(tval1_sec - tval0_sec); if( verbose ) printf( "units: %s t1: %lf t2: %lf delta-sec: %lf\n", d->units, tval0_user, tval1_user, delta_sec ); if( delta_sec < 57. ) { if(verbose) printf("data is TGRAN_SEC\n"); retval = TGRAN_SEC; } else if( delta_sec < 3590. ) { if(verbose) printf("data is TGRAN_MIN\n"); retval = TGRAN_MIN; } else if( delta_sec < 86300. ) { if(verbose) printf("data is TGRAN_HOUR\n"); retval = TGRAN_HOUR; } else if( delta_sec < 86395.*28. ) { if(verbose) printf("data is TGRAN_DAY\n"); retval = TGRAN_DAY; } else if( delta_sec < 86395.*365. ) { if(verbose) printf("data is TGRAN_MONTH\n"); retval = TGRAN_MONTH; } else { if(verbose) printf("data is TGRAN_YEAR\n"); retval = TGRAN_YEAR; } return( retval ); }
static int handleRequest(void) { int success = 0; if (getInputValue()) { if (getOutputRequest()) { if (_wantDefinition) { char buf[256]; ut_unit* unit = ut_scale(_haveUnitAmount, _haveUnit); int nbytes = ut_format(unit, buf, sizeof(buf), _formattingOptions); if (nbytes >= sizeof(buf)) { errMsg("Resulting unit specification is too long"); } else if (nbytes >= 0) { buf[nbytes] = 0; (void)printf(" %s\n", buf); } ut_free(unit); } else if (!ut_are_convertible(_wantUnit, _haveUnit)) { errMsg("Units are not convertible"); } else { cv_converter* conv = ut_get_converter(_haveUnit, _wantUnit); if (conv == NULL) { errMsg("Couldn't get unit converter"); } else { char haveExp[_POSIX_MAX_INPUT+1]; char exp[_POSIX_MAX_INPUT+1]; char whiteSpace[] = " \t\n\r\f\v\xa0"; int needsParens = strpbrk(_wantSpec, whiteSpace) != NULL; int n; (void)printf( needsParens ? " %g %s = %g (%s)\n" : " %g %s = %g %s\n", _haveUnitAmount, _haveUnitSpec, cv_convert_double(conv, _haveUnitAmount), _wantSpec); (void)sprintf(haveExp, strpbrk(_haveUnitSpec, whiteSpace) || strpbrk(_haveUnitSpec, "/") ? "(x/(%s))" : "(x/%s)", _haveUnitSpec); n = cv_get_expression(conv, exp, sizeof(exp), haveExp); if (n >= 0) (void)printf( strpbrk(_wantSpec, whiteSpace) || strpbrk(_wantSpec, "/") ? " x/(%s) = %*s\n" : " x/%s = %*s\n", _wantSpec, n, exp); cv_free(conv); } } success = 1; } } return success; }