File: tst_fills2.c

package info (click to toggle)
netcdf 1%3A4.1.3-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,032 kB
  • sloc: ansic: 169,389; fortran: 17,742; sh: 13,203; cpp: 10,960; f90: 7,903; yacc: 2,832; xml: 2,129; makefile: 2,027; lex: 1,210
file content (296 lines) | stat: -rw-r--r-- 13,272 bytes parent folder | download | duplicates (2)
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
/* This is part of the netCDF package. Copyright 2008 University
   Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
   conditions of use. See www.unidata.ucar.edu for more info.

   Create a test file with default fill values for variables of each type.

   $Id: tst_fills2.c 981 2011-03-15 10:19:08Z ed $
*/

#include <nc_tests.h>
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>

#define FILE_NAME "tst_fills2.nc" 

int
main(int argc, char **argv) 
{			/* create tst_classic_fills.nc */
   printf("\n*** Testing fill values.\n");
   printf("*** testing read of string record var with no data...");
   {
#define STRING_VAR_NAME "blood_toil_tears_sweat"
#define NDIMS_STRING 1
#define DATA_START 1 /* Real data here. */

      int  ncid, varid, dimid, varid_in;
      const char *data_out[1] = {
	 "We have before us an ordeal of the most grievous kind. We have before "
	 "us many, many long months of struggle and of suffering. You ask, what "
	 "is our policy? I can say: It is to wage war, by sea, land and air, "
	 "with all our might and with all the strength that God can give us; to "
	 "wage war against a monstrous tyranny, never surpassed in the dark, "
	 "lamentable catalogue of human crime. That is our policy. You ask, what "
	 "is our aim? "
	 "I can answer in one word: It is victory, victory at all costs, victory "
	 "in spite of all terror, victory, however long and hard the road may "
	 "be; for without victory, there is no survival."};
      char *data_in;
      size_t index = DATA_START;

      /* Create file with a 1D string var. Set its fill value to the
       * empty string. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, "sentence", NC_UNLIMITED, &dimid)) ERR;
      if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, 
		     &dimid, &varid)) ERR;

      /* Check it out. */
      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR;

      /* Write one string, leaving some blank records which will then
       * get the fill value. */
      if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR;

      /* Get all the data from the variable. */
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

      /* Now re-open file, read data, and check values again. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;

      data_in = NULL;
      if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR;
      if (strcmp(data_in, data_out[0])) ERR;
      free(data_in);

      if (nc_close(ncid)) ERR;

/*       free(data_in); */
   }
   SUMMARIZE_ERR;
/*    printf("*** testing read of string record var with no data..."); */
/*    { */
/* #define STRING_VAR_NAME "I_Have_A_Dream" */
/* #define NDIMS_STRING 1 */
/* #define FILLVALUE_LEN 1 /\* There is 1 string, the empty one. *\/ */
/* #define DATA_START 2 /\* Real data here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       const char *missing_val[FILLVALUE_LEN] = {""}; */
/*       const char *missing_val_in[FILLVALUE_LEN]; */
/*       const char *data_out[1] = { */
/* 	 "With this faith, we will be able to hew out of the mountain of " */
/* 	 "despair a stone of hope. With this faith, we will be able to " */
/* 	 "transform the jangling discords of our nation into a beautiful " */
/* 	 "symphony of brotherhood. With this faith, we will be able to work " */
/* 	 "together, to pray together, to struggle together, to go to jail " */
/* 	 "together, to stand up for freedom together, knowing that we will " */
/* 	 "be free one day."}; */
/*       char *data_in; */
/*       size_t index = DATA_START; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "sentence", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING,  */
/* 		     &dimid, &varid)) ERR; */
/* /\*      if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN,  */
/* 	missing_val)) ERR;*\/ */

/*       /\* Check it out. *\/ */
/*       /\* if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; *\/ */
/*       /\* if (nc_get_att_string(ncid, varid_in, "_FillValue",  *\/ */
/*       /\* 			    (char **)missing_val_in)) ERR; *\/ */
/*       /\* if (strcmp(missing_val[0], missing_val_in[0])) ERR; *\/ */
/*       /\* if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR; *\/ */

/*       /\* Write one string, leaving some blank records which will then */
/*        * get the fill value. *\/ */
/*       if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR; */

/*       /\* Get all the data from the variable. *\/ */
/*       if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR; */
/*       if (strcmp(data_in, data_out[0])) ERR; */
/*       free(data_in); */

/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, read data, and check values again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*       /\* if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; *\/ */
/*       /\* if (nc_get_att_string(ncid, varid_in, "_FillValue",  *\/ */
/*       /\* 			    (char **)missing_val_in)) ERR; *\/ */
/*       /\* if (strcmp(missing_val[0], missing_val_in[0])) ERR; *\/ */
/*       /\* if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR; *\/ */

/*       data_in = NULL; */
/*       if (nc_get_var1_string(ncid, varid_in, &index, &data_in)) ERR; */
/*       if (strcmp(data_in, data_out[0])) ERR; */
/*       free(data_in); */

/*       if (nc_close(ncid)) ERR; */

/* /\*       free(data_in); *\/ */
/*    } */
/*    SUMMARIZE_ERR; */
/*    printf("*** testing empty fill values of a string var..."); */
/*    { */
/* #define STRING_VAR_NAME "The_String" */
/* #define NDIMS_STRING 1 */
/* #define FILLVALUE_LEN 1 /\* There is 1 string, the empty one. *\/ */
/* #define DATA_START 2 /\* Real data here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       const char *missing_val[FILLVALUE_LEN] = {""}; */
/*       const char *missing_val_in[FILLVALUE_LEN]; */
/*       const char *data_out[1] = {"The evil that men do lives after them; " */
/* 				 "the good is oft interred with their bones."}; */
/*       char **data_in; */
/*       size_t index = DATA_START; */
/*       int i; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN, missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in)) ERR; */

/*       /\* Write one string, leaving some blank records which will then */
/*        * get the fill value. *\/ */
/*       if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR; */

/*       /\* Get all the data from the variable. *\/ */
/*       if (!(data_in = malloc((DATA_START + 1) * sizeof(char *)))) ERR; */
/*       if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */

/*       /\* First there should be fill values, then the data value we */
/*        * wrote. *\/ */
/*       for (i = 0; i < DATA_START; i++) */
/* 	 if (strcmp(data_in[i], "")) ERR; */
/*       if (strcmp(data_in[DATA_START], data_out[0])) ERR; */

/*       /\* Close everything up. Don't forget to free the string! *\/ */
/*       if (nc_free_string(DATA_START + 1, data_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, read data, and check values again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*      if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       /\*if (nc_free_string(FILLVALUE_LEN, (char **)missing_val_in[0])) ERR;*\/ */
/*       if (nc_close(ncid)) ERR; */
/*        free(data_in);  */
/*    } */

/*    SUMMARIZE_ERR; */
/*    printf("*** testing non-empty fill values of a string var..."); */
/*    { */
/* #define STRING_VAR_NAME2 "CASSIUS" */
/* #define FILLVALUE_LEN2 1 /\* There is 1 string in the fillvalue array. *\/ */
/* #define DATA_START2 9 /\* Real data starts here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       const char *missing_val[FILLVALUE_LEN2] = {"I know that virtue to be in you, Brutus"}; */
/*       const char *missing_val_in[FILLVALUE_LEN2]; */
/*       const char *data_out[1] = {"The evil that men do lives after them; " */
/* 				 "the good is oft interred with their bones."}; */
/*       char **data_in; */
/*       size_t index = DATA_START2; */
/*       int i; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * a non-empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME2, NC_STRING, NDIMS_STRING, &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN2, missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR; */

/*       /\* Write one string, leaving some blank records which will then */
/*        * get the fill value. *\/ */
/*       if (nc_put_var1_string(ncid, varid_in, &index, data_out)) ERR; */

/*       /\* Get all the data from the variable. *\/ */
/*       if (!(data_in = malloc((DATA_START2 + 1) * sizeof(char *)))) ERR; */
/*       if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */

/*       /\* First there should be fill values, then the data value we */
/*        * wrote. *\/ */
/*       for (i = 0; i < DATA_START2; i++) */
/* 	 if (strcmp(data_in[i], missing_val[0])) ERR; */
/*       if (strcmp(data_in[DATA_START2], data_out[0])) ERR; */

/*       /\* Close everything up. *\/ */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, read data, and check values again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME2, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", (char **)missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in[0])) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN2, (char **)missing_val_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */
/*       free(data_in); */
/*    } */
/*    printf("*** testing read of string record var with no data..."); */
/*    { */
/* #define STRING_VAR_NAME "Moon_Is_A_Harsh_Mistress" */
/* #define NDIMS_STRING 1 */
/* #define FILLVALUE_LEN 1 /\* There is 1 string, the empty one. *\/ */
/* #define DATA_START 2 /\* Real data here. *\/ */

/*       int  ncid, varid, dimid, varid_in; */
/*       char *missing_val[FILLVALUE_LEN] = {""}; */
/*       char *missing_val_in; */

/*       /\* Create file with a 1D string var. Set its fill value to the */
/*        * empty string. *\/ */
/*       if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/*       if (nc_def_dim(ncid, "Lunar_Years", NC_UNLIMITED, &dimid)) ERR; */
/*       if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, NDIMS_STRING,  */
/* 		     &dimid, &varid)) ERR; */
/*       if (nc_put_att_string(ncid, varid, "_FillValue", FILLVALUE_LEN,  */
/* 			    missing_val)) ERR; */

/*       /\* Check it out. *\/ */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", &missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in)) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, &missing_val_in)) ERR; */

/*       /\* Get all the data from the variable. There is none! *\/ */
/*       if (nc_get_var_string(ncid, varid_in, NULL)) ERR; */
      
/*       /\* Close file. *\/ */
/*       if (nc_close(ncid)) ERR; */

/*       /\* Now re-open file, and check again. *\/ */
/*       if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/*       if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/*       if (nc_get_att_string(ncid, varid_in, "_FillValue", &missing_val_in)) ERR; */
/*       if (strcmp(missing_val[0], missing_val_in)) ERR; */
/*       if (nc_free_string(FILLVALUE_LEN, &missing_val_in)) ERR; */
/*       if (nc_close(ncid)) ERR; */
/*    } */
/*    SUMMARIZE_ERR; */
   FINAL_RESULTS;
}