1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
/*
* Copyright 1993, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*
* This file supports netCDF variable I/O for generalized hyperslabs.
* A generalized hyperslab is one in which the locations of the
* memory-resident data values may be arbitrary, though they are
* constrained to have a regular structure. In addition, the values
* of the netCDF variable may be accessed using non-unity strides.
*
* $Id: putgetg.c,v 1.8 1997/11/13 20:19:46 acheng Exp $
*/
#include "local_nc.h"
/*
* Perform I/O on a generalized hyperslab. The efficiency of this
* implementation is dependent upon caching in the lower layers.
*/
#ifndef HDF
static
#endif
int
NCgenio(handle, varid, start, count, stride, imap, values)
NC *handle;
int varid;
const long *start; /* NULL => first corner */
const long *count; /* NULL => everything following start[] */
const long *stride; /* NULL => unity strides */
const long *imap; /* NULL => same structure as netCDF variable */
void *values ;
{
int maxidim; /* maximum dimensional index */
NC_var *vp = NC_hlookupvar( handle, varid );
if (vp == NULL)
return(-1) ;
maxidim = vp->assoc->count - 1;
if (maxidim < 0) {
/*
* The variable is a scalar; consequently, there's only one thing
* to get and only one place to put it. (Why was I called?)
*/
return NCvario(handle, varid, start, count, values);
} else {
/*
* The variable is an array.
*/
int idim;
char *valp = values;
long mycount[MAX_VAR_DIMS];
long mystart[MAX_VAR_DIMS];
long mystride[MAX_VAR_DIMS];
long myimap[MAX_VAR_DIMS];
long iocount[MAX_VAR_DIMS]; /* count vector for NCvario() */
long stop[MAX_VAR_DIMS]; /* stop indexes */
long length[MAX_VAR_DIMS]; /* edge lengths in bytes */
/*
* Verify stride argument.
*/
for (idim = 0; idim <= maxidim; ++idim) {
if (stride != NULL && stride[idim] < 1) {
NCadvise(NC_EINVAL, "Non-positive stride");
return(-1) ;
}
}
/*
* Initialize I/O parameters.
*/
for (idim = maxidim; idim >= 0; --idim) {
mystart[idim] = start != NULL
? start[idim]
: 0;
mycount[idim] = count != NULL
? count[idim]
: idim == 0 && IS_RECVAR(vp)
? handle->numrecs - mystart[idim]
: vp->shape[idim] - mystart[idim];
mystride[idim] = stride != NULL
? stride[idim]
: 1;
myimap[idim] = imap != NULL
? imap[idim]
: idim == maxidim
? vp->szof
: myimap[idim+1] * mycount[idim+1];
iocount[idim] = 1;
length[idim] = myimap[idim] * mycount[idim];
stop[idim] = mystart[idim] + mycount[idim]*mystride[idim];
}
/*
* As an optimization, adjust I/O parameters when the fastest
* dimension has unity stride both externally and internally.
* In this case, the user could have called a simpler routine
* (i.e. ncvarget() or ncvarput()).
*/
if (mystride[maxidim] == 1 && myimap[maxidim] == vp->szof) {
iocount[maxidim] = mycount[maxidim];
mystride[maxidim] = mycount[maxidim];
myimap[maxidim] = length[maxidim];
}
/*
* Perform I/O. Exit when done.
*/
for (;;) {
int iostat = NCvario(handle, varid, mystart, iocount,
(Void*)valp);
if (iostat != 0)
return iostat;
/*
* The following code permutes through the variable's external
* start-index space and it's internal address space. At the
* UPC, this algorithm is commonly called `odometer code'.
*/
idim = maxidim;
carry:
valp += myimap[idim];
mystart[idim] += mystride[idim];
if (mystart[idim] >= stop[idim]) {
mystart[idim] = start[idim];
valp -= length[idim];
if (--idim < 0)
return 0;
goto carry;
}
} /* I/O loop */
} /* variable is array */
}
/*
* Generalized hyperslab output.
*/
int
ncvarputg(cdfid, varid, start, count, stride, imap, values)
int cdfid ;
int varid ;
const long *start ;
const long *count ;
const long *stride ;
const long *imap ;
ncvoid *values ;
{
NC *handle ;
cdf_routine_name = "ncvarputg" ;
handle = NC_check_id(cdfid) ;
if(handle == NULL)
return(-1) ;
if(!(handle->flags & NC_RDWR))
{
NCadvise(NC_EPERM, "%s: NC_NOWRITE", handle->path) ;
return(-1) ;
}
handle->xdrs->x_op = XDR_ENCODE ;
return NCgenio(handle, varid, start, count, stride, imap, values);
}
/*
* Generalized hyperslab input.
*/
int
ncvargetg(cdfid, varid, start, count, stride, imap, values)
int cdfid ;
int varid ;
const long *start ;
const long *count ;
const long *stride ;
const long *imap ;
ncvoid *values ;
{
NC *handle ;
cdf_routine_name = "ncvargetg" ;
handle = NC_check_id(cdfid) ;
if (handle == NULL)
return(-1) ;
handle->xdrs->x_op = XDR_DECODE ;
return NCgenio(handle, varid, start, count,
stride, imap, (Void*)values);
}
/*
* Stride-oriented hyperslab output.
*/
int
ncvarputs(cdfid, varid, start, count, stride, values)
int cdfid ;
int varid ;
const long *start ;
const long *count ;
const long *stride ;
ncvoid *values ;
{
NC *handle ;
cdf_routine_name = "ncvarputs" ;
handle = NC_check_id(cdfid) ;
if(handle == NULL)
return(-1) ;
if(!(handle->flags & NC_RDWR))
{
NCadvise(NC_EPERM, "%s: NC_NOWRITE", handle->path) ;
return(-1) ;
}
handle->xdrs->x_op = XDR_ENCODE ;
return NCgenio(handle, varid, start, count, stride, NULL, values);
}
/*
* Stride-oriented hyperslab input.
*/
int
ncvargets(cdfid, varid, start, count, stride, values)
int cdfid ;
int varid ;
const long *start ;
const long *count ;
const long *stride ;
ncvoid *values ;
{
NC *handle ;
cdf_routine_name = "ncvargets" ;
handle = NC_check_id(cdfid) ;
if (handle == NULL)
return(-1) ;
handle->xdrs->x_op = XDR_DECODE ;
return NCgenio(handle, varid, start, count,
stride, (long*)0, (Void*)values);
}
|