| 1 | /* |
| 2 | part of this was modified from: https://github.com/pacificclimate/Rudunits2 |
| 3 | |
| 4 | (c) James Hiebert <[email protected]> |
| 5 | Pacific Climate Impacts Consortium |
| 6 | August, 16, 2010 |
| 7 | |
| 8 | Functions to support the R interface to the udunits (API version 2) library |
| 9 | */ |
| 10 | |
| 11 | #include "units_types.h" |
| 12 | |
| 13 | extern "C" { |
| 14 | int r_error_fn(const char* fmt, va_list args) { |
| 15 | char buf[256]; |
| 16 | vsnprintf(buf, (size_t) 256, fmt, args); |
| 17 | Rcpp::stop("%s", buf); |
| 18 | return 0; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | using namespace Rcpp; |
| 23 | |
| 24 | static ut_system *sys = NULL; |
| 25 | static ut_encoding enc = UT_UTF8; |
| 26 | |
| 27 | /* High-level functions *******************************************************/ |
| 28 | |
| 29 | // [[Rcpp::export(rng=false)]] |
| 30 | void ud_exit() { |
| 31 | R_gc(); |
| 32 | ut_free_system(sys); |
| 33 | sys = NULL; |
| 34 | } |
| 35 | |
| 36 | // [[Rcpp::export(rng=false)]] |
| 37 | void ud_init(CharacterVector path) { |
| 38 | ut_set_error_message_handler(ut_ignore); |
| 39 | ud_exit(); |
| 40 | for (int i = 0; i < path.size(); i++) { |
| 41 | if ((sys = ut_read_xml(path[i])) != NULL) |
| 42 | break; |
| 43 | } |
| 44 | if (sys == NULL) |
| 45 | sys = ut_read_xml(NULL); // #nocov |
| 46 | ut_set_error_message_handler(r_error_fn); |
| 47 | if (sys == NULL) |
| 48 | stop("no database found!"); // #nocov |
| 49 | } |
| 50 | |
| 51 | // [[Rcpp::export(rng=false)]] |
| 52 | void ud_set_encoding(std::string enc_str) { |
| 53 | if (enc_str.compare("utf8") == 0) |
| 54 | enc = UT_UTF8; |
| 55 | else if (enc_str.compare("ascii") == 0) |
| 56 | enc = UT_ASCII; |
| 57 | else if (enc_str.compare("iso-8859-1") == 0 || enc_str.compare("latin1") == 0) |
| 58 | enc = UT_LATIN1; |
| 59 | else |
| 60 | stop("Valid encoding string parameters are ('utf8'|'ascii'|'iso-8859-1','latin1')"); |
| 61 | } |
| 62 | |
| 63 | // [[Rcpp::export(rng=false)]] |
| 64 | IntegerVector ud_compare(NumericVector x, NumericVector y, |
| 65 | std::string xn, std::string yn) |
| 66 | { |
| 67 | bool swapped = false; |
| 68 | |
| 69 | if (y.size() > x.size()) { |
| 70 | std::swap(x, y); |
| 71 | std::swap(xn, yn); |
| 72 | swapped = true; |
| 73 | } |
| 74 | |
| 75 | IntegerVector out(x.size()); |
| 76 | if (y.size() == 0) return IntegerVector(0); |
| 77 | for (std::string &attr : x.attributeNames()) |
| 78 | out.attr(attr) = x.attr(attr); |
| 79 | |
| 80 | xut_unit ux(ut_parse(sys, ut_trim(xn.data(), enc), enc)); |
| 81 | xut_unit uy(ut_parse(sys, ut_trim(yn.data(), enc), enc)); |
| 82 | |
| 83 | if (ut_compare(ux.get(), uy.get()) != 0) { |
| 84 | NumericVector y_cv = clone(y); |
| 85 | xcv_converter cv(ut_get_converter(uy.get(), ux.get())); |
| 86 | cv_convert_doubles(cv, &(y_cv[0]), y_cv.size(), &(y_cv[0])); |
| 87 | std::swap(y, y_cv); |
| 88 | } |
| 89 | |
| 90 | for (int i=0, j=0; i < x.size(); i++, j++) { |
| 91 | if (j == y.size()) |
| 92 | j = 0; |
| 93 | double diff = x[i] - y[j]; |
| 94 | // double lnum = std::abs(x[i]) - std::abs(y[i]) > 0 ? x[i] : y[i]; |
| 95 | // double tol = std::abs(lnum) * std::numeric_limits<double>::epsilon(); |
| 96 | if (x[i] == y[j]) // || std::abs(diff) < tol) |
| 97 | out[i] = 0; |
| 98 | else if (ISNAN(diff)) |
| 99 | out[i] = NA_INTEGER; |
| 100 | else out[i] = diff < 0 ? -1 : 1; |
| 101 | } |
| 102 | |
| 103 | if (swapped) |
| 104 | out = -out; |
| 105 | return out; |
| 106 | } |
| 107 | |
| 108 | // [[Rcpp::export(rng=false)]] |
| 109 | LogicalVector ud_convertible(std::string from, std::string to) { |
| 110 | xut_unit u_from(ut_parse(sys, ut_trim(from.data(), enc), enc)); |
| 111 | xut_unit u_to(ut_parse(sys, ut_trim(to.data(), enc), enc)); |
| 112 | |
| 113 | if (!u_from || !u_to) |
| 114 | return false; |
| 115 | return ut_are_convertible(u_from, u_to) != 0; |
| 116 | } |
| 117 | |
| 118 | // [[Rcpp::export(rng=false)]] |
| 119 | NumericVector ud_convert_doubles(NumericVector x, std::string from, std::string to) { |
| 120 | if (x.size() == 0) return x; |
| 121 | NumericVector out = clone(x); |
| 122 | |
| 123 | xut_unit u_from(ut_parse(sys, ut_trim(from.data(), enc), enc)); |
| 124 | xut_unit u_to(ut_parse(sys, ut_trim(to.data(), enc), enc)); |
| 125 | |
| 126 | xcv_converter cv(ut_get_converter(u_from.get(), u_to.get())); |
| 127 | cv_convert_doubles(cv, &(x[0]), x.size(), &(out[0])); |
| 128 | |
| 129 | return out; |
| 130 | } |
| 131 | |
| 132 | // [[Rcpp::export(rng=false)]] |
| 133 | void ud_map_names(CharacterVector names, xut_unit unit) { |
| 134 | if (!names.size() || !unit) return; |
| 135 | |
| 136 | for (int i = 0; i < names.size(); i++) |
| 137 | ut_map_name_to_unit(ut_trim(names[i], enc), enc, unit); |
| 138 | ut_map_unit_to_name(unit, ut_trim(names[0], enc), enc); |
| 139 | } |
| 140 | |
| 141 | // [[Rcpp::export(rng=false)]] |
| 142 | void ud_unmap_names(CharacterVector names) { |
| 143 | if (!names.size()) return; |
| 144 | |
| 145 | xut_unit unit(ut_parse(sys, ut_trim(names[0], enc), enc)); |
| 146 | if (!unit) return; |
| 147 | |
| 148 | ut_unmap_unit_to_name(unit, enc); |
| 149 | for (int i = 0; i < names.size(); i++) |
| 150 | ut_unmap_name_to_unit(sys, ut_trim(names[i], enc), enc); |
| 151 | } |
| 152 | |
| 153 | // [[Rcpp::export(rng=false)]] |
| 154 | void ud_map_symbols(CharacterVector symbols, xut_unit unit) { |
| 155 | if (!symbols.size() || !unit) return; |
| 156 | |
| 157 | for (int i = 0; i < symbols.size(); i++) |
| 158 | ut_map_symbol_to_unit(ut_trim(symbols[i], enc), enc, unit); |
| 159 | ut_map_unit_to_symbol(unit, ut_trim(symbols[0], enc), enc); |
| 160 | } |
| 161 | |
| 162 | // [[Rcpp::export(rng=false)]] |
| 163 | void ud_unmap_symbols(CharacterVector symbols) { |
| 164 | if (!symbols.size()) return; |
| 165 | |
| 166 | xut_unit unit(ut_parse(sys, ut_trim(symbols[0], enc), enc)); |
| 167 | if (!unit) return; |
| 168 | |
| 169 | ut_unmap_unit_to_symbol(unit, enc); |
| 170 | for (int i = 0; i < symbols.size(); i++) |
| 171 | ut_unmap_symbol_to_unit(sys, ut_trim(symbols[i], enc), enc); |
| 172 | } |
| 173 | |
| 174 | /* Thin wrappers **************************************************************/ |
| 175 | |
| 176 | // # nocov start |
| 177 | |
| 178 | // [[Rcpp::export(rng=false)]] |
| 179 | xut_unit R_ut_get_dimensionless_unit_one() { |
| 180 | return xut_unit(ut_get_dimensionless_unit_one(sys)); |
| 181 | } |
| 182 | |
| 183 | // # nocov end |
| 184 | |
| 185 | // [[Rcpp::export(rng=false)]] |
| 186 | xut_unit R_ut_new_base_unit() { |
| 187 | return xut_unit(ut_new_base_unit(sys)); |
| 188 | } |
| 189 | |
| 190 | // [[Rcpp::export(rng=false)]] |
| 191 | xut_unit R_ut_new_dimensionless_unit() { |
| 192 | return xut_unit(ut_new_dimensionless_unit(sys)); |
| 193 | } |
| 194 | |
| 195 | // [[Rcpp::export(rng=false)]] |
| 196 | CharacterVector R_ut_get_name(xut_unit unit) { |
| 197 | const char *s = ut_get_name(unit, enc); |
| 198 | if (s == NULL) |
| 199 | return CharacterVector::create(); |
| 200 | return CharacterVector::create(s); // #nocov |
| 201 | } |
| 202 | |
| 203 | // [[Rcpp::export(rng=false)]] |
| 204 | CharacterVector R_ut_get_symbol(xut_unit unit) { |
| 205 | const char *s = ut_get_symbol(unit, enc); |
| 206 | if (s == NULL) |
| 207 | return CharacterVector::create(); |
| 208 | return CharacterVector::create(s); |
| 209 | } |
| 210 | |
| 211 | // # nocov start |
| 212 | |
| 213 | // [[Rcpp::export(rng=false)]] |
| 214 | xut_unit R_ut_scale(xut_unit unit, double factor) { |
| 215 | return xut_unit(ut_scale(factor, unit)); |
| 216 | } |
| 217 | |
| 218 | // [[Rcpp::export(rng=false)]] |
| 219 | xut_unit R_ut_offset(xut_unit unit, double origin) { |
| 220 | return xut_unit(ut_offset(unit, origin)); |
| 221 | } |
| 222 | |
| 223 | // [[Rcpp::export(rng=false)]] |
| 224 | xut_unit R_ut_multiply(xut_unit a, xut_unit b) { |
| 225 | return xut_unit(ut_multiply(a, b)); |
| 226 | } |
| 227 | |
| 228 | // [[Rcpp::export(rng=false)]] |
| 229 | xut_unit R_ut_invert(xut_unit a) { |
| 230 | return xut_unit(ut_invert(a)); |
| 231 | } |
| 232 | |
| 233 | // [[Rcpp::export(rng=false)]] |
| 234 | xut_unit R_ut_divide(xut_unit numer, xut_unit denom) { |
| 235 | return xut_unit(ut_divide(numer, denom)); |
| 236 | } |
| 237 | |
| 238 | // [[Rcpp::export(rng=false)]] |
| 239 | xut_unit R_ut_raise(xut_unit a, int i) { |
| 240 | return xut_unit(ut_raise(a, i)); |
| 241 | } |
| 242 | |
| 243 | // [[Rcpp::export(rng=false)]] |
| 244 | xut_unit R_ut_root(xut_unit a, int i) { |
| 245 | return xut_unit(ut_root(a, i)); |
| 246 | } |
| 247 | |
| 248 | // # nocov end |
| 249 | |
| 250 | // [[Rcpp::export(rng=false)]] |
| 251 | xut_unit R_ut_log(xut_unit a, double base) { |
| 252 | return xut_unit(ut_log(base, a)); |
| 253 | } |
| 254 | |
| 255 | // [[Rcpp::export(rng=false)]] |
| 256 | xut_unit R_ut_parse(std::string name) { |
| 257 | xut_unit u(ut_parse(sys, ut_trim(name.data(), enc), enc)); |
| 258 | if (!u) |
| 259 | stop("syntax error, cannot parse '%s'", name); |
| 260 | return u; |
| 261 | } |
| 262 | |
| 263 | // [[Rcpp::export(rng=false)]] |
| 264 | CharacterVector R_ut_format(xut_unit p, bool names = false, |
| 265 | bool definition = false, bool ascii = false) |
| 266 | { |
| 267 | int opt = UT_ASCII; |
| 268 | if (!ascii) |
| 269 | opt = enc; |
| 270 | if (names) |
| 271 | opt = opt | UT_NAMES; |
| 272 | if (definition) |
| 273 | opt = opt | UT_DEFINITION; |
| 274 | char buf[256]; |
| 275 | if (ut_format(p, buf, sizeof(buf), opt) == sizeof(buf)) |
| 276 | warning("buffer too small!"); // #nocov |
| 277 | return CharacterVector::create(buf); |
| 278 | } |