[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 970828: Help -- record variables



>To: address@hidden
>From: address@hidden (Chuck Denham)
>Subject: Help -- record variables
>Organization: U.S. Geological Survey
>Keywords: 199708282101.PAA20899

Hi chuck,

>         I am having trouble with the following simple situation, brought to
> my attention by one of my NetCDF/Matlab Toolbox users.
>         Imagine a new netcdf file defined with just one dimension (size =
> 0) and just one variable, which happens to use that "record" dimension.  I
> am unable reliably to "put/get" the variable, except with "ncvarput1/get1".
>  The"ncvarput/get" and "ncvarputg/getg" operations fail.
>         If any variable is multi-dimensional, or if there is more than one
> variable that uses the record-dimension, then everything works fine.
>         The testing is being done through my Matlab interface, which may be
> the culprit.
>         Have you ever seen this situation?

No, I haven't, but I just tried to reproduce the problem using the
netCDF 3.3.1 library on my Sun/Solaris platform, and was unable to get
it to fail.

Here's what I tried:

 1.  I created the following CDL file named "cd.cdl":
------------
netcdf cd {
dimensions:
        size = UNLIMITED ;
variables:
        int x(size) ;
data:

 x = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;
}
------------

 2.  I then generated the corresponding netCDF file "cd.nc" by invoking

     $ ncgen -b cd.cdl

 3.  Invoking ncdump on this showed it worked OK.  However, the above
     uses netcdf-3 interfaces (e.g. nc_put_var_int()) rather than
     netcdf-2 interfaces like ncvarput(), so I then created the C code
     necessary to create the file (using "ncgen -c nc.cdl > cd.c"), and
     edited the resulting C file to replace nc_put_var_int with
     ncvarput, as well as changing the types of the start and count
     arguments from the netCDF-3 sizet arrays to the long arrays that
     netCDF-2 requires.  I've appended the resulting C program, if you
     want to compile and run it against your library.

 4.  I then compiled and linked this program and ran it to recreate
     cd.nc, the result of which was as expected.

Please let me know if you get different results, or can come up with a
small example that demonstrates the bug.  I haven't tried the netCDF-2
ncgen to generate all netCDF-2 interface calls to link against the
netCDF-3 library, but I'd be surprised if that didn't work as well.

--Russ

------ cd.c, generated by ncgen and manually edited to use ncvarput ---
#include <stdio.h>
#include <stdlib.h>
#include <netcdf.h>

void
check_err(const int stat, const int line, const char *file) {
    if (stat != NC_NOERR) {
           (void) fprintf(stderr, "line %d of %s: %s\n", line, file, 
nc_strerror(stat));
        exit(1);
    }
}

int
main() {                        /* create cd.nc */

   int  ncid;                   /* netCDF id */

   /* dimension ids */
   int size_dim;

   /* dimension lengths */
   size_t size_len = NC_UNLIMITED;

   /* variable ids */
   int x_id;

   /* rank (number of dimensions) for each variable */
#  define RANK_x 1

   /* variable shapes */
   int x_dims[RANK_x];

   /* enter define mode */
   int stat = nc_create("cd.nc", NC_CLOBBER, &ncid);
   check_err(stat,__LINE__,__FILE__);

   /* define dimensions */
   stat = nc_def_dim(ncid, "size", size_len, &size_dim);
   check_err(stat,__LINE__,__FILE__);

   /* define variables */

   x_dims[0] = size_dim;
   stat = nc_def_var(ncid, "x", NC_INT, RANK_x, x_dims, &x_id);
   check_err(stat,__LINE__,__FILE__);

   /* leave define mode */
   stat = nc_enddef (ncid);
   check_err(stat,__LINE__,__FILE__);

   {                    /* store x */
/*    static sizet x_start[RANK_x]; */
/*    static sizet x_count[RANK_x]; */
    static long x_start[RANK_x];
    static long x_count[RANK_x];
    static int x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    size_len = 11;                      /* number of records of x data */
    x_start[0] = 0;
    x_count[0] = size_len;
/*    stat = nc_put_vara_int(ncid, x_id, x_start, x_count, x); */
    ncvarput(ncid, x_id, x_start, x_count, x);
   }
   stat = nc_close(ncid);
   check_err(stat,__LINE__,__FILE__);
   return 0;
}