NetCDF  4.9.2
nc4dim.c
Go to the documentation of this file.
1 /* Copyright 2003-2018, University Corporation for Atmospheric
2  * Research. See the COPYRIGHT file for copying and redistribution
3  * conditions. */
15 #include "nc4internal.h"
16 #include "nc4dispatch.h"
17 
32 int
33 NC4_inq_unlimdim(int ncid, int *unlimdimidp)
34 {
35  NC_GRP_INFO_T *grp, *g;
36  NC_FILE_INFO_T *h5;
37  NC_DIM_INFO_T *dim;
38  int found = 0;
39  int retval;
40  int i;
41 
42  LOG((2, "%s: called", __func__));
43 
44  if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
45  return retval;
46  assert(h5 && grp);
47 
48  if (unlimdimidp)
49  {
50  /* According to netcdf-3 manual, return -1 if there is no unlimited
51  dimension. */
52  *unlimdimidp = -1;
53  for (g = grp; g && !found; g = g->parent)
54  {
55  for(i=0;i<ncindexsize(grp->dim);i++)
56  {
57  dim = (NC_DIM_INFO_T*)ncindexith(grp->dim,i);
58  if(dim == NULL) continue;
59  if (dim->unlimited)
60  {
61  *unlimdimidp = dim->hdr.id;
62  found++;
63  break;
64  }
65  }
66  }
67  }
68 
69  return NC_NOERR;
70 }
71 
85 int
86 NC4_inq_dimid(int ncid, const char *name, int *idp)
87 {
88  NC *nc = NULL;
89  NC_GRP_INFO_T *grp = NULL;
90  NC_GRP_INFO_T *g = NULL;
91  NC_FILE_INFO_T *h5 = NULL;
92  NC_DIM_INFO_T *dim = NULL;
93  char norm_name[NC_MAX_NAME + 1];
94  int retval = NC_NOERR;;
95  int found = 0;
96 
97  LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name));
98 
99  /* Check input. */
100  if (!name)
101  {retval = NC_EINVAL; goto done;}
102 
103  /* If the first char is a /, this is a fully-qualified
104  * name. Otherwise, this had better be a local name (i.e. no / in
105  * the middle). */
106  if (name[0] != '/' && strstr(name, "/"))
107  {retval = NC_EINVAL; goto done;}
108 
109  /* Find metadata for this file. */
110  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
111  goto done;
112  assert(h5 && nc && grp);
113 
114  /* Normalize name. */
115  if ((retval = nc4_normalize_name(name, norm_name)))
116  goto done;;
117 
118  /* If this is a fqn, then walk the sequence of parent groups to the last group
119  and see if that group has a dimension of the right name */
120  if(name[0] == '/') { /* FQN */
121  int rootncid = (grp->nc4_info->root_grp->hdr.id | grp->nc4_info->controller->ext_ncid);
122  int parent = 0;
123  char* lastname = strrchr(norm_name,'/'); /* break off the last segment: the type name */
124  if(lastname == norm_name)
125  {retval = NC_EINVAL; goto done;}
126  *lastname++ = '\0'; /* break off the lastsegment */
127  if((retval = NC4_inq_grp_full_ncid(rootncid,norm_name,&parent)))
128  goto done;
129  /* Get parent info */
130  if((retval=nc4_find_nc4_grp(parent,&grp)))
131  goto done;
132  /* See if dim exists in this group */
133  dim = (NC_DIM_INFO_T*)ncindexlookup(grp->dim,lastname);
134  if(dim == NULL)
135  {retval = NC_EBADTYPE; goto done;}
136  goto done;
137  }
138 
139  /* check for a name match in this group and its parents */
140  found = 0;
141  for (g = grp; g ; g = g->parent) {
142  dim = (NC_DIM_INFO_T*)ncindexlookup(g->dim,norm_name);
143  if(dim != NULL) {found = 1; break;}
144  }
145  if(!found)
146  {retval = NC_EBADDIM; goto done;}
147 
148 done:
149  if(retval == NC_NOERR) {
150  assert(dim != NULL);
151  if (idp)
152  *idp = dim->hdr.id;
153  }
154  return retval;
155 }
156 
172 int
173 NC4_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
174 {
175  NC_DIM_INFO_T *dim;
176  NC_GRP_INFO_T *grp;
177  NC *nc;
178  NC_FILE_INFO_T *h5;
179  int num_unlim = 0;
180  int retval;
181  int i;
182 
183  LOG((2, "%s: ncid 0x%x", __func__, ncid));
184 
185  /* Find info for this file and group, and set pointer to each. */
186  if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
187  return retval;
188  assert(h5 && nc && grp);
189 
190  /* Get our dim info. */
191  assert(h5);
192  {
193  for(i=0;i<ncindexsize(grp->dim);i++)
194  {
195  dim = (NC_DIM_INFO_T*)ncindexith(grp->dim,i);
196  if(dim == NULL) continue;
197  if (dim->unlimited)
198  {
199  if (unlimdimidsp)
200  unlimdimidsp[num_unlim] = dim->hdr.id;
201  num_unlim++;
202  }
203  }
204  }
205 
206  /* Give the number if the user wants it. */
207  if (nunlimdimsp)
208  *nunlimdimsp = num_unlim;
209 
210  return NC_NOERR;
211 }
#define NC_EBADTYPE
Not a netcdf data type.
Definition: netcdf.h:410
#define NC_EINVAL
Invalid Argument.
Definition: netcdf.h:378
#define NC_MAX_NAME
Maximum for classic library.
Definition: netcdf.h:281
#define NC_NOERR
No Error.
Definition: netcdf.h:368
#define NC_EBADDIM
Invalid dimension id or name.
Definition: netcdf.h:411