#include <netcdf.h>#include <nc_tests.h>Include dependency graph for tst_compounds.c:

Go to the source code of this file.
Defines | |
| #define | FILE_NAME "tst_compounds.nc" |
| #define | SVC_REC "Service_Record" |
| #define | BATTLES_WITH_KLINGONS "Number_of_Battles_with_Klingons" |
| #define | DATES_WITH_ALIENS "Dates_with_Alien_Hotties" |
| #define | STARDATE "Stardate" |
| #define | DIM_LEN 3 |
| #define | SERVICE_RECORD "Kirk_Service_Record" |
| #define | NUM_DIMENSIONS 7 |
Functions | |
| int | main (int argc, char **argv) |
|
|
Definition at line 15 of file tst_compounds.c. Referenced by main(). |
|
|
Definition at line 16 of file tst_compounds.c. Referenced by main(). |
|
|
Definition at line 18 of file tst_compounds.c. Referenced by main(), test_axis(), and test_system(). |
|
|
Definition at line 13 of file tst_compounds.c. |
|
|
Referenced by main(). |
|
|
Definition at line 19 of file tst_compounds.c. Referenced by main(). |
|
|
Definition at line 17 of file tst_compounds.c. Referenced by main(). |
|
|
Definition at line 14 of file tst_compounds.c. Referenced by main(). |
|
||||||||||||
|
Definition at line 22 of file tst_compounds.c. References BATTLES_WITH_KLINGONS, DATES_WITH_ALIENS, ERR, FILE_NAME, FINAL_RESULTS, len, natts, NC_CHAR, nc_close, nc_create, nc_def_compound(), nc_def_dim, nc_def_var, NC_DOUBLE, NC_FILL_INT64, NC_FILL_UINT, NC_FILL_UINT64, NC_FILL_USHORT, NC_FLOAT, nc_get_att, nc_get_var(), NC_GLOBAL, nc_inq, nc_inq_att, nc_inq_compound(), nc_inq_compound_field(), nc_inq_compound_fieldindex(), nc_inq_compound_fieldname(), nc_inq_compound_fieldoffset(), nc_inq_compound_fieldtype(), nc_inq_compound_name(), nc_inq_compound_nfields(), nc_inq_compound_size(), nc_inq_var, nc_insert_array_compound(), nc_insert_compound(), NC_INT, NC_INT64, NC_MAX_DIMS, NC_MAX_NAME, NC_NETCDF4, nc_open, nc_put_att, nc_put_var(), NC_UBYTE, NC_UINT, NC_UINT64, NC_USHORT, NC_WRITE, ncid, ndims, NUM_DIMENSIONS, nvars, SERVICE_RECORD, size, STARDATE, SUMMARIZE_ERR, and SVC_REC. 00023 { 00024 int ncid, typeid, varid; 00025 size_t nfields; 00026 int dimid; 00027 int ndims, nvars, natts, unlimdimid; 00028 char name[NC_MAX_NAME + 1]; 00029 size_t size, len; 00030 nc_type xtype, field_xtype; 00031 int dimids[] = {0}, fieldid; 00032 int field_ndims, field_sizes[NC_MAX_DIMS]; 00033 size_t offset; 00034 int i, j; 00035 00036 /* The following structs are written and read as compound types by 00037 * the tests below. */ 00038 struct s1 00039 { 00040 int i1; 00041 int i2; 00042 }; 00043 struct s1 data[DIM_LEN], data_in[DIM_LEN]; 00044 00045 /* StarFleet Medical Record. */ 00046 struct sf_med_rec 00047 { 00048 unsigned char num_heads; 00049 unsigned short num_arms; 00050 unsigned int num_toes; 00051 long long ago; /* in a galaxy far far away... */ 00052 unsigned long long num_hairs; 00053 }; 00054 struct sf_med_rec med_data_out[DIM_LEN], med_data_in[DIM_LEN]; 00055 00056 /* Since some aliens exists in different, or more than one, 00057 * dimensions, StarFleet keeps track of the dimensional abilities 00058 * of everyone on 7 dimensions. */ 00059 #define NUM_DIMENSIONS 7 00060 struct dim_rec 00061 { 00062 int starfleet_id; 00063 int abilities[NUM_DIMENSIONS]; 00064 }; 00065 struct dim_rec dim_data_out[DIM_LEN]; 00066 00067 /* StarFleet Human Resources Department has data records for all 00068 * employees. */ 00069 struct hr_rec 00070 { 00071 int starfleet_id; 00072 struct s1 svc_rec; 00073 char name[NC_MAX_NAME + 1]; 00074 float max_temp, min_temp; /* temperature range */ 00075 double percent_transporter_errosion; 00076 }; 00077 struct hr_rec hr_data_out[DIM_LEN], hr_data_in[DIM_LEN]; 00078 00079 /* Create some phoney data. */ 00080 for (i=0; i<DIM_LEN; i++) 00081 { 00082 data[i].i1 = 5; 00083 data[i].i2 = 10; 00084 /* medical data */ 00085 med_data_out[i].num_heads = 254; 00086 med_data_out[i].num_arms = NC_FILL_USHORT - 1; 00087 med_data_out[i].num_toes = NC_FILL_UINT - 1; 00088 med_data_out[i].ago = NC_FILL_INT64 + 1; 00089 med_data_out[i].num_hairs = NC_FILL_UINT64 - 1; 00090 /* Dimensional data. */ 00091 dim_data_out[i].starfleet_id = i; 00092 for (j = 0; j < NUM_DIMENSIONS; j++) 00093 dim_data_out[i].abilities[j] = j; 00094 /* hr data */ 00095 hr_data_out[i].starfleet_id = i; 00096 hr_data_out[i].svc_rec = data[i]; 00097 if (sprintf(hr_data_out[i].name, "alien_%d", i) < 0) ERR; 00098 hr_data_out[i].max_temp = 99.99; 00099 hr_data_out[i].min_temp = -9.99; 00100 hr_data_out[i].percent_transporter_errosion = .030303; 00101 } 00102 00103 /*nc_set_log_level(3);*/ 00104 00105 printf("\n*** Testing netcdf-4 user defined type functions.\n"); 00106 printf("*** testing simple compound variable create..."); 00107 { 00108 /* Create a file with a compound type. Write a little data. */ 00109 if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; 00110 if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR; 00111 if (nc_inq_compound(ncid, typeid, name, &size, &nfields)) ERR; 00112 if (nfields) ERR; 00113 if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS, 00114 HOFFSET(struct s1, i1), NC_INT)) ERR; 00115 if (nc_insert_compound(ncid, typeid, DATES_WITH_ALIENS, 00116 HOFFSET(struct s1, i2), NC_INT)) ERR; 00117 if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR; 00118 if (nc_def_var(ncid, SERVICE_RECORD, typeid, 1, dimids, &varid)) ERR; 00119 if (nc_put_var(ncid, varid, data)) ERR; 00120 if (nc_close(ncid)) ERR; 00121 00122 /* Open the file and take a peek. */ 00123 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; 00124 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; 00125 if (ndims != 1 || nvars != 1 || natts != 0 || unlimdimid != -1) ERR; 00126 if (nc_close(ncid)) ERR; 00127 00128 /* Reopen the file and take a more detailed look at the compound 00129 * type. */ 00130 { 00131 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; 00132 if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR; 00133 if (strcmp(name, SERVICE_RECORD) || ndims != 1 || natts != 0 || dimids[0] != 0) ERR; 00134 if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR; 00135 if (nfields != 2 || size != 8 || strcmp(name, SVC_REC)) ERR; 00136 if (nc_inq_compound_name(ncid, xtype, name)) ERR; 00137 if (strcmp(name, SVC_REC)) ERR; 00138 if (nc_inq_compound_size(ncid, xtype, &size)) ERR; 00139 if (size != 8) ERR; 00140 if (nc_inq_compound_nfields(ncid, xtype, &nfields)) ERR; 00141 if (nfields != 2) ERR; 00142 if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00143 if (strcmp(name, BATTLES_WITH_KLINGONS) || offset != 0 || (field_xtype != NC_INT && field_ndims != 0)) ERR; 00144 if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00145 if (strcmp(name, DATES_WITH_ALIENS) || offset != 4 || field_xtype != NC_INT) ERR; 00146 if (nc_inq_compound_fieldname(ncid, xtype, 1, name)) ERR; 00147 if (strcmp(name, DATES_WITH_ALIENS)) ERR; 00148 if (nc_inq_compound_fieldindex(ncid, xtype, BATTLES_WITH_KLINGONS, &fieldid)) ERR; 00149 if (fieldid != 0) ERR; 00150 if (nc_inq_compound_fieldoffset(ncid, xtype, 1, &offset)) ERR; 00151 if (offset != 4) ERR; 00152 if (nc_inq_compound_fieldtype(ncid, xtype, 1, &field_xtype)) ERR; 00153 if (field_xtype != NC_INT) ERR; 00154 if (nc_get_var(ncid, varid, data_in)) ERR; 00155 for (i=0; i<DIM_LEN; i++) 00156 if (data[i].i1 != data_in[i].i1 || data[i].i2 != data_in[i].i2) ERR; 00157 if (nc_close(ncid)) ERR; 00158 } 00159 } 00160 00161 SUMMARIZE_ERR; 00162 printf("*** testing simple compound attribute create..."); 00163 00164 { 00165 /* Create a file with a global attribute of compound type. */ 00166 if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; 00167 if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR; 00168 if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS, 00169 HOFFSET(struct s1, i1), NC_INT)) ERR; 00170 if (nc_insert_compound(ncid, typeid, DATES_WITH_ALIENS, 00171 HOFFSET(struct s1, i2), NC_INT)) ERR; 00172 if (nc_put_att(ncid, NC_GLOBAL, SERVICE_RECORD, typeid, 3, data)) ERR; 00173 if (nc_close(ncid)) ERR; 00174 00175 /* Open the file and take a peek. */ 00176 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; 00177 if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR; 00178 if (ndims != 0 || nvars != 0 || natts != 1 || unlimdimid != -1) ERR; 00179 if (nc_close(ncid)) ERR; 00180 00181 /* Reopen the file and take a more detailed look at the compound 00182 * type. */ 00183 { 00184 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; 00185 if (nc_get_att(ncid, NC_GLOBAL, SERVICE_RECORD, data_in)) ERR; 00186 for (i=0; i<DIM_LEN; i++) 00187 if (data[i].i1 != data_in[i].i1 || data[i].i2 != data_in[i].i2) ERR;; 00188 if (nc_inq_att(ncid, NC_GLOBAL, SERVICE_RECORD, &xtype, &len)) ERR; 00189 if (len != 3) ERR; 00190 if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR; 00191 if (nfields != 2 || size != 8 || strcmp(name, SVC_REC)) ERR; 00192 if (nc_inq_compound_nfields(ncid, xtype, &nfields)) ERR; 00193 if (nfields != 2) ERR; 00194 if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00195 if (strcmp(name, BATTLES_WITH_KLINGONS) || offset != 0 || field_xtype != NC_INT) ERR; 00196 if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00197 if (strcmp(name, DATES_WITH_ALIENS) || offset != 4 || field_xtype != NC_INT) ERR; 00198 if (nc_inq_compound_fieldindex(ncid, xtype, BATTLES_WITH_KLINGONS, &fieldid)) ERR; 00199 if (fieldid != 0) ERR; 00200 if (nc_inq_compound_fieldoffset(ncid, xtype, 1, &offset)) ERR; 00201 if (offset != 4) ERR; 00202 if (nc_inq_compound_fieldtype(ncid, xtype, 1, &field_xtype)) ERR; 00203 if (field_xtype != NC_INT) ERR; 00204 if (nc_close(ncid)) ERR; 00205 } 00206 } 00207 00208 SUMMARIZE_ERR; 00209 printf("*** testing compound variable with new atomic types..."); 00210 { 00211 /* Create a file with a compound type. Write a little data. */ 00212 if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; 00213 if (nc_def_compound(ncid, sizeof(struct sf_med_rec), 00214 "SFMedRec", &typeid)) ERR; 00215 if (nc_insert_compound(ncid, typeid, "num_heads", 00216 HOFFSET(struct sf_med_rec, num_heads), NC_UBYTE)) ERR; 00217 if (nc_insert_compound(ncid, typeid, "num_arms", 00218 HOFFSET(struct sf_med_rec, num_arms), NC_USHORT)) ERR; 00219 if (nc_insert_compound(ncid, typeid, "num_toes", 00220 HOFFSET(struct sf_med_rec, num_toes), NC_UINT)) ERR; 00221 if (nc_insert_compound(ncid, typeid, "ago", 00222 HOFFSET(struct sf_med_rec, ago), NC_INT64)) ERR; 00223 if (nc_insert_compound(ncid, typeid, "num_hairs", 00224 HOFFSET(struct sf_med_rec, num_hairs), NC_UINT64)) ERR; 00225 if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR; 00226 if (nc_def_var(ncid, "starbase_13", typeid, 1, dimids, &varid)) ERR; 00227 if (nc_put_var(ncid, varid, med_data_out)) ERR; 00228 if (nc_close(ncid)) ERR; 00229 00230 /* Open the file and take a look. */ 00231 { 00232 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; 00233 if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR; 00234 if (strcmp(name, "starbase_13") || ndims != 1 || natts != 0 || dimids[0] != 0) ERR; 00235 if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR; 00236 if (nfields != 5 || size != sizeof(struct sf_med_rec) || strcmp(name, "SFMedRec")) ERR; 00237 if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00238 if (strcmp(name, "num_heads") || offset != 0 || field_xtype != NC_UBYTE) ERR; 00239 if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00240 if (strcmp(name, "num_arms") || offset != HOFFSET(struct sf_med_rec, num_arms) || 00241 field_xtype != NC_USHORT) ERR; 00242 if (nc_inq_compound_field(ncid, xtype, 2, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00243 if (strcmp(name, "num_toes") || offset != HOFFSET(struct sf_med_rec, num_toes) || 00244 field_xtype != NC_UINT) ERR; 00245 if (nc_inq_compound_field(ncid, xtype, 3, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00246 if (strcmp(name, "ago") || offset != HOFFSET(struct sf_med_rec, ago) || 00247 field_xtype != NC_INT64) ERR; 00248 if (nc_inq_compound_field(ncid, xtype, 4, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00249 if (strcmp(name, "num_hairs") || offset != HOFFSET(struct sf_med_rec, num_hairs) || 00250 field_xtype != NC_UINT64) ERR; 00251 if (nc_get_var(ncid, varid, med_data_in)) ERR; 00252 for (i=0; i<DIM_LEN; i++) 00253 if (med_data_in[i].num_heads != med_data_out[i].num_heads || 00254 med_data_in[i].num_arms != med_data_out[i].num_arms || 00255 med_data_in[i].num_toes != med_data_out[i].num_toes || 00256 med_data_in[i].ago != med_data_out[i].ago || 00257 med_data_in[i].num_hairs != med_data_out[i].num_hairs) ERR; 00258 if (nc_close(ncid)) ERR; 00259 } 00260 } 00261 00262 SUMMARIZE_ERR; 00263 printf("*** testing compound variable containing an array of ints..."); 00264 { 00265 int dim_sizes[] = {NUM_DIMENSIONS}; 00266 00267 /* Create a file with a compound type which contains an array of 00268 * int. Write a little data. */ 00269 if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; 00270 if (nc_def_compound(ncid, sizeof(struct dim_rec), "SFDimRec", &typeid)) ERR; 00271 if (nc_insert_compound(ncid, typeid, "starfleet_id", 00272 HOFFSET(struct dim_rec, starfleet_id), NC_INT)) ERR; 00273 if (nc_insert_array_compound(ncid, typeid, "abilities", 00274 HOFFSET(struct dim_rec, abilities), NC_INT, 1, dim_sizes)) ERR; 00275 if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR; 00276 if (nc_def_var(ncid, "dimension_data", typeid, 1, dimids, &varid)) ERR; 00277 if (nc_put_var(ncid, varid, dim_data_out)) ERR; 00278 if (nc_close(ncid)) ERR; 00279 00280 /* Open the file and take a look. */ 00281 { 00282 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; 00283 if (nc_inq_var(ncid, 0, name, &xtype, &ndims, dimids, &natts)) ERR; 00284 /* if (strcmp(name, "starbase_13") || ndims != 1 || natts != 0 || dimids[0] != 0) ERR; 00285 if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR; 00286 if (nfields != 5 || size != sizeof(struct sf_med_rec) || strcmp(name, "SFMedRec")) ERR; 00287 if (nc_get_var(ncid, varid, med_data_in)) ERR; 00288 for (i=0; i<DIM_LEN; i++) 00289 if (med_data_in[i].num_heads != med_data_out[i].num_heads || 00290 med_data_in[i].num_arms != med_data_out[i].num_arms || 00291 med_data_in[i].num_toes != med_data_out[i].num_toes || 00292 med_data_in[i].ago != med_data_out[i].ago || 00293 med_data_in[i].num_hairs != med_data_out[i].num_hairs) ERR;*/ 00294 if (nc_close(ncid)) ERR; 00295 } 00296 } 00297 00298 SUMMARIZE_ERR; 00299 printf("*** testing compound var containing compound type..."); 00300 00301 { 00302 nc_type svc_recid, hr_recid; 00303 00304 /* Create a file with a nested compound type attribute and variable. */ 00305 if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; 00306 00307 /* Define the inner type first. */ 00308 if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &svc_recid)) ERR; 00309 if (nc_insert_compound(ncid, svc_recid, BATTLES_WITH_KLINGONS, 00310 HOFFSET(struct s1, i1), NC_INT)) ERR; 00311 if (nc_insert_compound(ncid, svc_recid, DATES_WITH_ALIENS, 00312 HOFFSET(struct s1, i2), NC_INT)) ERR; 00313 00314 /* Now define the containing type. */ 00315 if (nc_def_compound(ncid, sizeof(struct hr_rec), "SF_HR_Record", &hr_recid)) ERR; 00316 if (nc_insert_compound(ncid, hr_recid, "StarFleet_ID", 00317 HOFFSET(struct hr_rec, starfleet_id), NC_INT)) ERR; 00318 if (nc_insert_compound(ncid, hr_recid, "Service_Record", 00319 HOFFSET(struct hr_rec, svc_rec), svc_recid)) ERR; 00320 if (nc_insert_compound(ncid, hr_recid, "Name", 00321 HOFFSET(struct hr_rec, name), NC_CHAR)) ERR; 00322 if (nc_insert_compound(ncid, hr_recid, "Max_Temp", 00323 HOFFSET(struct hr_rec, max_temp), NC_FLOAT)) ERR; 00324 if (nc_insert_compound(ncid, hr_recid, "Min_Temp", 00325 HOFFSET(struct hr_rec, min_temp), NC_FLOAT)) ERR; 00326 if (nc_insert_compound(ncid, hr_recid, "Percent_Transporter_Erosian", 00327 HOFFSET(struct hr_rec, percent_transporter_errosion), 00328 NC_DOUBLE)) ERR; 00329 00330 /* Write it as an attribute. */ 00331 if (nc_put_att(ncid, NC_GLOBAL, "HR_Records", hr_recid, DIM_LEN, 00332 hr_data_out)) ERR; 00333 if (nc_close(ncid)) ERR; 00334 00335 /* Reopen the file and take detailed look at the compound 00336 * attribute. */ 00337 { 00338 /* Read the att and check values. */ 00339 if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; 00340 if (nc_get_att(ncid, NC_GLOBAL, "HR_Records", hr_data_in)) ERR; 00341 for (i=0; i<DIM_LEN; i++) 00342 { 00343 if (hr_data_in[i].starfleet_id != hr_data_out[i].starfleet_id || 00344 hr_data_in[i].svc_rec.i1 != hr_data_out[i].svc_rec.i1 || 00345 hr_data_in[i].svc_rec.i2 != hr_data_out[i].svc_rec.i2 || 00346 strcmp(hr_data_in[i].name, hr_data_out[i].name) || 00347 hr_data_in[i].max_temp != hr_data_out[i].max_temp || 00348 hr_data_in[i].min_temp != hr_data_out[i].min_temp || 00349 hr_data_in[i].percent_transporter_errosion != 00350 hr_data_out[i].percent_transporter_errosion) ERR; 00351 } 00352 00353 /* Use the inq functions to learn about nested compound type. */ 00354 if (nc_inq_att(ncid, NC_GLOBAL, "HR_Records", &xtype, &len)) ERR; 00355 if (len != DIM_LEN) ERR; 00356 if (nc_inq_compound(ncid, xtype, name, &size, &nfields)) ERR; 00357 if (nfields != 6 || size != sizeof(struct hr_rec) || strcmp(name, "SF_HR_Record")) ERR; 00358 if (nc_inq_compound_field(ncid, xtype, 0, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00359 if (strcmp(name, "StarFleet_ID") || offset != 0 || field_xtype != NC_INT) ERR; 00360 if (nc_inq_compound_field(ncid, xtype, 1, name, &offset, &field_xtype, &field_ndims, field_sizes)) ERR; 00361 if (strcmp(name, "Service_Record") || offset != HOFFSET(struct hr_rec, svc_rec)) ERR; 00362 /* Check the internal compound type. */ 00363 00364 /* Finish checking the containing compound type. */ 00365 if (nc_close(ncid)) ERR; 00366 } 00367 } 00368 00369 SUMMARIZE_ERR; 00370 00371 FINAL_RESULTS; 00372 }
|
1.4.4