ncdf4a13/nc_test/test_read.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  *   Copyright 1996, UCAR/Unidata
00003  *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
00004  *   $Id: test_read.c,v 1.28 2005/11/07 20:25:32 ed Exp $
00005  *********************************************************************/
00006 
00007 #include "tests.h"
00008 
00009 /* 
00010  * Test nc_strerror.
00011  *    Try on a bad error status.
00012  *    Test for each defined error status.
00013  */
00014 void
00015 test_nc_strerror(void)
00016 {
00017     int i;
00018     const char *message;
00019 
00020     static const struct {
00021         int status;
00022         const char *msg;
00023     } ncerrs[] = {
00024         {NC_NOERR, "No error"},
00025         {NC_EBADID, "Not a netCDF id"},
00026         {NC_ENFILE, "Too many netCDF files open"},
00027         {NC_EEXIST, "netCDF file exists && NC_NOCLOBBER"},
00028         {NC_EINVAL, "Invalid argument"},
00029         {NC_EPERM, "Write to read only"},
00030         {NC_ENOTINDEFINE, "Operation not allowed in data mode"},
00031         {NC_EINDEFINE, "Operation not allowed in define mode"},
00032         {NC_EINVALCOORDS, "Index exceeds dimension bound"},
00033         {NC_EMAXDIMS, "NC_MAX_DIMS exceeded"},
00034         {NC_ENAMEINUSE, "String match to name in use"},
00035         {NC_ENOTATT, "Attribute not found"},
00036         {NC_EMAXATTS, "NC_MAX_ATTRS exceeded"},
00037         {NC_EBADTYPE, "Not a netCDF data type or _FillValue type mismatch"},
00038         {NC_EBADDIM, "Invalid dimension id or name"},
00039         {NC_EUNLIMPOS, "NC_UNLIMITED in the wrong index"},
00040         {NC_EMAXVARS, "NC_MAX_VARS exceeded"},
00041         {NC_ENOTVAR, "Variable not found"},
00042         {NC_EGLOBAL, "Action prohibited on NC_GLOBAL varid"},
00043         {NC_ENOTNC, "Not a netCDF file"},
00044         {NC_ESTS, "In Fortran, string too short"},
00045         {NC_EMAXNAME, "NC_MAX_NAME exceeded"},
00046         {NC_EUNLIMIT, "NC_UNLIMITED size already in use"},
00047         {NC_ENORECVARS, "nc_rec op when there are no record vars"},
00048         {NC_ECHAR, "Attempt to convert between text & numbers"},
00049         {NC_EEDGE, "Start+count exceeds dimension bound"},
00050         {NC_ESTRIDE, "Illegal stride"},
00051         {NC_EBADNAME, "Attribute or variable name contains illegal characters"},
00052         {NC_ERANGE, "Numeric conversion not representable"},
00053         {NC_ENOMEM, "Memory allocation (malloc) failure"},
00054         {NC_EVARSIZE, "One or more variable sizes violate format constraints"},
00055         {NC_EDIMSIZE, "Invalid dimension size"}
00056     };
00057 
00058     /* Try on a bad error status */
00059     message = nc_strerror(-666);/* should fail */
00060     IF (strcmp(message, "Unknown Error") != 0)
00061         error("nc_strerror on bad error status returned: %s", message);
00062 
00063     /* Try on each legitimate error status */
00064     for (i=0; i<LEN_OF(ncerrs); i++) {
00065         const char *message = nc_strerror(ncerrs[i].status);
00066         IF (strcmp(message, ncerrs[i].msg) != 0)
00067             error("nc_strerror(%d) should return `%s', not `%s'",
00068                   ncerrs[i].status, ncerrs[i].msg, message);
00069     }
00070 }
00071 
00072 
00073 /* 
00074  * Test nc_open.
00075  * If in read-only section of tests,
00076  *    Try to open a non-existent netCDF file, check error return.
00077  *    Open a file that is not a netCDF file, check error return.
00078  *    Open a netCDF file with a bad mode argument, check error return.
00079  *    Open a netCDF file with NC_NOWRITE mode, try to write, check error.
00080  *    Try to open a netcdf twice, check whether returned netcdf ids different.
00081  * If in writable section of tests,
00082  *    Open a netCDF file with NC_WRITE mode, write something, close it.
00083  * On exit, any open netCDF files are closed.
00084  */
00085 void
00086 test_nc_open(void)
00087 {
00088     int err;
00089     int ncid;
00090     int ncid2;
00091     
00092     /* Try to open a nonexistent file */
00093     err = nc_open("tooth-fairy.nc", NC_NOWRITE, &ncid);/* should fail */
00094     IF (err == NC_NOERR)
00095         error("nc_open of nonexistent file should have failed");
00096     IF (! NC_ISSYSERR(err))
00097         error("nc_open of nonexistent file should have returned system error");
00098 
00099     /* Open a file that is not a netCDF file. */
00100     err = nc_open("nc_test.o", NC_NOWRITE, &ncid);/* should fail */
00101     IF (err != NC_ENOTNC)
00102         error("nc_open of non-netCDF file: status = %d", err);
00103 
00104     /* Open a netCDF file in read-only mode, check that write fails */
00105     err = nc_open(testfile, NC_NOWRITE, &ncid);
00106     IF (err)
00107         error("nc_open: %s", nc_strerror(err));
00108     err = nc_redef(ncid);       /* should fail */
00109     IF (err != NC_EPERM)
00110         error("nc_redef of read-only file should fail");
00111     /* Opened OK, see if can open again and get a different netCDF ID */
00112     err = nc_open(testfile, NC_NOWRITE, &ncid2);
00113     IF (err)
00114         error("nc_open: %s", nc_strerror(err));
00115     else {
00116         (void) nc_close(ncid2);
00117     }
00118     IF (ncid2 == ncid)
00119         error("netCDF IDs for first and second nc_open calls should differ");
00120 
00121     err = nc_create(scratch, NC_NOCLOBBER, &ncid2);
00122     IF (err) 
00123        error("nc_create: %s", nc_strerror(err));
00124     else 
00125        (void) nc_close(ncid2);
00126     err = nc_open(scratch, NC_WRITE, &ncid2);
00127     IF (err) 
00128        error("nc_open: %s", nc_strerror(err));
00129     else 
00130        (void) nc_close(ncid2);
00131     err = remove(scratch);
00132     IF (err) 
00133        error("remove of %s failed", scratch);
00134 
00135     err = nc_close(ncid);
00136     IF (err)
00137         error("nc_close: %s", nc_strerror(err));
00138 }
00139 
00140 
00141 /* 
00142  * Test nc_close.
00143  *    Try to close a netCDF file twice, check whether second close fails.
00144  *    Try on bad handle, check error return.
00145  *    Try in define mode and data mode.
00146  */
00147 void
00148 test_nc_close(void)
00149 {
00150     int ncid;
00151     int err = nc_open(testfile, NC_NOWRITE, &ncid);
00152 
00153     IF (err)
00154         error("nc_open: %s", nc_strerror(err));
00155 
00156     /* Close a netCDF file twice, second time should fail */
00157     err = nc_close(ncid);
00158     IF (err)
00159         error("nc_close failed: %s", nc_strerror(err));
00160     err = nc_close(ncid);
00161     IF (err != NC_EBADID)
00162         error("nc_close of closed file should have failed");
00163     
00164     /* Try with a bad netCDF ID */
00165     err = nc_close(BAD_ID);/* should fail */
00166     IF (err != NC_EBADID)
00167         error("nc_close with bad netCDF ID returned wrong error (%d)", err);
00168 
00169     /* Close in data mode */
00170     err = nc_open(testfile, NC_NOWRITE, &ncid);
00171     IF (err)
00172         error("nc_open: %s", nc_strerror(err));
00173     err = nc_close(ncid);
00174     IF (err)
00175         error("nc_close in data mode failed: %s", nc_strerror(err));
00176 
00177     err = nc_create(scratch, NC_NOCLOBBER, &ncid);
00178     IF (err) 
00179        error("nc_create: %s", nc_strerror(err));
00180     err = nc_close(ncid);
00181     IF (err)
00182        error("nc_close in define mode: %s", nc_strerror(err));
00183     err = remove(scratch);
00184     IF (err)
00185        error("remove of %s failed", scratch);
00186 }
00187 
00188 
00189 /* 
00190  * Test nc_inq.
00191  *    Try on bad handle, check error return.
00192  *    Try in data mode, check returned values.
00193  *    Try asking for subsets of info.
00194  * If in writable section of tests,
00195  *    Try in define mode, after adding an unlimited dimension, variable.
00196  * On exit, any open netCDF files are closed.
00197  */
00198 void
00199 test_nc_inq(void)
00200 {
00201     int ncid;
00202     int ndims;                  /* number of dimensions */
00203     int nvars;                  /* number of variables */
00204     int ngatts;                 /* number of global attributes */
00205     int recdim;                 /* id of unlimited dimension */
00206     int err = nc_open(testfile, NC_NOWRITE, &ncid);
00207 
00208     IF (err)
00209         error("nc_open: %s", nc_strerror(err));
00210     
00211     /* Try on bad handle */
00212     err = nc_inq(BAD_ID, 0, 0, 0, 0);
00213     IF (err != NC_EBADID)
00214         error("bad ncid: status = %d", err);
00215     
00216     err = nc_inq(ncid, &ndims, &nvars, &ngatts, &recdim);
00217     IF (err)
00218         error("nc_inq: %s", nc_strerror(err));
00219     else IF (ndims != NDIMS)
00220         error("nc_inq: wrong number of dimensions returned, %d", ndims);
00221     else IF (nvars != NVARS)
00222         error("nc_inq: wrong number of variables returned, %d", nvars);
00223     else IF (ngatts != NGATTS)
00224         error("nc_inq: wrong number of global atts returned, %d", ngatts);
00225     else IF (recdim != RECDIM)
00226         error("nc_inq: wrong record dimension ID returned, %d", recdim);
00227     
00228     /* Inguire for no info (useless, but should still work) */
00229     err = nc_inq(ncid, 0, 0, 0, 0);
00230     IF (err)
00231         error("nc_inq for no info failed: %s", nc_strerror(err));
00232 
00233     /* Inguire for subsets of info */
00234     ngatts = NGATTS - 1;        /* wipe out previous correct value */
00235     err = nc_inq(ncid, 0, 0, &ngatts, 0);
00236     IF (err)
00237         error("nc_inq for one item failed: %s", nc_strerror(err));
00238     else IF (ngatts != NGATTS)
00239         error("nc_inq subset: wrong number of global atts returned, %d", ngatts);
00240     ndims = NDIMS - 1;
00241     nvars = NVARS - 1;
00242     err = nc_inq(ncid, &ndims, &nvars, 0, 0);
00243     IF (err)
00244         error("nc_inq for two items failed: %s", nc_strerror(err));
00245     else IF (ndims != NDIMS)
00246         error("nc_inq subset: wrong number of dimensions returned, %d", ndims);
00247     else IF (nvars != NVARS)
00248         error("nc_inq subset: wrong number of variables returned, %d", nvars);
00249 
00250     {           /* tests using netCDF scratch file */
00251         int ncid2;              /* for scratch netCDF dataset */
00252 
00253         err = nc_create(scratch, NC_NOCLOBBER, &ncid2);
00254         IF (err) {
00255             error("nc_create: %s", nc_strerror(err));
00256         } else {                /* add dim, var, gatt, check inq */
00257             int ndims0;
00258             int nvars0;
00259             int ngatts0;
00260             int recdim0;
00261             err = nc_enddef(ncid2); /* enter data mode */
00262             err = nc_inq(ncid2, &ndims0, &nvars0, &ngatts0, &recdim0);
00263             IF (err)
00264                 error("nc_inq: %s", nc_strerror(err));
00265             err = nc_redef(ncid2); /* enter define mode */
00266             /* Check that inquire still works in define mode */
00267             err = nc_inq(ncid2, &ndims, &nvars, &ngatts, &recdim);
00268             IF (err)
00269                 error("nc_inq in define mode: %s", nc_strerror(err));
00270             else IF (ndims != ndims0)
00271                 error("nc_inq in define mode: ndims wrong, %d", ndims);
00272             else IF (nvars != nvars0)
00273                 error("nc_inq in define mode: nvars wrong, %d", nvars);
00274             else IF (ngatts != ngatts0)
00275                 error("nc_inq in define mode: ngatts wrong, %d", ngatts);
00276             else IF (recdim != recdim0)
00277                 error("nc_inq in define mode: recdim wrong, %d", recdim);
00278 
00279             {
00280                 int did, vid;
00281                 /* Add dim, var, global att */
00282                 err = nc_def_dim(ncid2, "inqd", 1L, &did);
00283                 IF (err)
00284                     error("nc_def_dim: %s", nc_strerror(err));
00285                 err = nc_def_var(ncid2, "inqv", NC_FLOAT, 0, 0, &vid);
00286                 IF (err)
00287                     error("nc_def_var: %s", nc_strerror(err));
00288             }
00289             err = nc_put_att_text(ncid2, NC_GLOBAL, "inqa", 1+strlen("stuff"),
00290                                    "stuff");
00291             IF (err)
00292                 error("nc_put_att_text: %s", nc_strerror(err));
00293 
00294             /* Make sure nc_inq sees the additions while in define mode */
00295             err = nc_inq(ncid2, &ndims, &nvars, &ngatts, &recdim);
00296             IF (err)
00297                 error("nc_inq in define mode: %s", nc_strerror(err));
00298             else IF (ndims != ndims0 + 1)
00299                 error("nc_inq in define mode: ndims wrong, %d", ndims);
00300             else IF (nvars != nvars0 + 1)
00301                 error("nc_inq in define mode: nvars wrong, %d", nvars);
00302             else IF (ngatts != ngatts0 + 1)
00303                 error("nc_inq in define mode: ngatts wrong, %d", ngatts);
00304             err = nc_enddef(ncid2);
00305             IF (err)
00306                 error("nc_enddef: %s", nc_strerror(err));
00307 
00308             /* Make sure nc_inq stills sees additions in data mode */
00309             err = nc_inq(ncid2, &ndims, &nvars, &ngatts, &recdim);
00310             IF (err)
00311                 error("nc_inq failed in data mode: %s", nc_strerror(err));
00312             else IF (ndims != ndims0 + 1)
00313                 error("nc_inq in define mode: ndims wrong, %d", ndims);
00314             else IF (nvars != nvars0 + 1)
00315                 error("nc_inq in define mode: nvars wrong, %d", nvars);
00316             else IF (ngatts != ngatts0 + 1)
00317                 error("nc_inq in define mode: ngatts wrong, %d", ngatts);
00318             (void) nc_close(ncid2);
00319             err = remove(scratch);
00320             IF (err)
00321                 error("remove of %s failed", scratch);
00322         }
00323     }
00324 
00325     err = nc_close(ncid);
00326     IF (err)
00327         error("nc_close: %s", nc_strerror(err));
00328 }
00329 
00330 
00331 void
00332 test_nc_inq_natts(void)
00333 {
00334     int ncid;
00335     int ngatts;                 /* number of global attributes */
00336     int err;
00337 
00338     err = nc_inq_natts(BAD_ID, &ngatts);
00339     IF (err != NC_EBADID)
00340         error("bad ncid: status = %d", err);
00341     err = nc_open(testfile, NC_NOWRITE, &ncid);
00342     IF (err)
00343         error("nc_open: %s", nc_strerror(err));
00344     err = nc_inq_natts(ncid, &ngatts);
00345     IF (err)
00346         error("nc_inq_natts: %s", nc_strerror(err));
00347     else IF (ngatts != NGATTS)
00348         error("nc_inq_natts: wrong number of global atts returned, %d", ngatts);
00349     err = nc_close(ncid);
00350     IF (err)
00351         error("nc_close: %s", nc_strerror(err));
00352 }
00353 
00354 
00355 void
00356 test_nc_inq_ndims(void)
00357 {
00358     int ncid;
00359     int ndims;
00360     int err;
00361 
00362     err = nc_inq_ndims(BAD_ID, &ndims);
00363     IF (err != NC_EBADID)
00364         error("bad ncid: status = %d", err);
00365     err = nc_open(testfile, NC_NOWRITE, &ncid);
00366     IF (err)
00367         error("nc_open: %s", nc_strerror(err));
00368     err = nc_inq_ndims(ncid, &ndims);
00369     IF (err)
00370         error("nc_inq_ndims: %s", nc_strerror(err));
00371     else IF (ndims != NDIMS)
00372         error("nc_inq_ndims: wrong number returned, %d", ndims);
00373     err = nc_close(ncid);
00374     IF (err)
00375         error("nc_close: %s", nc_strerror(err));
00376 }
00377 
00378 
00379 void
00380 test_nc_inq_nvars(void)
00381 {
00382     int ncid;
00383     int nvars;
00384     int err;
00385 
00386     err = nc_inq_nvars(BAD_ID, &nvars);
00387     IF (err != NC_EBADID)
00388         error("bad ncid: status = %d", err);
00389     err = nc_open(testfile, NC_NOWRITE, &ncid);
00390     IF (err)
00391         error("nc_open: %s", nc_strerror(err));
00392     err = nc_inq_nvars(ncid, &nvars);
00393     IF (err)
00394         error("nc_inq_nvars: %s", nc_strerror(err));
00395     else IF (nvars != NVARS)
00396         error("nc_inq_nvars: wrong number returned, %d", nvars);
00397     err = nc_close(ncid);
00398     IF (err)
00399         error("nc_close: %s", nc_strerror(err));
00400 }
00401 
00402 
00403 void
00404 test_nc_inq_unlimdim(void)
00405 {
00406     int ncid;
00407     int unlimdim;
00408     int err;
00409 
00410     err = nc_inq_unlimdim(BAD_ID, &unlimdim);
00411     IF (err != NC_EBADID)
00412         error("bad ncid: status = %d", err);
00413     err = nc_open(testfile, NC_NOWRITE, &ncid);
00414     IF (err)
00415         error("nc_open: %s", nc_strerror(err));
00416     err = nc_inq_unlimdim(ncid, &unlimdim);
00417     IF (err)
00418         error("nc_inq_unlimdim: %s", nc_strerror(err));
00419     else IF (unlimdim != RECDIM)
00420         error("nc_inq_unlimdim: wrong number returned, %d", unlimdim);
00421     err = nc_close(ncid);
00422     IF (err)
00423         error("nc_close: %s", nc_strerror(err));
00424 }
00425 
00426 
00427 void
00428 test_nc_inq_dimid(void)
00429 {
00430     int ncid;
00431     int dimid;
00432     int i;
00433     int err;
00434 
00435     err = nc_open(testfile, NC_NOWRITE, &ncid);
00436     IF (err)
00437         error("nc_open: %s", nc_strerror(err));
00438     err = nc_inq_dimid(ncid, "noSuch", &dimid);
00439     IF (err != NC_EBADDIM)
00440         error("bad dim name: status = %d", err);
00441     for (i = 0; i < NDIMS; i++) {
00442         err = nc_inq_dimid(BAD_ID, dim_name[i], &dimid);
00443         IF (err != NC_EBADID)
00444             error("bad ncid: status = %d", err);
00445         err = nc_inq_dimid(ncid, dim_name[i], &dimid);
00446         IF (err)
00447             error("nc_inq_dimid: %s", nc_strerror(err));
00448         else IF (dimid != i)
00449             error("expected %d, got %d", i, dimid);
00450     }
00451     err = nc_close(ncid);
00452     IF (err)
00453         error("nc_close: %s", nc_strerror(err));
00454 }
00455 
00456 
00457 void
00458 test_nc_inq_dim(void)
00459 {
00460     int ncid;
00461     int i;
00462     int err;
00463     char name[NC_MAX_NAME];
00464     size_t length;
00465 
00466     err = nc_open(testfile, NC_NOWRITE, &ncid);
00467     IF (err)
00468         error("nc_open: %s", nc_strerror(err));
00469     for (i = 0; i < NDIMS; i++) {
00470         err = nc_inq_dim(BAD_ID, i, name, &length);
00471         IF (err != NC_EBADID)
00472             error("bad ncid: status = %d", err);
00473         err = nc_inq_dim(ncid, BAD_DIMID, name, &length);
00474         IF (err != NC_EBADDIM)
00475             error("bad dimid: status = %d", err);
00476         err = nc_inq_dim(ncid, i, 0, 0);
00477         IF (err)
00478             error("nc_inq_dim: %s", nc_strerror(err));
00479         err = nc_inq_dim(ncid, i, name, &length);
00480         IF (err)
00481             error("nc_inq_dim: %s", nc_strerror(err));
00482         else IF (strcmp(dim_name[i],name)) 
00483             error("name expected: %s, got: %s",dim_name[i],name);
00484         else IF (dim_len[i] != length)
00485             error("size expected: %d, got: %d",dim_len[i],length);
00486         err = nc_inq_dim(ncid, i, name, 0);
00487         IF (err)
00488             error("nc_inq_dim: %s", nc_strerror(err));
00489         else IF (strcmp(dim_name[i],name)) 
00490             error("name expected: %s, got: %s",dim_name[i],name);
00491         err = nc_inq_dim(ncid, i, 0, &length);
00492         IF (err)
00493             error("nc_inq_dim: %s", nc_strerror(err));
00494         else IF (dim_len[i] != length)
00495             error("size expected: %d, got: %d",dim_len[i],length);
00496     }
00497     err = nc_close(ncid);
00498     IF (err)
00499         error("nc_close: %s", nc_strerror(err));
00500 }
00501 
00502 
00503 void
00504 test_nc_inq_dimlen(void)
00505 {
00506     int ncid;
00507     int i;
00508     int err;
00509     size_t length;
00510 
00511     err = nc_open(testfile, NC_NOWRITE, &ncid);
00512     IF (err)
00513         error("nc_open: %s", nc_strerror(err));
00514     for (i = 0; i < NDIMS; i++) {
00515         err = nc_inq_dimlen(BAD_ID, i, &length);
00516         IF (err != NC_EBADID)
00517             error("bad ncid: status = %d", err);
00518         err = nc_inq_dimlen(ncid, BAD_DIMID, &length);
00519         IF (err != NC_EBADDIM)
00520             error("bad dimid: status = %d", err);
00521         err = nc_inq_dimlen(ncid, i, &length);
00522         IF (err)
00523             error("nc_inq_dimlen: %s", nc_strerror(err));
00524         else IF (dim_len[i] != length)
00525             error("size expected: %d, got: %d",dim_len[i],length);
00526     }
00527     err = nc_close(ncid);
00528     IF (err)
00529         error("nc_close: %s", nc_strerror(err));
00530 }
00531 
00532 
00533 void
00534 test_nc_inq_dimname(void)
00535 {
00536     int ncid;
00537     int i;
00538     int err;
00539     char name[NC_MAX_NAME];
00540 
00541     err = nc_open(testfile, NC_NOWRITE, &ncid);
00542     IF (err)
00543         error("nc_open: %s", nc_strerror(err));
00544     for (i = 0; i < NDIMS; i++) {
00545         err = nc_inq_dimname(BAD_ID, i, name);
00546         IF (err != NC_EBADID)
00547             error("bad ncid: status = %d", err);
00548         err = nc_inq_dimname(ncid, BAD_DIMID, name);
00549         IF (err != NC_EBADDIM)
00550             error("bad dimid: status = %d", err);
00551         err = nc_inq_dimname(ncid, i, name);
00552         IF (err)
00553             error("nc_inq_dimname: %s", nc_strerror(err));
00554         else IF (strcmp(dim_name[i],name)) 
00555             error("name expected: %s, got: %s",dim_name[i],name);
00556     }
00557     err = nc_close(ncid);
00558     IF (err)
00559         error("nc_close: %s", nc_strerror(err));
00560 }
00561 
00562 
00563 void
00564 test_nc_inq_varid(void)
00565 {
00566     int ncid;
00567     int varid;
00568     int i;
00569     int err;
00570 
00571     err = nc_open(testfile, NC_NOWRITE, &ncid);
00572     IF (err)
00573         error("nc_open: %s", nc_strerror(err));
00574 
00575     err = nc_inq_varid(ncid, "noSuch", &varid);
00576     IF (err != NC_ENOTVAR)
00577         error("bad ncid: status = %d", err);
00578 
00579     for (i = 0; i < NVARS; i++) {
00580         err = nc_inq_varid(BAD_ID, var_name[i], &varid);
00581         IF (err != NC_EBADID)
00582             error("bad ncid: status = %d", err);
00583         err = nc_inq_varid(ncid, var_name[i], &varid);
00584         IF (err)
00585             error("nc_inq_varid: %s", nc_strerror(err));
00586         else IF (varid != i)
00587             error("expected %d, got %d", i, varid);
00588     }
00589 
00590     err = nc_close(ncid);
00591     IF (err)
00592         error("nc_close: %s", nc_strerror(err));
00593 }
00594 
00595 
00596 void
00597 test_nc_inq_var(void)
00598 {
00599     int ncid;
00600     int i;
00601     int err;
00602     char name[NC_MAX_NAME];
00603     nc_type datatype;
00604     int ndims;
00605     int dimids[MAX_RANK];
00606     int natts;
00607 
00608     err = nc_open(testfile, NC_NOWRITE, &ncid);
00609     IF (err)
00610         error("nc_open: %s", nc_strerror(err));
00611     for (i = 0; i < NVARS; i++) {
00612         err = nc_inq_var(BAD_ID, i, name, &datatype, &ndims, dimids, &natts);
00613         IF (err != NC_EBADID)
00614             error("bad ncid: status = %d", err);
00615         err = nc_inq_var(ncid,BAD_VARID,name,&datatype,&ndims,dimids,&natts);
00616         IF (err != NC_ENOTVAR)
00617             error("bad var id: status = %d", err);
00618         err = nc_inq_var(ncid, i, 0, 0, 0, 0, 0);
00619         IF (err)
00620             error("nc_inq_var: %s", nc_strerror(err));
00621         err = nc_inq_var(ncid, i, name, &datatype, &ndims, dimids, &natts);
00622         IF (err)
00623             error("nc_inq_var: %s", nc_strerror(err));
00624         else IF (strcmp(var_name[i],name)) 
00625             error("name expected: %s, got: %s",var_name[i],name);
00626         else IF (var_type[i] != datatype)
00627             error("type expected: %d, got: %d",var_type[i],datatype);
00628         else IF (var_rank[i] != ndims)
00629             error("ndims expected: %d, got: %d",var_rank[i],ndims);
00630         else IF (!int_vec_eq(var_dimid[i],dimids,ndims))
00631             error("unexpected dimid");
00632         else IF (var_natts[i] != natts)
00633             error("natts expected: %d, got: %d",var_natts[i],natts);
00634         err = nc_inq_var(ncid, i, name, 0, 0, 0, 0);
00635         IF (err)
00636             error("nc_inq_var: %s", nc_strerror(err));
00637         else IF (strcmp(var_name[i],name)) 
00638             error("name expected: %s, got: %s",var_name[i],name);
00639         err = nc_inq_var(ncid, i, 0, &datatype, 0, 0, 0);
00640         IF (err)
00641             error("nc_inq_var: %s", nc_strerror(err));
00642         else IF (var_type[i] != datatype)
00643             error("type expected: %d, got: %d",var_type[i],datatype);
00644         err = nc_inq_var(ncid, i, 0, 0, &ndims, 0, 0);
00645         IF (err)
00646             error("nc_inq_var: %s", nc_strerror(err));
00647         else IF (var_rank[i] != ndims)
00648             error("ndims expected: %d, got: %d",var_rank[i],ndims);
00649         err = nc_inq_var(ncid, i, 0, 0, 0, dimids, 0);
00650         IF (err)
00651             error("nc_inq_var: %s", nc_strerror(err));
00652         else IF (!int_vec_eq(var_dimid[i],dimids,ndims))
00653             error("unexpected dimid");
00654         err = nc_inq_var(ncid, i, 0, 0, 0, 0, &natts);
00655         IF (err)
00656             error("nc_inq_var: %s", nc_strerror(err));
00657         else IF (var_natts[i] != natts)
00658             error("natts expected: %d, got: %d",var_natts[i],natts);
00659     }
00660     err = nc_close(ncid);
00661     IF (err)
00662         error("nc_close: %s", nc_strerror(err));
00663 }
00664 
00665 
00666 void
00667 test_nc_inq_vardimid(void)
00668 {
00669     int ncid;
00670     int i;
00671     int err;
00672     int dimids[MAX_RANK];
00673 
00674     err = nc_open(testfile, NC_NOWRITE, &ncid);
00675     IF (err)
00676         error("nc_open: %s", nc_strerror(err));
00677     for (i = 0; i < NVARS; i++) {
00678         err = nc_inq_vardimid(BAD_ID, i, dimids);
00679         IF (err != NC_EBADID)
00680             error("bad ncid: status = %d", err);
00681         err = nc_inq_vardimid(ncid, BAD_VARID, dimids);
00682         IF (err != NC_ENOTVAR)
00683             error("bad var id: status = %d", err);
00684         err = nc_inq_vardimid(ncid, i, dimids);
00685         IF (err)
00686             error("nc_inq_vardimid: %s", nc_strerror(err));
00687         else IF (!int_vec_eq(var_dimid[i], dimids, var_rank[i]))
00688             error("unexpected dimid");
00689     }
00690     err = nc_close(ncid);
00691     IF (err)
00692         error("nc_close: %s", nc_strerror(err));
00693 }
00694 
00695 
00696 void
00697 test_nc_inq_varname(void)
00698 {
00699     int ncid;
00700     int i;
00701     int err;
00702     char name[NC_MAX_NAME];
00703 
00704     err = nc_open(testfile, NC_NOWRITE, &ncid);
00705     IF (err)
00706         error("nc_open: %s", nc_strerror(err));
00707     for (i = 0; i < NVARS; i++) {
00708         err = nc_inq_varname(BAD_ID, i, name);
00709         IF (err != NC_EBADID)
00710             error("bad ncid: status = %d", err);
00711         err = nc_inq_varname(ncid, BAD_VARID, name);
00712         IF (err != NC_ENOTVAR)
00713             error("bad var id: status = %d", err);
00714         err = nc_inq_varname(ncid, i, name);
00715         IF (err)
00716             error("nc_inq_varname: %s", nc_strerror(err));
00717         else IF (strcmp(var_name[i],name)) 
00718             error("name expected: %s, got: %s",var_name[i],name);
00719     }
00720     err = nc_close(ncid);
00721     IF (err)
00722         error("nc_close: %s", nc_strerror(err));
00723 }
00724 
00725 
00726 void
00727 test_nc_inq_varnatts(void)
00728 {
00729     int ncid;
00730     int i;
00731     int err;
00732     int natts;
00733 
00734     err = nc_open(testfile, NC_NOWRITE, &ncid);
00735     IF (err)
00736         error("nc_open: %s", nc_strerror(err));
00737     for (i = -1; i < NVARS; i++) {
00738         err = nc_inq_varnatts(BAD_ID, i, &natts);
00739         IF (err != NC_EBADID)
00740             error("bad ncid: status = %d", err);
00741         err = nc_inq_varnatts(ncid, BAD_VARID, &natts);
00742         IF (err != NC_ENOTVAR)
00743             error("bad var id: status = %d", err);
00744         err = nc_inq_varnatts(ncid, VARID(i), &natts);
00745         IF (err)
00746             error("nc_inq_varnatts: %s", nc_strerror(err));
00747         else IF (NATTS(i) != natts)
00748             error("natts expected: %d, got: %d",NATTS(i),natts);
00749     }
00750     err = nc_close(ncid);
00751     IF (err)
00752         error("nc_close: %s", nc_strerror(err));
00753 }
00754 
00755 
00756 void
00757 test_nc_inq_varndims(void)
00758 {
00759     int ncid;
00760     int i;
00761     int err;
00762     int ndims;
00763 
00764     err = nc_open(testfile, NC_NOWRITE, &ncid);
00765     IF (err)
00766         error("nc_open: %s", nc_strerror(err));
00767     for (i = 0; i < NVARS; i++) {
00768         err = nc_inq_varndims(BAD_ID, i, &ndims);
00769         IF (err != NC_EBADID)
00770             error("bad ncid: status = %d", err);
00771         err = nc_inq_varndims(ncid, BAD_VARID, &ndims);
00772         IF (err != NC_ENOTVAR)
00773             error("bad var id: status = %d", err);
00774         err = nc_inq_varndims(ncid, i, &ndims);
00775         IF (err)
00776             error("nc_inq_varndims: %s", nc_strerror(err));
00777         else IF (var_rank[i] != ndims)
00778             error("ndims expected: %d, got: %d",var_rank[i],ndims);
00779     }
00780     err = nc_close(ncid);
00781     IF (err)
00782         error("nc_close: %s", nc_strerror(err));
00783 }
00784 
00785 
00786 void
00787 test_nc_inq_vartype(void)
00788 {
00789     int ncid;
00790     int i;
00791     int err;
00792     nc_type datatype;
00793 
00794     err = nc_open(testfile, NC_NOWRITE, &ncid);
00795     IF (err)
00796         error("nc_open: %s", nc_strerror(err));
00797     for (i = 0; i < NVARS; i++) {
00798         err = nc_inq_vartype(BAD_ID, i, &datatype);
00799         IF (err != NC_EBADID)
00800             error("bad ncid: status = %d", err);
00801         err = nc_inq_vartype(ncid, BAD_VARID, &datatype);
00802         IF (err != NC_ENOTVAR)
00803             error("bad var id: status = %d", err);
00804         err = nc_inq_vartype(ncid, i, &datatype);
00805         IF (err)
00806             error("nc_inq_vartype: %s", nc_strerror(err));
00807         else IF (var_type[i] != datatype)
00808             error("type expected: %d, got: %d", var_type[i], datatype);
00809     }
00810     err = nc_close(ncid);
00811     IF (err)
00812         error("nc_close: %s", nc_strerror(err));
00813 }
00814 
00815 
00816 /*
00817  * Test nc_put_var1
00818  */
00819 void
00820 test_nc_get_var1(void)
00821 {
00822     int ncid;
00823     int i;
00824     int j;
00825     int err;
00826     size_t index[MAX_RANK];
00827     double expect;
00828     int nok = 0;                /* count of valid comparisons */
00829     double buf[1];              /* (void *) buffer */
00830     double value;
00831 
00832     err = nc_open(testfile, NC_NOWRITE, &ncid);
00833     IF (err)
00834         error("nc_open: %s", nc_strerror(err));
00835     for (i = 0; i < NVARS; i++) {
00836         for (j = 0; j < var_rank[i]; j++)
00837             index[j] = 0;
00838         err = nc_get_var1(BAD_ID, i, index, buf);
00839         IF (err != NC_EBADID)
00840             error("bad ncid: status = %d", err);
00841         err = nc_get_var1(ncid, BAD_VARID, index, buf);
00842         IF (err != NC_ENOTVAR)
00843             error("bad var id: status = %d", err);
00844         for (j = 0; j < var_rank[i]; j++) {
00845             index[j] = var_shape[i][j];
00846             err = nc_get_var1(ncid, i, index, buf);
00847             IF (err != NC_EINVALCOORDS)
00848                 error("bad index: status = %d", err);
00849             index[j] = 0;
00850         }
00851         for (j = 0; j < var_nels[i]; j++) {
00852             err = toMixedBase(j, var_rank[i], var_shape[i], index);
00853             IF (err)
00854                 error("error in toMixedBase 2");
00855             expect = hash( var_type[i], var_rank[i], index );
00856             if (var_rank[i] == 0 && i%2 )
00857                 err = nc_get_var1(ncid, i, NULL, buf);
00858             else
00859                 err = nc_get_var1(ncid, i, index, buf);
00860             IF (err)
00861                 error("%s", nc_strerror(err));
00862             err = nc2dbl( var_type[i], buf, &value );
00863             IF (err)
00864                 error("error in nc2dbl");
00865             if (inRange(expect,var_type[i])) {
00866                 IF (!equal(value,expect,var_type[i],NCT_DOUBLE)) {
00867                     error("expected: %G, got: %G", expect, value);
00868                 } else {
00869                     nok++;
00870                 }
00871             }
00872         }
00873     }
00874     err = nc_close(ncid);
00875     IF (err)
00876         error("nc_close: %s", nc_strerror(err));
00877     print_nok(nok);
00878 }
00879 
00880 /*
00881  * Test nc_get_vara
00882  * Choose a random point dividing each dim into 2 parts
00883  * Get 2^rank (nslabs) slabs so defined
00884  * Each get overwrites buffer, so check after each get.
00885  */
00886 void
00887 test_nc_get_vara(void)
00888 {
00889     int ncid;
00890     int d;
00891     int i;
00892     int j;
00893     int k;
00894     int err;
00895     int nels;
00896     int nslabs;
00897     int nok = 0;      /* count of valid comparisons */
00898     size_t start[MAX_RANK];
00899     size_t edge[MAX_RANK];
00900     size_t index[MAX_RANK];
00901     size_t mid[MAX_RANK];
00902     double buf[MAX_NELS];       /* (void *) buffer */
00903     char *p;                    /* (void *) pointer */
00904     double expect;
00905     double got;
00906 
00907     err = nc_open(testfile, NC_NOWRITE, &ncid);
00908     IF (err)
00909         error("nc_open: %s", nc_strerror(err));
00910     for (i = 0; i < NVARS; i++) {
00911         assert(var_rank[i] <= MAX_RANK);
00912         assert(var_nels[i] <= MAX_NELS);
00913         for (j = 0; j < var_rank[i]; j++) {
00914             start[j] = 0;
00915             edge[j] = 1;
00916         }
00917         err = nc_get_vara(BAD_ID, i, start, edge, buf);
00918         IF (err != NC_EBADID)
00919             error("bad ncid: status = %d", err);
00920         err = nc_get_vara(ncid, BAD_VARID, start, edge, buf);
00921         IF (err != NC_ENOTVAR)
00922             error("bad var id: status = %d", err);
00923         for (j = 0; j < var_rank[i]; j++) {
00924             start[j] = var_shape[i][j];
00925             err = nc_get_vara(ncid, i, start, edge, buf);
00926             IF (err != NC_EINVALCOORDS)
00927                 error("bad index: status = %d", err);
00928             start[j] = 0;
00929             edge[j] = var_shape[i][j] + 1;
00930             err = nc_get_vara(ncid, i, start, edge, buf);
00931             IF (err != NC_EEDGE)
00932                 error("bad edge: status = %d", err);
00933             edge[j] = 1;
00934         }
00935             /* Choose a random point dividing each dim into 2 parts */
00936             /* get 2^rank (nslabs) slabs so defined */
00937         nslabs = 1;
00938         for (j = 0; j < var_rank[i]; j++) {
00939             mid[j] = roll( var_shape[i][j] );
00940             nslabs *= 2;
00941         }
00942             /* bits of k determine whether to get lower or upper part of dim */
00943         for (k = 0; k < nslabs; k++) {
00944             nels = 1;
00945             for (j = 0; j < var_rank[i]; j++) {
00946                 if ((k >> j) & 1) {
00947                     start[j] = 0;
00948                     edge[j] = mid[j];
00949                 }else{
00950                     start[j] = mid[j];
00951                     edge[j] = var_shape[i][j] - mid[j];
00952                 }
00953                 nels *= edge[j];
00954             }
00955             if (var_rank[i] == 0 && i%2 )
00956                 err = nc_get_vara(ncid, i, NULL, NULL, buf);
00957             else
00958                 err = nc_get_vara(ncid, i, start, edge, buf);
00959             IF (err) {
00960                 error("%s", nc_strerror(err));
00961             } else {
00962                 for (j = 0; j < nels; j++) {
00963                     p = (char *) buf;
00964                     p += j * nctypelen(var_type[i]);
00965                     err = nc2dbl( var_type[i], p, & got );
00966                     IF (err)
00967                         error("error in nc2dbl");
00968                     err = toMixedBase(j, var_rank[i], edge, index);
00969                     IF (err)
00970                         error("error in toMixedBase 1");
00971                     for (d = 0; d < var_rank[i]; d++)
00972                         index[d] += start[d];
00973                     expect = hash(var_type[i], var_rank[i], index);
00974                     if (inRange(expect,var_type[i])) {
00975                         IF (!equal(got,expect,var_type[i],NCT_DOUBLE)) {
00976                             error("value read not that expected");
00977                             if (verbose) {
00978                                 error("\n");
00979                                 error("varid: %d, ", i);
00980                                 error("var_name: %s, ", var_name[i]);
00981                                 error("element number: %d ", j);
00982                                 error("expect: %g", expect);
00983                                 error("got: %g", got);
00984                             }
00985                         } else {
00986                             nok++;
00987                         }
00988                     }
00989                 }
00990             }
00991         }
00992     }
00993     err = nc_close(ncid);
00994     IF (err)
00995         error("nc_close: %s", nc_strerror(err));
00996     print_nok(nok);
00997 }
00998 
00999 
01000 /*
01001  * Test nc_get_vars
01002  * Choose a random point dividing each dim into 2 parts
01003  * Get 2^rank (nslabs) slabs so defined
01004  * Each get overwrites buffer, so check after each get.
01005  */
01006 void
01007 test_nc_get_vars(void)
01008 {
01009     int ncid;
01010     int d;
01011     int i;
01012     int j;
01013     int k;
01014     int m;
01015     int err;
01016     int nels;
01017     int nslabs;
01018     int nstarts;        /* number of different starts */
01019     int nok = 0;        /* total count of valid comparisons */
01020     int n;              /* count of valid comparisons within var */
01021     size_t start[MAX_RANK];
01022     size_t edge[MAX_RANK];
01023     size_t index[MAX_RANK];
01024     size_t index2[MAX_RANK];
01025     size_t mid[MAX_RANK];
01026     size_t count[MAX_RANK];
01027     size_t sstride[MAX_RANK];
01028     ptrdiff_t stride[MAX_RANK];
01029     double buf[MAX_NELS];     /* (void *) buffer */
01030     char *p;                    /* (void *) pointer */
01031     double expect;
01032     double got;
01033 
01034     err = nc_open(testfile, NC_NOWRITE, &ncid);
01035     IF (err)
01036         error("nc_open: %s", nc_strerror(err));
01037     for (i = 0; i < NVARS; i++) {
01038         assert(var_rank[i] <= MAX_RANK);
01039         assert(var_nels[i] <= MAX_NELS);
01040         for (j = 0; j < var_rank[i]; j++) {
01041             start[j] = 0;
01042             edge[j] = 1;
01043             stride[j] = 1;
01044         }
01045         err = nc_get_vars(BAD_ID, i, start, edge, stride, buf);
01046         IF (err != NC_EBADID)
01047             error("bad ncid: status = %d", err);
01048         err = nc_get_vars(ncid, BAD_VARID, start, edge, stride, buf);
01049         IF (err != NC_ENOTVAR)
01050             error("bad var id: status = %d", err);
01051         for (j = 0; j < var_rank[i]; j++) {
01052             start[j] = var_shape[i][j];
01053             err = nc_get_vars(ncid, i, start, edge, stride, buf);
01054             IF (err != NC_EINVALCOORDS)
01055                 error("bad index: status = %d", err);
01056             start[j] = 0;
01057             edge[j] = var_shape[i][j] + 1;
01058             err = nc_get_vars(ncid, i, start, edge, stride, buf);
01059             IF (err != NC_EEDGE)
01060                 error("bad edge: status = %d", err);
01061             edge[j] = 1;
01062             stride[j] = 0;
01063             err = nc_get_vars(ncid, i, start, edge, stride, buf);
01064             IF (err != NC_ESTRIDE)
01065                 error("bad stride: status = %d", err);
01066             stride[j] = 1;
01067         }
01068             /* Choose a random point dividing each dim into 2 parts */
01069             /* get 2^rank (nslabs) slabs so defined */
01070         nslabs = 1;
01071         for (j = 0; j < var_rank[i]; j++) {
01072             mid[j] = roll( var_shape[i][j] );
01073             nslabs *= 2;
01074         }
01075             /* bits of k determine whether to get lower or upper part of dim */
01076             /* choose random stride from 1 to edge */
01077         n = 0;
01078         for (k = 0; k < nslabs; k++) {
01079             nstarts = 1;
01080             for (j = 0; j < var_rank[i]; j++) {
01081                 if ((k >> j) & 1) {
01082                     start[j] = 0;
01083                     edge[j] = mid[j];
01084                 }else{
01085                     start[j] = mid[j];
01086                     edge[j] = var_shape[i][j] - mid[j];
01087                 }
01088                 sstride[j] = stride[j] = edge[j] > 0 ? 1+roll(edge[j]) : 1;
01089                 nstarts *= stride[j];
01090             }
01091             for (m = 0; m < nstarts; m++) {
01092                 err = toMixedBase(m, var_rank[i], sstride, index);
01093                 IF (err)
01094                     error("error in toMixedBase");
01095                 nels = 1;
01096                 for (j = 0; j < var_rank[i]; j++) {
01097                     count[j] = 1 + (edge[j] - index[j] - 1) / stride[j];
01098                     nels *= count[j];
01099                     index[j] += start[j];
01100                 }
01101                         /* Random choice of forward or backward */
01102 /* TODO
01103                 if ( roll(2) ) {
01104                     for (j = 0; j < var_rank[i]; j++) {
01105                         index[j] += (count[j] - 1) * stride[j];
01106                         stride[j] = -stride[j];
01107                     }
01108                 }
01109  */
01110                 if (var_rank[i] == 0 && i%2 )
01111                     err = nc_get_vars(ncid, i, NULL, NULL, NULL, buf);
01112                 else
01113                     err = nc_get_vars(ncid, i, index, count, stride, buf);
01114                 IF (err) {
01115                     error("%s", nc_strerror(err));
01116                 } else {
01117                     for (j = 0; j < nels; j++) {
01118                         p = (char *) buf;
01119                         p += j * nctypelen(var_type[i]);
01120                         err = nc2dbl( var_type[i], p, & got );
01121                         IF (err)
01122                             error("error in nc2dbl");
01123                         err = toMixedBase(j, var_rank[i], count, index2);
01124                         IF (err)
01125                             error("error in toMixedBase 1");
01126                         for (d = 0; d < var_rank[i]; d++)
01127                             index2[d] = index[d] + index2[d] * stride[d];
01128                         expect = hash(var_type[i], var_rank[i], index2);
01129                         if (inRange(expect,var_type[i])) {
01130                             IF (!equal(got,expect,var_type[i],NCT_DOUBLE)) {
01131                                 error("value read not that expected");
01132                                 if (verbose) {
01133                                     error("\n");
01134                                     error("varid: %d, ", i);
01135                                     error("var_name: %s, ", var_name[i]);
01136                                     error("element number: %d ", j);
01137                                     error("expect: %g, ", expect);
01138                                     error("got: %g ", got);
01139                                 }
01140                             } else {
01141                                 nok++;
01142                             }
01143                         }
01144                         n++;
01145                     }
01146                 }
01147             }
01148         }
01149         IF (n != var_nels[i]) {
01150             error("count != nels");
01151             if (verbose) {
01152                 error("\n");
01153                 error("varid: %d, ", i);
01154                 error("var_name: %s, ", var_name[i]);
01155                 error("count: %d, ", n);
01156                 error("nels: %d ", var_nels[i]);
01157             }
01158         }
01159     }
01160     err = nc_close(ncid);
01161     IF (err)
01162         error("nc_close: %s", nc_strerror(err));
01163     print_nok(nok);
01164 }
01165 
01166 
01167 /*
01168  * Test nc_get_varm
01169  * Choose a random point dividing each dim into 2 parts
01170  * Get 2^rank (nslabs) slabs so defined
01171  * Choose random stride from 1 to edge
01172  * Buffer should end up being bit image of external variable.
01173  * So all gets for a variable store in different elements of buffer
01174  */
01175 void
01176 test_nc_get_varm(void)
01177 {
01178     int ncid;
01179     int i;
01180     int j;
01181     int k;
01182     int m;
01183     int err;
01184     int nslabs;
01185     int nstarts;        /* number of different starts */
01186     int nok = 0;        /* total count of valid comparisons */
01187     size_t start[MAX_RANK];
01188     size_t edge[MAX_RANK];
01189     size_t index[MAX_RANK];
01190     size_t mid[MAX_RANK];
01191     size_t count[MAX_RANK];
01192     size_t sstride[MAX_RANK];
01193     ptrdiff_t stride[MAX_RANK];
01194     ptrdiff_t imap[MAX_RANK];
01195     ptrdiff_t imap2[MAX_RANK];
01196     double buf[MAX_NELS];       /* (void *) buffer */
01197     char *p;                    /* (void *) pointer */
01198     double expect;
01199     double got;
01200 
01201     err = nc_open(testfile, NC_NOWRITE, &ncid);
01202     IF (err)
01203         error("nc_open: %s", nc_strerror(err));
01204     for (i = 0; i < NVARS; i++) {
01205         assert(var_rank[i] <= MAX_RANK);
01206         assert(var_nels[i] <= MAX_NELS);
01207         for (j = 0; j < var_rank[i]; j++) {
01208             start[j] = 0;
01209             edge[j] = 1;
01210             stride[j] = 1;
01211         }
01212         if (var_rank[i] > 0) {
01213             j = var_rank[i] - 1;
01214             imap[j] = nctypelen(var_type[i]);
01215             for (; j > 0; j--)
01216                 imap[j-1] = imap[j] * var_shape[i][j];
01217         }
01218         err = nc_get_varm(BAD_ID, i, start, edge, stride, imap, buf);
01219         IF (err != NC_EBADID)
01220             error("bad ncid: status = %d", err);
01221         err = nc_get_varm(ncid, BAD_VARID, start, edge, stride, imap, buf);
01222         IF (err != NC_ENOTVAR)
01223             error("bad var id: status = %d", err);
01224         for (j = 0; j < var_rank[i]; j++) {
01225             start[j] = var_shape[i][j];
01226             err = nc_get_varm(ncid, i, start, edge, stride, imap, buf);
01227             IF (err != NC_EINVALCOORDS)
01228                 error("bad index: status = %d", err);
01229             start[j] = 0;
01230             edge[j] = var_shape[i][j] + 1;
01231             err = nc_get_varm(ncid, i, start, edge, stride, imap, buf);
01232             IF (err != NC_EEDGE)
01233                 error("bad edge: status = %d", err);
01234             edge[j] = 1;
01235             stride[j] = 0;
01236             err = nc_get_varm(ncid, i, start, edge, stride, imap, buf);
01237             IF (err != NC_ESTRIDE)
01238                 error("bad stride: status = %d", err);
01239             stride[j] = 1;
01240         }
01241             /* Choose a random point dividing each dim into 2 parts */
01242             /* get 2^rank (nslabs) slabs so defined */
01243         nslabs = 1;
01244         for (j = 0; j < var_rank[i]; j++) {
01245             mid[j] = roll( var_shape[i][j] );
01246             nslabs *= 2;
01247         }
01248             /* bits of k determine whether to get lower or upper part of dim */
01249             /* choose random stride from 1 to edge */
01250         for (k = 0; k < nslabs; k++) {
01251             nstarts = 1;
01252             for (j = 0; j < var_rank[i]; j++) {
01253                 if ((k >> j) & 1) {
01254                     start[j] = 0;
01255                     edge[j] = mid[j];
01256                 }else{
01257                     start[j] = mid[j];
01258                     edge[j] = var_shape[i][j] - mid[j];
01259                 }
01260                 sstride[j] = stride[j] = edge[j] > 0 ? 1+roll(edge[j]) : 1;
01261                 imap2[j] = imap[j] * sstride[j];
01262                 nstarts *= stride[j];
01263             }
01264             for (m = 0; m < nstarts; m++) {
01265                 if (var_rank[i] == 0 && i%2 ) {
01266                     err = nc_get_varm(ncid, i, NULL, NULL, NULL, NULL, buf);
01267                 } else {
01268                     err = toMixedBase(m, var_rank[i], sstride, index);
01269                     IF (err)
01270                         error("error in toMixedBase");
01271                     for (j = 0; j < var_rank[i]; j++) {
01272                         count[j] = 1 + (edge[j] - index[j] - 1) / stride[j];
01273                         index[j] += start[j];
01274                     }
01275                             /* Random choice of forward or backward */
01276 /* TODO
01277                     if ( roll(2) ) {
01278                         for (j = 0; j < var_rank[i]; j++) {
01279                             index[j] += (count[j] - 1) * stride[j];
01280                             stride[j] = -stride[j];
01281                         }
01282                     }
01283  */
01284                     j = fromMixedBase(var_rank[i], index, var_shape[i]);
01285                     p = (char *) buf + j * nctypelen(var_type[i]);
01286                     err = nc_get_varm(ncid, i, index, count, stride, imap2, p);
01287                 }
01288                 IF (err)
01289                     error("%s", nc_strerror(err));
01290             }
01291         }
01292         p = (char *) buf;
01293         for (j = 0; j < var_nels[i]; j++) {
01294             err = toMixedBase(j, var_rank[i], var_shape[i], index);
01295             IF (err)
01296                 error("error in toMixedBase");
01297             expect = hash( var_type[i], var_rank[i], index);
01298             err = nc2dbl( var_type[i], p, & got );
01299             IF (err)
01300                 error("error in nc2dbl");
01301             if (inRange(expect,var_type[i])) {
01302                 IF (!equal(got,expect,var_type[i],NCT_DOUBLE)) {
01303                     error("value read not that expected");
01304                     if (verbose) {
01305                         error("\n");
01306                         error("varid: %d, ", i);
01307                         error("var_name: %s, ", var_name[i]);
01308                         error("element number: %d ", j);
01309                         error("expect: %g, ", expect);
01310                         error("got: %g ", got);
01311                     }
01312                 } else {
01313                     nok++;
01314                 }
01315             }
01316             p += nctypelen(var_type[i]);
01317         }
01318     }
01319     err = nc_close(ncid);
01320     IF (err)
01321         error("nc_close: %s", nc_strerror(err));
01322     print_nok(nok);
01323 }
01324 
01325 
01326 void
01327 test_nc_get_att(void)
01328 {
01329     int ncid;
01330     int i;
01331     int j;
01332     size_t k;
01333     int err;
01334     double buf[MAX_NELS];       /* (void *) buffer */
01335     char *p;                    /* (void *) pointer */
01336     double expect;
01337     double got;
01338     int nok = 0;      /* count of valid comparisons */
01339 
01340     err = nc_open(testfile, NC_NOWRITE, &ncid);
01341     IF (err) 
01342         error("nc_open: %s", nc_strerror(err));
01343 
01344     for (i = -1; i < NVARS; i++) {
01345         for (j = 0; j < NATTS(i); j++) {
01346             err = nc_get_att(BAD_ID, i, ATT_NAME(i,j), buf);
01347             IF (err != NC_EBADID) 
01348                 error("bad ncid: status = %d", err);
01349             err = nc_get_att(ncid, BAD_VARID, ATT_NAME(i,j), buf);
01350             IF (err != NC_ENOTVAR) 
01351                 error("bad var id: status = %d", err);
01352             err = nc_get_att(ncid, i, "noSuch", buf);
01353             IF (err != NC_ENOTATT) 
01354                 error("Bad attribute name: status = %d", err);
01355             err = nc_get_att(ncid, i, ATT_NAME(i,j), buf);
01356             IF (err) {
01357                 error("%s", nc_strerror(err));
01358             } else {
01359                 for (k = 0; k < ATT_LEN(i,j); k++) {
01360                     expect = hash(ATT_TYPE(i,j), -1, &k );
01361                     p = (char *) buf;
01362                     p += k * nctypelen(ATT_TYPE(i,j));
01363                     err = nc2dbl( ATT_TYPE(i,j), p, &got );
01364                     IF (err)
01365                         error("error in nc2dbl");
01366                     if (inRange(expect,ATT_TYPE(i,j))) {
01367                         IF (!equal(got,expect,ATT_TYPE(i,j),NCT_DOUBLE)) {
01368                             error("value read not that expected");
01369                             if (verbose) {
01370                                 error("\n");
01371                                 error("varid: %d, ", i);
01372                                 error("var_name: %s, ",
01373                                         i >= 0 ? var_name[i] : "Global");
01374                                 error("att_name: %s, ", ATT_NAME(i,j));
01375                                 error("element number: %d\n", k);
01376                                 error("expect: %-23.16e\n", expect);
01377                                 error("   got: %-23.16e", got);
01378                             }
01379                         } else {
01380                             nok++;
01381                         }
01382                     }
01383                 }
01384             }
01385         }
01386     }
01387 
01388     err = nc_close(ncid);
01389     IF (err)
01390         error("nc_close: %s", nc_strerror(err));
01391     print_nok(nok);
01392 }
01393 
01394 
01395 void
01396 test_nc_inq_att(void)
01397 {
01398     int ncid;
01399     int i;
01400     int j;
01401     int err;
01402     nc_type t;
01403     size_t n;
01404 
01405     err = nc_open(testfile, NC_NOWRITE, &ncid);
01406     IF (err) 
01407         error("nc_open: %s", nc_strerror(err));
01408 
01409     for (i = -1; i < NVARS; i++) {
01410         for (j = 0; j < NATTS(i); j++) {
01411             err = nc_inq_att(BAD_ID, i, ATT_NAME(i,j), &t, &n);
01412             IF (err != NC_EBADID) 
01413                 error("bad ncid: status = %d", err);
01414             err = nc_inq_att(ncid, BAD_VARID, ATT_NAME(i,j), &t, &n);
01415             IF (err != NC_ENOTVAR) 
01416                 error("bad var id: status = %d", err);
01417             err = nc_inq_att(ncid, i, "noSuch", &t, &n);
01418             IF (err != NC_ENOTATT) 
01419                 error("Bad attribute name: status = %d", err);
01420             err = nc_inq_att(ncid, i, ATT_NAME(i,j), &t, &n);
01421             IF (err) {
01422                 error("%s", nc_strerror(err));
01423             } else {
01424                 IF (t != ATT_TYPE(i,j))
01425                     error("type not that expected");
01426                 IF (n != ATT_LEN(i,j)) 
01427                     error("length not that expected");
01428             }
01429         }
01430     }
01431 
01432     err = nc_close(ncid);
01433     IF (err)
01434         error("nc_close: %s", nc_strerror(err));
01435 }
01436 
01437 
01438 void
01439 test_nc_inq_attlen(void)
01440 {
01441     int ncid;
01442     int i;
01443     int j;
01444     int err;
01445     size_t len;
01446 
01447     err = nc_open(testfile, NC_NOWRITE, &ncid);
01448     IF (err)
01449         error("nc_open: %s", nc_strerror(err));
01450 
01451     for (i = -1; i < NVARS; i++) {
01452         err = nc_inq_attlen(ncid, i, "noSuch", &len);
01453         IF (err != NC_ENOTATT)
01454             error("Bad attribute name: status = %d", err);
01455         for (j = 0; j < NATTS(i); j++) {
01456             err = nc_inq_attlen(BAD_ID, i, ATT_NAME(i,j), &len);
01457             IF (err != NC_EBADID)
01458                 error("bad ncid: status = %d", err);
01459             err = nc_inq_attlen(ncid, BAD_VARID, ATT_NAME(i,j), &len);
01460             IF (err != NC_ENOTVAR)
01461                 error("bad varid: status = %d", err);
01462             err = nc_inq_attlen(ncid, i, ATT_NAME(i,j), &len);
01463             IF (err) {
01464                 error("%s", nc_strerror(err));
01465             } else {
01466                 IF (len != ATT_LEN(i,j))
01467                     error("len not that expected");
01468             }
01469         }
01470     }
01471 
01472     err = nc_close(ncid);
01473     IF (err)
01474         error("nc_close: %s", nc_strerror(err));
01475 }
01476 
01477 
01478 void
01479 test_nc_inq_atttype(void)
01480 {
01481     int ncid;
01482     int i;
01483     int j;
01484     int err;
01485     nc_type datatype;
01486 
01487     err = nc_open(testfile, NC_NOWRITE, &ncid);
01488     IF (err)
01489         error("nc_open: %s", nc_strerror(err));
01490 
01491     for (i = -1; i < NVARS; i++) {
01492         err = nc_inq_atttype(ncid, i, "noSuch", &datatype);
01493         IF (err != NC_ENOTATT)
01494             error("Bad attribute name: status = %d", err);
01495         for (j = 0; j < NATTS(i); j++) {
01496             err = nc_inq_atttype(BAD_ID, i, ATT_NAME(i,j), &datatype);
01497             IF (err != NC_EBADID)
01498                 error("bad ncid: status = %d", err);
01499             err = nc_inq_atttype(ncid, BAD_VARID, ATT_NAME(i,j), &datatype);
01500             IF (err != NC_ENOTVAR)
01501                 error("bad varid: status = %d", err);
01502             err = nc_inq_atttype(ncid, i, ATT_NAME(i,j), &datatype);
01503             IF (err) {
01504                 error("%s", nc_strerror(err));
01505             } else {
01506                 IF (datatype != ATT_TYPE(i,j))
01507                     error("type not that expected");
01508             }
01509         }
01510     }
01511 
01512     err = nc_close(ncid);
01513     IF (err)
01514         error("nc_close: %s", nc_strerror(err));
01515 }
01516 
01517 
01518 void
01519 test_nc_inq_attname(void)
01520 {
01521     int ncid;
01522     int i;
01523     int j;
01524     int err;
01525     char name[NC_MAX_NAME];
01526 
01527     err = nc_open(testfile, NC_NOWRITE, &ncid);
01528     IF (err)
01529         error("nc_open: %s", nc_strerror(err));
01530 
01531     for (i = -1; i < NVARS; i++) {
01532         err = nc_inq_attname(ncid, i, BAD_ATTNUM, name);
01533         IF (err != NC_ENOTATT)
01534             error("Bad attribute number: status = %d", err);
01535         err = nc_inq_attname(ncid, i, NATTS(i), name);
01536         IF (err != NC_ENOTATT)
01537             error("Bad attribute number: status = %d", err);
01538         for (j = 0; j < NATTS(i); j++) {
01539             err = nc_inq_attname(BAD_ID, i, j, name);
01540             IF (err != NC_EBADID)
01541                 error("bad ncid: status = %d", err);
01542             err = nc_inq_attname(ncid, BAD_VARID, j, name);
01543             IF (err != NC_ENOTVAR)
01544                 error("bad var id: status = %d", err);
01545             err = nc_inq_attname(ncid, i, j, name);
01546             IF (err) {
01547                 error("%s", nc_strerror(err));
01548             } else {
01549                 IF (strcmp(ATT_NAME(i,j), name) != 0)
01550                     error("name not that expected");
01551             }
01552         }
01553     }
01554 
01555     err = nc_close(ncid);
01556     IF (err)
01557         error("nc_close: %s", nc_strerror(err));
01558 }
01559 
01560 
01561 void
01562 test_nc_inq_attid(void)
01563 {
01564     int ncid;
01565     int i;
01566     int j;
01567     int err;
01568     int attnum;
01569 
01570     err = nc_open(testfile, NC_NOWRITE, &ncid);
01571     IF (err)
01572         error("nc_open: %s", nc_strerror(err));
01573 
01574     for (i = -1; i < NVARS; i++) {
01575         err = nc_inq_attid(ncid, i, "noSuch", &attnum);
01576         IF (err != NC_ENOTATT)
01577             error("Bad attribute name: status = %d", err);
01578         for (j = 0; j < NATTS(i); j++) {
01579             err = nc_inq_attid(BAD_ID, i, ATT_NAME(i,j), &attnum);
01580             IF (err != NC_EBADID)
01581                 error("bad ncid: status = %d", err);
01582             err = nc_inq_attid(ncid, BAD_VARID, ATT_NAME(i,j), &attnum);
01583             IF (err != NC_ENOTVAR)
01584                 error("bad varid: status = %d", err);
01585             err = nc_inq_attid(ncid, i, ATT_NAME(i,j), &attnum);
01586             IF (err) {
01587                 error("%s", nc_strerror(err));
01588             } else {
01589                 IF (attnum != j)
01590                     error("attnum not that expected");
01591             }
01592         }
01593     }
01594 
01595     err = nc_close(ncid);
01596     IF (err)
01597         error("nc_close: %s", nc_strerror(err));
01598 }
01599 

Generated on Thu Mar 16 18:10:13 2006 for nco by  doxygen 1.4.4