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

[netCDF #RJB-398714]: [netcdfgroup] Obtaining the group name from a dimension ID



Hi Ashish,

Sorry it took us so long to respond to this.
> How would I go about obtaining the group name in which a dimension ID was
> defined in, given the dimension id?
> 
> 
> For example, after a nc_inq_dimids call with include_parents set to
> non-zero:
> 
> int nc_inq_dimids(int ncid, int *ndims, int *dimids, int
> include_parents);
> 
> 
> could I obtain a gid or the group name in which a particular dimid was
> defined in?

I also needed this capability in ncdump, but I implemented it in some
inline code rather than a separate function, see this fragment from
ncdump.c around line 1543:

         /* Subtlety: The following code block is needed because
          * nc_inq_dimname() currently returns only a simple dimension
          * name, without a prefix identifying the group it came from.
          * That's OK unless the dimid identifies a dimension in an
          * ancestor group that has the same simple name as a
          * dimension in the current group (or some intermediate
          * group), in which case the simple name is ambiguous.  This
          * code tests for that case and provides an absolute dimname
          * only in the case where a simple name would be
          * ambiguous. */
         {
             int dimid_test;    /* to see if dimname is ambiguous */
             int locid;         /* group id where dimension is defined */
             NC_CHECK( nc_inq_dimid(ncid, dim_name, &dimid_test) );
             locid = ncid;
             while(var.dims[id] != dimid_test) { /* not in locid, try ancestors 
*/
                 int parent_id;
                 NC_CHECK( nc_inq_grp_parent(locid, &parent_id) );
                 locid = parent_id;
                 NC_CHECK( nc_inq_dimid(locid, dim_name, &dimid_test) );
             }
             /* dimid is in group locid, prefix dimname with group name if 
needed */
             if(locid != ncid) {
                 size_t len;
                 char *locname; /* the group name */
                 NC_CHECK( nc_inq_grpname_full(locid, &len, NULL) );
                 locname = emalloc(len + 1);
                 NC_CHECK( nc_inq_grpname_full(locid, &len, locname) );
                 print_name (locname);
                 if(strcmp("/", locname) != 0) { /* not the root group */
                     printf("/");                /* ensure a trailing slash */
                 }
                 free(locname);
             }
         }

Since you expressed a need for this functionality also, I'm proposing
that we add it as a new API in netCDF version 4.2.  You can follow 
progress on this here, if you're interested:
  
  https://www.unidata.ucar.edu/jira/browse/NCF-90

--Russ

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



Ticket Details
===================
Ticket ID: RJB-398714
Department: Support netCDF
Priority: Normal
Status: Closed