/src/netcdf-c/libsrc4/nc4dim.c
| Line | Count | Source | 
| 1 |  | /* Copyright 2003-2018, University Corporation for Atmospheric | 
| 2 |  |  * Research. See the COPYRIGHT file for copying and redistribution | 
| 3 |  |  * conditions. */ | 
| 4 |  | /** | 
| 5 |  |  * @file | 
| 6 |  |  * @internal This file is part of netcdf-4, a netCDF-like interface | 
| 7 |  |  * for HDF5, or a HDF5 backend for netCDF, depending on your point of | 
| 8 |  |  * view. | 
| 9 |  |  * | 
| 10 |  |  * This file handles the nc4 dimension functions. | 
| 11 |  |  * | 
| 12 |  |  * @author Ed Hartnett | 
| 13 |  |  */ | 
| 14 |  |  | 
| 15 |  | #include "nc4internal.h" | 
| 16 |  | #include "nc4dispatch.h" | 
| 17 |  |  | 
| 18 |  | /** | 
| 19 |  |  * @internal Netcdf-4 files might have more than one unlimited | 
| 20 |  |  * dimension, but return the first one anyway. | 
| 21 |  |  * | 
| 22 |  |  * @note that this code is inconsistent with nc_inq | 
| 23 |  |  * | 
| 24 |  |  * @param ncid File and group ID. | 
| 25 |  |  * @param unlimdimidp Pointer that gets ID of first unlimited | 
| 26 |  |  * dimension, or -1. | 
| 27 |  |  * | 
| 28 |  |  * @return ::NC_NOERR No error. | 
| 29 |  |  * @return ::NC_EBADID Bad ncid. | 
| 30 |  |  * @author Ed Hartnett | 
| 31 |  |  */ | 
| 32 |  | int | 
| 33 |  | NC4_inq_unlimdim(int ncid, int *unlimdimidp) | 
| 34 | 0 | { | 
| 35 | 0 |     NC_GRP_INFO_T *grp, *g; | 
| 36 | 0 |     NC_FILE_INFO_T *h5; | 
| 37 | 0 |     NC_DIM_INFO_T *dim; | 
| 38 | 0 |     int found = 0; | 
| 39 | 0 |     int retval; | 
| 40 |  | 
 | 
| 41 | 0 |     LOG((2, "%s: called", __func__)); | 
| 42 |  | 
 | 
| 43 | 0 |     if ((retval = nc4_find_grp_h5(ncid, &grp, &h5))) | 
| 44 | 0 |         return retval; | 
| 45 | 0 |     assert(h5 && grp); | 
| 46 |  | 
 | 
| 47 | 0 |     if (unlimdimidp) | 
| 48 | 0 |     { | 
| 49 |  |         /* According to netcdf-3 manual, return -1 if there is no unlimited | 
| 50 |  |            dimension. */ | 
| 51 | 0 |         *unlimdimidp = -1; | 
| 52 | 0 |         for (g = grp; g && !found; g = g->parent) | 
| 53 | 0 |         { | 
| 54 | 0 |             for(size_t i=0;i<ncindexsize(grp->dim);i++) | 
| 55 | 0 |             { | 
| 56 | 0 |                 dim = (NC_DIM_INFO_T*)ncindexith(grp->dim,i); | 
| 57 | 0 |                 if(dim == NULL) continue; | 
| 58 | 0 |                 if (dim->unlimited) | 
| 59 | 0 |                 { | 
| 60 | 0 |                     *unlimdimidp = dim->hdr.id; | 
| 61 | 0 |                     found++; | 
| 62 | 0 |                     break; | 
| 63 | 0 |                 } | 
| 64 | 0 |             } | 
| 65 | 0 |         } | 
| 66 | 0 |     } | 
| 67 |  | 
 | 
| 68 | 0 |     return NC_NOERR; | 
| 69 | 0 | } | 
| 70 |  |  | 
| 71 |  | /** | 
| 72 |  |  * @internal Given dim name, find its id. | 
| 73 |  |  * Fully qualified names are legal | 
| 74 |  |  * @param ncid File and group ID. | 
| 75 |  |  * @param name Name of the dimension to find. | 
| 76 |  |  * @param idp Pointer that gets dimension ID. | 
| 77 |  |  * | 
| 78 |  |  * @return ::NC_NOERR No error. | 
| 79 |  |  * @return ::NC_EBADID Bad ncid. | 
| 80 |  |  * @return ::NC_EBADDIM Dimension not found. | 
| 81 |  |  * @return ::NC_EINVAL Invalid input. Name must be provided. | 
| 82 |  |  * @author Ed Hartnett | 
| 83 |  |  */ | 
| 84 |  | int | 
| 85 |  | NC4_inq_dimid(int ncid, const char *name, int *idp) | 
| 86 | 0 | { | 
| 87 | 0 |     NC *nc = NULL; | 
| 88 | 0 |     NC_GRP_INFO_T *grp = NULL; | 
| 89 | 0 |     NC_GRP_INFO_T *g = NULL; | 
| 90 | 0 |     NC_FILE_INFO_T *h5 = NULL; | 
| 91 | 0 |     NC_DIM_INFO_T *dim = NULL; | 
| 92 | 0 |     char norm_name[NC_MAX_NAME + 1]; | 
| 93 | 0 |     int retval = NC_NOERR;; | 
| 94 | 0 |     int found = 0; | 
| 95 |  | 
 | 
| 96 | 0 |     LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name)); | 
| 97 |  |  | 
| 98 |  |     /* Check input. */ | 
| 99 | 0 |     if (!name) | 
| 100 | 0 |         {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 | 0 |     if (name[0] != '/' && strstr(name, "/")) | 
| 106 | 0 |         {retval = NC_EINVAL; goto done;} | 
| 107 |  |  | 
| 108 |  |     /* Find metadata for this file. */ | 
| 109 | 0 |     if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5))) | 
| 110 | 0 |         goto done; | 
| 111 | 0 |     assert(h5 && nc && grp); | 
| 112 |  |  | 
| 113 |  |     /* Normalize name. */ | 
| 114 | 0 |     if ((retval = nc4_normalize_name(name, norm_name))) | 
| 115 | 0 |         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 | 0 |     if(name[0] == '/') { /* FQN */ | 
| 120 | 0 |   int rootncid = (grp->nc4_info->root_grp->hdr.id | grp->nc4_info->controller->ext_ncid); | 
| 121 | 0 |   int parent = 0; | 
| 122 | 0 |   char* lastname = strrchr(norm_name,'/'); /* break off the last segment: the type name */ | 
| 123 | 0 |   if(lastname == norm_name) | 
| 124 | 0 |       {retval = NC_EINVAL; goto done;} | 
| 125 | 0 |   *lastname++ = '\0'; /* break off the lastsegment */ | 
| 126 | 0 |   if((retval = NC4_inq_grp_full_ncid(rootncid,norm_name,&parent)))  | 
| 127 | 0 |       goto done; | 
| 128 |  |   /* Get parent info */ | 
| 129 | 0 |   if((retval=nc4_find_nc4_grp(parent,&grp))) | 
| 130 | 0 |       goto done; | 
| 131 |  |   /* See if dim exists in this group */ | 
| 132 | 0 |         dim = (NC_DIM_INFO_T*)ncindexlookup(grp->dim,lastname); | 
| 133 | 0 |   if(dim == NULL)    | 
| 134 | 0 |       {retval = NC_EBADTYPE; goto done;} | 
| 135 | 0 |   goto done; | 
| 136 | 0 |     } | 
| 137 |  |  | 
| 138 |  |     /* check for a name match in this group and its parents */ | 
| 139 | 0 |     found = 0; | 
| 140 | 0 |     for (g = grp; g ; g = g->parent) { | 
| 141 | 0 |         dim = (NC_DIM_INFO_T*)ncindexlookup(g->dim,norm_name); | 
| 142 | 0 |         if(dim != NULL) {found = 1; break;} | 
| 143 | 0 |     } | 
| 144 | 0 |     if(!found) | 
| 145 | 0 |         {retval = NC_EBADDIM; goto done;} | 
| 146 |  |  | 
| 147 | 0 | done: | 
| 148 | 0 |      if(retval == NC_NOERR) { | 
| 149 | 0 |          assert(dim != NULL); | 
| 150 | 0 |          if (idp) | 
| 151 | 0 |             *idp = dim->hdr.id; | 
| 152 | 0 |     } | 
| 153 | 0 |     return retval; | 
| 154 | 0 | } | 
| 155 |  |  | 
| 156 |  | /** | 
| 157 |  |  * @internal Returns an array of unlimited dimension ids.The user can | 
| 158 |  |  * get the number of unlimited dimensions by first calling this with | 
| 159 |  |  * NULL for the second pointer. | 
| 160 |  |  * | 
| 161 |  |  * @param ncid File and group ID. | 
| 162 |  |  * @param nunlimdimsp Pointer that gets the number of unlimited | 
| 163 |  |  * dimensions. Ignored if NULL. | 
| 164 |  |  * @param unlimdimidsp Pointer that gets arrray of unlimited dimension | 
| 165 |  |  * ID. Ignored if NULL. | 
| 166 |  |  * | 
| 167 |  |  * @return ::NC_NOERR No error. | 
| 168 |  |  * @return ::NC_EBADID Bad ncid. | 
| 169 |  |  * @author Ed Hartnett, Dennis Heimbigner | 
| 170 |  |  */ | 
| 171 |  | int | 
| 172 |  | NC4_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp) | 
| 173 | 0 | { | 
| 174 | 0 |     NC_DIM_INFO_T *dim; | 
| 175 | 0 |     NC_GRP_INFO_T *grp; | 
| 176 | 0 |     NC *nc; | 
| 177 | 0 |     NC_FILE_INFO_T *h5; | 
| 178 | 0 |     int num_unlim = 0; | 
| 179 | 0 |     int retval; | 
| 180 |  | 
 | 
| 181 | 0 |     LOG((2, "%s: ncid 0x%x", __func__, ncid)); | 
| 182 |  |  | 
| 183 |  |     /* Find info for this file and group, and set pointer to each. */ | 
| 184 | 0 |     if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5))) | 
| 185 | 0 |         return retval; | 
| 186 | 0 |     assert(h5 && nc && grp); | 
| 187 |  |  | 
| 188 |  |     /* Get our dim info. */ | 
| 189 | 0 |     assert(h5); | 
| 190 | 0 |     { | 
| 191 | 0 |         for(size_t i=0;i<ncindexsize(grp->dim);i++) | 
| 192 | 0 |         { | 
| 193 | 0 |             dim = (NC_DIM_INFO_T*)ncindexith(grp->dim,i); | 
| 194 | 0 |             if(dim == NULL) continue; | 
| 195 | 0 |             if (dim->unlimited) | 
| 196 | 0 |             { | 
| 197 | 0 |                 if (unlimdimidsp) | 
| 198 | 0 |                     unlimdimidsp[num_unlim] = dim->hdr.id; | 
| 199 | 0 |                 num_unlim++; | 
| 200 | 0 |             } | 
| 201 | 0 |         } | 
| 202 | 0 |     } | 
| 203 |  |  | 
| 204 |  |     /* Give the number if the user wants it. */ | 
| 205 | 0 |     if (nunlimdimsp) | 
| 206 | 0 |         *nunlimdimsp = num_unlim; | 
| 207 |  | 
 | 
| 208 | 0 |     return NC_NOERR; | 
| 209 | 0 | } |