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 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
|
/* This is part of the netCDF package. Copyright 2005 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use. See www.unidata.ucar.edu for more info.
Test netcdf-4 cross platform compound type.
$Id: tst_xplatform2.c,v 1.18 2009/10/26 18:44:59 russ Exp $
*/
#include <nc_tests.h>
#include "err_macros.h"
#include "netcdf.h"
#include <hdf5.h>
#define FILE_NAME_1 "tst_xplatform2_1.nc"
#define REF_FILE_NAME_1 "ref_tst_xplatform2_1.nc"
#define FILE_NAME_2 "tst_xplatform2_2.nc"
#define REF_FILE_NAME_2 "ref_tst_xplatform2_2.nc"
#define FILE_NAME_3 "tst_xplatform2_3.nc"
#define REF_FILE_NAME_3 "ref_tst_xplatform2_3.nc"
#define FILE_NAME_4 "tst_xplatform2_4.nc"
#define REF_FILE_NAME_4 "ref_tst_xplatform2_4.nc"
#define S1_TYPE_NAME "cmp_t"
#define X_NAME "x"
#define Y_NAME "y"
#define S1_NAME "s1"
#define S2_ATT_NAME "Runnymede"
#define S2_TYPE_NAME "date_1215"
#define DIM1_LEN 5
#define DIM2_LEN 3
#define VLEN_NAME "Magna_Carta_VLEN"
#define VLEN_ATT_NAME "We_will_sell_to_no_man_we_will_not_deny_or_defer_to_any_man_either_Justice_or_Right"
#define TWO_TYPES 2
#define NUM_S1 4
#define DIM3_LEN 1
#define DIM3_NAME "DIMENSION->The city of London shall enjoy all its ancient liberties and free customs, both by land and by water."
#define VAR3_NAME "VARIABLE->In future we will allow no one to levy an `aid' from his free men, except to ransom his person, to make his eldest son a knight, and (once) to marry his eldest daughter."
#define NUM_VL 1
#define S3_ATT_NAME "King_John"
#define S3_TYPE_NAME "barons"
#define VL_NAME "No scutage or aid may be levied in our kingdom without its general consent"
#define THREE_TYPES 3
struct s1
{
float x;
double y;
};
struct s2
{
struct s1 data[NUM_S1];
};
struct s3
{
nc_vlen_t data[NUM_VL];
};
int
check_file_1(int ncid, nc_vlen_t *data_out)
{
int ntypes_in, ndims_in;
char name_in[NC_MAX_NAME + 1];
size_t size_in, nfields_in, offset_in;
nc_type field_type_in;
nc_type typeids_in[TWO_TYPES], base_nc_type_in;
nc_vlen_t data_in[DIM1_LEN];
int i, j;
/* There should be two types. */
if (nc_inq_typeids(ncid, &ntypes_in, typeids_in)) ERR;
if (ntypes_in != 2) ERR;
/* The compound type is first: check it out. */
if (nc_inq_compound(ncid, typeids_in[0], name_in, &size_in, &nfields_in)) ERR;
if (nfields_in != 2 || strcmp(name_in, S1_TYPE_NAME) || size_in != sizeof(struct s1)) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 0, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, X_NAME) || offset_in != 0 || field_type_in != NC_FLOAT ||
ndims_in) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 1, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, Y_NAME) || offset_in != NC_COMPOUND_OFFSET(struct s1, y) || field_type_in != NC_DOUBLE ||
ndims_in) ERR;
/* How does the vlen type look? */
if (nc_inq_vlen(ncid, typeids_in[1], name_in, &size_in, &base_nc_type_in)) ERR;
if (strcmp(name_in, VLEN_NAME) || size_in != sizeof(nc_vlen_t) ||
base_nc_type_in != typeids_in[0]) ERR;
/* Now read the attribute. */
if (nc_get_att(ncid, NC_GLOBAL, VLEN_ATT_NAME, data_in)) ERR;
/* Did we get the correct data? */
for (i = 0; i < DIM1_LEN; i++)
{
if (data_in[i].len != data_out[i].len) ERR;
for (j = 0; j < data_in[i].len; j++)
if (((struct s1 *)data_in[i].p)->x != ((struct s1 *)data_out[i].p)->x ||
((struct s1 *)data_in[i].p)->y != ((struct s1 *)data_out[i].p)->y) ERR_RET;
}
/* Free the memory that was malloced when the VLEN was read. */
for (i = 0; i < DIM1_LEN; i++)
free(data_in[i].p);
/* We're done! */
return NC_NOERR;
}
int
check_file_2(int ncid, struct s2 *data_out)
{
int ntypes_in, ndims_in;
char name_in[NC_MAX_NAME + 1];
size_t size_in, nfields_in, offset_in;
nc_type field_type_in;
nc_type typeids_in[TWO_TYPES];
struct s2 data_in[DIM2_LEN];
int field_dims_in[1];
int i, j;
/* There should be two types. */
if (nc_inq_typeids(ncid, &ntypes_in, typeids_in)) ERR;
if (ntypes_in != TWO_TYPES) ERR;
/* The compound type is first: check it out. */
if (nc_inq_compound(ncid, typeids_in[0], name_in, &size_in, &nfields_in)) ERR;
if (nfields_in != 2 || strcmp(name_in, S1_TYPE_NAME) || size_in != sizeof(struct s1)) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 0, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, X_NAME) || offset_in != 0 || field_type_in != NC_FLOAT ||
ndims_in) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 1, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, Y_NAME) || offset_in != NC_COMPOUND_OFFSET(struct s1, y) || field_type_in != NC_DOUBLE ||
ndims_in) ERR;
/* How does the containing compound type look? */
if (nc_inq_compound(ncid, typeids_in[1], name_in, &size_in, &nfields_in)) ERR;
if (strcmp(name_in, S2_TYPE_NAME) || size_in != sizeof(struct s2) ||
nfields_in != 1) ERR;
if (nc_inq_compound_field(ncid, typeids_in[1], 0, name_in, &offset_in, &field_type_in,
&ndims_in, field_dims_in)) ERR;
if (strcmp(name_in, S1_NAME) || offset_in != NC_COMPOUND_OFFSET(struct s2, data) ||
field_type_in != typeids_in[0] || ndims_in != 1 || field_dims_in[0] != NUM_S1) ERR;
/* Now read the attribute. */
if (nc_get_att(ncid, NC_GLOBAL, S2_ATT_NAME, data_in)) ERR;
/* Did we get the correct data? */
for (i = 0; i < DIM2_LEN; i++)
for (j = 0; j < NUM_S1; j++)
if (data_out[i].data[j].x != data_in[i].data[j].x ||
data_out[i].data[j].y != data_in[i].data[j].y) ERR;
/* We're done! */
return NC_NOERR;
}
int
check_file_3(int ncid, struct s3 *data_out)
{
int ntypes_in, ndims_in;
char name_in[NC_MAX_NAME + 1];
size_t size_in, nfields_in, offset_in;
nc_type field_type_in, base_nc_type_in;
nc_type typeids_in[THREE_TYPES];
struct s3 data_in[DIM3_LEN];
int field_dims_in[1];
int i, j, k;
/* There should be three types. */
if (nc_inq_typeids(ncid, &ntypes_in, typeids_in)) ERR;
if (ntypes_in != THREE_TYPES) ERR;
/* The s1 compound type is first: check it out. */
if (nc_inq_compound(ncid, typeids_in[0], name_in, &size_in, &nfields_in)) ERR;
if (nfields_in != 2 || strcmp(name_in, S1_TYPE_NAME) || size_in != sizeof(struct s1)) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 0, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, X_NAME) || offset_in != 0 || field_type_in != NC_FLOAT ||
ndims_in) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 1, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, Y_NAME) || offset_in != NC_COMPOUND_OFFSET(struct s1, y) || field_type_in != NC_DOUBLE ||
ndims_in) ERR;
/* How does the vlen type look? */
if (nc_inq_vlen(ncid, typeids_in[1], name_in, &size_in, &base_nc_type_in)) ERR;
if (strcmp(name_in, VLEN_NAME) || size_in != sizeof(nc_vlen_t) ||
base_nc_type_in != typeids_in[0]) ERR;
/* How does the containing compound type look? */
if (nc_inq_compound(ncid, typeids_in[2], name_in, &size_in, &nfields_in)) ERR;
if (strcmp(name_in, S3_TYPE_NAME) || size_in != sizeof(struct s3) ||
nfields_in != 1) ERR;
if (nc_inq_compound_field(ncid, typeids_in[2], 0, name_in, &offset_in, &field_type_in,
&ndims_in, field_dims_in)) ERR;
if (strcmp(name_in, VL_NAME) || offset_in != NC_COMPOUND_OFFSET(struct s3, data) ||
field_type_in != typeids_in[1] || ndims_in != 1 || field_dims_in[0] != NUM_VL) ERR;
/* Now read the attribute. */
if (nc_get_att(ncid, NC_GLOBAL, S3_ATT_NAME, data_in)) ERR;
/* Did we get the correct data? */
for (i = 0; i < DIM3_LEN; i++)
for (j = 0; j < NUM_VL; j++)
{
if (data_in[i].data[j].len != data_out[i].data[j].len) ERR;
for (k = 0; k < data_out[i].data[j].len; k++)
if (((struct s1 *)data_in[i].data[j].p)[k].x != ((struct s1 *)data_out[i].data[j].p)[k].x ||
((struct s1 *)data_in[i].data[j].p)[k].y != ((struct s1 *)data_out[i].data[j].p)[k].y) ERR;
}
/* Free our vlens. */
for (i = 0; i < DIM3_LEN; i++)
for (j = 0; j < NUM_VL; j++)
nc_free_vlen(&(data_in[i].data[j]));
/* We're done! */
return NC_NOERR;
}
int
check_file_4(int ncid, struct s3 *data_out)
{
int ntypes_in, ndims_in;
char name_in[NC_MAX_NAME + 1];
size_t size_in, nfields_in, offset_in;
nc_type field_type_in, base_nc_type_in;
nc_type typeids_in[THREE_TYPES];
struct s3 data_in[DIM3_LEN];
int field_dims_in[1];
int i, j, k;
/* There should be three types. */
if (nc_inq_typeids(ncid, &ntypes_in, typeids_in)) ERR;
if (ntypes_in != THREE_TYPES) ERR;
/* The s1 compound type is first: check it out. */
if (nc_inq_compound(ncid, typeids_in[0], name_in, &size_in, &nfields_in)) ERR;
if (nfields_in != 2 || strcmp(name_in, S1_TYPE_NAME) || size_in != sizeof(struct s1)) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 0, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, X_NAME) || offset_in != 0 || field_type_in != NC_FLOAT ||
ndims_in) ERR;
if (nc_inq_compound_field(ncid, typeids_in[0], 1, name_in, &offset_in,
&field_type_in, &ndims_in, NULL)) ERR;
if (strcmp(name_in, Y_NAME) || offset_in != NC_COMPOUND_OFFSET(struct s1, y) || field_type_in != NC_DOUBLE ||
ndims_in) ERR;
/* How does the vlen type look? */
if (nc_inq_vlen(ncid, typeids_in[1], name_in, &size_in, &base_nc_type_in)) ERR;
if (strcmp(name_in, VLEN_NAME) || size_in != sizeof(nc_vlen_t) ||
base_nc_type_in != typeids_in[0]) ERR;
/* How does the containing compound type look? */
if (nc_inq_compound(ncid, typeids_in[2], name_in, &size_in, &nfields_in)) ERR;
if (strcmp(name_in, S3_TYPE_NAME) || size_in != sizeof(struct s3) ||
nfields_in != 1) ERR;
if (nc_inq_compound_field(ncid, typeids_in[2], 0, name_in, &offset_in, &field_type_in,
&ndims_in, field_dims_in)) ERR;
if (strcmp(name_in, VL_NAME) || offset_in != NC_COMPOUND_OFFSET(struct s3, data) ||
field_type_in != typeids_in[1] || ndims_in != 1 || field_dims_in[0] != NUM_VL) ERR;
/* Now read the variable. */
if (nc_get_var(ncid, 0, data_in)) ERR;
/* Did we get the correct data? */
for (i = 0; i < DIM3_LEN; i++)
for (j = 0; j < NUM_VL; j++)
{
if (data_in[i].data[j].len != data_out[i].data[j].len) ERR;
for (k = 0; k < data_out[i].data[j].len; k++)
if (((struct s1 *)data_in[i].data[j].p)[k].x != ((struct s1 *)data_out[i].data[j].p)[k].x ||
((struct s1 *)data_in[i].data[j].p)[k].y != ((struct s1 *)data_out[i].data[j].p)[k].y) ERR;
}
/* Free our vlens. */
for (i = 0; i < DIM3_LEN; i++)
for (j = 0; j < NUM_VL; j++)
nc_free_vlen(&(data_in[i].data[j]));
/* We're done! */
return NC_NOERR;
}
int
main(int argc, char **argv)
{
int ncid;
int i, j, k;
nc_vlen_t *vlen_of_comp_out;
struct s2 *comp_array_of_comp_out;
struct s3 *comp_array_of_vlen_of_comp_out;
printf("\nTesting nested types across platforms.\n");
if (!(vlen_of_comp_out = calloc(sizeof(nc_vlen_t), DIM1_LEN))) ERR;
if (!(comp_array_of_comp_out = calloc(sizeof(struct s2), DIM2_LEN))) ERR;
if (!(comp_array_of_vlen_of_comp_out = calloc(sizeof(struct s3), DIM3_LEN))) ERR;
/* Create some output data: a vlen of struct s1. */
for (i = 0; i < DIM1_LEN; i++)
{
vlen_of_comp_out[i].len = i + 1;
if (!(vlen_of_comp_out[i].p = malloc(sizeof(struct s1) * vlen_of_comp_out[i].len)))
return NC_ENOMEM;
for (j = 0; j < vlen_of_comp_out[i].len; j++)
{
((struct s1 *)vlen_of_comp_out[i].p)[j].x = 42.42;
((struct s1 *)vlen_of_comp_out[i].p)[j].y = 2.0;
}
}
/* Create some output data: a struct which holds an array of
* struct s1. */
for (i = 0; i < DIM2_LEN; i++)
for (j = 0; j < NUM_S1; j++)
{
comp_array_of_comp_out[i].data[j].x = 42.42;
comp_array_of_comp_out[i].data[j].y = 2.0;
}
/* Create some output data: a struct which holds an array of
* vlen of struct s1. */
for (i = 0; i < DIM3_LEN; i++)
for (j = 0; j < NUM_VL; j++)
{
comp_array_of_vlen_of_comp_out[i].data[j].len = i + 1;
if (!(comp_array_of_vlen_of_comp_out[i].data[j].p = malloc(sizeof(struct s1) * comp_array_of_vlen_of_comp_out[i].data[j].len)))
return NC_ENOMEM;
for (k = 0; k < comp_array_of_vlen_of_comp_out[i].data[j].len; k++)
{
((struct s1 *)comp_array_of_vlen_of_comp_out[i].data[j].p)[k].x = 42.42;
((struct s1 *)comp_array_of_vlen_of_comp_out[i].data[j].p)[k].y = 2.0;
}
}
printf("*** testing of vlen of compound type...");
{
nc_type s1_typeid, vlen_typeid;
/* Create a netCDF-4 file. */
if (nc_create(FILE_NAME_1, NC_NETCDF4, &ncid)) ERR;
/* Create a simple compound type which has different sizes on
* different platforms - our old friend struct s1. */
if (nc_def_compound(ncid, sizeof(struct s1), S1_TYPE_NAME, &s1_typeid)) ERR;
if (nc_insert_compound(ncid, s1_typeid, X_NAME,
NC_COMPOUND_OFFSET(struct s1, x), NC_FLOAT)) ERR;
if (nc_insert_compound(ncid, s1_typeid, Y_NAME,
NC_COMPOUND_OFFSET(struct s1, y), NC_DOUBLE)) ERR;
/* Now make a new type: a vlen of our compound type. */
if (nc_def_vlen(ncid, VLEN_NAME, s1_typeid, &vlen_typeid)) ERR;
/* Write the output data as an attribute. */
if (nc_put_att(ncid, NC_GLOBAL, VLEN_ATT_NAME, vlen_typeid,
DIM1_LEN, vlen_of_comp_out)) ERR;
/* How does it look? */
if (check_file_1(ncid, vlen_of_comp_out)) ERR;
/* We're done - wasn't that easy? */
if (nc_close(ncid)) ERR;
/* Check it out. */
if (nc_open(FILE_NAME_1, NC_NOWRITE, &ncid)) ERR;
if (check_file_1(ncid, vlen_of_comp_out)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing Solaris-written vlen of compound type...");
{
char file_in[NC_MAX_NAME + 1];
strcpy(file_in, "");
if (getenv("srcdir"))
{
strcat(file_in, getenv("srcdir"));
strcat(file_in, "/");
}
strcat(file_in, REF_FILE_NAME_1);
/* Check out the same file, generated on buddy and included with
* the distribution. */
if (nc_open(file_in, NC_NOWRITE, &ncid)) ERR;
if (check_file_1(ncid, vlen_of_comp_out)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing compound type containing array of compound type...");
{
nc_type s1_typeid, s2_typeid;
int dimsizes[1] = {NUM_S1};
/* Create a netCDF-4 file. */
if (nc_create(FILE_NAME_2, NC_NETCDF4, &ncid)) ERR;
/* Create a simple compound type which has different sizes on
* different platforms - our old friend struct s1. */
if (nc_def_compound(ncid, sizeof(struct s1), S1_TYPE_NAME, &s1_typeid)) ERR;
if (nc_insert_compound(ncid, s1_typeid, X_NAME,
NC_COMPOUND_OFFSET(struct s1, x), NC_FLOAT)) ERR;
if (nc_insert_compound(ncid, s1_typeid, Y_NAME,
NC_COMPOUND_OFFSET(struct s1, y), NC_DOUBLE)) ERR;
/* Now make a compound type that holds an array of the struct s1
* type. */
if (nc_def_compound(ncid, sizeof(struct s2), S2_TYPE_NAME, &s2_typeid)) ERR;
if (nc_insert_array_compound(ncid, s2_typeid, S1_NAME,
NC_COMPOUND_OFFSET(struct s2, data),
s1_typeid, 1, dimsizes)) ERR;
/* Write the output data as an attribute. */
if (nc_put_att(ncid, NC_GLOBAL, S2_ATT_NAME, s2_typeid,
DIM2_LEN, comp_array_of_comp_out)) ERR;
/* How does it look? */
if (check_file_2(ncid, comp_array_of_comp_out)) ERR;
/* We're done - wasn't that easy? */
if (nc_close(ncid)) ERR;
/* Check it out. */
if (nc_open(FILE_NAME_2, NC_NOWRITE, &ncid)) ERR;
if (check_file_2(ncid, comp_array_of_comp_out)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing Solaris-written compound type containing array of compound type...");
{
char file_in[NC_MAX_NAME + 1];
strcpy(file_in, "");
if (getenv("srcdir"))
{
strcat(file_in, getenv("srcdir"));
strcat(file_in, "/");
}
strcat(file_in, REF_FILE_NAME_2);
/* Check out the same file, generated on buddy and included with
* the distribution. */
if (nc_open(file_in, NC_NOWRITE, &ncid)) ERR;
if (check_file_2(ncid, comp_array_of_comp_out)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing compound attribute containing array of vlen of compound type...");
{
nc_type vlen_typeid, s3_typeid, s1_typeid;
int dimsizes[1] = {NUM_VL};
/* Create a netCDF-4 file. */
if (nc_create(FILE_NAME_3, NC_NETCDF4, &ncid)) ERR;
/* Create a simple compound type which has different sizes on
* different platforms - our old friend struct s1. */
if (nc_def_compound(ncid, sizeof(struct s1), S1_TYPE_NAME, &s1_typeid)) ERR;
if (nc_insert_compound(ncid, s1_typeid, X_NAME,
NC_COMPOUND_OFFSET(struct s1, x), NC_FLOAT)) ERR;
if (nc_insert_compound(ncid, s1_typeid, Y_NAME,
NC_COMPOUND_OFFSET(struct s1, y), NC_DOUBLE)) ERR;
/* Now make a new type: a vlen of our s1 compound type. */
if (nc_def_vlen(ncid, VLEN_NAME, s1_typeid, &vlen_typeid)) ERR;
/* Now make a compound type that holds an array of the VLEN
* type. */
if (nc_def_compound(ncid, sizeof(struct s3), S3_TYPE_NAME, &s3_typeid)) ERR;
if (nc_insert_array_compound(ncid, s3_typeid, VL_NAME,
NC_COMPOUND_OFFSET(struct s3, data),
vlen_typeid, 1, dimsizes)) ERR;
/* Write the output data as an attribute. */
if (nc_put_att(ncid, NC_GLOBAL, S3_ATT_NAME, s3_typeid,
DIM3_LEN, comp_array_of_vlen_of_comp_out)) ERR;
/* How does it look? Uncomment this line to see memory issue. */
/* if (check_file_3(ncid, comp_array_of_vlen_of_comp_out)) ERR; */
/* We're done - wasn't that easy? */
if (nc_close(ncid)) ERR;
/* Check it out. */
if (nc_open(FILE_NAME_3, NC_NOWRITE, &ncid)) ERR;
if (check_file_3(ncid, comp_array_of_vlen_of_comp_out)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing compound variable containing array of vlen of compound type...");
{
nc_type vlen_typeid, s3_typeid, s1_typeid;
int varid, dimid;
int dimsizes[1] = {NUM_VL};
/* Create a netCDF-4 file. */
if (nc_create(FILE_NAME_4, NC_NETCDF4, &ncid)) ERR;
/* Create a simple compound type which has different sizes on
* different platforms - our old friend struct s1. */
if (nc_def_compound(ncid, sizeof(struct s1), S1_TYPE_NAME, &s1_typeid)) ERR;
if (nc_insert_compound(ncid, s1_typeid, X_NAME,
NC_COMPOUND_OFFSET(struct s1, x), NC_FLOAT)) ERR;
if (nc_insert_compound(ncid, s1_typeid, Y_NAME,
NC_COMPOUND_OFFSET(struct s1, y), NC_DOUBLE)) ERR;
/* Now make a new type: a vlen of our s1 compound type. */
if (nc_def_vlen(ncid, VLEN_NAME, s1_typeid, &vlen_typeid)) ERR;
/* Now make a compound type that holds an array of the VLEN
* type. */
if (nc_def_compound(ncid, sizeof(struct s3), S3_TYPE_NAME, &s3_typeid)) ERR;
if (nc_insert_array_compound(ncid, s3_typeid, VL_NAME,
NC_COMPOUND_OFFSET(struct s3, data),
vlen_typeid, 1, dimsizes)) ERR;
/* Create a dimension and a var of s3 type, then write the
* data. */
if (nc_def_dim(ncid, DIM3_NAME, DIM3_LEN, &dimid)) ERR;
if (nc_def_var(ncid, VAR3_NAME, s3_typeid, 1, &dimid, &varid)) ERR;
if (nc_put_var(ncid, varid, comp_array_of_vlen_of_comp_out)) ERR;
/* How does it look? */
if (check_file_4(ncid, comp_array_of_vlen_of_comp_out)) ERR;
/* We're done - wasn't that easy? */
if (nc_close(ncid)) ERR;
/* Check it out. */
if (nc_open(FILE_NAME_4, NC_NOWRITE, &ncid)) ERR;
if (check_file_4(ncid, comp_array_of_vlen_of_comp_out)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
/* Free our mallocs. */
for (i = 0; i < DIM1_LEN; i++)
free(vlen_of_comp_out[i].p);
for (i = 0; i < DIM3_LEN; i++)
for (j = 0; j < NUM_VL; j++)
free(comp_array_of_vlen_of_comp_out[i].data[j].p);
free(comp_array_of_comp_out);
free(comp_array_of_vlen_of_comp_out);
free(vlen_of_comp_out);
/* Now run the tests formerly in tst_h_atts2.c. */
#define REF_FILE_NAME "tst_xplatform2_3.nc"
#define NUM_OBJ 3
printf("\n*** Checking HDF5 attribute functions some more.\n");
printf("*** Opening tst_xplatform2_3.nc...");
{
hid_t fileid, grpid, attid;
hid_t file_typeid1[NUM_OBJ], native_typeid1[NUM_OBJ];
hid_t file_typeid2, native_typeid2;
hsize_t num_obj, i;
H5O_info_t obj_info;
char obj_name[NC_MAX_NAME + 1];
/* Open one of the netCDF test files. */
if ((fileid = H5Fopen(REF_FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gopen(fileid, "/")) < 0) ERR;
/* How many objects in this group? */
if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR;
if (num_obj != NUM_OBJ) ERR;
/* For each object in the group... */
for (i = 0; i < num_obj; i++)
{
/* Get the name. */
if (H5Oget_info_by_idx(grpid, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC,
i, &obj_info, H5P_DEFAULT) < 0) ERR_RET;
if (H5Lget_name_by_idx(grpid, ".", H5_INDEX_NAME, H5_ITER_INC, i,
obj_name, NC_MAX_NAME + 1, H5P_DEFAULT) < 0) ERR_RET;
printf(" reading type %s ", obj_name);
if (obj_info.type != H5O_TYPE_NAMED_DATATYPE) ERR_RET;
/* Get the typeid. */
if ((file_typeid1[i] = H5Topen2(grpid, obj_name, H5P_DEFAULT)) < 0) ERR_RET;
if ((native_typeid1[i] = H5Tget_native_type(file_typeid1[i], H5T_DIR_DEFAULT)) < 0) ERR_RET;
}
/* There is one att: open it by index. */
if ((attid = H5Aopen_idx(grpid, 0)) < 0) ERR;
/* Get file and native typeids. */
if ((file_typeid2 = H5Aget_type(attid)) < 0) ERR;
if ((native_typeid2 = H5Tget_native_type(file_typeid2, H5T_DIR_DEFAULT)) < 0) ERR;
/* Close the attribute. */
if (H5Aclose(attid) < 0) ERR;
/* Close the typeids. */
if (H5Tclose(file_typeid2) < 0) ERR_RET;
if (H5Tclose(native_typeid2) < 0) ERR_RET;
for (i = 0; i < NUM_OBJ; i++)
{
if (H5Tclose(file_typeid1[i]) < 0) ERR_RET;
if (H5Tclose(native_typeid1[i]) < 0) ERR_RET;
}
/* Close the group and file. */
if (H5Gclose(grpid) < 0 ||
H5Fclose(fileid) < 0) ERR;
}
SUMMARIZE_ERR;
printf("*** Opening tst_xplatform2_3.nc again...");
{
hid_t fileid, grpid, attid, file_typeid, native_typeid;
hid_t file_typeid2, native_typeid2;
/* Open one of the netCDF test files. */
if ((fileid = H5Fopen(REF_FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR;
if ((grpid = H5Gopen(fileid, "/")) < 0) ERR;
/* There is one att: open it by index. */
if ((attid = H5Aopen_idx(grpid, 0)) < 0) ERR;
/* Get file and native typeids. */
if ((file_typeid = H5Aget_type(attid)) < 0) ERR;
if ((native_typeid = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0) ERR;
/* Now getting another copy of the native typeid will fail! WTF? */
if ((file_typeid2 = H5Aget_type(attid)) < 0) ERR;
if ((native_typeid2 = H5Tget_native_type(file_typeid, H5T_DIR_DEFAULT)) < 0) ERR;
/* Close the attribute. */
if (H5Aclose(attid) < 0) ERR;
/* Close the typeids. */
if (H5Tclose(file_typeid) < 0) ERR;
if (H5Tclose(native_typeid) < 0) ERR;
if (H5Tclose(file_typeid2) < 0) ERR;
if (H5Tclose(native_typeid2) < 0) ERR;
/* Close the group and file. */
if (H5Gclose(grpid) < 0 ||
H5Fclose(fileid) < 0) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}
|