static ut_system * get_u_system() {

    ut_system * u_system ;

    /* Initialize the udunits-2 library */
    ut_set_error_message_handler(ut_ignore) ;
    if( (u_system = ut_read_xml( NULL )) == NULL ) {
        std::cout << "Error initializing udunits-2 unit system" << std::endl ;
        exit(-1);
    }
    ut_set_error_message_handler(ut_write_to_stderr) ;

    return u_system ;
}
Exemple #2
0
/*
 * Initialize the units(3) package.
 */
int
utInit(
    const char	*path)
{
    int status;

    (void)ut_set_error_message_handler(ut_ignore);
    if (unitSystem != NULL) {
	ut_free_system(unitSystem);
	unitSystem = NULL;
    }
    unitSystem = ut_read_xml(NULL);
    if (unitSystem == NULL) {
	status = ut_get_status() == UT_PARSE
	    ? UT_ESYNTAX
	    : UT_EIO;
    }
    else {
	second = ut_get_unit_by_name(unitSystem, "second");
	encodedTimeUnit =
	    ut_offset_by_time(second, ut_encode_time(2001, 1, 1, 0, 0, 0.0));
	buffer = malloc(buflen);
	if (buffer == NULL) {
	    buflen = 0;
	    status = UT_EALLOC;
	}
	else {
	    status = 0;
	}
    }
    return status;
}
Exemple #3
0
// OCaml-friendly initialization
value ml_init( value unit ) {
    CAMLparam1( unit );

    ut_set_error_message_handler( ut_ignore );

    CAMLreturn( Val_unit );
}
Exemple #4
0
/*
 * Function for initializing Udunits-2 package.
 */
ut_system *utopen_ncl()
{
  ut_system *us;
  const char *path = NULL;
  char udunits_file[_NhlMAXFNAMELEN];
  extern int setenv(__const char *__name, __const char *__value, int __replace);


/*
 *  If NCARG_UDUNITS is set, then this directory is used to
 *  look for the "udunits2.xml" file.
 *
 *  Otherwise, the path within NCL ($NCARG_ROOT/lib/ncarg/udunits/)
 *  is used.
 *
 *  UDUNITS2_XML_PATH is not recognized internally by NCL. This is
 *  because it could be very common for the user to have this set
 *  for some other package, like Udunits, and this other file might
 *  not be compatible with what NCL expects.
 *
 */
  path = getenv("NCARG_UDUNITS");
  if ( (void *)path == (void *)NULL) {
    path = _NGGetNCARGEnv("udunits");
  }
  strcpy(udunits_file,path);
  strcat(udunits_file,_NhlPATHDELIMITER);
  strcat(udunits_file,"udunits2.xml");
/*
 * Internally set UDUNITS2_XML_PATH, forcing it to overwrite
 * any setting the user might have.
 */
  (void)setenv("UDUNITS2_XML_PATH",udunits_file,1);

  /* Turn annoying "override" errors off */
  ut_set_error_message_handler( ut_ignore );

  /* Init udunits-2 lib */
  us = ut_read_xml(NULL);

  /* Turn errors back on */
  ut_set_error_message_handler( ut_write_to_stderr );

  return(us);
}
Exemple #5
0
void R_ut_init(const int *print_warning_on_failure) {
  ut_status stat;

  ut_set_error_message_handler((ut_error_message_handler) Rvprintf);
  if (sys != NULL) {
    ut_free_system(sys);
  }
  ut_set_error_message_handler(ut_ignore);
  sys = ut_read_xml(NULL);
  ut_set_error_message_handler((ut_error_message_handler) Rvprintf);
  if (sys == NULL) {
    stat = ut_get_status();
    if (*print_warning_on_failure)
		ut_handle_error_message("Warning in R_ut_init: %s\n", ut_status_strings[stat]);
    return;
  }
  enc = UT_UTF8;
  return;
}
  udunits::udunits(std::string const& filename) 
  {
    unit_system_ = ut_read_xml(filename.c_str());
    ut_status stat = ut_get_status();
    if(stat == UT_OPEN_ARG)   throw udunits_exception("Could not open unit database!");
    else if(stat == UT_OS)    throw udunits_exception("Unit database encountered operating system error!");
    else if(stat == UT_PARSE) throw udunits_exception("Unit database could not process unit XML file!");

    ut_set_error_message_handler(&ut_ignore);
  }
