File: test_nstride_cached.c

package info (click to toggle)
netcdf 1%3A4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (314 lines) | stat: -rw-r--r-- 7,854 bytes parent folder | download | duplicates (7)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*! \file

Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018
University Corporation for Atmospheric Research/Unidata.

See \ref copyright file for more info.

*/
/*
Report from Ansely Manke:
I've attached a file with a short c program that
demonstrates what I'm seeing. I don't know that we'd call
this a constraint, so that was a misleading description.

The thing I'm mimicking is a sequence where I read the 2-D
coordinate variables, make some decisions about the size of
a subset region, and then define index ranges and strides
for reading other variables.  Reading the full variable
happens correctly, but the read with strides winds up
messing up the data.  Here it shows up returning some valid
data and some zero's, but in other sequences of events it
seems to be actually mis-ordered.

I notice that the results are correct if the initial read of
the variable uses counts that are size-1. So that might be a
clue.  And the read is correct if I either close and reopen
the dataset after reading the original full variable, or if
I don't do that step of first reading the whole grid. It's
also fine with NetCDF 4.2.1.1.

-Ansley

Problem was two-fold:
1. struct Getvara has the dsttype field as OC_Type rather than nc_type
2. the dap odometer code dapnew_segment was incorrectly
   handling strides > 1: specifically, the stop position was incorrect.

*/

/* vars_whoi_test */
/* acm 4/2013 */
/* ansley.b.manke@noaa.gov */

/* test nc_get_vars_float with calls similar to Ferret calls */


/*linked with:

cc vars_whoi_test.c -g -o vars_whoi_test_4211 /usr/local/netcdf_4211/lib/libnetcdf.a /usr/local/hdf5_189/lib/libhdf5_hl.a /usr/local/hdf5_189/lib/libhdf5.a /usr/local/lib/libz.a -L/usr/lib64 -lc -lm -lcurl
cc vars_whoi_test.c -g -o vars_whoi_test /home/users/ansley/local/x86_nc43/lib/libnetcdf.a /usr/local/hdf5_189/lib/libhdf5_hl.a /usr/local/hdf5_189/lib/libhdf5.a /usr/local/lib/libz.a -L/usr/lib64 -lc -lm -lcurl

Closing and reopening the dataset between the two reads fixes the
incorrect data return

Setting the count to one less in the full data read also fixes the
incorrect data return
*/

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include "netcdf.h"
#include "ncdispatch.h"
#include "nctestserver.h"

#define VERBOSE 1

/*
2014-07-01: (DMH) Changed the URL to use one from remotetest server.
to remove dependence on an external server.
*/

/*
static char* URL="http://geoport.whoi.edu/thredds/dodsC/coawst_4/use/fmrc/coawst_4_use_best.ncd";
Float64 lon_rho[eta_rho = 336][xi_rho = 896];
Float64 lat_rho[eta_rho = 336][xi_rho = 896];
*/

#define URL "%s/dodsC/testdods/rtofs.nc"
#define VAR1 "Latitude"
#define VAR2 "Longitude"
#define XSIZE 850
#define YSIZE 712

static char url[1024];

