mirror of
https://github.com/Unidata/netcdf-c.git
synced 2024-12-03 08:01:25 +08:00
ec5b3f9a4f
This is a follow-on to pull request ````https://github.com/Unidata/netcdf-c/pull/1959````, which fixed up type scoping. The primary changes are to _nc\_inq\_dimid()_ and to ncdump. The _nc\_inq\_dimid()_ function is supposed to allow the name to be and FQN, but this apparently never got implemented. So if was modified to support FQNs. The ncdump program is supposed to output fully qualified dimension names in its generated CDL file under certain conditions. Suppose ncdump has a netcdf-4 file F with variable V, and V's parent group is G. For each dimension id D referenced by V, ncdump needs to determine whether to print its name as a simple name or as a fully qualified name (FQN). The algorithm is as follows: 1. Search up the tree of ancestor groups. 2. If one of those ancestor groups contains the dimid, then call it dimgrp. 3. If one of those ancestor groups contains a dim with the same name as the dimid, but with a different dimid, then record that as duplicate=true. 4. If dimgrp is defined and duplicate == false, then we do not need an fqn. 5. If dimgrp is defined and duplicate == true, then we do need an fqn to avoid incorrectly using the duplicate. 6. If dimgrp is undefined, then do a preorder breadth-first search of all the groups looking for the dimid. 7. If found, then use the fqn of the first found such dimension location. 8. If not found, then fail. Test case ncdump/test_scope.sh was modified to test the proper operation of ncdump and _nc\_inq\_dimid()_. Misc. Other Changes: * Fix nc_inq_ncid (NC4_inq_ncid actually) to return root group id if the name argument is NULL. * Modify _ncdump/printfqn_ to print out a dimid FQN; this supports verification that the resulting .nc files were properly created.
202 lines
4.7 KiB
C
202 lines
4.7 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
#if defined(_WIN32) && ! defined(__MINGW32__)
|
|
#include "XGetopt.h"
|
|
#else
|
|
#include <getopt.h>
|
|
#endif
|
|
|
|
#include <netcdf.h>
|
|
|
|
#define CHECK(err) {if(err) report(err,__LINE__);}
|
|
|
|
/* Command line options */
|
|
struct Options {
|
|
int debug;
|
|
enum What {NONE=0, DIM=1, TYPE=2} what;
|
|
char file[4096];
|
|
char var[NC_MAX_NAME+1];
|
|
char object[NC_MAX_NAME+1];
|
|
} options;
|
|
|
|
/**************************************************/
|
|
|
|
|
|
static void
|
|
report(int err, int lineno)
|
|
{
|
|
fprintf(stderr,"Error: %d: %s\n", lineno, nc_strerror(err));
|
|
exit(1);
|
|
}
|
|
|
|
void
|
|
usage(void)
|
|
{
|
|
fprintf(stderr,"usage: printfqn [-D] [-V] -t|-d -v <varname> -f <filename> \n");
|
|
exit(0);
|
|
}
|
|
|
|
int
|
|
get_id_parent(int ncid, int id, int* parentp, enum What what)
|
|
{
|
|
int stat = NC_NOERR;
|
|
int i;
|
|
int nids;
|
|
int ids[4096];
|
|
|
|
/* Does this group have the id we are searching for? */
|
|
if(what == TYPE) {
|
|
if((stat=nc_inq_typeids(ncid,&nids,ids))) goto done;
|
|
} else if(what == DIM) {
|
|
if((stat=nc_inq_dimids(ncid,&nids,ids,0))) goto done;
|
|
} else
|
|
abort();
|
|
assert(nids < 4096);
|
|
|
|
/* Search for this id */
|
|
for(i=0;i<nids;i++) {
|
|
if(ids[i] == id) {
|
|
if(parentp) *parentp = ncid;
|
|
goto done;
|
|
}
|
|
}
|
|
|
|
/* Else Search subgroups. */
|
|
if((stat=nc_inq_grps(ncid,&nids,ids))) goto done;
|
|
assert(nids < 4096);
|
|
|
|
/* Recurse on each subgroup */
|
|
for(i=0;i<nids;i++) {
|
|
switch (stat = get_id_parent(ids[i],id,parentp,what)) {
|
|
case NC_ENOTFOUND: break; /* not found; keep looking */
|
|
case NC_NOERR: goto done; /* found it */
|
|
default: goto done; /* some other error */
|
|
}
|
|
}
|
|
stat = NC_ENOTFOUND; /* Not found */
|
|
|
|
done:
|
|
return stat;
|
|
|
|
}
|
|
|
|
int
|
|
get_variable_info(int ncid, const char* name, int* gidp, int* vidp, int* tidp, int* ndimsp, int* dimids)
|
|
{
|
|
int stat = NC_NOERR;
|
|
int i;
|
|
int nids;
|
|
int ids[4096];
|
|
char varname[NC_MAX_NAME];
|
|
|
|
/* Assume only one occurrence of the variable in dataset */
|
|
|
|
/* Does this group have the variable we are searching for? */
|
|
if((stat=nc_inq_varids(ncid,&nids,ids))) goto done;
|
|
assert(nids < 4096);
|
|
|
|
/* Search for this variable name */
|
|
for(i=0;i<nids;i++) {
|
|
if((stat = nc_inq_varname(ncid,ids[i],varname))) goto done;
|
|
if(strcmp(name,varname)==0) {
|
|
if(gidp) *gidp = ncid;
|
|
if(vidp) *vidp = ids[i];
|
|
if((stat = nc_inq_vartype(ncid,ids[i],tidp))) goto done;
|
|
if((stat = nc_inq_varndims(ncid,ids[i],ndimsp))) goto done;
|
|
if((stat = nc_inq_vardimid(ncid,ids[i],dimids))) goto done;
|
|
goto done;
|
|
}
|
|
}
|
|
|
|
/* Else Search subgroups. */
|
|
if((stat=nc_inq_grps(ncid,&nids,ids))) goto done;
|
|
assert(nids < 4096);
|
|
|
|
/* Recurse on each subgroup */
|
|
for(i=0;i<nids;i++) {
|
|
switch (stat = get_variable_info(ids[i],name,gidp,vidp,tidp,ndimsp,dimids)) {
|
|
case NC_ENOTVAR: break; /* not found; keep looking */
|
|
case NC_NOERR: goto done; /* found it */
|
|
default: goto done; /* some other error */
|
|
}
|
|
}
|
|
stat = NC_ENOTVAR; /* Not found */
|
|
|
|
done:
|
|
return stat;
|
|
}
|
|
|
|
int
|
|
main(int argc, char** argv)
|
|
{
|
|
int ncid, varid, gid, tid;
|
|
size_t fqnlen, namelen;
|
|
char fqn[4096];
|
|
char name[NC_MAX_NAME];
|
|
int ndims;
|
|
int dimids[NC_MAX_VAR_DIMS];
|
|
int c;
|
|
|
|
memset((void*)&options,0,sizeof(options));
|
|
|
|
while ((c = getopt(argc, argv, "DVdtv:f:")) != EOF) {
|
|
switch(c) {
|
|
case 'D':
|
|
options.debug = 1;
|
|
break;
|
|
case 'V':
|
|
usage();
|
|
break;
|
|
case 'd':
|
|
options.what = DIM;
|
|
break;
|
|
case 't':
|
|
options.what = TYPE;
|
|
break;
|
|
case 'v':
|
|
strcpy(options.var,optarg);
|
|
break;
|
|
case 'f':
|
|
strcpy(options.file,optarg);
|
|
break;
|
|
case ':':
|
|
fprintf(stderr,"option has no argument: %c\n",c);
|
|
usage();
|
|
case '?':
|
|
fprintf(stderr,"unknown option\n");
|
|
usage();
|
|
}
|
|
}
|
|
|
|
CHECK(nc_open(options.file,NC_NETCDF4,&ncid));
|
|
|
|
/* Locate the parent group for the variable */
|
|
CHECK(get_variable_info(ncid,options.var,&gid,&varid,&tid,&ndims,dimids));
|
|
|
|
if(options.what == TYPE) {
|
|
/* Get the simple type name from the variable */
|
|
CHECK(nc_inq_type(ncid,tid,name,&namelen));
|
|
/* Get the containing group id for the type (might not be same as ncid) */
|
|
CHECK(get_id_parent(ncid,tid,&gid,TYPE));
|
|
} else if(options.what == DIM) {
|
|
/* get name of dimids[0] from the variable */
|
|
CHECK(nc_inq_dimname(ncid,dimids[0],name));
|
|
/* Get the containing group id for the type */
|
|
CHECK(get_id_parent(ncid,dimids[0],&gid,DIM));
|
|
}
|
|
|
|
/* Get the FQN name for the containing group of the id */
|
|
CHECK(nc_inq_grpname_full(gid,&fqnlen,fqn));
|
|
assert(fqnlen < sizeof(fqn));
|
|
|
|
if(strcmp(fqn,"/")==0) fqn[0] = '\0';
|
|
|
|
/* report result with no newline */
|
|
printf("%s/%s",fqn,name);
|
|
|
|
return 0;
|
|
}
|