Exemple #7
0
void udu_utinit( char *path )
{
	/* Turn annoying "override" errors off */
	ut_set_error_message_handler( ut_ignore );

	/* Init udunits-2 lib */
	unitsys = ut_read_xml( path );

	/* Turn errors back on */
	ut_set_error_message_handler( ut_write_to_stderr );

	if( unitsys != NULL )
		valid_udunits_pkg = 1;
	else
		{
		valid_udunits_pkg = 0;
		fprintf( stderr, "Note: Udunits-2 library could not be initialized; no units conversion will be attmpted.\n" );
		fprintf( stderr, "(To fix, put the path of the units file into environmental variable UDUNITS2_XML_PATH)\n");
		}
}
Exemple #8
0
int /* [rcd] Successful conversion returns NCO_NOERR */
nco_cln_prs_tm /* UDUnits2 Extract time stamp from parsed UDUnits string */
(const char *unt_sng, /* I [ptr] units attribute string */
 tm_cln_sct *tm_in) /* O [sct] Time structure to be populated */
{
  const char fnc_nm[]="nco_cln_prs_tm()"; /* [sng] Function name */

  /* 20141230: fxm figure out a better length */
  char bfr[200];

  char *dt_sng;

  int ut_rcd; /* [enm] UDUnits2 status */

  ut_system *ut_sys;
  ut_unit *ut_sct_in; /* UDUnits structure, input units */

  /* When empty, ut_read_xml() uses environment variable UDUNITS2_XML_PATH, if any
     Otherwise it uses default initial location hardcoded when library was built */
  if(nco_dbg_lvl_get() >= nco_dbg_vrb) ut_set_error_message_handler(ut_write_to_stderr); else ut_set_error_message_handler(ut_ignore);
  ut_sys=ut_read_xml(NULL);
  if(ut_sys == NULL){
    (void)fprintf(stdout,"%s: %s failed to initialize UDUnits2 library\n",nco_prg_nm_get(),fnc_nm);
    return NCO_ERR; /* Failure */
  } /* end if err */ 

  /* Units string to convert from */
  ut_sct_in=ut_parse(ut_sys,unt_sng,UT_ASCII); 
  if(ut_sct_in == NULL){ /* Problem with 'units' attribute */
    ut_rcd=ut_get_status(); /* [enm] UDUnits2 status */
    if(ut_rcd == UT_BAD_ARG) (void)fprintf(stderr,"ERROR: empty units attribute string\n");
    if(ut_rcd == UT_SYNTAX)  (void)fprintf(stderr,"ERROR: units attribute \"%s\" has a syntax error\n",unt_sng);
    if(ut_rcd == UT_UNKNOWN) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is not listed in UDUnits2 SI system database\n",unt_sng);

    return NCO_ERR; /* Failure */
  } /* endif coordinate on disk has no units attribute */

  /* Print timestamp to buffer in standard, dependable format */
  ut_format(ut_sct_in,bfr,sizeof(bfr),UT_ASCII|UT_NAMES);

  /* Extract parsed time units from print string (kludgy)
     20141230 change to using ut_decode_time() instead? */
  dt_sng=strstr(bfr,"since");  
  sscanf(dt_sng,"%*s %d-%d-%d %d:%d:%f",&tm_in->year,&tm_in->month,&tm_in->day,&tm_in->hour,&tm_in->min,&tm_in->sec);

  ut_free_system(ut_sys); /* Free memory taken by UDUnits library */
  ut_free(ut_sct_in);

  return NCO_NOERR;
} /* end UDUnits2 nco_cln_prs_tm() */
Exemple #9
0
static int
readXmlDatabase(void)
{
    int		success = 0;

    if (!_reveal)
        ut_set_error_message_handler(ut_ignore);

    _unitSystem = ut_read_xml(_xmlPath);

    ut_set_error_message_handler(ut_write_to_stderr);

    if (_unitSystem != NULL) {
        success = 1;
    }
    else {
        ut_status	status;

        errMsg("Couldn't initialize unit-system from database \"%s\": %s",
                ut_get_path_xml(_xmlPath, &status), strerror(errno));
    }

    return success;
}
Exemple #10
0
static int unit_system_init(void)
{
    if (unit_system == NULL)
    {
        ut_set_error_message_handler(ut_ignore);

        unit_system = ut_read_xml(NULL);
        if (unit_system == NULL)
        {
            handle_udunits_error();
            return -1;
        }
    }

    return 0;
}
Exemple #11
0
void check_ut_read(char *xmlFile) {
  extern ut_system *ut_read;
  int status=0;
  if (ut_read != NULL) {
    return;
  }
  ut_set_error_message_handler(ut_ignore);
  ut_read = ut_read_xml(xmlFile);
  if (ut_read == NULL) {
    printf("FAILED INIT\n");
    status = ut_get_status() == UT_PARSE
      ? UT_ESYNTAX
      : UT_EIO;
  }
  return;
}
Exemple #12
0
int /* [rcd] Return code */
nco_cln_sng_rbs /* [fnc] Rebase calendar string for legibility */
(const ptr_unn val, /* I [sct] Value to rebase */
 const long val_idx, /* I [idx] Index into 1-D array of values */
 const nc_type val_typ, /* I [enm] Value type */
 const char *unit_sng, /* I [sng] Units string */
 char *lgb_sng) /* O [sng] Legible version of input string */
{
  /* Purpose: Rebase calendar string for legibility
     Assumptions: Input units string unit_sng is a calendar date, i.e., contains "from", "since", or "after"

     ncdump handles this in nctime0.c
     dumplib.c/nctime_val_tostring() by Dave Allured, NOAA
     cdRel2Iso() from CDMS by Bob Drach, LLNL
     cdParseRelunits() from CDMS by Bob Drach, LLNL */

#ifdef HAVE_UDUNITS2_H

  const char fnc_nm[]="nco_cln_sng_rbs()"; /* [sng] Function name */
  
  double val_dbl; /* [day] Calendar offset converted to double */

  int ut_rcd; /* [enm] UDUnits2 status */
  
  ut_system *ut_sys;
  ut_unit *ut_sct_in; /* [sct] UDUnits structure, input units */
  ut_unit *ut_sct_out; /* [sct] UDUnits structure, output units */

  /* Quick return if units DNE */
  if(!unit_sng) return NCO_NOERR;
  
  /* When empty, ut_read_xml() uses environment variable UDUNITS2_XML_PATH, if any
     Otherwise it uses default initial location hardcoded when library was built */
  if(nco_dbg_lvl_get() >= nco_dbg_vrb) ut_set_error_message_handler(ut_write_to_stderr); else ut_set_error_message_handler(ut_ignore);
  ut_sys=ut_read_xml(NULL);
  if(!ut_sys){
    (void)fprintf(stdout,"%s: %s() failed to initialize UDUnits2 library\n",nco_prg_nm_get(),fnc_nm);
    return NCO_ERR; /* Failure */
  } /* end if err */ 

  /* Units string containing calendar origin converted to UDUnit structure */
  ut_sct_in=ut_parse(ut_sys,unit_sng,UT_ASCII); 
  if(!ut_sct_in){ /* Problem with 'units' attribute */
    ut_rcd=ut_get_status(); /* [enm] UDUnits2 status */
    if(ut_rcd == UT_BAD_ARG) (void)fprintf(stderr,"ERROR: empty units attribute string\n");
    if(ut_rcd == UT_SYNTAX) (void)fprintf(stderr,"ERROR: units attribute \"%s\" has a syntax error\n",unit_sng);
    if(ut_rcd == UT_UNKNOWN) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is not listed in UDUnits2 SI system database\n",unit_sng);
    return NCO_ERR; /* Failure */
  } /* endif coordinate on disk has no units attribute */

  /* Convert time since calendar origin to double */
  val_dbl=ptr_unn_2_scl_dbl(val,val_typ); 
  
  /* Units string to convert to */
  ut_sct_out=ut_offset(ut_sct_in,val_dbl);
  if(!ut_sct_out){ /* Problem with 'units' attribute */
    ut_rcd=ut_get_status(); /* [enm] UDUnits2 status */
    if(ut_rcd == UT_BAD_ARG) (void)fprintf(stderr,"ERROR: Empty units attribute string\n");
    if(ut_rcd == UT_SYNTAX) (void)fprintf(stderr,"ERROR: units attribute  \"%s\" has a syntax error\n",unit_sng);
    if(ut_rcd == UT_UNKNOWN) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is not listed in UDUnits2 SI system database\n",unit_sng);
    return NCO_ERR; /* Failure */
  } /* endif */

  val_dbl+=0*val_idx; /* CEWI */

  ut_free(ut_sct_in);
  ut_free(ut_sct_out);
  ut_free_system(ut_sys); /* Free memory taken by UDUnits library */
#endif /* !HAVE_UDUNITS2 */

  lgb_sng[0]='\0'; /* CEWI */

  return NCO_NOERR;

} /* end nco_cln_sng_rbs() */
Exemple #13
0
int /* [rcd] Return code */
nco_cln_clc_dff /* [fnc] UDUnits2 Compute difference between two coordinate units */
(const char *fl_unt_sng, /* I [ptr] units attribute string from disk */
 const char *fl_bs_sng, /* I [ptr] units attribute string from disk */
 double crr_val,
 double *og_val) /* O [] Difference between two units strings */
{
  const char fnc_nm[]="nco_cln_clc_dff()"; /* [sng] Function name */
  
  cv_converter *ut_cnv; /* UDUnits converter */

  int ut_rcd; /* [enm] UDUnits2 status */
  
  ut_system *ut_sys;
  ut_unit *ut_sct_in; /* [sct] UDUnits structure, input units */
  ut_unit *ut_sct_out; /* [sct] UDUnits structure, output units */
  
  /* Quick return if units identical */
  if(!strcasecmp(fl_unt_sng,fl_bs_sng)){
    *og_val=crr_val;  
    return NCO_NOERR;
  } /* end if */
  
  /* When empty, ut_read_xml() uses environment variable UDUNITS2_XML_PATH, if any
     Otherwise it uses default initial location hardcoded when library was built */
  if(nco_dbg_lvl_get() >= nco_dbg_vrb) ut_set_error_message_handler(ut_write_to_stderr); else ut_set_error_message_handler(ut_ignore);
  ut_sys=ut_read_xml(NULL);
  if(ut_sys == NULL){
    (void)fprintf(stdout,"%s: %s() failed to initialize UDUnits2 library\n",nco_prg_nm_get(),fnc_nm);
    return NCO_ERR; /* Failure */
  } /* end if err */ 
  
  /* Units string to convert from */
  ut_sct_in=ut_parse(ut_sys,fl_unt_sng,UT_ASCII); 
  if(!ut_sct_in){ /* Problem with 'units' attribute */
    ut_rcd=ut_get_status(); /* [enm] UDUnits2 status */
    if(ut_rcd == UT_BAD_ARG) (void)fprintf(stderr,"ERROR: empty units attribute string\n");
    if(ut_rcd == UT_SYNTAX) (void)fprintf(stderr,"ERROR: units attribute \"%s\" has a syntax error\n",fl_unt_sng);
    if(ut_rcd == UT_UNKNOWN) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is not listed in UDUnits2 SI system database\n",fl_unt_sng);
    return NCO_ERR; /* Failure */
  } /* endif coordinate on disk has no units attribute */

  /* Units string to convert to */
  ut_sct_out=ut_parse(ut_sys,fl_bs_sng,UT_ASCII); 
  if(!ut_sct_out){ /* Problem with 'units' attribute */
    ut_rcd=ut_get_status(); /* [enm] UDUnits2 status */
    if(ut_rcd == UT_BAD_ARG) (void)fprintf(stderr,"ERROR: Empty units attribute string\n");
    if(ut_rcd == UT_SYNTAX) (void)fprintf(stderr,"ERROR: units attribute  \"%s\" has a syntax error\n",fl_bs_sng);
    if(ut_rcd == UT_UNKNOWN) (void)fprintf(stderr,"ERROR: units attribute \"%s\" is not listed in UDUnits2 SI system database\n",fl_bs_sng);
    return NCO_ERR; /* Failure */
  } /* endif */

  /* Create converter */
  ut_cnv=ut_get_converter(ut_sct_in,ut_sct_out); /* UDUnits converter */
  if(!ut_cnv){
    ut_rcd=ut_get_status(); /* [enm] UDUnits2 status */
    if(ut_rcd == UT_BAD_ARG) (void)fprintf(stderr,"WARNING: One of units, %s or %s, is NULL\n",fl_bs_sng,fl_unt_sng);
    if(ut_rcd == UT_NOT_SAME_SYSTEM) (void)fprintf(stderr,"WARNING: Units %s and %s belong to different unit systems\n",fl_bs_sng,fl_unt_sng);
    if(ut_rcd == UT_MEANINGLESS) (void)fprintf(stderr,"WARNING: Conversion between user-specified unit \"%s\" and file units \"%s\" is meaningless\n",fl_bs_sng,fl_unt_sng);
    return NCO_ERR; /* Failure */
  } /* endif */

  /* Convert */
  *og_val=cv_convert_double(ut_cnv,crr_val);
  
  if(nco_dbg_lvl_get() >= nco_dbg_var) fprintf(stderr, "%s: INFO %s() reports conversion between systems \"%s\" and \"%s\" is %f\n",nco_prg_nm_get(),fnc_nm,fl_unt_sng,fl_bs_sng,*og_val);

  ut_free(ut_sct_in);
  ut_free(ut_sct_out);
  cv_free(ut_cnv);
  ut_free_system(ut_sys); /* Free memory taken by UDUnits library */

  return NCO_NOERR;
}  /* end UDUnits2 nco_cln_clc_dff() */