1 /*
2 * This tests uses HDF NetCDF APIs to read the NetCDF file test_unlim.nc
3 * generated with the NetCDF Library v3.5 from test_unlim.cdl
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "h4config.h"
10 #ifdef H4_HAVE_NETCDF
11 #include "netcdf.h"
12 #else
13 #include "hdf4_netcdf.h"
14 #endif
15
16 #include "testcdf.h" /* defines in-memory test cdf structure */
17 #include "error.h"
18 #include "tests.h"
19 #include "alloc.h"
20 #include "emalloc.h"
21 #ifdef HDF
22 #include "hdf.h"
23 #endif
24
25 float a_val[2][3] = {
26 {1.0, 2.0, 3.0},
27 {4.0, 5.0, 6.0}
28 };
29 int date_val[12] = {840116, 840214, 840316, 840415, 840516, 840615, 840716, 840816,
30 840915, 841016, 841115, 841216 };
31 int time_val[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
32 short b_val[][3][2] = {
33 {{1, 1}, {2, 2}, {3, 3}},
34 {{4, 4}, {5, 5}, {6, 6}},
35 {{7, 7}, {8, 8}, {9, 9}},
36 {{10, 10}, {11, 11}, {12, 12}},
37 {{13, 13}, {14, 14}, {15, 15}},
38 {{16, 16}, {17, 17}, {18, 18}},
39 {{19, 19}, {20, 20}, {21, 22}},
40 {{23, 23}, {24, 24}, {25, 25}},
41 {{26, 26}, {27, 27}, {28, 28}},
42 {{29, 29}, {30, 30}, {31, 31}},
43 {{32, 32}, {33, 33}, {34, 34}},
44 {{35, 35}, {36, 36}, {37, 37}}
45 };
46 /*
47 * Test ncvarget for variables with unlimited dimensions (bug #897)
48 */
49 void
test_ncvarget_unlim(basefile)50 test_ncvarget_unlim(basefile)
51 char *basefile; /* name of writable netcdf file to open */
52 {
53 int nerrs = 0;
54 static char pname[] = "test_ncvarget_unlim";
55 char testfile[512];
56 char *srcdir = getenv("srcdir");
57 int status;
58 int ncid;
59 int var_id;
60 float a[2][3];
61 int date[12];
62 int time[12];
63 short val[12][3][2];
64 long start[3], count[3];
65 int name_size = 0;
66 int i, j, n;
67
68 (void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
69
70 if (srcdir) {
71 strcpy(testfile, srcdir);
72 if (srcdir[strlen(srcdir) - 1] != '/')
73 strcat(testfile, "/");
74 }
75 else
76 strcpy(testfile, "./");
77
78 strcat(testfile, basefile);
79 if ((ncid = ncopen(testfile, NC_NOWRITE)) == -1) {
80 error("%s: ncopen failed", pname);
81 return;
82 }
83
84 /* Reading 3D array with unlimited dimension */
85
86 var_id = ncvarid( ncid, "b");
87 start[0] = 0;
88 start[1] = 0;
89 start[2] = 0;
90 count[0] = 12;
91 count[1] = 3;
92 count[2] = 2;
93
94 if((status = ncvarget (ncid, var_id, start, count, val)) == -1) {
95 error("%s: ncvarget failed for variable b in ", pname);
96 ncclose(ncid);
97 return;
98 }
99 for (n=0; n <12 ; n++) {
100 for (i=0; i <3; i++) {
101 for (j=0; j<2 ; j++) {
102 if (val[n][i][j] != b_val[n][i][j]) {
103 nerrs++;
104 printf(" Wrong value of variable b at index %d,%d,%d\n", n,i,j);
105 }
106 }
107 }
108 }
109
110 /* Reading 2D array */
111
112 var_id = ncvarid( ncid, "a");
113 start[0] = 0;
114 start[1] = 0;
115 count[0] = 2;
116 count[1] = 3;
117
118 if((status = ncvarget (ncid, var_id, start, count, a)) == -1) {
119 error("%s: ncvarget failed for variable a in ", pname);
120 ncclose(ncid);
121 return;
122 }
123
124 for (i=0; i <2; i++) {
125 for (j=0; j<3 ; j++) {
126 if (a[i][j] != a_val[i][j]) {
127 nerrs++;
128 printf(" Wrong value of variable a at index %d,%d\n", i,j);
129 }
130 }
131 }
132
133
134 /* Reading 1D array with unlimited dimension */
135
136 var_id = ncvarid( ncid, "date");
137 start[0] = 0;
138 count[0] = 12;
139
140 if((status = ncvarget (ncid, var_id, start, count, date)) == -1) {
141 error("%s: ncvarget failed for variable date in ", pname);
142 ncclose(ncid);
143 return;
144 }
145
146 for (n=0; n <12 ; n++) {
147 if (date[n] != date_val[n]) {
148 nerrs++;
149 printf(" Wrong value of variable date at index %d: %d vs %d\n", n, date[n], date_val[n]);
150 }
151 }
152
153 /* Reading 1D array with unlimited dimension */
154
155 var_id = ncvarid( ncid, "time");
156 start[0] = 0;
157 count[0] = 12;
158
159 if((status = ncvarget (ncid, var_id, start, count, time)) == -1) {
160 error("%s: ncvarget failed varaible time in ", pname);
161 ncclose(ncid);
162 return;
163 }
164
165 for (n=0; n <12 ; n++) {
166 if (time[n] != time_val[n]) {
167 nerrs++;
168 printf(" Wrong value of variable time at index %d\n", n);
169 }
170 }
171
172 status = ncclose(ncid);
173
174 if (nerrs > 0)
175 (void) fprintf(stderr,"FAILED! ***\n");
176 else
177 (void) fprintf(stderr,"ok ***\n");
178 }
179