netcdf-c/libdap4/d4odom.c
Dennis Heimbigner 3db4f013bf Primary change: add dap4 support
Specific changes:
1. Add dap4 code: libdap4 and dap4_test.
   Note that until the d4ts server problem is solved, dap4 is turned off.
2. Modify various files to support dap4 flags:
	configure.ac, Makefile.am, CMakeLists.txt, etc.
3. Add nc_test/test_common.sh. This centralizes
   the handling of the locations of various
   things in the build tree: e.g. where is
   ncgen.exe located. See nc_test/test_common.sh
   for details.
4. Modify .sh files to use test_common.sh
5. Obsolete separate oc2 by moving it to be part of
   netcdf-c. This means replacing code with netcdf-c
   equivalents.
5. Add --with-testserver to configure.ac to allow
   override of the servers to be used for --enable-dap-remote-tests.
6. There were multiple versions of nctypealignment code. Try to
   centralize in libdispatch/doffset.c and include/ncoffsets.h
7. Add a unit test for the ncuri code because of its complexity.
8. Move the findserver code out of libdispatch and into
   a separate, self contained program in ncdap_test and dap4_test.
9. Move the dispatch header files (nc{3,4}dispatch.h) to
   .../include because they are now shared by modules.
10. Revamp the handling of TOPSRCDIR and TOPBUILDDIR for shell scripts.
11. Make use of MREMAP if available
12. Misc. minor changes e.g.
	- #include <config.h> -> #include "config.h"
	- Add some no-install headers to /include
	- extern -> EXTERNL and vice versa as needed
	- misc header cleanup
	- clean up checking for misc. unix vs microsoft functions
13. Change copyright decls in some files to point to LICENSE file.
14. Add notes to RELEASENOTES.md
2017-03-08 17:01:10 -07:00

171 lines
4.1 KiB
C

/*********************************************************************
* Copyright 2016, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
#include "config.h"
#include <stdlib.h>
#include <assert.h>
#include "netcdf.h"
#include "d4util.h"
#include "d4odom.h"
/**********************************************/
/* Define methods for a dimension dapodometer*/
/* Build an odometer covering slices startslice upto,
but not including, stopslice
*/
/*
Future optimization. It is sometimes the case that
some suffix of the index set cover a contiguous chunk
of the index space. This happens when the index positions
starting at p upto rank have a start of zero, a count
the same as the max index size, and a stride of one.
Then we can effectively tread indices 0 thru p-1 as
a short odometer that steps through the chunks.
In practice, it is usually only ncdump that does this.
A simpler optimization that is to see if the (start,count)
covers the whole index space and has stride one.
We can detect and use this optimization elsewhere to
circumvent use of the odometer altogether.
See d4odometer#NCD4_odomWhole().
*/
D4odometer*
d4odom_new(size_t rank,
const size_t* start, const size_t* count,
const ptrdiff_t* stride, const size_t* size)
{
int i;
D4odometer* odom = (D4odometer*)calloc(1,sizeof(D4odometer));
if(odom == NULL)
return NULL;
odom->rank = rank;
assert(odom->rank <= NC_MAX_VAR_DIMS);
for(i=0;i<odom->rank;i++) {
size_t istart,icount,istop,ideclsize;
ptrdiff_t istride;
istart = (start != NULL ? start[i] : 0);
icount = (count != NULL ? count[i] : (size != NULL ? size[i] : 1));
istride = (size_t)(stride != NULL ? stride[i] : 1);
istop = istart + icount*istride;
ideclsize = (size != NULL ? size[i]: (istop - istart));
odom->start[i] = istart;
odom->stop[i] = istop;
odom->stride[i] = istride;
odom->declsize[i] = ideclsize;
odom->index[i] = odom->start[i];
}
return odom;
}
void
d4odom_free(D4odometer* odom)
{
if(odom) free(odom);
}
#if 0
char*
d4odom_print(D4odometer* odom)
{
int i;
static char line[1024];
char tmp[64];
line[0] = '\0';
if(odom->rank == 0) {
strcat(line,"[]");
} else for(i=0;i<odom->rank;i++) {
sprintf(tmp,"[%lu/%lu:%lu:%lu]",
(size_t)odom->index[i],
(size_t)odom->start[i],
(size_t)odom->stride[i],
(size_t)odom->length[i]);
strcat(line,tmp);
}
return line;
}
#endif
int
d4odom_more(D4odometer* odom)
{
return (odom->index[0] < odom->stop[0]);
}
d4size_t
d4odom_next(D4odometer* odom)
{
int i; /* do not make unsigned */
d4size_t count;
if(odom->rank == 0) { /*scalar*/
odom->index[0]++;
return 0;
}
count = d4odom_offset(odom); /* convenience */
for(i=odom->rank-1;i>=0;i--) {
odom->index[i] += odom->stride[i];
if(odom->index[i] < odom->stop[i]) break;
if(i == 0) break; /* leave the 0th entry if it overflows*/
odom->index[i] = odom->start[i]; /* reset this position*/
}
return count;
}
/* Convert current d4odometer settings to a single integer offset*/
d4size_t
d4odom_offset(D4odometer* odom)
{
int i;
d4size_t offset = 0;
for(i=0;i<odom->rank;i++) {
offset *= odom->declsize[i];
offset += odom->index[i];
}
return offset;
}
/**************************************************/
/*
Given a d4odometer, compute the total
number of elements in its space.
*/
d4size_t
d4odom_nelements(D4odometer* odom)
{
size_t i;
d4size_t count = 1;
for(i=0;i<odom->rank;i++) {
count *= odom->declsize[i];
}
return count;
}
int
d4odom_isWhole(D4odometer* odom)
{
int i;
for(i=0;i<odom->rank;i++) {
if(odom->start[i] != 0
|| odom->stride[i] != 1
|| odom->stop[i] != odom->declsize[i])
return 0;
}
return 1;
}
/* Scalar Odometer support */
D4odometer*
d4scalarodom_new(void)
{
D4odometer* odom = (D4odometer*)calloc(1,sizeof(D4odometer));
odom->rank = 0;
/* Fake things to execute exactly once */
odom->index[0] = 0;
odom->stop[0] = 1;
return odom;
}