2018-01-31 23:44:33 +08:00
|
|
|
/* Copyright 2003-2018, University Corporation for Atmospheric
|
|
|
|
* Research. See COPYRIGHT file for copying and redistribution
|
|
|
|
* conditions. */
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
2018-06-06 04:40:49 +08:00
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @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.
|
2017-12-04 22:07:45 +08:00
|
|
|
*
|
|
|
|
* This file handles the nc4 attribute functions.
|
|
|
|
*
|
|
|
|
* Remember that with atts, type conversion can take place when
|
|
|
|
* writing them, and when reading them.
|
|
|
|
*
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2018-05-15 20:47:52 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#include "nc.h"
|
2018-03-17 01:46:18 +08:00
|
|
|
#include "nc4internal.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
#include "nc4dispatch.h"
|
2010-08-05 10:44:59 +08:00
|
|
|
#include "ncdispatch.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2018-11-26 23:21:32 +08:00
|
|
|
/**
|
|
|
|
* @internal Get or put attribute metadata from our linked list of
|
|
|
|
* file info. Always locate the attribute by name, never by attnum.
|
|
|
|
* The mem_type is ignored if data=NULL.
|
|
|
|
*
|
2022-11-16 11:29:21 +08:00
|
|
|
* @param h5 File object
|
|
|
|
* @param grp Group object
|
|
|
|
* @param var Variable object
|
2018-12-01 04:28:18 +08:00
|
|
|
* @param name Name of attribute. Must already be normalized.
|
2018-11-26 23:21:32 +08:00
|
|
|
* @param xtype Pointer that gets (file) type of attribute. Ignored if
|
|
|
|
* NULL.
|
|
|
|
* @param mem_type The type of attribute data in memory.
|
|
|
|
* @param lenp Pointer that gets length of attribute array. Ignored if
|
|
|
|
* NULL.
|
|
|
|
* @param attnum Pointer that gets the index number of this
|
|
|
|
* attribute. Ignored if NULL.
|
|
|
|
* @param data Pointer that gets attribute data. Ignored if NULL.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
nc4_get_att_ptrs(NC_FILE_INFO_T *h5, NC_GRP_INFO_T *grp, NC_VAR_INFO_T *var,
|
|
|
|
const char *name, nc_type *xtype, nc_type mem_type,
|
|
|
|
size_t *lenp, int *attnum, void *data)
|
|
|
|
{
|
2019-02-19 20:55:22 +08:00
|
|
|
NC_ATT_INFO_T *att = NULL;
|
|
|
|
int my_attnum = -1;
|
|
|
|
int need_to_convert = 0;
|
|
|
|
int range_error = NC_NOERR;
|
|
|
|
void *bufr = NULL;
|
|
|
|
size_t type_size;
|
|
|
|
int varid;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
LOG((3, "%s: mem_type %d", __func__, mem_type));
|
|
|
|
|
|
|
|
/* Get the varid, or NC_GLOBAL. */
|
|
|
|
varid = var ? var->hdr.id : NC_GLOBAL;
|
|
|
|
|
|
|
|
if (attnum)
|
|
|
|
my_attnum = *attnum;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
BAIL(NC_EBADNAME);
|
|
|
|
|
|
|
|
/* Find the attribute, if it exists. */
|
|
|
|
if ((retval = nc4_find_grp_att(grp, varid, name, my_attnum, &att)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* If mem_type is NC_NAT, it means we want to use the attribute's
|
|
|
|
* file type as the mem type as well. */
|
|
|
|
if (mem_type == NC_NAT)
|
|
|
|
mem_type = att->nc_typeid;
|
|
|
|
|
|
|
|
/* If the attribute is NC_CHAR, and the mem_type isn't, or vice
|
|
|
|
* versa, that's a freakish attempt to convert text to
|
|
|
|
* numbers. Some pervert out there is trying to pull a fast one!
|
|
|
|
* Send him an NC_ECHAR error. */
|
|
|
|
if (data && att->len)
|
|
|
|
if ((att->nc_typeid == NC_CHAR && mem_type != NC_CHAR) ||
|
|
|
|
(att->nc_typeid != NC_CHAR && mem_type == NC_CHAR))
|
|
|
|
BAIL(NC_ECHAR); /* take that, you freak! */
|
|
|
|
|
|
|
|
/* Copy the info. */
|
|
|
|
if (lenp)
|
|
|
|
*lenp = att->len;
|
|
|
|
if (xtype)
|
|
|
|
*xtype = att->nc_typeid;
|
|
|
|
if (attnum) {
|
|
|
|
*attnum = att->hdr.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Zero len attributes are easy to read! */
|
|
|
|
if (!att->len)
|
|
|
|
BAIL(NC_NOERR);
|
|
|
|
|
|
|
|
/* Later on, we will need to know the size of this type. */
|
|
|
|
if ((retval = nc4_get_typelen_mem(h5, mem_type, &type_size)))
|
|
|
|
BAIL(retval);
|
|
|
|
|
|
|
|
/* We may have to convert data. Treat NC_CHAR the same as
|
|
|
|
* NC_UBYTE. If the mem_type is NAT, don't try any conversion - use
|
|
|
|
* the attribute's type. */
|
|
|
|
if (data && att->len && mem_type != att->nc_typeid &&
|
|
|
|
mem_type != NC_NAT &&
|
|
|
|
!(mem_type == NC_CHAR &&
|
|
|
|
(att->nc_typeid == NC_UBYTE || att->nc_typeid == NC_BYTE)))
|
|
|
|
{
|
2023-11-25 02:20:52 +08:00
|
|
|
if (!(bufr = malloc((size_t)(att->len) * type_size)))
|
2019-02-19 20:55:22 +08:00
|
|
|
BAIL(NC_ENOMEM);
|
|
|
|
need_to_convert++;
|
|
|
|
if ((retval = nc4_convert_type(att->data, bufr, att->nc_typeid,
|
|
|
|
mem_type, (size_t)att->len, &range_error,
|
2021-08-25 15:31:26 +08:00
|
|
|
NULL, (h5->cmode & NC_CLASSIC_MODEL),
|
|
|
|
NC_NOQUANTIZE, 0)))
|
2018-11-26 23:21:32 +08:00
|
|
|
BAIL(retval);
|
|
|
|
|
2019-02-19 20:55:22 +08:00
|
|
|
/* For strict netcdf-3 rules, ignore erange errors between UBYTE
|
|
|
|
* and BYTE types. */
|
|
|
|
if ((h5->cmode & NC_CLASSIC_MODEL) &&
|
|
|
|
(att->nc_typeid == NC_UBYTE || att->nc_typeid == NC_BYTE) &&
|
|
|
|
(mem_type == NC_UBYTE || mem_type == NC_BYTE) &&
|
|
|
|
range_error)
|
|
|
|
range_error = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bufr = att->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the caller wants data, copy it for him. If he hasn't
|
|
|
|
allocated enough memory for it, he will burn in segmentation
|
|
|
|
fault hell, writhing with the agony of undiscovered memory
|
|
|
|
bugs! */
|
|
|
|
if (data)
|
|
|
|
{
|
Fix various problem around VLEN's
re: https://github.com/Unidata/netcdf-c/issues/541
re: https://github.com/Unidata/netcdf-c/issues/1208
re: https://github.com/Unidata/netcdf-c/issues/2078
re: https://github.com/Unidata/netcdf-c/issues/2041
re: https://github.com/Unidata/netcdf-c/issues/2143
For a long time, there have been known problems with the
management of complex types containing VLENs. This also
involves the string type because it is stored as a VLEN of
chars.
This PR (mostly) fixes this problem. But note that it adds new
functions to netcdf.h (see below) and this may require bumping
the .so number. These new functions can be removed, if desired,
in favor of functions in netcdf_aux.h, but netcdf.h seems the
better place for them because they are intended as alternatives
to the nc_free_vlen and nc_free_string functions already in
netcdf.h.
The term complex type refers to any type that directly or
transitively references a VLEN type. So an array of VLENS, a
compound with a VLEN field, and so on.
In order to properly handle instances of these complex types, it
is necessary to have function that can recursively walk
instances of such types to perform various actions on them. The
term "deep" is also used to mean recursive.
At the moment, the two operations needed by the netcdf library are:
* free'ing an instance of the complex type
* copying an instance of the complex type.
The current library does only shallow free and shallow copy of
complex types. This means that only the top level is properly
free'd or copied, but deep internal blocks in the instance are
not touched.
Note that the term "vector" will be used to mean a contiguous (in
memory) sequence of instances of some type. Given an array with,
say, dimensions 2 X 3 X 4, this will be stored in memory as a
vector of length 2*3*4=24 instances.
The use cases are primarily these.
## nc_get_vars
Suppose one is reading a vector of instances using nc_get_vars
(or nc_get_vara or nc_get_var, etc.). These functions will
return the vector in the top-level memory provided. All
interior blocks (form nested VLEN or strings) will have been
dynamically allocated.
After using this vector of instances, it is necessary to free
(aka reclaim) the dynamically allocated memory, otherwise a
memory leak occurs. So, the recursive reclaim function is used
to walk the returned instance vector and do a deep reclaim of
the data.
Currently functions are defined in netcdf.h that are supposed to
handle this: nc_free_vlen(), nc_free_vlens(), and
nc_free_string(). Unfortunately, these functions only do a
shallow free, so deeply nested instances are not properly
handled by them.
Note that internally, the provided data is immediately written so
there is no need to copy it. But the caller may need to reclaim the
data it passed into the function.
## nc_put_att
Suppose one is writing a vector of instances as the data of an attribute
using, say, nc_put_att.
Internally, the incoming attribute data must be copied and stored
so that changes/reclamation of the input data will not affect
the attribute.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. As a result, one sees effects such as described
in Github Issue https://github.com/Unidata/netcdf-c/issues/2143.
Also, after defining the attribute, it may be necessary for the user
to free the data that was provided as input to nc_put_att().
## nc_get_att
Suppose one is reading a vector of instances as the data of an attribute
using, say, nc_get_att.
Internally, the existing attribute data must be copied and returned
to the caller, and the caller is responsible for reclaiming
the returned data.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. So this can lead to memory leaks and errors
because the deep data is shared between the library and the user.
# Solution
The solution is to build properly recursive reclaim and copy
functions and use those as needed.
These recursive functions are defined in libdispatch/dinstance.c
and their signatures are defined in include/netcdf.h.
For back compatibility, corresponding "ncaux_XXX" functions
are defined in include/netcdf_aux.h.
````
int nc_reclaim_data(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_reclaim_data_all(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_copy_data(int ncid, nc_type xtypeid, const void* memory, size_t count, void* copy);
int nc_copy_data_all(int ncid, nc_type xtypeid, const void* memory, size_t count, void** copyp);
````
There are two variants. The first two, nc_reclaim_data() and
nc_copy_data(), assume the top-level vector is managed by the
caller. For reclaim, this is so the user can use, for example, a
statically allocated vector. For copy, it assumes the user
provides the space into which the copy is stored.
The second two, nc_reclaim_data_all() and
nc_copy_data_all(), allows the functions to manage the
top-level. So for nc_reclaim_data_all, the top level is
assumed to be dynamically allocated and will be free'd by
nc_reclaim_data_all(). The nc_copy_data_all() function
will allocate the top level and return a pointer to it to the
user. The user can later pass that pointer to
nc_reclaim_data_all() to reclaim the instance(s).
# Internal Changes
The netcdf-c library internals are changed to use the proper
reclaim and copy functions. It turns out that the places where
these functions are needed is quite pervasive in the netcdf-c
library code. Using these functions also allows some
simplification of the code since the stdata and vldata fields of
NC_ATT_INFO are no longer needed. Currently this is commented
out using the SEPDATA \#define macro. When any bugs are largely
fixed, all this code will be removed.
# Known Bugs
1. There is still one known failure that has not been solved.
All the failures revolve around some variant of this .cdl file.
The proximate cause of failure is the use of a VLEN FillValue.
````
netcdf x {
types:
float(*) row_of_floats ;
dimensions:
m = 5 ;
variables:
row_of_floats ragged_array(m) ;
row_of_floats ragged_array:_FillValue = {-999} ;
data:
ragged_array = {10, 11, 12, 13, 14}, {20, 21, 22, 23}, {30, 31, 32},
{40, 41}, _ ;
}
````
When a solution is found, I will either add it to this PR or post a new PR.
# Related Changes
* Mark nc_free_vlen(s) as deprecated in favor of ncaux_reclaim_data.
* Remove the --enable-unfixed-memory-leaks option.
* Remove the NC_VLENS_NOTEST code that suppresses some vlen tests.
* Document this change in docs/internal.md
* Disable the tst_vlen_data test in ncdump/tst_nccopy4.sh.
* Mark types as fixed size or not (transitively) to optimize the reclaim
and copy functions.
# Misc. Changes
* Make Doxygen process libdispatch/daux.c
* Make sure the NC_ATT_INFO_T.container field is set.
2022-01-09 09:30:00 +08:00
|
|
|
{
|
Improve performance of the nc_reclaim_data and nc_copy_data functions.
re: Issue https://github.com/Unidata/netcdf-c/issues/2685
re: PR https://github.com/Unidata/netcdf-c/pull/2179
As noted in PR https://github.com/Unidata/netcdf-c/pull/2179,
the old code did not allow for reclaiming instances of types,
nor for properly copying them. That PR provided new functions
capable of reclaiming/copying instances of arbitrary types.
However, as noted by Issue https://github.com/Unidata/netcdf-c/issues/2685, using these
most general functions resulted in a significant performance
degradation, even for common cases.
This PR attempts to mitigate the cost of using the general
reclaim/copy functions in two ways.
First, the previous functions operating at the top level by
using ncid and typeid arguments. These functions were augmented
with equivalent versions that used the netcdf-c library internal
data structures to allow direct access to needed information.
These new functions are used internally to the library.
The second mitigation involves optimizing the internal functions
by providing early tests for common cases. This avoids
unnecessary recursive function calls.
The overall result is a significant improvement in speed by a
factor of roughly twenty -- your mileage may vary. These
optimized functions are still not as fast as the original (more
limited) functions, but they are getting close. Additional optimizations are
possible. But the cost is a significant "uglification" of the
code that I deemed a step too far, at least for now.
## Misc. Changes
1. Added a test case to check the proper reclamation/copy of complex types.
2. Found and fixed some places where nc_reclaim/copy should have been used.
3. Replaced, in the netcdf-c library, (almost all) occurrences of nc_reclaim_copy with calls to NC_reclaim/copy. This plus the optimizations is the primary speed-up mechanism.
4. In DAP4, the metadata is held in a substrate in-memory file; this required some changes so that the reclaim/copy code accessed that substrate dispatcher rather than the DAP4 dispatcher.
5. Re-factored and isolated the code that computes if a type is (transitively) variable-sized or not.
6. Clean up the reclamation code in ncgen; adding the use of nc_reclaim exposed some memory problems.
2023-05-21 07:11:25 +08:00
|
|
|
if((retval = NC_copy_data(h5->controller,mem_type,bufr,att->len,data)))
|
Fix various problem around VLEN's
re: https://github.com/Unidata/netcdf-c/issues/541
re: https://github.com/Unidata/netcdf-c/issues/1208
re: https://github.com/Unidata/netcdf-c/issues/2078
re: https://github.com/Unidata/netcdf-c/issues/2041
re: https://github.com/Unidata/netcdf-c/issues/2143
For a long time, there have been known problems with the
management of complex types containing VLENs. This also
involves the string type because it is stored as a VLEN of
chars.
This PR (mostly) fixes this problem. But note that it adds new
functions to netcdf.h (see below) and this may require bumping
the .so number. These new functions can be removed, if desired,
in favor of functions in netcdf_aux.h, but netcdf.h seems the
better place for them because they are intended as alternatives
to the nc_free_vlen and nc_free_string functions already in
netcdf.h.
The term complex type refers to any type that directly or
transitively references a VLEN type. So an array of VLENS, a
compound with a VLEN field, and so on.
In order to properly handle instances of these complex types, it
is necessary to have function that can recursively walk
instances of such types to perform various actions on them. The
term "deep" is also used to mean recursive.
At the moment, the two operations needed by the netcdf library are:
* free'ing an instance of the complex type
* copying an instance of the complex type.
The current library does only shallow free and shallow copy of
complex types. This means that only the top level is properly
free'd or copied, but deep internal blocks in the instance are
not touched.
Note that the term "vector" will be used to mean a contiguous (in
memory) sequence of instances of some type. Given an array with,
say, dimensions 2 X 3 X 4, this will be stored in memory as a
vector of length 2*3*4=24 instances.
The use cases are primarily these.
## nc_get_vars
Suppose one is reading a vector of instances using nc_get_vars
(or nc_get_vara or nc_get_var, etc.). These functions will
return the vector in the top-level memory provided. All
interior blocks (form nested VLEN or strings) will have been
dynamically allocated.
After using this vector of instances, it is necessary to free
(aka reclaim) the dynamically allocated memory, otherwise a
memory leak occurs. So, the recursive reclaim function is used
to walk the returned instance vector and do a deep reclaim of
the data.
Currently functions are defined in netcdf.h that are supposed to
handle this: nc_free_vlen(), nc_free_vlens(), and
nc_free_string(). Unfortunately, these functions only do a
shallow free, so deeply nested instances are not properly
handled by them.
Note that internally, the provided data is immediately written so
there is no need to copy it. But the caller may need to reclaim the
data it passed into the function.
## nc_put_att
Suppose one is writing a vector of instances as the data of an attribute
using, say, nc_put_att.
Internally, the incoming attribute data must be copied and stored
so that changes/reclamation of the input data will not affect
the attribute.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. As a result, one sees effects such as described
in Github Issue https://github.com/Unidata/netcdf-c/issues/2143.
Also, after defining the attribute, it may be necessary for the user
to free the data that was provided as input to nc_put_att().
## nc_get_att
Suppose one is reading a vector of instances as the data of an attribute
using, say, nc_get_att.
Internally, the existing attribute data must be copied and returned
to the caller, and the caller is responsible for reclaiming
the returned data.
Again, the code inside the netcdf library does only shallow copying
rather than deep copy. So this can lead to memory leaks and errors
because the deep data is shared between the library and the user.
# Solution
The solution is to build properly recursive reclaim and copy
functions and use those as needed.
These recursive functions are defined in libdispatch/dinstance.c
and their signatures are defined in include/netcdf.h.
For back compatibility, corresponding "ncaux_XXX" functions
are defined in include/netcdf_aux.h.
````
int nc_reclaim_data(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_reclaim_data_all(int ncid, nc_type xtypeid, void* memory, size_t count);
int nc_copy_data(int ncid, nc_type xtypeid, const void* memory, size_t count, void* copy);
int nc_copy_data_all(int ncid, nc_type xtypeid, const void* memory, size_t count, void** copyp);
````
There are two variants. The first two, nc_reclaim_data() and
nc_copy_data(), assume the top-level vector is managed by the
caller. For reclaim, this is so the user can use, for example, a
statically allocated vector. For copy, it assumes the user
provides the space into which the copy is stored.
The second two, nc_reclaim_data_all() and
nc_copy_data_all(), allows the functions to manage the
top-level. So for nc_reclaim_data_all, the top level is
assumed to be dynamically allocated and will be free'd by
nc_reclaim_data_all(). The nc_copy_data_all() function
will allocate the top level and return a pointer to it to the
user. The user can later pass that pointer to
nc_reclaim_data_all() to reclaim the instance(s).
# Internal Changes
The netcdf-c library internals are changed to use the proper
reclaim and copy functions. It turns out that the places where
these functions are needed is quite pervasive in the netcdf-c
library code. Using these functions also allows some
simplification of the code since the stdata and vldata fields of
NC_ATT_INFO are no longer needed. Currently this is commented
out using the SEPDATA \#define macro. When any bugs are largely
fixed, all this code will be removed.
# Known Bugs
1. There is still one known failure that has not been solved.
All the failures revolve around some variant of this .cdl file.
The proximate cause of failure is the use of a VLEN FillValue.
````
netcdf x {
types:
float(*) row_of_floats ;
dimensions:
m = 5 ;
variables:
row_of_floats ragged_array(m) ;
row_of_floats ragged_array:_FillValue = {-999} ;
data:
ragged_array = {10, 11, 12, 13, 14}, {20, 21, 22, 23}, {30, 31, 32},
{40, 41}, _ ;
}
````
When a solution is found, I will either add it to this PR or post a new PR.
# Related Changes
* Mark nc_free_vlen(s) as deprecated in favor of ncaux_reclaim_data.
* Remove the --enable-unfixed-memory-leaks option.
* Remove the NC_VLENS_NOTEST code that suppresses some vlen tests.
* Document this change in docs/internal.md
* Disable the tst_vlen_data test in ncdump/tst_nccopy4.sh.
* Mark types as fixed size or not (transitively) to optimize the reclaim
and copy functions.
# Misc. Changes
* Make Doxygen process libdispatch/daux.c
* Make sure the NC_ATT_INFO_T.container field is set.
2022-01-09 09:30:00 +08:00
|
|
|
BAIL(retval);
|
|
|
|
}
|
2019-02-19 20:55:22 +08:00
|
|
|
}
|
2018-11-26 23:21:32 +08:00
|
|
|
|
|
|
|
exit:
|
2019-02-19 20:55:22 +08:00
|
|
|
if (need_to_convert)
|
|
|
|
free(bufr);
|
|
|
|
if (range_error)
|
|
|
|
retval = NC_ERANGE;
|
|
|
|
return retval;
|
2018-11-26 23:21:32 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
|
|
|
* @internal Get or put attribute metadata from our linked list of
|
|
|
|
* file info. Always locate the attribute by name, never by attnum.
|
|
|
|
* The mem_type is ignored if data=NULL.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
2018-11-26 23:24:18 +08:00
|
|
|
* @param xtype Pointer that gets (file) type of attribute. Ignored if
|
|
|
|
* NULL.
|
2017-12-04 22:07:45 +08:00
|
|
|
* @param mem_type The type of attribute data in memory.
|
2018-11-26 23:24:18 +08:00
|
|
|
* @param lenp Pointer that gets length of attribute array. Ignored if
|
|
|
|
* NULL.
|
|
|
|
* @param attnum Pointer that gets the index number of this
|
|
|
|
* attribute. Ignored if NULL.
|
|
|
|
* @param data Pointer that gets attribute data. Ignored if NULL.
|
2017-12-04 22:07:45 +08:00
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2018-11-26 23:24:18 +08:00
|
|
|
int
|
2018-01-31 23:44:33 +08:00
|
|
|
nc4_get_att(int ncid, int varid, const char *name, nc_type *xtype,
|
|
|
|
nc_type mem_type, size_t *lenp, int *attnum, void *data)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2019-02-19 20:55:22 +08:00
|
|
|
NC_FILE_INFO_T *h5;
|
|
|
|
NC_GRP_INFO_T *grp;
|
|
|
|
NC_VAR_INFO_T *var = NULL;
|
|
|
|
char norm_name[NC_MAX_NAME + 1];
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
LOG((3, "%s: ncid 0x%x varid %d mem_type %d", __func__, ncid,
|
|
|
|
varid, mem_type));
|
|
|
|
|
|
|
|
/* Find info for this file, group, and h5 info. */
|
|
|
|
if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
|
|
|
|
return retval;
|
|
|
|
assert(h5 && grp);
|
|
|
|
|
|
|
|
/* Check varid */
|
|
|
|
if (varid != NC_GLOBAL)
|
|
|
|
{
|
|
|
|
if (!(var = (NC_VAR_INFO_T*)ncindexith(grp->vars,varid)))
|
|
|
|
return NC_ENOTVAR;
|
|
|
|
assert(var->hdr.id == varid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Name is required. */
|
|
|
|
if (!name)
|
|
|
|
return NC_EBADNAME;
|
|
|
|
|
|
|
|
/* Normalize name. */
|
|
|
|
if ((retval = nc4_normalize_name(name, norm_name)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
return nc4_get_att_ptrs(h5, grp, var, norm_name, xtype, mem_type, lenp,
|
|
|
|
attnum, data);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
2018-01-31 23:44:33 +08:00
|
|
|
* @internal Learn about an att. All the nc4 nc_inq_ functions just
|
|
|
|
* call nc4_get_att to get the metadata on an attribute.
|
2017-12-04 22:07:45 +08:00
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
2018-01-31 23:44:33 +08:00
|
|
|
* @param xtypep Pointer that gets type of attribute.
|
|
|
|
* @param lenp Pointer that gets length of attribute data array.
|
2017-12-04 22:07:45 +08:00
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2018-01-31 23:44:33 +08:00
|
|
|
int
|
|
|
|
NC4_inq_att(int ncid, int varid, const char *name, nc_type *xtypep,
|
|
|
|
size_t *lenp)
|
|
|
|
{
|
2019-02-19 20:55:22 +08:00
|
|
|
LOG((2, "%s: ncid 0x%x varid %d name %s", __func__, ncid, varid, name));
|
|
|
|
return nc4_get_att(ncid, varid, name, xtypep, NC_NAT, lenp, NULL, NULL);
|
2018-01-31 23:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Learn an attnum, given a name.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param attnump Pointer that gets the attribute index number.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_inq_attid(int ncid, int varid, const char *name, int *attnump)
|
|
|
|
{
|
2019-02-19 20:55:22 +08:00
|
|
|
LOG((2, "%s: ncid 0x%x varid %d name %s", __func__, ncid, varid, name));
|
|
|
|
return nc4_get_att(ncid, varid, name, NULL, NC_NAT, NULL, attnump, NULL);
|
2018-01-31 23:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal Given an attnum, find the att's name.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param attnum The index number of the attribute.
|
2019-09-18 10:27:43 +08:00
|
|
|
* @param name Pointer that gets name of attribute.
|
2018-01-31 23:44:33 +08:00
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
NC4_inq_attname(int ncid, int varid, int attnum, char *name)
|
|
|
|
{
|
2019-02-19 20:55:22 +08:00
|
|
|
NC_ATT_INFO_T *att;
|
|
|
|
int retval;
|
2018-01-31 23:44:33 +08:00
|
|
|
|
2019-02-19 20:55:22 +08:00
|
|
|
LOG((2, "nc_inq_attname: ncid 0x%x varid %d attnum %d", ncid, varid,
|
|
|
|
attnum));
|
2018-01-31 23:44:33 +08:00
|
|
|
|
2019-02-19 20:55:22 +08:00
|
|
|
/* Find the attribute metadata. */
|
|
|
|
if ((retval = nc4_find_nc_att(ncid, varid, NULL, attnum, &att)))
|
|
|
|
return retval;
|
2018-01-31 23:44:33 +08:00
|
|
|
|
2019-02-19 20:55:22 +08:00
|
|
|
/* Get the name. */
|
|
|
|
if (name)
|
|
|
|
strcpy(name, att->hdr.name);
|
2018-01-31 23:44:33 +08:00
|
|
|
|
2019-02-19 20:55:22 +08:00
|
|
|
return NC_NOERR;
|
2018-01-31 23:44:33 +08:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:07:45 +08:00
|
|
|
/**
|
|
|
|
* @internal Get an attribute.
|
|
|
|
*
|
|
|
|
* @param ncid File and group ID.
|
|
|
|
* @param varid Variable ID.
|
|
|
|
* @param name Name of attribute.
|
|
|
|
* @param value Pointer that gets attribute data.
|
|
|
|
* @param memtype The type the data should be converted to as it is read.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @return ::NC_EBADID Bad ncid.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
int
|
|
|
|
NC4_get_att(int ncid, int varid, const char *name, void *value, nc_type memtype)
|
|
|
|
{
|
2019-02-19 20:55:22 +08:00
|
|
|
return nc4_get_att(ncid, varid, name, NULL, memtype, NULL, NULL, value);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|