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