netcdf-c/libsrc4/nc4dim.c

181 lines
4.3 KiB
C
Raw Normal View History

/* Copyright 2003-2018, University Corporation for Atmospheric
* Research. See the COPYRIGHT file for copying and redistribution
* conditions. */
2017-12-04 22:07:45 +08:00
/**
2018-01-31 00:35:56 +08:00
* @file
2017-12-04 22:07:45 +08:00
* @internal This file is part of netcdf-4, a netCDF-like interface
* for HDF5, or a HDF5 backend for netCDF, depending on your point of
* view.
*
* This file handles the nc4 dimension functions.
*
* @author Ed Hartnett
2018-01-31 00:35:56 +08:00
*/
2010-06-03 21:24:43 +08:00
#include "nc4internal.h"
#include "nc4dispatch.h"
2010-06-03 21:24:43 +08:00
2017-12-04 22:07:45 +08:00
/**
* @internal Netcdf-4 files might have more than one unlimited
* dimension, but return the first one anyway.
2018-01-31 00:35:56 +08:00
*
* @note that this code is inconsistent with nc_inq
2017-12-04 22:07:45 +08:00
*
* @param ncid File and group ID.
2017-12-05 03:21:14 +08:00
* @param unlimdimidp Pointer that gets ID of first unlimited
2017-12-04 22:07:45 +08:00
* dimension, or -1.
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @author Ed Hartnett
*/
2018-01-31 00:35:56 +08:00
int
2010-06-03 21:24:43 +08:00
NC4_inq_unlimdim(int ncid, int *unlimdimidp)
{
NC *nc;
2010-06-03 21:24:43 +08:00
NC_GRP_INFO_T *grp, *g;
NC_HDF5_FILE_INFO_T *h5;
NC_DIM_INFO_T *dim;
int found = 0;
int retval;
int i;
2018-01-31 00:35:56 +08:00
LOG((2, "%s: called", __func__));
2010-06-03 21:24:43 +08:00
if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
return retval;
2018-01-31 00:35:56 +08:00
assert(h5 && nc && grp);
2010-06-03 21:24:43 +08:00
2018-01-31 00:35:56 +08:00
if (unlimdimidp)
2010-06-03 21:24:43 +08:00
{
2018-01-31 00:35:56 +08:00
/* According to netcdf-3 manual, return -1 if there is no unlimited
dimension. */
*unlimdimidp = -1;
for (g = grp; g && !found; g = g->parent)
2010-06-03 21:24:43 +08:00
{
2018-05-15 20:47:52 +08:00
for(i=0;i<ncindexsize(grp->dim);i++)
2018-01-31 00:35:56 +08:00
{
2018-05-15 20:47:52 +08:00
dim = (NC_DIM_INFO_T*)ncindexith(grp->dim,i);
if(dim == NULL) continue;
2018-01-31 00:35:56 +08:00
if (dim->unlimited)
{
*unlimdimidp = dim->hdr.id;
2018-01-31 00:35:56 +08:00
found++;
break;
}
}
2010-06-03 21:24:43 +08:00
}
}
return NC_NOERR;
}
2017-12-04 22:07:45 +08:00
/**
* @internal Given dim name, find its id.
*
* @param ncid File and group ID.
* @param name Name of the dimension to find.
* @param idp Pointer that gets dimension ID.
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @return ::NC_EBADDIM Dimension not found.
2018-01-31 00:35:56 +08:00
* @return ::NC_EINVAL Invalid input. Name must be provided.
2017-12-04 22:07:45 +08:00
* @author Ed Hartnett
*/
2010-06-03 21:24:43 +08:00
int
NC4_inq_dimid(int ncid, const char *name, int *idp)
{
NC *nc;
2010-06-03 21:24:43 +08:00
NC_GRP_INFO_T *grp, *g;
NC_HDF5_FILE_INFO_T *h5;
NC_DIM_INFO_T *dim;
char norm_name[NC_MAX_NAME + 1];
int retval;
int found;
2018-01-31 00:35:56 +08:00
LOG((2, "%s: ncid 0x%x name %s", __func__, ncid, name));
2010-06-03 21:24:43 +08:00
2018-01-31 00:35:56 +08:00
/* Check input. */
if (!name)
return NC_EINVAL;
2010-06-03 21:24:43 +08:00
/* Find metadata for this file. */
if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
return retval;
2018-01-31 00:35:56 +08:00
assert(h5 && nc && grp);
2013-01-18 10:25:12 +08:00
2010-06-03 21:24:43 +08:00
/* Normalize name. */
if ((retval = nc4_normalize_name(name, norm_name)))
return retval;
/* check for a name match in this group and its parents */
found = 0;
for (g = grp; g ; g = g->parent) {
2018-05-15 20:47:52 +08:00
dim = (NC_DIM_INFO_T*)ncindexlookup(g->dim,norm_name);
if(dim != NULL) {found = 1; break;}
}
if(!found)
return NC_EBADDIM;
assert(dim != NULL);
if (idp)
2018-05-15 20:47:52 +08:00
*idp = dim->hdr.id;
2010-06-03 21:24:43 +08:00
return NC_NOERR;
}
2017-12-04 22:07:45 +08:00
/**
* @internal Returns an array of unlimited dimension ids.The user can
* get the number of unlimited dimensions by first calling this with
* NULL for the second pointer.
*
* @param ncid File and group ID.
* @param nunlimdimsp Pointer that gets the number of unlimited
* dimensions. Ignored if NULL.
* @param unlimdimidsp Pointer that gets arrray of unlimited dimension
* ID. Ignored if NULL.
*
* @return ::NC_NOERR No error.
* @return ::NC_EBADID Bad ncid.
* @author Ed Hartnett, Dennis Heimbigner
*/
2018-01-31 00:35:56 +08:00
int
NC4_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
2010-06-03 21:24:43 +08:00
{
2018-01-31 00:35:56 +08:00
NC_DIM_INFO_T *dim;
NC_GRP_INFO_T *grp;
NC *nc;
NC_HDF5_FILE_INFO_T *h5;
int num_unlim = 0;
int retval;
int i;
2018-01-31 00:35:56 +08:00
LOG((2, "%s: ncid 0x%x", __func__, ncid));
/* Find info for this file and group, and set pointer to each. */
if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
return retval;
assert(h5 && nc && grp);
2010-06-03 21:24:43 +08:00
/* Get our dim info. */
assert(h5);
2010-06-03 21:24:43 +08:00
{
2018-05-15 20:47:52 +08:00
for(i=0;i<ncindexsize(grp->dim);i++)
2010-06-03 21:24:43 +08:00
{
2018-05-15 20:47:52 +08:00
dim = (NC_DIM_INFO_T*)ncindexith(grp->dim,i);
if(dim == NULL) continue;
2018-01-31 00:35:56 +08:00
if (dim->unlimited)
{
if (unlimdimidsp)
unlimdimidsp[num_unlim] = dim->hdr.id;
2018-01-31 00:35:56 +08:00
num_unlim++;
}
2010-06-03 21:24:43 +08:00
}
}
/* Give the number if the user wants it. */
if (nunlimdimsp)
*nunlimdimsp = num_unlim;
return NC_NOERR;
}