Unidata / netcdf-c

Official GitHub repository for netCDF-C libraries and utilities.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation on reading unkown dimensions in netCDF-4 misleading

mwiesenberger opened this issue · comments

The documentation
https://docs.unidata.ucar.edu/netcdf-c/current/reading_unknown.html
recommends using nc_inq to get the number of dimension and then using nc_inq_dim to get the name and length of each of the dimensions. However code like this

int ndims;
nc_inq( ncid, &ndims, 0,0,0);
for( int i=0; i<ndims; i++)
{
        char dimname[256];
        size_t len;
        err = nc_inq_dim( ncid, i, dimname, &len);
}

can have unexpected behaviour in NetCDF-4 if dimensions are defined in groups, e.g.

    int ncid;
    int err;
    err = nc_create( "test.nc", NC_NETCDF4|NC_CLOBBER, &ncid);
    int dimID;
    int grpid;
    err = nc_def_dim( ncid, "x", 10, &dimID);
    err = nc_def_grp(ncid,"group",&grpid);
    err = nc_def_dim( grpid, "gdim", 10, &dimID);
    err = nc_def_dim( ncid, "y", 10, &dimID);

Because the dimIDs will not be sequential, the first code thinks "x" and "gdim" are the dimensions in ncid instead of "x" and "y".

The code in ncdump.c shows how to do it correctly using nc_inq_dimids.
I think the documentation at the above place should show and explain how to do it correctly as well

I agree. Can you submit a PR with the change?