1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
/* Copyright (C) 2003-2008 by the
* Institute of Global Environment and Society (IGES)
* See the file COPYRIGHT for more information.
*
*
* gadap.cc - Implementation of the library's interface
*
* Last modified: $Date: 2008/07/25 20:43:25 $
* Revision for this file: $Revision: 1.1 $
* Release name: $Name: gadods-2_0 $
* Original for this file: $Source: /homes/cvsroot/gadods/gadods/src/gadap.cc,v $
*/
#include "config.h"
#include "gadap.h"
#include "gaConnect.h"
#include "gaReports.h"
// #include "Connections.h"
#include "Connections.cc"
#include <string.h>
#include <vector>
/** Static data */
// List of open datasets
Connections<gaConnect*> datasets(GADAP_MAX_DATASETS);
int numDatasets = 0;
// List of open report collections (query results)
std::vector<gaReports*> reports;
/** Internal utility functions */
int
bad_d_handle(GADAP_DATASET d_handle) {
return d_handle < 0
|| d_handle > GADAP_MAX_DATASETS
|| datasets[d_handle] == NULL;
}
int
bad_r_handle(GADAP_RPTCOL r_handle) {
return r_handle < 0
|| r_handle > reports.size()
|| reports[r_handle] == NULL;
}
/*****************************************************************
* Return library name and version number (taken from libdap)
*/
extern "C"
const char *
libgadap_version() {
return PACKAGE_STRING;
}
/*****************************************************************
* Opening and closing datasets
*/
int
gadap_open(const char *url, GADAP_DATASET *d_handle) {
try {
gaConnect *newDataset = new gaConnect(url);
*d_handle = datasets.add_connect(newDataset);
numDatasets++;
return GADAP_SUCCESS;
} catch (const libdap::Error& error) {
cerr << "DAP error: " << error.get_error_message() << endl;
*d_handle = -1;
return errval(GADAP_ERR_DAP_ERROR);
}
}
int
gadap_numopen() {
return numDatasets;
}
int
gadap_handle(GADAP_DATASET *d_handle, int d_index) {
if (d_index >= 0 && d_index < numDatasets) {
int counter = -1;
for (int i = 0; i < GADAP_MAX_DATASETS; i++) {
if (datasets[i] != NULL) {
counter++;
}
if (counter == d_index) {
*d_handle = i;
return GADAP_SUCCESS;
}
}
}
return errval(GADAP_ERR_INDEX_OUT_OF_RANGE);
}
void
gadap_close(GADAP_DATASET d_handle, int free_rptcols) {
if (bad_d_handle(d_handle)) {
return;
}
for (int i = 0; i < reports.size() && free_rptcols; i++) {
if (reports[i] && reports[i]->getDataset() == d_handle) {
gadap_r_free(i);
}
}
delete datasets[d_handle];
datasets.del_connect(d_handle);
numDatasets--;
}
/*****************************************************************
* Identifying variables
*/
int
gadap_d_varindex(GADAP_DATASET d_handle, const char *varname) {
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return datasets[d_handle]->getVarIndex(varname);
}
}
const char *
gadap_d_varname(GADAP_DATASET d_handle, int var_index) {
if (bad_d_handle(d_handle)) {
return NULL;
} else {
return datasets[d_handle]->getVarName(var_index);
}
}
int
gadap_d_varlen(GADAP_DATASET d_handle, int var_index) {
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return datasets[d_handle]->getVarLen(var_index);
}
}
GADAP_VAR_TYPE
gadap_d_vartype(GADAP_DATASET d_handle, int var_index) {
if (bad_d_handle(d_handle)) {
return GADAP_INVALID_TYPE;
} else {
return datasets[d_handle]->getVarType(var_index);
}
}
int
gadap_d_numvars(GADAP_DATASET d_handle) {
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return datasets[d_handle]->getNumVars();
}
}
int
gadap_d_numlivars(GADAP_DATASET d_handle) {
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return datasets[d_handle]->getNumLIVars();
}
}
/*****************************************************************
* Obtaining metadata
*/
const char *
gadap_d_name(GADAP_DATASET d_handle) {
if (bad_d_handle(d_handle)) {
return NULL;
} else {
return datasets[d_handle]->getName();
}
}
const char *
gadap_d_fullname(GADAP_DATASET d_handle) {
if (bad_d_handle(d_handle)) {
return NULL;
} else {
return datasets[d_handle]->getFullName();
}
}
const char *
gadap_d_url(GADAP_DATASET d_handle) {
if (bad_d_handle(d_handle)) {
return NULL;
} else {
return datasets[d_handle]->getURL();
}
}
int
gadap_d_numattrs(GADAP_DATASET d_handle, int var_index) {
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return datasets[d_handle]->getNumAttrs(var_index);
}
}
const char *
gadap_d_attrname(GADAP_DATASET d_handle, int var_index, int attr_index) {
if (bad_d_handle(d_handle)) {
return NULL;
} else {
return datasets[d_handle]->getAttrName(var_index, attr_index);
}
}
int
gadap_d_attrindex(GADAP_DATASET d_handle, int var_index, const char *attrname) {
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return datasets[d_handle]->getAttrIndex(var_index, attrname);
}
}
const char *
gadap_d_attrstr(GADAP_DATASET d_handle, int var_index, int attr_index) {
if (bad_d_handle(d_handle)) {
return NULL;
} else {
return datasets[d_handle]->getAttr(var_index, attr_index);
}
}
int
gadap_d_attrdbl(GADAP_DATASET d_handle, int var_index, int attr_index,
double *value) {
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return datasets[d_handle]->getAttrDbl(var_index, attr_index, value);
}
}
/*****************************************************************
* Metadata shorthand functions
*/
const char *
gadap_d_title(GADAP_DATASET d_handle) {
return gadap_d_attrstr(d_handle, -1, gadap_d_attrindex(d_handle, -1, "title"));
}
int
gadap_d_fill(GADAP_DATASET d_handle, int var_index, double *value) {
if (gadap_d_attrdbl(d_handle, var_index,
gadap_d_attrindex(d_handle, var_index, "missing_value"),
value) == GADAP_SUCCESS) {
return GADAP_SUCCESS;
} else {
return gadap_d_attrdbl(d_handle, var_index,
gadap_d_attrindex(d_handle, var_index,
"_FillValue"), value);
}
}
const char *
gadap_d_longname(GADAP_DATASET d_handle, int var_index) {
return gadap_d_attrstr(d_handle, var_index,
gadap_d_attrindex(d_handle, var_index, "long_name"));
}
/*****************************************************************
* Sending a query
*/
GADAP_STN_QUERY *
gadap_sq_new(GADAP_DATASET d_handle) {
if (bad_d_handle(d_handle)) {
return NULL;
}
// Create and clear query
GADAP_STN_QUERY *new_query = new GADAP_STN_QUERY();
memset(new_query, 0, sizeof(GADAP_STN_QUERY));
// Set dataset handle
new_query->d_handle = d_handle;
// Create var array
int numVars = datasets[d_handle]->getNumVars();
new_query->varflags = (int *)calloc(numVars, sizeof(int));
return new_query;
}
const char *
gadap_sq_url(GADAP_STN_QUERY *query) {
GADAP_DATASET d_handle = query->d_handle;
if (bad_d_handle(d_handle)) {
return NULL;
}
return datasets[d_handle]->getURL(query);
}
GADAP_STATUS
gadap_sq_send(GADAP_STN_QUERY *query,
GADAP_RPTCOL *r_handle) {
try {
GADAP_DATASET d_handle = query->d_handle;
if (bad_d_handle(d_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
}
//DBG cout << "sending query..." << endl;
// Get reports back for query
gaReports *newReports = datasets[d_handle]->getReports(query);
if (newReports == NULL) {
return errval(GADAP_ERR_INVALID_QUERY);
}
newReports->setDataset(d_handle);
newReports->setQuery(new GADAP_STN_QUERY(*query));
// Find next empty slot in reports vector
int nextFree = 0;
while (nextFree < reports.size() && reports[nextFree] != NULL) {
nextFree++;
}
if (nextFree == reports.size()) {
reports.push_back(newReports);
} else {
reports[nextFree] = newReports;
}
// Return handle
*r_handle = nextFree;
return GADAP_SUCCESS;
} catch (const libdap::Error& error) {
cout << "DAP error: " << error.get_error_message() << endl;
*r_handle = -1;
return errval(GADAP_ERR_DAP_ERROR);
}
}
void
gadap_sq_free(GADAP_STN_QUERY *query) {
if (query->url) {
free(query->url);
}
if (query->varflags) {
free(query->varflags);
}
delete query;
}
/*****************************************************************
* Managing report collections
*/
GADAP_STATUS
gadap_r_dataset(GADAP_RPTCOL r_handle, GADAP_DATASET *d_handle) {
if (bad_r_handle(r_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
*d_handle = reports[r_handle]->getDataset();
return GADAP_SUCCESS;
}
}
GADAP_STN_QUERY *
gadap_r_query(GADAP_RPTCOL r_handle) {
if (bad_r_handle(r_handle)) {
return NULL;
} else {
return reports[r_handle]->getQuery();
}
}
const char *
gadap_r_url(GADAP_RPTCOL r_handle) {
if (bad_r_handle(r_handle)) {
return NULL;
} else {
return reports[r_handle]->getURL();
}
}
int
gadap_r_numvars(GADAP_RPTCOL r_handle) {
if (bad_r_handle(r_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return reports[r_handle]->getNumVars();
}
}
int
gadap_r_numlivars(GADAP_RPTCOL r_handle) {
if (bad_r_handle(r_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return reports[r_handle]->getNumLIVars();
}
}
const char *
gadap_r_varname(GADAP_RPTCOL r_handle, int var_index) {
if (bad_r_handle(r_handle)) {
return NULL;
} else {
return reports[r_handle]->getVarName(var_index);
}
}
int
gadap_r_varindex(GADAP_RPTCOL r_handle, const char *varname) {
if (bad_r_handle(r_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return reports[r_handle]->getVarIndex(varname);
}
}
int
gadap_r_numrpts(GADAP_RPTCOL r_handle) {
if (bad_r_handle(r_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return reports[r_handle]->getNumReports();
}
}
int
gadap_r_numlev(GADAP_RPTCOL r_handle, int rpt_index) {
if (bad_r_handle(r_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return reports[r_handle]->getNumLevels(rpt_index);
}
}
void
gadap_r_free(GADAP_RPTCOL r_handle) {
if (bad_r_handle(r_handle)) {
return;
}
delete reports[r_handle];
reports[r_handle] = NULL;
}
/*****************************************************************
* Retrieving data from report collections
*/
GADAP_STATUS
gadap_r_valdbl(GADAP_RPTCOL r_handle,
int rpt_index, int lev_index,
int var_index, int array_index,
double *value) {
if (bad_r_handle(r_handle)) {
return errval(GADAP_ERR_INVALID_HANDLE);
} else {
return reports[r_handle]->getVarDbl(rpt_index, lev_index,
var_index, array_index,
value);
}
}
const char *
gadap_r_valstr(GADAP_RPTCOL r_handle,
int rpt_index, int lev_index,
int var_index, int array_index) {
if (bad_r_handle(r_handle)) {
return NULL;
} else {
return reports[r_handle]->getVarStr(rpt_index, lev_index,
var_index, array_index);
}
}
|