00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #define REDEF
00019
00020
00021 #undef NDEBUG
00022
00023 #include <stdio.h>
00024 #include <stddef.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <assert.h>
00028 #include "netcdf.h"
00029
00030
00031 #define MAXSHORT 32767
00032 #define MAXINT 2147483647
00033 #define MAXBYTE 127
00034
00035
00036 #define FNAME "test.nc"
00037 #define NUM_DIMS 3
00038 #define DONT_CARE -1
00039
00040 #define NUM_RECS 8
00041 #define SIZE_1 7
00042 #define SIZE_2 8
00043
00044 static struct {
00045 int num_dims;
00046 int num_vars;
00047 int num_attrs;
00048 int xtendim;
00049 } cdesc[1];
00050
00051 static struct {
00052 char mnem[NC_MAX_NAME];
00053 nc_type type;
00054 int ndims;
00055 int dims[NC_MAX_DIMS];
00056 int num_attrs;
00057 } vdesc[1];
00058
00059 static struct {
00060 char mnem[NC_MAX_NAME];
00061 nc_type type;
00062 size_t len;
00063 } adesc[1];
00064
00065 union getret
00066 {
00067 char by[8];
00068 short sh[4];
00069 int in[2];
00070 float fl[2];
00071 double dbl;
00072 };
00073
00074
00075 static void
00076 chkgot(nc_type type, union getret got, double check)
00077 {
00078 switch(type){
00079 case NC_BYTE :
00080 assert( (char)check == got.by[0] );
00081 break;
00082 case NC_CHAR :
00083 assert( (char)check == got.by[0] );
00084 break;
00085 case NC_SHORT :
00086 assert( (short)check == got.sh[0] );
00087 break;
00088 case NC_INT :
00089 assert( (int)check == got.in[0] );
00090 break;
00091 case NC_FLOAT :
00092 assert( (float)check == got.fl[0] );
00093 break;
00094 case NC_DOUBLE :
00095 assert( check == got.dbl );
00096 break;
00097 default:
00098 break;
00099 }
00100 }
00101
00102 static char *fname = FNAME;
00103
00104
00105 static size_t num_dims = NUM_DIMS;
00106 static size_t sizes[] = { NC_UNLIMITED, SIZE_1 , SIZE_2 };
00107 static char *dim_names[] = { "record", "ixx", "iyy"};
00108
00109 static void
00110 createtestdims(int cdfid, size_t num_dims, size_t *sizes, char *dim_names[])
00111 {
00112 int dimid;
00113 while(num_dims-- != 0)
00114 {
00115 assert( nc_def_dim(cdfid, *dim_names++, *sizes, &dimid)
00116 == NC_NOERR);
00117 sizes++;
00118 }
00119
00120 }
00121
00122
00123 static void
00124 testdims(int cdfid, size_t num_dims, size_t *sizes, char *dim_names[])
00125 {
00126 int ii;
00127 size_t size;
00128 char cp[NC_MAX_NAME];
00129 for(ii=0; (size_t) ii < num_dims; ii++, sizes++)
00130 {
00131 assert( nc_inq_dim(cdfid, ii, cp, &size) == NC_NOERR);
00132 if( size != *sizes)
00133 (void) fprintf(stderr, "%d: %lu != %lu\n",
00134 ii, (unsigned long)size, (unsigned long)*sizes);
00135 assert( size == *sizes);
00136 assert( strcmp(cp, *dim_names++) == 0);
00137 }
00138
00139 }
00140
00141
00142
00143 static char *reqattr[] = {
00144 "UNITS",
00145 "VALIDMIN",
00146 "VALIDMAX",
00147 "SCALEMIN",
00148 "SCALEMAX",
00149 "FIELDNAM",
00150 _FillValue
00151 };
00152 #define NUM_RATTRS 6
00153
00154 static struct tcdfvar {
00155 char *mnem;
00156 nc_type type;
00157 char *fieldnam;
00158 double validmin;
00159 double validmax;
00160 double scalemin;
00161 double scalemax;
00162 char *units;
00163 int ndims;
00164 int dims[NUM_DIMS];
00165 } testvars[] = {
00166 #define Byte_id 0
00167 { "Byte", NC_BYTE, "Byte sized integer variable",
00168 -MAXBYTE, MAXBYTE, -MAXBYTE, MAXBYTE , "ones",
00169 2, {0,1,DONT_CARE} },
00170 #define Char_id 1
00171 { "Char", NC_CHAR, "char (string) variable",
00172 DONT_CARE, DONT_CARE, DONT_CARE, DONT_CARE, "(unitless)",
00173 2, {0,2,DONT_CARE} },
00174 #define Short_id 2
00175 { "Short", NC_SHORT, "Short variable",
00176 -MAXSHORT, MAXSHORT, -MAXSHORT, MAXSHORT , "ones",
00177 2, {0, 2, DONT_CARE }},
00178 #define Long_id 3
00179 { "Long", NC_INT, "Long Integer variable",
00180 -MAXINT, MAXINT, -MAXINT, MAXINT, "ones",
00181 2, {1, 2, DONT_CARE}},
00182 #define Float_id 4
00183 { "Float", NC_FLOAT, "Single Precision Floating Point variable",
00184 -MAXINT, MAXINT, -MAXINT, MAXINT, "flots",
00185 3, {0, 1, 2 }},
00186 #define Double_id 5
00187 { "Double", NC_DOUBLE, "Double Precision Floating Point variable",
00188 -MAXINT, MAXINT, -MAXINT, MAXINT, "dflots",
00189 3, {0, 1, 2 }},
00190 };
00191 #define NUM_TESTVARS 6
00192
00193 static void
00194 createtestvars(int id, struct tcdfvar *testvars, size_t count)
00195 {
00196 int ii;
00197 int varid;
00198 struct tcdfvar *vp = testvars;
00199
00200 for(ii = 0; (size_t) ii < count; ii++, vp++ )
00201 {
00202 assert(nc_def_var(id, vp->mnem, vp->type, vp->ndims, vp->dims,
00203 &varid)
00204 == NC_NOERR );
00205
00206 assert(
00207 nc_put_att_text(id,ii,reqattr[0],strlen(vp->units),
00208 vp->units)
00209 == NC_NOERR);
00210 assert(
00211 nc_put_att_double(id,ii,reqattr[1],NC_DOUBLE,1,
00212 &vp->validmin)
00213 == NC_NOERR);
00214 assert(
00215 nc_put_att_double(id,ii,reqattr[2],NC_DOUBLE,1,
00216 &vp->validmax)
00217 == NC_NOERR);
00218 assert(
00219 nc_put_att_double(id,ii,reqattr[3],NC_DOUBLE,1,
00220 &vp->scalemin)
00221 == NC_NOERR);
00222 assert(
00223 nc_put_att_double(id,ii,reqattr[4],NC_DOUBLE,1,
00224 &vp->scalemax)
00225 == NC_NOERR);
00226 assert(
00227 nc_put_att_text(id,ii,reqattr[5],strlen(vp->fieldnam),
00228 vp->fieldnam)
00229 == NC_NOERR);
00230 }
00231 }
00232
00233 static void
00234 parray(char *label, size_t count, size_t array[])
00235 {
00236 (void) fprintf(stdout, "%s", label);
00237 (void) fputc('\t',stdout);
00238 for(; count != 0; count--, array++)
00239 (void) fprintf(stdout," %lu", (unsigned long) *array);
00240 }
00241
00242
00243 static void
00244 fill_seq(int id)
00245 {
00246 float values[NUM_RECS * SIZE_1 * SIZE_2];
00247 size_t vindices[NUM_DIMS];
00248
00249 {
00250 size_t ii = 0;
00251 for(; ii < sizeof(values)/sizeof(values[0]); ii++)
00252 {
00253 values[ii] = (float) ii;
00254 }
00255 }
00256
00257
00258 {
00259 size_t *cc = vindices;
00260 while (cc < &vindices[num_dims])
00261 *cc++ = 0;
00262 }
00263
00264 sizes[0] = NUM_RECS;
00265
00266 assert( nc_put_vara_float(id, Float_id, vindices, sizes, values)== NC_NOERR);
00267
00268 }
00269
00270 static void
00271 check_fill_seq(int id)
00272 {
00273 size_t vindices[NUM_DIMS];
00274 size_t *cc, *mm;
00275 union getret got;
00276 int ii = 0;
00277 float val;
00278
00279 sizes[0] = NUM_RECS;
00280 cc = vindices;
00281 while (cc < &vindices[num_dims])
00282 *cc++ = 0;
00283
00284
00285 cc = vindices;
00286 mm = sizes;
00287 while (*vindices < *sizes)
00288 {
00289 while (*cc < *mm)
00290 {
00291 if (mm == &sizes[num_dims - 1])
00292 {
00293 if(nc_get_var1_float(id, Float_id, vindices, &got.fl[0]) == -1)
00294 goto bad_ret;
00295 val = (float) ii;
00296 if(val != got.fl[0])
00297 {
00298 parray("indices", NUM_DIMS, vindices);
00299 (void) printf("\t%f != %f\n", val, got.fl[0]);
00300 }
00301 (*cc)++; ii++;
00302 continue;
00303 }
00304 cc++;
00305 mm++;
00306 }
00307 if(cc == vindices)
00308 break;
00309 *cc = 0;
00310 cc--;
00311 mm--;
00312 (*cc)++;
00313 }
00314 return;
00315 bad_ret :
00316 (void) printf("couldn't get a var in check_fill_seq() %d\n",
00317 ii);
00318 return;
00319 }
00320
00321 static size_t indices[][3] = {
00322 {0, 1, 3},
00323 {0, 3, 0},
00324 {1, 2, 3},
00325 {3, 2, 1},
00326 {2, 1, 3},
00327 {1, 0, 0},
00328 {0, 0, 0},
00329 };
00330
00331 static char chs[] = {'A','B', ((char)0xff) };
00332 static size_t s_start[] = {0,1};
00333 static size_t s_edges[] = {NUM_RECS, SIZE_1 - 1};
00334 static char sentence[NUM_RECS* SIZE_1 -1] =
00335 "The red death had long devastated the country.";
00336 static short shs[] = {97, 99};
00337 static int birthday = 82555;
00338 #define M_E 2.7182818284590452354
00339 static float e = (float) M_E;
00340 static double pinot = 3.25;
00341 static double zed = 0.0;
00342
00343
00344
00345 int
00346 main(int ac, char *av[])
00347 {
00348 int ret;
00349 int id;
00350 char buf[256];
00351 #ifdef SYNCDEBUG
00352 char *str = "one";
00353 #endif
00354 int ii;
00355 size_t ui;
00356 struct tcdfvar *tvp = testvars;
00357 union getret got;
00358 const size_t initialsz = 8192;
00359 size_t chunksz = 8192;
00360 size_t align = 8192/32;
00361
00362 ret = nc__create(fname,NC_NOCLOBBER, initialsz, &chunksz, &id);
00363 if(ret != NC_NOERR) {
00364 (void) fprintf(stderr, "trying again\n");
00365 ret = nc__create(fname,NC_CLOBBER, initialsz, &chunksz, &id);
00366 }
00367 if(ret != NC_NOERR)
00368 exit(ret);
00369
00370 assert( nc_put_att_text(id, NC_GLOBAL,
00371 "TITLE", 12, "another name") == NC_NOERR);
00372 assert( nc_get_att_text(id, NC_GLOBAL,
00373 "TITLE", buf) == NC_NOERR);
00374
00375 assert( nc_put_att_text(id, NC_GLOBAL,
00376 "TITLE", strlen(fname), fname) == NC_NOERR);
00377 assert( nc_get_att_text(id, NC_GLOBAL,
00378 "TITLE", buf) == NC_NOERR);
00379 buf[strlen(fname)] = 0;
00380
00381 assert( strcmp(fname, buf) == 0);
00382
00383 createtestdims(id, NUM_DIMS, sizes, dim_names);
00384 testdims(id, NUM_DIMS, sizes, dim_names);
00385
00386 createtestvars(id, testvars, NUM_TESTVARS);
00387
00388 {
00389 int ifill = -1; double dfill = -9999;
00390 assert( nc_put_att_int(id, Long_id,
00391 _FillValue, NC_INT, 1, &ifill) == NC_NOERR);
00392 assert( nc_put_att_double(id, Double_id,
00393 _FillValue, NC_DOUBLE, 1, &dfill) == NC_NOERR);
00394 }
00395
00396 #ifdef REDEF
00397 assert( nc__enddef(id, 0, align, 0, 2*align) == NC_NOERR );
00398 assert( nc_put_var1_int(id, Long_id, indices[3], &birthday)
00399 == NC_NOERR );
00400 fill_seq(id);
00401 assert( nc_redef(id) == NC_NOERR );
00402
00403 #endif
00404
00405 assert( nc_rename_dim(id,1, "IXX") == NC_NOERR);
00406 assert( nc_inq_dim(id, 1, buf, &ui) == NC_NOERR);
00407 (void) printf("dimrename: %s\n", buf);
00408 assert( nc_rename_dim(id,1, dim_names[1]) == NC_NOERR);
00409
00410 #ifdef ATTRX
00411 assert( nc_rename_att(id, 1, "UNITS", "units") == NC_NOERR);
00412 assert( nc_del_att(id, 4, "FIELDNAM")== NC_NOERR);
00413 assert( nc_del_att(id, 2, "SCALEMIN")== NC_NOERR);
00414 assert( nc_del_att(id, 2, "SCALEMAX")== NC_NOERR);
00415 #endif
00416
00417 assert( nc__enddef(id, 0, align, 0, 2*align) == NC_NOERR );
00418
00419 #ifndef REDEF
00420 fill_seq(id);
00421 assert( nc_put_var1_int(id, Long_id, indices[3], &birthday)== NC_NOERR );
00422 #endif
00423
00424 assert( nc_put_vara_schar(id, Byte_id, s_start, s_edges,
00425 (signed char *)sentence)
00426 == NC_NOERR);
00427 assert( nc_put_var1_schar(id, Byte_id, indices[6], (signed char *)(chs+1))
00428 == NC_NOERR);
00429 assert( nc_put_var1_schar(id, Byte_id, indices[5], (signed char *)chs)
00430 == NC_NOERR);
00431
00432 assert( nc_put_vara_text(id, Char_id, s_start, s_edges, sentence)
00433 == NC_NOERR);
00434 assert( nc_put_var1_text(id, Char_id, indices[6], (chs+1))
00435 == NC_NOERR) ;
00436 assert( nc_put_var1_text(id, Char_id, indices[5], chs)
00437 == NC_NOERR);
00438
00439 assert( nc_put_var1_short(id, Short_id, indices[4], shs)
00440 == NC_NOERR);
00441
00442 assert( nc_put_var1_float(id, Float_id, indices[2], &e)
00443 == NC_NOERR);
00444
00445 assert( nc_put_var1_double(id, Double_id, indices[1], &zed)
00446 == NC_NOERR);
00447 assert( nc_put_var1_double(id, Double_id, indices[0], &pinot)
00448 == NC_NOERR);
00449
00450
00451 #ifdef SYNCDEBUG
00452 (void) printf("Hit Return to sync\n");
00453 gets(str);
00454 nc_sync(id,0);
00455 (void) printf("Sync done. Hit Return to continue\n");
00456 gets(str);
00457 #endif
00458
00459 ret = nc_close(id);
00460 (void) printf("nc_close ret = %d\n\n", ret);
00461
00462
00463
00464
00465
00466 ret = nc__open(fname,NC_NOWRITE, &chunksz, &id);
00467 if(ret != NC_NOERR)
00468 {
00469 (void) printf("Could not open %s: %s\n", fname,
00470 nc_strerror(ret));
00471 exit(1);
00472 }
00473 (void) printf("reopen id = %d for filename %s\n",
00474 id, fname);
00475
00476
00477 (void) printf("NC ");
00478 assert( nc_inq(id, &(cdesc->num_dims), &(cdesc->num_vars),
00479 &(cdesc->num_attrs), &(cdesc->xtendim) ) == NC_NOERR);
00480 assert((size_t) cdesc->num_dims == num_dims);
00481 assert(cdesc->num_attrs == 1);
00482 assert(cdesc->num_vars == NUM_TESTVARS);
00483 (void) printf("done\n");
00484
00485
00486 (void) printf("GATTR ");
00487
00488 assert( nc_inq_attname(id, NC_GLOBAL, 0, adesc->mnem) == 0);
00489 assert(strcmp("TITLE",adesc->mnem) == 0);
00490 assert( nc_inq_att(id, NC_GLOBAL, adesc->mnem, &(adesc->type), &(adesc->len))== NC_NOERR);
00491 assert( adesc->type == NC_CHAR );
00492 assert( adesc->len == strlen(fname) );
00493 assert( nc_get_att_text(id, NC_GLOBAL, "TITLE", buf)== NC_NOERR);
00494 buf[adesc->len] = 0;
00495 assert( strcmp(fname, buf) == 0);
00496
00497
00498 (void) printf("VAR ");
00499 assert( cdesc->num_vars == NUM_TESTVARS );
00500
00501 for(ii = 0; ii < cdesc->num_vars; ii++, tvp++ )
00502 {
00503 int jj;
00504 assert( nc_inq_var(id, ii,
00505 vdesc->mnem,
00506 &(vdesc->type),
00507 &(vdesc->ndims),
00508 vdesc->dims,
00509 &(vdesc->num_attrs)) == NC_NOERR);
00510 if(strcmp(tvp->mnem , vdesc->mnem) != 0)
00511 {
00512 (void) printf("attr %d mnem mismatch %s, %s\n",
00513 ii, tvp->mnem, vdesc->mnem);
00514 continue;
00515 }
00516 if(tvp->type != vdesc->type)
00517 {
00518 (void) printf("attr %d type mismatch %d, %d\n",
00519 ii, tvp->type, vdesc->type);
00520 continue;
00521 }
00522 for(jj = 0; jj < vdesc->ndims; jj++ )
00523 {
00524 if(tvp->dims[jj] != vdesc->dims[jj] )
00525 {
00526 (void) printf(
00527 "inconsistant dim[%d] for variable %d: %d != %d\n",
00528 jj, ii, tvp->dims[jj], vdesc->dims[jj] );
00529 continue;
00530 }
00531 }
00532
00533
00534 (void) printf("VATTR\n");
00535 for(jj=0; jj<vdesc->num_attrs; jj++ )
00536 {
00537 assert( nc_inq_attname(id, ii, jj, adesc->mnem) == NC_NOERR);
00538 if( strcmp(adesc->mnem, reqattr[jj]) != 0 )
00539 {
00540 (void) printf("var %d attr %d mismatch %s != %s\n",
00541 ii, jj, adesc->mnem, reqattr[jj] );
00542 break;
00543 }
00544 }
00545
00546 if( nc_inq_att(id, ii, reqattr[0], &(adesc->type), &(adesc->len))
00547 != -1) {
00548 assert( adesc->type == NC_CHAR );
00549 assert( adesc->len == strlen(tvp->units) );
00550 assert( nc_get_att_text(id,ii,reqattr[0],buf)== NC_NOERR);
00551 buf[adesc->len] = 0;
00552 assert( strcmp(tvp->units, buf) == 0);
00553 }
00554
00555 if(
00556 nc_inq_att(id, ii, reqattr[1], &(adesc->type), &(adesc->len))
00557 != -1)
00558 {
00559 assert( adesc->type == NC_DOUBLE );
00560 assert( adesc->len == 1 );
00561 assert( nc_get_att_double(id, ii, reqattr[1], &got.dbl)== NC_NOERR);
00562 chkgot(adesc->type, got, tvp->validmin);
00563 }
00564
00565 if(
00566 nc_inq_att(id, ii, reqattr[2], &(adesc->type), &(adesc->len))
00567 != -1)
00568 {
00569 assert( adesc->type == NC_DOUBLE );
00570 assert( adesc->len == 1 );
00571 assert( nc_get_att_double(id, ii, reqattr[2], &got.dbl)== NC_NOERR);
00572 chkgot(adesc->type, got, tvp->validmax);
00573 }
00574
00575 if(
00576 nc_inq_att(id, ii, reqattr[3], &(adesc->type), &(adesc->len))
00577 != -1)
00578 {
00579 assert( adesc->type == NC_DOUBLE );
00580 assert( adesc->len ==1 );
00581 assert( nc_get_att_double(id, ii, reqattr[3], &got.dbl)== NC_NOERR);
00582 chkgot(adesc->type, got, tvp->scalemin);
00583 }
00584
00585 if(
00586 nc_inq_att(id, ii, reqattr[4], &(adesc->type), &(adesc->len))
00587 != -1)
00588 {
00589 assert( adesc->type == NC_DOUBLE );
00590 assert( adesc->len == 1 );
00591 assert( nc_get_att_double(id, ii, reqattr[4], &got.dbl)== NC_NOERR);
00592 chkgot(adesc->type, got, tvp->scalemax);
00593 }
00594
00595 if( nc_inq_att(id, ii, reqattr[5], &(adesc->type), &(adesc->len))== NC_NOERR)
00596 {
00597 assert( adesc->type == NC_CHAR );
00598 assert( adesc->len == strlen(tvp->fieldnam) );
00599 assert( nc_get_att_text(id,ii,reqattr[5],buf)== NC_NOERR);
00600 buf[adesc->len] = 0;
00601 assert( strcmp(tvp->fieldnam, buf) == 0);
00602 }
00603 }
00604
00605 (void) printf("fill_seq ");
00606 check_fill_seq(id);
00607 (void) printf("Done\n");
00608
00609 assert( nc_get_var1_double(id, Double_id, indices[0], &got.dbl)== NC_NOERR);
00610 (void) printf("got val = %f\n", got.dbl );
00611
00612 assert( nc_get_var1_double(id, Double_id, indices[1], &got.dbl)== NC_NOERR);
00613 (void) printf("got val = %f\n", got.dbl );
00614
00615 assert( nc_get_var1_float(id, Float_id, indices[2], &got.fl[0])== NC_NOERR);
00616 (void) printf("got val = %f\n", got.fl[0] );
00617
00618 assert( nc_get_var1_int(id, Long_id, indices[3], &got.in[0])== NC_NOERR);
00619 (void) printf("got val = %d\n", got.in[0] );
00620
00621 assert( nc_get_var1_short(id, Short_id, indices[4], &got.sh[0])== NC_NOERR);
00622 (void) printf("got val = %d\n", got.sh[0] );
00623
00624 assert( nc_get_var1_text(id, Char_id, indices[5], &got.by[0]) == NC_NOERR);
00625 (void) printf("got NC_CHAR val = %c (0x%02x) \n",
00626 got.by[0] , got.by[0]);
00627
00628 assert( nc_get_var1_text(id, Char_id, indices[6], &got.by[0]) == NC_NOERR);
00629 (void) printf("got NC_CHAR val = %c (0x%02x) \n",
00630 got.by[0], got.by[0] );
00631
00632 (void) memset(buf,0,sizeof(buf));
00633 assert( nc_get_vara_text(id, Char_id, s_start, s_edges, buf) == NC_NOERR);
00634 (void) printf("got NC_CHAR val = \"%s\"\n", buf);
00635
00636 assert( nc_get_var1_schar(id, Byte_id, indices[5],
00637 (signed char *)&got.by[0])== NC_NOERR);
00638 (void) printf("got val = %c (0x%02x) \n", got.by[0] , got.by[0]);
00639
00640 assert( nc_get_var1_schar(id, Byte_id, indices[6],
00641 (signed char *)&got.by[0])== NC_NOERR);
00642 (void) printf("got val = %c (0x%02x) \n", got.by[0], got.by[0] );
00643
00644 (void) memset(buf,0,sizeof(buf));
00645 assert( nc_get_vara_schar(id, Byte_id, s_start, s_edges,
00646 (signed char *)buf)== NC_NOERR );
00647 (void) printf("got val = \"%s\"\n", buf);
00648
00649 {
00650 double dbuf[NUM_RECS * SIZE_1 * SIZE_2];
00651 assert(nc_get_var_double(id, Float_id, dbuf) == NC_NOERR);
00652 (void) printf("got vals = %f ... %f\n", dbuf[0],
00653 dbuf[NUM_RECS * SIZE_1 * SIZE_2 -1] );
00654 }
00655
00656 ret = nc_close(id);
00657 (void) printf("re nc_close ret = %d\n", ret);
00658
00659 return 0;
00660 }