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

[netCDF #NMW-119005]: different ordering of dim ids -- is this expected?



Hi Ellen,

> Thank you again for the fix for my previous question about the unlimited
> dimension error!
> 
> I have another question regarding different behavior between 4.1.3 and
> 4.3.3.1, and am wondering if it is expected behavior.
> 
> Did the ordering of returned dimension ids change between these two
> versions?

Yes, but that shouldn't matter to applications that just use the
public API to access the metadata and data in a file.

The ncdump program only uses the public API, and it produces identical
output for the dimtest.nc file independent from whether it was written
using the old netcdf-4.1.3 library or the new 4.3.3.1 library, and
independent from whether the 4.1.3 version of ncdump is used or the
4.3.3.1 version.

The IDs for netCDF objects should be treated as opaque handles. There
are certain guarantees from the documentation, such as the fact that
when there are N netCDF dimensions in a file, all the IDs are in the
range 0 to N-1 for the C API (or 1 to N for the Fortran API). But if 
you want to get at the same dimension between two applications linked
against different versions of the library, you should be using the
dimension name to get a dimension ID rather than the other way around.

Maybe the documentation doesn't make this clear enough. I think the 
reason for changing the order in which dimension IDs are returned had
to do with fixing a bug that occurred when dimensions were declared in 
one order and the associated coordinate variables were declared in a
different order, but the underlying HDF5 library combines a dimension
and its coordinate variable into a single object, a "dimension scale",
which meant that the original different orders were not both preserved
in the HDF5 dimension scale, and not stored in the HDF5 file.

I may post a more concise version of this answer to the netcdfgroup 
mailing list early next week, so please let me know if you find it
wrong or incoherent!

--Russ

> I wrote another standalone C program to test this outside of our MATLAB
> regression tests.
> 
> In the new NetCDF version 4.3.3.1, this program outputs the group 2
> dimension ids as 0, 1, 2, whereas in our current NetCDF version 4.1.3 this
> outputs the dimension is as 0, 1, 4.
> 
> Hence the group 2 dimension names in 4.3.3.1 display as rt_x1, g2_t1,
> and g2_x1, whereas in 4.1.3 they display as g2_t1, g2_x1, rt_x1.
> 
> 
> Below is the standalone C program.  Again built this with HDF5 1.8.12.
> 
> Thank you!
> 
> Ellen Johnson
> 
> Software Engineer
> Image and Scientific File Formats
> MathWorks
> 
> 
> 
> #include <stdio.h>
> #include <string.h>
> #include <netcdf.h>
> 
> /* This is the name of the data file we will create. */
> 
> #define FILE_NAME "dimtest.nc"
> #define NDIMS 5
> 
> /* Handle errors by printing an error message */
> 
> #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
> 
> int
> main()
> 
> {
> 
> int i;
> int retval;
> int ncid, gid1, gid2, gid3, gid4, rt_x1_dimid, g2_t1_dimid, g2_x1_dimid, 
> g3_v1_dimid, g4_x1_dimid;
> int dimids[NDIMS];
> int ndims_in;
> int dimids_in[5];
> int grpid_in;
> char name_in[100];
> size_t len_in;
> 
> /* create the file */
> 
> if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
> ERR(retval);
> 
> /* at root group, create one regular dimension */
> 
> if ((retval = nc_def_dim(ncid, "rt_x1", 10, &rt_x1_dimid)))
> ERR(retval);
> 
> /* create group 1 */
> if ((retval = nc_def_grp(ncid, "grp1", &gid1)))
> ERR(retval);
> 
> /* create group 2 */
> if ((retval = nc_def_grp(gid1, "grp2", &gid2)))
> ERR (retval);
> 
> /* in group 2 create one unlimited and one regular dimension */
> if ((retval = nc_def_dim(gid2, "g2_t1", NC_UNLIMITED, &g2_t1_dimid)))
> ERR(retval);
> 
> if ((retval = nc_def_dim(gid2, "g2_x1", 10, &g2_x1_dimid)))
> ERR(retval);
> 
> /* create group 3 */
> if ((retval = nc_def_grp(gid1, "grp3", &gid3)))
> ERR (retval);
> 
> /* in group 3 create one regular dimension */
> if ((retval = nc_def_dim(gid3, "group3_x1", 5, &g3_v1_dimid)))
> ERR(retval);
> 
> /* create group 4 */
> if ((retval = nc_def_grp(ncid, "grp4", &gid4)))
> ERR (retval);
> 
> /* in group 4 create one regular dimension */
> if ((retval = nc_def_dim(gid4, "group4_x1", 5, &g4_x1_dimid)))
> ERR(retval);
> 
> /* end define mode */
> if ((retval = nc_enddef(ncid)))
> ERR(retval);
> 
> /* close the file */
> if ((retval = nc_close(ncid)))
> ERR(retval);
> 
> printf("\n\n*** SUCCESS writing example file %s!\n\n", FILE_NAME);
> 
> /* reopen file */
> if ((nc_open(FILE_NAME, NC_NOWRITE, &ncid)))
> ERR(retval);
> 
> /* find group 2 by name */
> if ((retval = nc_inq_ncid(gid1, "grp2", &grpid_in)))
> ERR(retval);
> 
> printf("grpid2 = %d\n", grpid_in);
> 
> /* now, get number of dims for group 2, including parent */
> if ((retval = nc_inq_dimids(gid2, &ndims_in, dimids_in, 1)))
> ERR(retval);
> 
> printf("numdims for group 2 = %d\n", ndims_in);
> 
> /* print the dim ids for group 2, including parent */
> for (i=0; i<ndims_in; i++)
> {
> printf("group 2 dim id %d === %d\n", i, dimids_in[i]);
> }
> 
> /* get the names for group 2 */
> if ((retval = nc_inq_dim(grpid_in, dimids_in[0], name_in, &len_in)))
> ERR(retval);
> 
> printf("name for dimid 0 = %s\n", name_in);
> 
> if ((retval = nc_inq_dim(grpid_in, dimids_in[1], name_in, &len_in)))
> ERR(retval);
> 
> printf("name for dimid 1 = %s\n", name_in);
> 
> if ((retval = nc_inq_dim(grpid_in, dimids_in[2], name_in, &len_in)))
> ERR(retval);
> 
> printf("name for dimid 2 = %s\n", name_in);
> 
> printf("\n\n");
> 
> /* close the file */
> if ((retval = nc_close(ncid)))
> ERR(retval);
> 
> printf("End of test.\n\n");
> 
> return 0;
> 
> }
> 
> _______________________________________________
> netcdfgroup mailing list
> address@hidden
> For list information or to unsubscribe,  visit:
> http://www.unidata.ucar.edu/mailing_lists/
> 

Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: NMW-119005
Department: Support netCDF
Priority: Normal
Status: Closed