netcdf-c icon indicating copy to clipboard operation
netcdf-c copied to clipboard

Documentation on reading unkown dimensions in netCDF-4 misleading

Open mwiesenberger opened this issue 1 year ago • 1 comments
trafficstars

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

mwiesenberger avatar Feb 29 '24 14:02 mwiesenberger

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

edwardhartnett avatar Mar 02 '24 14:03 edwardhartnett