Merge branch 'release-v4.6.0' of github.com:wrf-model/WRF
[WRF.git] / tools / nc4_test.c
blob26ff9641aadc40245fcb47c0ff10016ccd5c7423
1 #include <stdio.h>
2 #include "netcdf.h"
4 #define ERR_RET do { \
5 fflush(stdout); /* Make sure our stdout is synced with stderr. */ \
6 fprintf(stderr, "Sorry! Unexpected result, %s, line: %d\n", \
7 __FILE__, __LINE__); \
8 return -1; \
9 } while (0)
11 #define NDIMS 3
12 #define FILE_NAME "nc4_test.nc"
13 #define X_LEN 120
14 #define Y_LEN 64
15 #define Z_LEN 128
17 int main(void)
19 float data[X_LEN * Y_LEN * Z_LEN];
20 int i, ncmode, ncid, dimids[NDIMS], var;
21 size_t start[NDIMS] = {0, 0, 0};
22 size_t count[NDIMS] = {X_LEN, Y_LEN, Z_LEN};
23 ptrdiff_t stride[NDIMS] = {1, 1, 1};
25 /* Initialize data. */
26 for (i = 0; i < (X_LEN * Y_LEN * Z_LEN); i++)
27 data[i] = i;
29 printf("*** Testing netcdf-4 writes with compressed data...\n");
31 ncmode = NC_CLOBBER|NC_NETCDF4;
33 if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR_RET;
34 if (nc_def_dim(ncid, "time", X_LEN, &dimids[0])) ERR_RET;
35 if (nc_def_dim(ncid, "lat", Y_LEN, &dimids[1])) ERR_RET;
36 if (nc_def_dim(ncid, "lon", Z_LEN, &dimids[2])) ERR_RET;
37 if (nc_def_var(ncid, "test", NC_FLOAT, NDIMS, dimids, &var)) ERR_RET;
38 if (nc_def_var_deflate(ncid, var, 1, 1, 2)) ERR_RET;
39 if (nc_enddef(ncid)) ERR_RET;
40 if (nc_put_vars_float(ncid, var, start, count, stride, data)) ERR_RET;
41 if (nc_close(ncid)) ERR_RET;
43 printf("*** Tests successful!\n");
45 return 0;