int
main()
{

    int ncid;
    int varid;
    int i;
    int ncstatus;
    size_t start[5], count[5];
    ptrdiff_t stride[5];
    int pass = 1;
    int nelems = XSIZE*YSIZE;
    int idim, ndim;
    float *dat = (float*)malloc(sizeof(float)*nelems);
    float sdat[10];
    char* svc;

    /* Find Test Server */
    svc = nc_findtestserver("thredds",REMOTETESTSERVERS);

    if(svc == NULL) {
        fprintf(stderr,"WARNING: Cannot locate test server\n");
	exit(0);
    }

    snprintf(url,sizeof(url),URL,svc);

    for (idim=0; idim<5; idim++) {
        start[idim] = 0;
        count[idim] = 1;
        stride[idim] = 1;
    }

    ndim=2;

    printf(" \n");
    printf("********************\n");
    printf("open URL %s\n",url);
    printf(" \n");
    fflush(stdout);

    ncstatus = nc_open(url, NC_NOWRITE, &ncid);

    if(ncstatus != NC_NOERR) {
	fprintf(stderr,"Could not open: %s; server may be down; test ignored\n",url);
	exit(0);
    }

    ncstatus = nc_inq_varid(ncid, VAR1, &varid);

    ndim=2;


#ifdef VERBOSE
    printf(" \n");
    printf("********************\n");
    printf("Read %s data w/o strides\n",VAR1);
    printf(" \n");
#endif

    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    start[3] = 0;

    count[0] =  XSIZE;
    count[1] =  YSIZE;

    stride[0] = 1;
    stride[1] = 1;

#ifdef VERBOSE
    for (idim=0; idim<ndim; idim++)
	printf("start[%1d]=%3lu count[%1d]=%3lu stride[%1d]=%3lu\n",
		idim,(unsigned long)start[idim],idim,(unsigned long)count[idim],idim,(unsigned long)stride[idim]);
#endif

    ncstatus = nc_get_vars_float (ncid, varid, start, count, stride, (float*) dat);

#ifdef VERBOSE
    printf(" \n");
    printf("********************\n");
    printf("Print some of %s\n",VAR1);
    printf(" \n");

    for (i=0; i<10; i++)
        printf("%s[%d] = %f\n",VAR1,i,dat[i]);
    printf(" \n");

    for (i=(nelems-11); i<(nelems-1); i++)
        printf("%s[%d] = %f\n",VAR1,i,dat[i]);
    printf(" \n");
    fflush(stdout);
#endif

    memset((void*)dat,0,sizeof(dat));

    /* Read a second variable */

    ncstatus = nc_inq_varid(ncid, VAR2, &varid);

    ndim=2;

#ifdef VERBOSE
    printf(" \n");
    printf("********************\n");
    printf("Read %s data w/o strides\n",VAR2);
    printf(" \n");
    fflush(stdout);
#endif

    start[0] = 0;
    start[1] = 0;
    start[2] = 0;
    start[3] = 0;

    count[0] =  XSIZE;
    count[1] =  YSIZE;

    stride[0] = 1;
    stride[1] = 1;

#ifdef VERBOSE
    for (idim=0; idim<ndim; idim++)
        printf("start[%d]=%3lu count[%d]=%3lu stride[%d]=%3lu\n",
		idim, (unsigned long)start[idim], idim, (unsigned long)count[idim], idim, (unsigned long)stride[idim]);
    fflush(stdout);
#endif

    ncstatus = nc_get_vars_float (ncid, varid, start, count, stride, (float*) dat);

#ifdef VERBOSE
    printf(" \n");
    printf("********************\n");
    printf("Print some of %s\n",VAR2);
    printf(" \n");
    for (i=0; i<10; i++)
        printf("%s[%d] = %f\n",VAR2,i,dat[i]);
    printf(" \n");

    printf(" \n");
    for (i=(nelems-11); i<(nelems-1); i++)
        printf("%s[%d] = %f\n",VAR2,i,dat[i]);
    printf(" \n");
    fflush(stdout);
#endif

    memset((void*)dat,0,sizeof(dat));

    /* close and reopen the dataset, then the below read is correct */

#ifdef VERBOSE
    printf(" \n");
    printf("********************\n");
    printf("Close and reopen the dataset\n");
    fflush(stdout);
#endif

    ncstatus = nc_close (ncid);
    ncstatus = nc_open(url, NC_NOWRITE, &ncid);

    /*  ----------------------------------------------------- */
    /* Read a subset of the data with strides */

    ncstatus = nc_inq_varid(ncid, VAR1, &varid);

#ifdef VERBOSE
    printf(" \n");
    printf("********************\n");
    printf("Read a subset of %s data with strides\n",VAR1);
    printf(" \n");
    fflush(stdout);
#endif

    start[0] = 250;
    start[1] = 704;

    count[0] =  5;
    count[1] =  2;

    stride[0] = 2;
    stride[1] = 4;

#ifdef VERBOSE
    for (idim=0; idim<ndim; idim++)
	printf("start[%d]=%lu count[%d]=%lu stride[%d]=%lu\n",
		idim,(unsigned long)start[idim],idim,(unsigned long)count[idim],idim,(unsigned long)stride[idim]);
    fflush(stdout);
#endif

    memset((void*)sdat,0,sizeof(sdat));
    ncstatus = nc_get_vars_float (ncid, varid, start, count, stride,  (float*) sdat);

    printf("status = %d\n", ncstatus);

    /* Verify that all read values are 50 <= n < 51 */
    for (i=0; i<10; i++) {
	if(sdat[i] <= 50.0 || sdat[i] > 51.0) {
	    printf("Out of range: %s[%d] = %f\n",VAR1, i,sdat[i]);
	    pass = 0;
	}
    }

#ifdef VERBOSE
    printf(" \n");
    printf("********************\n");
    printf("Print  values read. They should all be 50.xxxx \n");
    printf(" \n");

    for (i=0; i<10; i++)
        printf("%s[%d] = %f\n",VAR1,i,sdat[i]);
    fflush(stdout);
#endif

    ncstatus = nc_close (ncid);

    if(!pass) {
	printf("*** FAIL: %s value out of range.\n",VAR1);
	exit(1);
    }

    printf("*** PASS\n");
    free(dat);
    exit(0);

}