2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @file
|
2018-12-07 05:29:57 +08:00
|
|
|
* Copyright 2018 University Corporation for Atmospheric
|
2017-12-05 03:21:14 +08:00
|
|
|
* Research/Unidata. See COPYRIGHT file for more info.
|
|
|
|
*
|
|
|
|
* This file has the var and att copy functions.
|
|
|
|
*
|
|
|
|
* @author Dennis Heimbigner
|
2010-06-03 21:24:43 +08:00
|
|
|
*/
|
This PR adds EXPERIMENTAL support for accessing data in the
cloud using a variant of the Zarr protocol and storage
format. This enhancement is generically referred to as "NCZarr".
The data model supported by NCZarr is netcdf-4 minus the user-defined
types and the String type. In this sense it is similar to the CDF-5
data model.
More detailed information about enabling and using NCZarr is
described in the document NUG/nczarr.md and in a
[Unidata Developer's blog entry](https://www.unidata.ucar.edu/blogs/developer/en/entry/overview-of-zarr-support-in).
WARNING: this code has had limited testing, so do use this version
for production work. Also, performance improvements are ongoing.
Note especially the following platform matrix of successful tests:
Platform | Build System | S3 support
------------------------------------
Linux+gcc | Automake | yes
Linux+gcc | CMake | yes
Visual Studio | CMake | no
Additionally, and as a consequence of the addition of NCZarr,
major changes have been made to the Filter API. NOTE: NCZarr
does not yet support filters, but these changes are enablers for
that support in the future. Note that it is possible
(probable?) that there will be some accidental reversions if the
changes here did not correctly mimic the existing filter testing.
In any case, previously filter ids and parameters were of type
unsigned int. In order to support the more general zarr filter
model, this was all converted to char*. The old HDF5-specific,
unsigned int operations are still supported but they are
wrappers around the new, char* based nc_filterx_XXX functions.
This entailed at least the following changes:
1. Added the files libdispatch/dfilterx.c and include/ncfilter.h
2. Some filterx utilities have been moved to libdispatch/daux.c
3. A new entry, "filter_actions" was added to the NCDispatch table
and the version bumped.
4. An overly complex set of structs was created to support funnelling
all of the filterx operations thru a single dispatch
"filter_actions" entry.
5. Move common code to from libhdf5 to libsrc4 so that it is accessible
to nczarr.
Changes directly related to Zarr:
1. Modified CMakeList.txt and configure.ac to support both C and C++
-- this is in support of S3 support via the awd-sdk libraries.
2. Define a size64_t type to support nczarr.
3. More reworking of libdispatch/dinfermodel.c to
support zarr and to regularize the structure of the fragments
section of a URL.
Changes not directly related to Zarr:
1. Make client-side filter registration be conditional, with default off.
2. Hack include/nc4internal.h to make some flags added by Ed be unique:
e.g. NC_CREAT, NC_INDEF, etc.
3. cleanup include/nchttp.h and libdispatch/dhttp.c.
4. Misc. changes to support compiling under Visual Studio including:
* Better testing under windows for dirent.h and opendir and closedir.
5. Misc. changes to the oc2 code to support various libcurl CURLOPT flags
and to centralize error reporting.
6. By default, suppress the vlen tests that have unfixed memory leaks; add option to enable them.
7. Make part of the nc_test/test_byterange.sh test be contingent on remotetest.unidata.ucar.edu being accessible.
Changes Left TO-DO:
1. fix provenance code, it is too HDF5 specific.
2020-06-29 08:02:47 +08:00
|
|
|
#include "config.h"
|
2010-08-05 10:44:59 +08:00
|
|
|
#include "ncdispatch.h"
|
2015-08-16 06:26:35 +08:00
|
|
|
#include "nc_logging.h"
|
2021-03-07 05:09:37 +08:00
|
|
|
#include "nclist.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2024-05-16 08:46:25 +08:00
|
|
|
static int NC_find_equal_type(int ncid1, nc_type xtype1, int ncid2, nc_type *xtype2);
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#ifdef USE_NETCDF4
|
2021-03-07 05:09:37 +08:00
|
|
|
|
|
|
|
static int searchgroup(int ncid1, int tid1, int grp, int* tid2);
|
|
|
|
static int searchgrouptree(int ncid1, int tid1, int grp, int* tid2);
|
|
|
|
|
2024-05-16 08:46:25 +08:00
|
|
|
#endif /*USE_NETCDF4*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
2017-12-05 04:11:54 +08:00
|
|
|
/**
|
|
|
|
* @internal Compare two netcdf types for equality. Must have the
|
2018-12-07 05:29:57 +08:00
|
|
|
* ncids as well, to find user-defined types.
|
2017-12-05 04:11:54 +08:00
|
|
|
*
|
|
|
|
* @param ncid1 File ID.
|
|
|
|
* @param typeid1 Type ID.
|
|
|
|
* @param ncid2 File ID.
|
|
|
|
* @param typeid2 Type ID.
|
|
|
|
* @param equalp Pointer that gets 1 of the types are equal, 0
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
static int
|
2021-03-07 05:09:37 +08:00
|
|
|
NC_compare_nc_types(int ncid1, int typeid1, int ncid2, int typeid2, int *equalp)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
2010-10-09 02:54:53 +08:00
|
|
|
|
|
|
|
/* If you don't care about the answer, neither do I! */
|
2015-07-11 05:10:32 +08:00
|
|
|
if(equalp == NULL)
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Assume the types are not equal. If we find any inequality, then
|
|
|
|
exit with NC_NOERR and we're done. */
|
|
|
|
*equalp = 0;
|
|
|
|
|
|
|
|
/* Atomic types are so easy! */
|
2015-07-11 05:10:32 +08:00
|
|
|
if (typeid1 <= NC_MAX_ATOMIC_TYPE)
|
2010-10-09 02:54:53 +08:00
|
|
|
{
|
2015-07-11 05:10:32 +08:00
|
|
|
if (typeid2 != typeid1)
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
*equalp = 1;
|
|
|
|
}
|
2015-07-11 05:10:32 +08:00
|
|
|
else
|
2010-10-09 02:54:53 +08:00
|
|
|
{
|
2010-06-03 21:24:43 +08:00
|
|
|
int i, ret, equal1;
|
|
|
|
char name1[NC_MAX_NAME];
|
|
|
|
char name2[NC_MAX_NAME];
|
|
|
|
size_t size1, size2;
|
|
|
|
nc_type base1, base2;
|
|
|
|
size_t nelems1, nelems2;
|
|
|
|
int class1, class2;
|
|
|
|
void* value1 = NULL;
|
|
|
|
void* value2 = NULL;
|
|
|
|
size_t offset1, offset2;
|
|
|
|
nc_type ftype1, ftype2;
|
|
|
|
int ndims1, ndims2;
|
|
|
|
int dimsizes1[NC_MAX_VAR_DIMS];
|
|
|
|
int dimsizes2[NC_MAX_VAR_DIMS];
|
|
|
|
|
2010-10-09 02:54:53 +08:00
|
|
|
/* Find out about the two types. */
|
|
|
|
if ((ret = nc_inq_user_type(ncid1, typeid1, name1, &size1,
|
|
|
|
&base1, &nelems1, &class1)))
|
2015-07-11 05:10:32 +08:00
|
|
|
return ret;
|
2010-10-09 02:54:53 +08:00
|
|
|
if ((ret = nc_inq_user_type(ncid2, typeid2, name2, &size2,
|
|
|
|
&base2, &nelems2, &class2)))
|
2015-07-11 05:10:32 +08:00
|
|
|
return ret;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Check the obvious. */
|
2023-12-17 08:06:27 +08:00
|
|
|
if(size1 != size2 || class1 != class2 || strcmp(name1,name2) != 0)
|
2010-06-03 21:24:43 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
|
|
|
|
/* Check user-defined types in detail. */
|
2015-07-11 05:10:32 +08:00
|
|
|
switch(class1)
|
2010-10-09 02:54:53 +08:00
|
|
|
{
|
2010-06-03 21:24:43 +08:00
|
|
|
case NC_VLEN:
|
2010-10-09 02:54:53 +08:00
|
|
|
if((ret = NC_compare_nc_types(ncid1, base1, ncid2,
|
|
|
|
base1, &equal1)))
|
|
|
|
return ret;
|
2015-07-11 05:10:32 +08:00
|
|
|
if(!equal1)
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
break;
|
|
|
|
case NC_OPAQUE:
|
|
|
|
/* Already checked size above. */
|
|
|
|
break;
|
|
|
|
case NC_ENUM:
|
|
|
|
if(base1 != base2 || nelems1 != nelems2) return NC_NOERR;
|
2010-10-09 02:54:53 +08:00
|
|
|
|
|
|
|
if (!(value1 = malloc(size1)))
|
|
|
|
return NC_ENOMEM;
|
2015-07-11 05:10:32 +08:00
|
|
|
if (!(value2 = malloc(size2))) {
|
|
|
|
free(value1);
|
|
|
|
return NC_ENOMEM;
|
|
|
|
}
|
2010-10-09 02:54:53 +08:00
|
|
|
|
2015-07-11 05:10:32 +08:00
|
|
|
for(i = 0; i < nelems1; i++)
|
2010-10-09 02:54:53 +08:00
|
|
|
{
|
|
|
|
if ((ret = nc_inq_enum_member(ncid1, typeid1, i, name1,
|
|
|
|
value1)) ||
|
|
|
|
(ret = nc_inq_enum_member(ncid2, typeid2, i, name2,
|
|
|
|
value2)) ||
|
2023-12-17 08:06:27 +08:00
|
|
|
strcmp(name1, name2) != 0 || memcmp(value1, value2, size1) != 0)
|
2010-10-09 02:54:53 +08:00
|
|
|
{
|
2015-07-11 05:10:32 +08:00
|
|
|
free(value1);
|
2010-10-09 02:54:53 +08:00
|
|
|
free(value2);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2015-07-11 05:10:32 +08:00
|
|
|
free(value1);
|
2010-10-09 02:54:53 +08:00
|
|
|
free(value2);
|
2010-06-03 21:24:43 +08:00
|
|
|
break;
|
|
|
|
case NC_COMPOUND:
|
2015-07-11 05:10:32 +08:00
|
|
|
if(nelems1 != nelems2)
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Compare each field. Each must be equal! */
|
2015-07-11 05:10:32 +08:00
|
|
|
for(i = 0; i < nelems1; i++)
|
2010-10-09 02:54:53 +08:00
|
|
|
{
|
2010-06-03 21:24:43 +08:00
|
|
|
int j;
|
2015-07-11 05:10:32 +08:00
|
|
|
if ((ret = nc_inq_compound_field(ncid1, typeid1, i, name1, &offset1,
|
2010-10-09 02:54:53 +08:00
|
|
|
&ftype1, &ndims1, dimsizes1)))
|
|
|
|
return ret;
|
|
|
|
if ((ret = nc_inq_compound_field(ncid2, typeid2, i, name2, &offset2,
|
|
|
|
&ftype2, &ndims2, dimsizes2)))
|
|
|
|
return ret;
|
2015-07-11 05:10:32 +08:00
|
|
|
if(ndims1 != ndims2)
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_NOERR;
|
2015-07-11 05:10:32 +08:00
|
|
|
for(j = 0; j < ndims1;j++)
|
|
|
|
if(dimsizes1[j] != dimsizes2[j])
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
|
|
|
|
/* Compare user-defined field types. */
|
|
|
|
if((ret = NC_compare_nc_types(ncid1, ftype1, ncid2, ftype2,
|
|
|
|
&equal1)))
|
|
|
|
return ret;
|
2015-07-11 05:10:32 +08:00
|
|
|
if(!equal1)
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_NOERR;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NC_EINVAL;
|
|
|
|
}
|
|
|
|
*equalp = 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-05 04:11:54 +08:00
|
|
|
/**
|
2021-03-07 05:09:37 +08:00
|
|
|
* @internal Recursively hunt for a netCDF type id, tid2, that is "equal" to tid1.
|
|
|
|
* Question is: what search order do we use? Ncgen uses root group tree in pre-order.
|
|
|
|
* But NC4_inq_typeid uses these rules:
|
|
|
|
* 1. ncid2
|
|
|
|
* 2. parents of ncid2 (up the tree to root)
|
|
|
|
* 3. root group tree in pre-order.
|
|
|
|
* We will leave ncgen for another day and use the nc_inq_typeid rule.
|
|
|
|
*
|
|
|
|
* Return matching typeid or 0 if not found.
|
2017-12-05 04:11:54 +08:00
|
|
|
*
|
|
|
|
* @param ncid1 File ID.
|
|
|
|
* @param tid1 Type ID.
|
|
|
|
* @param ncid2 File ID.
|
|
|
|
* @param tid2 Pointer that gets type ID of equal type.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
2021-03-07 05:09:37 +08:00
|
|
|
* @author Ed Hartnett, Dennis Heimbigner
|
2017-12-05 04:11:54 +08:00
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
static int
|
|
|
|
NC_rec_find_nc_type(int ncid1, nc_type tid1, int ncid2, nc_type* tid2)
|
|
|
|
{
|
2021-03-07 05:09:37 +08:00
|
|
|
int ret = NC_NOERR;
|
|
|
|
int parent;
|
|
|
|
|
|
|
|
if((ret = searchgroup(ncid1,tid1,ncid2,tid2)))
|
|
|
|
goto done;
|
|
|
|
if(*tid2 != 0)
|
|
|
|
goto done; /* found */
|
|
|
|
|
|
|
|
/* Look in the parents of ncid2 upto the root */
|
|
|
|
switch (ret = nc_inq_grp_parent(ncid2,&parent)) {
|
|
|
|
case NC_NOERR:
|
|
|
|
/* Recurse up using parent grp */
|
|
|
|
ret = NC_rec_find_nc_type(ncid1, tid1, parent, tid2);
|
|
|
|
break;
|
|
|
|
case NC_ENOGRP:
|
|
|
|
/* do the breadth-first pre-order search of the whole tree */
|
|
|
|
/* ncid2 should be root group */
|
|
|
|
ret = searchgrouptree(ncid1,tid1,ncid2,tid2);
|
|
|
|
break;
|
|
|
|
default: break;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2015-07-11 05:10:32 +08:00
|
|
|
|
2021-03-07 05:09:37 +08:00
|
|
|
done:
|
|
|
|
return ret;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2024-05-16 08:46:25 +08:00
|
|
|
#endif /* USE_NETCDF4 */
|
|
|
|
|
2017-12-05 04:11:54 +08:00
|
|
|
/**
|
|
|
|
* @internal Given a type in one file, find its equal (if any) in
|
2018-12-07 05:29:57 +08:00
|
|
|
* another file. It sounds so simple, but it's a real pain!
|
2017-12-05 04:11:54 +08:00
|
|
|
*
|
|
|
|
* @param ncid1 File ID.
|
|
|
|
* @param xtype1 Type ID.
|
|
|
|
* @param ncid2 File ID.
|
|
|
|
* @param xtype2 Pointer that gets type ID of equal type.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Ed Hartnett
|
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
static int
|
|
|
|
NC_find_equal_type(int ncid1, nc_type xtype1, int ncid2, nc_type *xtype2)
|
|
|
|
{
|
|
|
|
int ret = NC_NOERR;
|
2010-10-09 02:54:53 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Check input */
|
2015-07-11 05:10:32 +08:00
|
|
|
if(xtype1 <= NC_NAT)
|
2010-10-09 02:54:53 +08:00
|
|
|
return NC_EINVAL;
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Handle atomic types. */
|
2015-07-11 05:10:32 +08:00
|
|
|
if (xtype1 <= NC_MAX_ATOMIC_TYPE)
|
2010-10-09 02:54:53 +08:00
|
|
|
{
|
2015-07-11 05:10:32 +08:00
|
|
|
if(xtype2)
|
2010-10-09 02:54:53 +08:00
|
|
|
*xtype2 = xtype1;
|
2010-06-03 21:24:43 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:46:25 +08:00
|
|
|
#ifdef USE_NETCDF4
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Recursively search group ncid2 and its children
|
|
|
|
to find a type that is equal (using compare_type)
|
|
|
|
to xtype1. */
|
|
|
|
ret = NC_rec_find_nc_type(ncid1, xtype1 , ncid2, xtype2);
|
2024-05-16 08:46:25 +08:00
|
|
|
#endif /* USE_NETCDF4 */
|
2010-06-03 21:24:43 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* This will copy a variable that is an array of primitive type and
|
|
|
|
* its attributes from one file to another, assuming dimensions in the
|
|
|
|
* output file are already defined and have same dimension IDs and
|
|
|
|
* length. However it doesn't work for copying netCDF-4 variables of
|
|
|
|
* type string or a user-defined type.
|
|
|
|
*
|
|
|
|
* This function works even if the files are different formats,
|
|
|
|
* (for example, one netcdf classic, the other netcdf-4).
|
|
|
|
*
|
|
|
|
* If you're copying into a classic-model file, from a netcdf-4 file,
|
|
|
|
* you must be copying a variable of one of the six classic-model
|
|
|
|
* types, and similarly for the attributes.
|
|
|
|
*
|
|
|
|
* For large netCDF-3 files, this can be a very inefficient way to
|
|
|
|
* copy data from one file to another, because adding a new variable
|
|
|
|
* to the target file may require more space in the header and thus
|
|
|
|
* result in moving data for other variables in the target file. This
|
|
|
|
* is not a problem for netCDF-4 files, which support efficient
|
|
|
|
* addition of variables without moving data for other variables.
|
|
|
|
*
|
|
|
|
* @param ncid_in File ID to copy from.
|
|
|
|
* @param varid_in Variable ID to copy.
|
|
|
|
* @param ncid_out File ID to copy to.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
2012-12-14 02:00:03 +08:00
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
int
|
|
|
|
nc_copy_var(int ncid_in, int varid_in, int ncid_out)
|
|
|
|
{
|
|
|
|
char name[NC_MAX_NAME + 1];
|
|
|
|
char att_name[NC_MAX_NAME + 1];
|
|
|
|
nc_type xtype;
|
2013-01-24 05:46:22 +08:00
|
|
|
int ndims, dimids_in[NC_MAX_VAR_DIMS], dimids_out[NC_MAX_VAR_DIMS], natts, real_ndims;
|
2010-06-03 21:24:43 +08:00
|
|
|
int varid_out;
|
|
|
|
int a, d;
|
|
|
|
void *data = NULL;
|
|
|
|
size_t *count = NULL, *start = NULL;
|
|
|
|
size_t reclen = 1;
|
|
|
|
size_t *dimlen = NULL;
|
|
|
|
int retval = NC_NOERR;
|
|
|
|
size_t type_size;
|
|
|
|
int src_format, dest_format;
|
|
|
|
char type_name[NC_MAX_NAME+1];
|
2013-01-24 05:46:22 +08:00
|
|
|
char dimname_in[NC_MAX_NAME + 1];
|
|
|
|
int i;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Learn about this var. */
|
2015-07-11 05:10:32 +08:00
|
|
|
if ((retval = nc_inq_var(ncid_in, varid_in, name, &xtype,
|
2013-01-24 05:46:22 +08:00
|
|
|
&ndims, dimids_in, &natts)))
|
2010-06-03 21:24:43 +08:00
|
|
|
return retval;
|
2015-07-11 05:10:32 +08:00
|
|
|
/* find corresponding dimids in the output file */
|
2013-01-24 05:46:22 +08:00
|
|
|
for(i = 0; i < ndims; i++) {
|
|
|
|
dimids_out[i] = dimids_in[i];
|
|
|
|
if ((retval = nc_inq_dimname(ncid_in, dimids_in[i], dimname_in)))
|
|
|
|
return retval;
|
|
|
|
if ((retval = nc_inq_dimid(ncid_out, dimname_in, &dimids_out[i])))
|
|
|
|
return retval;
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2015-07-11 05:10:32 +08:00
|
|
|
LOG((2, "nc_copy_var: ncid_in 0x%x varid_in %d ncid_out 0x%x",
|
2010-06-03 21:24:43 +08:00
|
|
|
ncid_in, varid_in, ncid_out));
|
|
|
|
|
|
|
|
/* Make sure we are not trying to write into a netcdf-3 file
|
|
|
|
* anything that won't fit in netcdf-3. */
|
|
|
|
if ((retval = nc_inq_format(ncid_in, &src_format)))
|
|
|
|
return retval;
|
|
|
|
if ((retval = nc_inq_format(ncid_out, &dest_format)))
|
|
|
|
return retval;
|
2015-08-16 06:26:35 +08:00
|
|
|
if ((dest_format == NC_FORMAT_CLASSIC
|
|
|
|
|| dest_format == NC_FORMAT_64BIT_DATA
|
|
|
|
|| dest_format == NC_FORMAT_64BIT_OFFSET) &&
|
2010-06-03 21:24:43 +08:00
|
|
|
src_format == NC_FORMAT_NETCDF4 && xtype > NC_DOUBLE)
|
|
|
|
return NC_ENOTNC4;
|
|
|
|
|
|
|
|
/* Later on, we will need to know the size of this type. */
|
|
|
|
if ((retval = nc_inq_type(ncid_in, xtype, type_name, &type_size)))
|
|
|
|
return retval;
|
|
|
|
LOG((3, "type %s has size %d", type_name, type_size));
|
|
|
|
|
|
|
|
/* Switch back to define mode, and create the output var. */
|
|
|
|
retval = nc_redef(ncid_out);
|
|
|
|
if (retval && retval != NC_EINDEFINE)
|
|
|
|
BAIL(retval);
|
|
|
|
if ((retval = nc_def_var(ncid_out, name, xtype,
|
2013-01-24 05:46:22 +08:00
|
|
|
ndims, dimids_out, &varid_out)))
|
2010-06-03 21:24:43 +08:00
|
|
|
BAIL(retval);
|
|
|
|
|
|
|
|
/* Copy the attributes. */
|
|
|
|
for (a=0; a<natts; a++)
|
|
|
|
{
|
|
|
|
if ((retval = nc_inq_attname(ncid_in, varid_in, a, att_name)))
|
|
|
|
BAIL(retval);
|
2015-07-11 05:10:32 +08:00
|
|
|
if ((retval = nc_copy_att(ncid_in, varid_in, att_name,
|
2010-06-03 21:24:43 +08:00
|
|
|
ncid_out, varid_out)))
|
|
|
|
BAIL(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End define mode, to write metadata and create file. */
|
|
|
|
nc_enddef(ncid_out);
|
|
|
|
nc_sync(ncid_out);
|
|
|
|
|
|
|
|
/* Allocate memory for our start and count arrays. If ndims = 0
|
|
|
|
this is a scalar, which I will treat as a 1-D array with one
|
|
|
|
element. */
|
|
|
|
real_ndims = ndims ? ndims : 1;
|
2014-02-14 22:07:14 +08:00
|
|
|
if (!(start = malloc((size_t)real_ndims * sizeof(size_t))))
|
2010-06-03 21:24:43 +08:00
|
|
|
BAIL(NC_ENOMEM);
|
2014-02-14 22:07:14 +08:00
|
|
|
if (!(count = malloc((size_t)real_ndims * sizeof(size_t))))
|
2010-06-03 21:24:43 +08:00
|
|
|
BAIL(NC_ENOMEM);
|
|
|
|
|
|
|
|
/* The start array will be all zeros, except the first element,
|
|
|
|
which will be the record number. Count will be the dimension
|
|
|
|
size, except for the first element, which will be one, because
|
|
|
|
we will copy one record at a time. For this we need the var
|
|
|
|
shape. */
|
2014-02-14 22:07:14 +08:00
|
|
|
if (!(dimlen = malloc((size_t)real_ndims * sizeof(size_t))))
|
2010-06-03 21:24:43 +08:00
|
|
|
BAIL(NC_ENOMEM);
|
|
|
|
|
2013-08-06 04:36:33 +08:00
|
|
|
/* Set to 0, to correct for an unlikely dereference
|
|
|
|
error reported by clang/llvm. */
|
|
|
|
dimlen[0] = 0;
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Find out how much data. */
|
|
|
|
for (d=0; d<ndims; d++)
|
|
|
|
{
|
2013-01-24 05:46:22 +08:00
|
|
|
if ((retval = nc_inq_dimlen(ncid_in, dimids_in[d], &dimlen[d])))
|
2010-06-03 21:24:43 +08:00
|
|
|
BAIL(retval);
|
|
|
|
LOG((4, "nc_copy_var: there are %d data", dimlen[d]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is really a scalar, then set the dimlen to 1. */
|
|
|
|
if (ndims == 0)
|
|
|
|
dimlen[0] = 1;
|
|
|
|
|
|
|
|
for (d=0; d<real_ndims; d++)
|
|
|
|
{
|
|
|
|
start[d] = 0;
|
|
|
|
count[d] = d ? dimlen[d] : 1;
|
|
|
|
if (d) reclen *= dimlen[d];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there are no records, we're done. */
|
|
|
|
if (!dimlen[0])
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
/* Allocate memory for one record. */
|
2014-08-01 06:20:50 +08:00
|
|
|
if (!(data = malloc(reclen * type_size))) {
|
|
|
|
if(count) free(count);
|
|
|
|
if(dimlen) free(dimlen);
|
|
|
|
if(start) free(start);
|
|
|
|
return NC_ENOMEM;
|
2015-07-11 05:10:32 +08:00
|
|
|
}
|
2014-08-01 06:20:50 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Copy the var data one record at a time. */
|
|
|
|
for (start[0]=0; !retval && start[0]<(size_t)dimlen[0]; start[0]++)
|
|
|
|
{
|
|
|
|
switch (xtype)
|
|
|
|
{
|
|
|
|
case NC_BYTE:
|
|
|
|
retval = nc_get_vara_schar(ncid_in, varid_in, start, count,
|
|
|
|
(signed char *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_schar(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(const signed char *)data);
|
|
|
|
break;
|
|
|
|
case NC_CHAR:
|
|
|
|
retval = nc_get_vara_text(ncid_in, varid_in, start, count,
|
|
|
|
(char *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_text(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(char *)data);
|
|
|
|
break;
|
|
|
|
case NC_SHORT:
|
|
|
|
retval = nc_get_vara_short(ncid_in, varid_in, start, count,
|
|
|
|
(short *)data);
|
|
|
|
if (!retval)
|
|
|
|
retval = nc_put_vara_short(ncid_out, varid_out, start, count,
|
|
|
|
(short *)data);
|
|
|
|
break;
|
|
|
|
case NC_INT:
|
|
|
|
retval = nc_get_vara_int(ncid_in, varid_in, start, count,
|
|
|
|
(int *)data);
|
|
|
|
if (!retval)
|
|
|
|
retval = nc_put_vara_int(ncid_out, varid_out, start, count,
|
|
|
|
(int *)data);
|
|
|
|
break;
|
|
|
|
case NC_FLOAT:
|
|
|
|
retval = nc_get_vara_float(ncid_in, varid_in, start, count,
|
|
|
|
(float *)data);
|
|
|
|
if (!retval)
|
|
|
|
retval = nc_put_vara_float(ncid_out, varid_out, start, count,
|
|
|
|
(float *)data);
|
|
|
|
break;
|
|
|
|
case NC_DOUBLE:
|
|
|
|
retval = nc_get_vara_double(ncid_in, varid_in, start, count,
|
|
|
|
(double *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_double(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(double *)data);
|
|
|
|
break;
|
|
|
|
case NC_UBYTE:
|
2010-06-23 06:41:04 +08:00
|
|
|
retval = nc_get_vara_uchar(ncid_in, varid_in, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(unsigned char *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_uchar(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(unsigned char *)data);
|
|
|
|
break;
|
|
|
|
case NC_USHORT:
|
|
|
|
retval = nc_get_vara_ushort(ncid_in, varid_in, start, count,
|
|
|
|
(unsigned short *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_ushort(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(unsigned short *)data);
|
|
|
|
break;
|
|
|
|
case NC_UINT:
|
|
|
|
retval = nc_get_vara_uint(ncid_in, varid_in, start, count,
|
|
|
|
(unsigned int *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_uint(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(unsigned int *)data);
|
|
|
|
break;
|
|
|
|
case NC_INT64:
|
|
|
|
retval = nc_get_vara_longlong(ncid_in, varid_in, start, count,
|
|
|
|
(long long *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_longlong(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(long long *)data);
|
|
|
|
break;
|
|
|
|
case NC_UINT64:
|
|
|
|
retval = nc_get_vara_ulonglong(ncid_in, varid_in, start, count,
|
|
|
|
(unsigned long long *)data);
|
|
|
|
if (!retval)
|
2015-07-11 05:10:32 +08:00
|
|
|
retval = nc_put_vara_ulonglong(ncid_out, varid_out, start, count,
|
2010-06-03 21:24:43 +08:00
|
|
|
(unsigned long long *)data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = NC_EBADTYPE;
|
|
|
|
}
|
|
|
|
}
|
2015-07-11 05:10:32 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
exit:
|
|
|
|
if (data) free(data);
|
|
|
|
if (dimlen) free(dimlen);
|
|
|
|
if (start) free(start);
|
|
|
|
if (count) free(count);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-12-05 04:11:54 +08:00
|
|
|
/**
|
|
|
|
* Copy an attribute from one open file to another. This is called by
|
|
|
|
* nc_copy_att().
|
|
|
|
*
|
|
|
|
* @param ncid_in File ID to copy from.
|
|
|
|
* @param varid_in Variable ID to copy from.
|
|
|
|
* @param name Name of attribute to copy.
|
|
|
|
* @param ncid_out File ID to copy to.
|
|
|
|
* @param varid_out Variable ID to copy to.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
|
|
|
*/
|
2010-11-30 06:23:16 +08:00
|
|
|
static int
|
2015-07-11 05:10:32 +08:00
|
|
|
NC_copy_att(int ncid_in, int varid_in, const char *name,
|
2010-06-03 21:24:43 +08:00
|
|
|
int ncid_out, int varid_out)
|
|
|
|
{
|
|
|
|
nc_type xtype;
|
|
|
|
size_t len;
|
|
|
|
void *data=NULL;
|
|
|
|
int res;
|
2015-07-11 05:10:32 +08:00
|
|
|
|
|
|
|
LOG((2, "nc_copy_att: ncid_in 0x%x varid_in %d name %s",
|
2010-06-03 21:24:43 +08:00
|
|
|
ncid_in, varid_in, name));
|
2015-07-11 05:10:32 +08:00
|
|
|
|
2010-11-30 06:23:16 +08:00
|
|
|
/* Find out about the attribute to be copied. */
|
2010-06-03 21:24:43 +08:00
|
|
|
if ((res = nc_inq_att(ncid_in, varid_in, name, &xtype, &len)))
|
|
|
|
return res;
|
2015-07-11 05:10:32 +08:00
|
|
|
|
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
|
|
|
{
|
|
|
|
/* Copy arbitrary attributes. */
|
|
|
|
int class;
|
|
|
|
size_t size;
|
|
|
|
nc_type xtype_out = NC_NAT;
|
|
|
|
|
|
|
|
if(xtype <= NC_MAX_ATOMIC_TYPE) {
|
|
|
|
xtype_out = xtype;
|
|
|
|
if((res = nc_inq_type(ncid_out,xtype_out,NULL,&size))) return res;
|
|
|
|
} else { /* User defined type */
|
|
|
|
/* Find out if there is an equal type in the output file. */
|
|
|
|
/* Note: original code used a libsrc4 specific internal function
|
|
|
|
which we had to "duplicate" here */
|
|
|
|
if ((res = NC_find_equal_type(ncid_in, xtype, ncid_out, &xtype_out)))
|
|
|
|
return res;
|
|
|
|
if (xtype_out) {
|
|
|
|
/* We found an equal type! */
|
|
|
|
if ((res = nc_inq_user_type(ncid_in, xtype, NULL, &size, NULL, NULL, &class)))
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((data = malloc(size * len))==NULL) {return NC_ENOMEM;}
|
|
|
|
res = nc_get_att(ncid_in, varid_in, name, data);
|
|
|
|
if(!res)
|
|
|
|
res = nc_put_att(ncid_out, varid_out, name, xtype_out, len, data);
|
|
|
|
(void)nc_reclaim_data_all(ncid_out,xtype_out,data,len);
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* Copy an attribute from one open file to another.
|
|
|
|
*
|
|
|
|
* Special programming challenge: this function must work even if one
|
|
|
|
* of the other of the files is a netcdf version 1.0 file (i.e. not
|
|
|
|
* HDF5). So only use top level netcdf api functions.
|
|
|
|
*
|
|
|
|
* From the netcdf-3 docs: The output netCDF dataset should be in
|
|
|
|
* define mode if the attribute to be copied does not already exist
|
|
|
|
* for the target variable, or if it would cause an existing target
|
|
|
|
* attribute to grow.
|
|
|
|
*
|
|
|
|
* @param ncid_in File ID to copy from.
|
|
|
|
* @param varid_in Variable ID to copy from.
|
|
|
|
* @param name Name of attribute to copy.
|
|
|
|
* @param ncid_out File ID to copy to.
|
|
|
|
* @param varid_out Variable ID to copy to.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Glenn Davis, Ed Hartnett, Dennis Heimbigner
|
2010-11-30 06:23:16 +08:00
|
|
|
*/
|
|
|
|
int
|
2015-07-11 05:10:32 +08:00
|
|
|
nc_copy_att(int ncid_in, int varid_in, const char *name,
|
2010-11-30 06:23:16 +08:00
|
|
|
int ncid_out, int varid_out)
|
|
|
|
{
|
2010-12-16 05:45:05 +08:00
|
|
|
int format, target_natts, target_attid;
|
2010-11-30 06:23:16 +08:00
|
|
|
char att_name[NC_MAX_NAME + 1];
|
|
|
|
int a, retval;
|
|
|
|
|
|
|
|
/* What is the destination format? */
|
|
|
|
if ((retval = nc_inq_format(ncid_out, &format)))
|
|
|
|
return retval;
|
2015-07-11 05:10:32 +08:00
|
|
|
|
2010-11-30 06:23:16 +08:00
|
|
|
/* Can't copy to same var in same file. */
|
|
|
|
if (ncid_in == ncid_out && varid_in == varid_out)
|
|
|
|
return NC_NOERR;
|
2015-07-11 05:10:32 +08:00
|
|
|
|
2010-11-30 06:23:16 +08:00
|
|
|
/* For classic model netCDF-4 files, order of attributes must be
|
|
|
|
* maintained during copies. We MUST MAINTAIN ORDER! */
|
|
|
|
if (format == NC_FORMAT_NETCDF4_CLASSIC)
|
|
|
|
{
|
|
|
|
/* Does this attribute already exist in the target file? */
|
|
|
|
retval = nc_inq_attid(ncid_out, varid_out, name, &target_attid);
|
|
|
|
if (retval == NC_ENOTATT)
|
|
|
|
{
|
|
|
|
/* Attribute does not exist. No order to be preserved. */
|
|
|
|
return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
|
|
|
|
}
|
|
|
|
else if (retval == NC_NOERR)
|
|
|
|
{
|
|
|
|
/* How many atts for this var? */
|
|
|
|
if ((retval = nc_inq_varnatts(ncid_out, varid_out, &target_natts)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/* If this is the last attribute in the target file, we are
|
|
|
|
* off the hook. */
|
|
|
|
if (target_attid == target_natts - 1)
|
|
|
|
return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
|
2015-07-11 05:10:32 +08:00
|
|
|
|
2010-11-30 06:23:16 +08:00
|
|
|
/* Order MUST BE MAINTAINED! Copy all existing atts in the target
|
|
|
|
* file, stopping at our target att. */
|
|
|
|
for (a = 0; a < target_natts; a++)
|
|
|
|
{
|
|
|
|
if (a == target_attid)
|
|
|
|
{
|
|
|
|
if ((retval = NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out)))
|
|
|
|
return retval;
|
2015-07-11 05:10:32 +08:00
|
|
|
}
|
2010-11-30 06:23:16 +08:00
|
|
|
else
|
|
|
|
{
|
2010-12-16 05:45:05 +08:00
|
|
|
if ((retval = nc_inq_attname(ncid_out, varid_out, a, att_name)))
|
2010-11-30 06:23:16 +08:00
|
|
|
return retval;
|
2015-07-11 05:10:32 +08:00
|
|
|
if ((retval = NC_copy_att(ncid_out, varid_out, att_name,
|
2010-11-30 06:23:16 +08:00
|
|
|
ncid_out, varid_out)))
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-08-20 17:42:05 +08:00
|
|
|
return retval; /* Some other error occurred. */
|
2010-11-30 06:23:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return NC_copy_att(ncid_in, varid_in, name, ncid_out, varid_out);
|
|
|
|
|
|
|
|
return NC_NOERR;
|
|
|
|
}
|
2021-03-07 05:09:37 +08:00
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
|
|
|
|
/* Helper function for NC_rec_find_nc_type();
|
|
|
|
search a specified group for matching type.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
searchgroup(int ncid1, int tid1, int grp, int* tid2)
|
|
|
|
{
|
|
|
|
int i,ret = NC_NOERR;
|
|
|
|
int nids;
|
|
|
|
int* ids = NULL;
|
|
|
|
|
|
|
|
/* Get all types in grp */
|
|
|
|
if(tid2)
|
|
|
|
*tid2 = 0;
|
|
|
|
if ((ret = nc_inq_typeids(grp, &nids, NULL)))
|
|
|
|
goto done;
|
|
|
|
if (nids)
|
|
|
|
{
|
|
|
|
if (!(ids = (int *)malloc((size_t)nids * sizeof(int))))
|
|
|
|
{ret = NC_ENOMEM; goto done;}
|
|
|
|
if ((ret = nc_inq_typeids(grp, &nids, ids)))
|
|
|
|
goto done;
|
|
|
|
for(i = 0; i < nids; i++)
|
|
|
|
{
|
|
|
|
int equal = 0;
|
|
|
|
if ((ret = NC_compare_nc_types(ncid1, tid1, grp, ids[i], &equal)))
|
|
|
|
goto done;
|
|
|
|
if(equal)
|
|
|
|
{
|
|
|
|
if(tid2)
|
|
|
|
*tid2 = ids[i];
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
nullfree(ids);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function for NC_rec_find_nc_type();
|
|
|
|
search a tree of groups for a matching type
|
|
|
|
using a breadth first queue
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
searchgrouptree(int ncid1, int tid1, int grp, int* tid2)
|
|
|
|
{
|
|
|
|
int i,ret = NC_NOERR;
|
|
|
|
int nids;
|
|
|
|
int* ids = NULL;
|
|
|
|
NClist* queue = nclistnew();
|
|
|
|
int gid;
|
|
|
|
uintptr_t id;
|
|
|
|
|
2023-11-28 09:46:10 +08:00
|
|
|
id = (uintptr_t)grp;
|
2021-03-07 05:09:37 +08:00
|
|
|
nclistpush(queue,(void*)id); /* prime the queue */
|
|
|
|
while(nclistlength(queue) > 0) {
|
|
|
|
id = (uintptr_t)nclistremove(queue,0);
|
|
|
|
gid = (int)id;
|
|
|
|
if((ret = searchgroup(ncid1,tid1,gid,tid2)))
|
|
|
|
goto done;
|
|
|
|
if(*tid2 != 0)
|
|
|
|
goto done; /*we found it*/
|
|
|
|
/* Get subgroups of gid and push onto front of the queue (for breadth first) */
|
|
|
|
if((ret = nc_inq_grps(gid,&nids,NULL)))
|
|
|
|
goto done;
|
|
|
|
if (!(ids = (int *)malloc((size_t)nids * sizeof(int))))
|
|
|
|
{ret = NC_ENOMEM; goto done;}
|
|
|
|
if ((ret = nc_inq_grps(gid, &nids, ids)))
|
|
|
|
goto done;
|
|
|
|
/* push onto the end of the queue */
|
|
|
|
for(i=0;i<nids;i++) {
|
2023-11-28 09:46:10 +08:00
|
|
|
id = (uintptr_t)ids[i];
|
2021-03-07 05:09:37 +08:00
|
|
|
nclistpush(queue,(void*)id);
|
|
|
|
}
|
Regularize the scoping of dimensions
This is a follow-on to pull request
````https://github.com/Unidata/netcdf-c/pull/1959````,
which fixed up type scoping.
The primary changes are to _nc\_inq\_dimid()_ and to ncdump.
The _nc\_inq\_dimid()_ function is supposed to allow the name to be
and FQN, but this apparently never got implemented. So if was modified
to support FQNs.
The ncdump program is supposed to output fully qualified dimension names
in its generated CDL file under certain conditions.
Suppose ncdump has a netcdf-4 file F with variable V, and V's parent group
is G. For each dimension id D referenced by V, ncdump needs to determine
whether to print its name as a simple name or as a fully qualified name (FQN).
The algorithm is as follows:
1. Search up the tree of ancestor groups.
2. If one of those ancestor groups contains the dimid, then call it dimgrp.
3. If one of those ancestor groups contains a dim with the same name as the dimid, but with a different dimid, then record that as duplicate=true.
4. If dimgrp is defined and duplicate == false, then we do not need an fqn.
5. If dimgrp is defined and duplicate == true, then we do need an fqn to avoid incorrectly using the duplicate.
6. If dimgrp is undefined, then do a preorder breadth-first search of all the groups looking for the dimid.
7. If found, then use the fqn of the first found such dimension location.
8. If not found, then fail.
Test case ncdump/test_scope.sh was modified to test the proper
operation of ncdump and _nc\_inq\_dimid()_.
Misc. Other Changes:
* Fix nc_inq_ncid (NC4_inq_ncid actually) to return root group id if the name argument is NULL.
* Modify _ncdump/printfqn_ to print out a dimid FQN; this supports verification that the resulting .nc files were properly created.
2021-06-01 05:51:12 +08:00
|
|
|
free(ids); ids = NULL;
|
2021-03-07 05:09:37 +08:00
|
|
|
}
|
|
|
|
/* Not found */
|
|
|
|
ret = NC_EBADTYPE;
|
|
|
|
|
|
|
|
done:
|
|
|
|
nclistfree(queue);
|
|
|
|
nullfree(ids);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:46:25 +08:00
|
|
|
#endif /* USE_NETCDF4 */
|
|
|
|
|