mirror of
https://github.com/Unidata/netcdf-c.git
synced 2025-02-11 16:40:36 +08:00
This PR started as an attempt to add unlimited dimensions to NCZarr. It did that, but this exposed significant problems with test interference. So this PR is mostly about fixing -- well mitigating anyway -- test interference. The problem of test interference is now documented in the document docs/internal.md. The solutions implemented here are also describe in that document. The solution is somewhat fragile but multiple cleanup mechanisms are provided. Note that this feature requires that the AWS command line utility must be installed. ## Unlimited Dimensions. The existing NCZarr extensions to Zarr are modified to support unlimited dimensions. NCzarr extends the Zarr meta-data for the ".zgroup" object to include netcdf-4 model extensions. This information is stored in ".zgroup" as dictionary named "_nczarr_group". Inside "_nczarr_group", there is a key named "dims" that stores information about netcdf-4 named dimensions. The value of "dims" is a dictionary whose keys are the named dimensions. The value associated with each dimension name has one of two forms Form 1 is a special case of form 2, and is kept for backward compatibility. Whenever a new file is written, it uses format 1 if possible, otherwise format 2. * Form 1: An integer representing the size of the dimension, which is used for simple named dimensions. * Form 2: A dictionary with the following keys and values" - "size" with an integer value representing the (current) size of the dimension. - "unlimited" with a value of either "1" or "0" to indicate if this dimension is an unlimited dimension. For Unlimited dimensions, the size is initially zero, and as variables extend the length of that dimension, the size value for the dimension increases. That dimension size is shared by all arrays referencing that dimension, so if one array extends an unlimited dimension, it is implicitly extended for all other arrays that reference that dimension. This is the standard semantics for unlimited dimensions. Adding unlimited dimensions required a number of other changes to the NCZarr code-base. These included the following. * Did a partial refactor of the slice handling code in zwalk.c to clean it up. * Added a number of tests for unlimited dimensions derived from the same test in nc_test4. * Added several NCZarr specific unlimited tests; more are needed. * Add test of endianness. ## Misc. Other Changes * Modify libdispatch/ncs3sdk_aws.cpp to optionally support use of the AWS Transfer Utility mechanism. This is controlled by the ```#define TRANSFER```` command in that file. It defaults to being disabled. * Parameterize both the standard Unidata S3 bucket (S3TESTBUCKET) and the netcdf-c test data prefix (S3TESTSUBTREE). * Fixed an obscure memory leak in ncdump. * Removed some obsolete unit testing code and test cases. * Uncovered a bug in the netcdf-c handling of big-endian floats and doubles. Have not fixed yet. See tst_h5_endians.c. * Renamed some nczarr_tests testcases to avoid name conflicts with nc_test4. * Modify the semantics of zmap\#ncsmap_write to only allow total rewrite of objects. * Modify the semantics of zodom to properly handle stride > 1. * Add a truncate operation to the libnczarr zmap code.
207 lines
5.5 KiB
C
207 lines
5.5 KiB
C
/*********************************************************************
|
|
* Copyright 2018, UCAR/Unidata
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
*********************************************************************/
|
|
#include "zincludes.h"
|
|
|
|
/*Forward*/
|
|
static int buildodom(int rank, NCZOdometer** odomp);
|
|
|
|
void
|
|
nczodom_reset(NCZOdometer* odom)
|
|
{
|
|
int r;
|
|
for(r=0;r<odom->rank;r++)
|
|
odom->index[r] = odom->start[r];
|
|
}
|
|
|
|
NCZOdometer*
|
|
nczodom_new(int rank, const size64_t* start, const size64_t* stop, const size64_t* stride, const size64_t* len)
|
|
{
|
|
int i;
|
|
NCZOdometer* odom = NULL;
|
|
if(buildodom(rank,&odom)) return NULL;
|
|
odom->properties.stride1 = 1; /* assume */
|
|
odom->properties.start0 = 1; /* assume */
|
|
for(i=0;i<rank;i++) {
|
|
odom->start[i] = (size64_t)start[i];
|
|
odom->stride[i] = (size64_t)stride[i];
|
|
odom->stop[i] = (size64_t)stop[i];
|
|
odom->len[i] = (size64_t)len[i];
|
|
if(odom->start[i] != 0) odom->properties.start0 = 0;
|
|
if(odom->stride[i] != 1) odom->properties.stride1 = 0;
|
|
}
|
|
nczodom_reset(odom);
|
|
for(i=0;i<rank;i++)
|
|
assert(stop[i] >= start[i] && stride[i] > 0 && (len[i]+1) >= stop[i]);
|
|
return odom;
|
|
}
|
|
|
|
NCZOdometer*
|
|
nczodom_fromslices(int rank, const NCZSlice* slices)
|
|
{
|
|
size_t i;
|
|
NCZOdometer* odom = NULL;
|
|
|
|
if(buildodom(rank,&odom)) return NULL;
|
|
odom->properties.stride1 = 1; /* assume */
|
|
odom->properties.start0 = 1; /* assume */
|
|
for(i=0;i<rank;i++) {
|
|
odom->start[i] = slices[i].start;
|
|
odom->stop[i] = slices[i].stop;
|
|
odom->stride[i] = slices[i].stride;
|
|
odom->len[i] = slices[i].len;
|
|
if(odom->start[i] != 0) odom->properties.start0 = 0;
|
|
if(odom->stride[i] != 1) odom->properties.stride1 = 0;
|
|
}
|
|
nczodom_reset(odom);
|
|
for(i=0;i<rank;i++) {
|
|
assert(slices[i].stop >= slices[i].start && slices[i].stride > 0);
|
|
assert((slices[i].stop - slices[i].start) <= slices[i].len);
|
|
}
|
|
return odom;
|
|
}
|
|
|
|
void
|
|
nczodom_free(NCZOdometer* odom)
|
|
{
|
|
if(odom == NULL) return;
|
|
nullfree(odom->start);
|
|
nullfree(odom->stop);
|
|
nullfree(odom->stride);
|
|
nullfree(odom->len);
|
|
nullfree(odom->index);
|
|
nullfree(odom);
|
|
}
|
|
|
|
int
|
|
nczodom_more(const NCZOdometer* odom)
|
|
{
|
|
return (odom->index[0] < odom->stop[0]);
|
|
}
|
|
|
|
void
|
|
nczodom_next(NCZOdometer* odom)
|
|
{
|
|
int i;
|
|
int rank;
|
|
rank = odom->rank;
|
|
for(i=rank-1;i>=0;i--) {
|
|
odom->index[i] += odom->stride[i];
|
|
if(odom->index[i] < odom->stop[i]) break;
|
|
if(i == 0) goto done; /* leave the 0th entry if it overflows */
|
|
odom->index[i] = odom->start[i]; /* reset this position */
|
|
}
|
|
done:
|
|
return;
|
|
}
|
|
|
|
/* Get the value of the odometer */
|
|
size64_t*
|
|
nczodom_indices(const NCZOdometer* odom)
|
|
{
|
|
return odom->index;
|
|
}
|
|
|
|
size64_t
|
|
nczodom_offset(const NCZOdometer* odom)
|
|
{
|
|
int i;
|
|
size64_t offset;
|
|
int rank = odom->rank;
|
|
|
|
offset = 0;
|
|
for(i=0;i<rank;i++) {
|
|
#if 1
|
|
offset *= odom->len[i];
|
|
#else
|
|
offset *= odom->stop[i];
|
|
#endif
|
|
offset += odom->index[i];
|
|
}
|
|
return offset;
|
|
}
|
|
|
|
static int
|
|
buildodom(int rank, NCZOdometer** odomp)
|
|
{
|
|
int stat = NC_NOERR;
|
|
NCZOdometer* odom = NULL;
|
|
if(odomp) {
|
|
if((odom = calloc(1,sizeof(NCZOdometer))) == NULL)
|
|
goto done;
|
|
odom->rank = rank;
|
|
if((odom->start=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
|
|
if((odom->stop=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
|
|
if((odom->stride=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
|
|
if((odom->len=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
|
|
if((odom->index=calloc(1,(sizeof(size64_t)*rank)))==NULL) goto nomem;
|
|
*odomp = odom; odom = NULL;
|
|
}
|
|
done:
|
|
nczodom_free(odom);
|
|
return stat;
|
|
nomem:
|
|
stat = NC_ENOMEM;
|
|
goto done;
|
|
}
|
|
|
|
/* Compute the total avail in last position */
|
|
size64_t
|
|
nczodom_avail(const NCZOdometer* odom)
|
|
{
|
|
size64_t avail;
|
|
/* The best we can do is compute the count for the rightmost index */
|
|
avail = (odom->stop[odom->rank-1] - odom->start[odom->rank-1]);
|
|
return avail;
|
|
}
|
|
|
|
/*
|
|
Incr the odometer by nczodom_avail() values.
|
|
Calling nczodom_next at that point should properly increment
|
|
rest of the odometer.
|
|
*/
|
|
void
|
|
nczodom_skipavail(NCZOdometer* odom)
|
|
{
|
|
if(odom->rank > 0)
|
|
odom->index[odom->rank-1] = odom->stop[odom->rank-1];
|
|
}
|
|
|
|
size64_t
|
|
nczodom_laststride(const NCZOdometer* odom)
|
|
{
|
|
assert(odom != NULL && odom->rank > 0);
|
|
return odom->stride[odom->rank-1];
|
|
}
|
|
|
|
size64_t
|
|
nczodom_lastlen(const NCZOdometer* odom)
|
|
{
|
|
assert(odom != NULL && odom->rank > 0);
|
|
return odom->len[odom->rank-1];
|
|
}
|
|
|
|
void
|
|
nczodom_print(const NCZOdometer* odom)
|
|
{
|
|
size_t i;
|
|
fprintf(stderr,"odom{rank=%d offset=%llu avail=%llu",odom->rank,nczodom_offset(odom),nczodom_avail(odom));
|
|
fprintf(stderr," start=(");
|
|
for(i=0;i<odom->rank;i++) {fprintf(stderr,"%s%llu",(i==0?"":" "),(unsigned long long)odom->start[i]);}
|
|
fprintf(stderr,")");
|
|
fprintf(stderr," stride=(");
|
|
for(i=0;i<odom->rank;i++) {fprintf(stderr,"%s%llu",(i==0?"":" "),(unsigned long long)odom->stride[i]);}
|
|
fprintf(stderr,")");
|
|
fprintf(stderr," stop=(");
|
|
for(i=0;i<odom->rank;i++) {fprintf(stderr,"%s%llu",(i==0?"":" "),(unsigned long long)odom->stop[i]);}
|
|
fprintf(stderr,")");
|
|
fprintf(stderr," len=(");
|
|
for(i=0;i<odom->rank;i++) {fprintf(stderr,"%s%llu",(i==0?"":" "),(unsigned long long)odom->len[i]);}
|
|
fprintf(stderr,")");
|
|
fprintf(stderr," index=(");
|
|
for(i=0;i<odom->rank;i++) {fprintf(stderr,"%s%llu",(i==0?"":" "),(unsigned long long)odom->index[i]);}
|
|
fprintf(stderr,")");
|
|
fprintf(stderr,"}\n");
|
|
}
|