Debian Codegraph @

src/H5Z-SPERR/example/simple_xy_nc4_wr.c

raw
1 /* Copyright 2019 University Corporation for Atmospheric
2 Research/Unidata. See COPYRIGHT file for conditions of use. */
3 /**
4 * @file
5 * Write the simple_xy file, with some of the features of netCDF-4.
6 *
7 * This is a very simple example which is based on the simple_xy
8 * example, but which uses netCDF-4 features, such as
9 * compression. Please see the simple_xy example to learn more about
10 * the netCDF-3 API.
11 *
12 * Like simple_xy_wr.c, this program writes a 2D netCDF variable
13 * (called "data") and fills it with sample data. It has two
14 * dimensions, "x" and "y".
15 *
16 * Full documentation for netCDF can be found at:
17 * https://docs.unidata.ucar.edu/netcdf-c.
18 *
19 * @author Ed Hartnett
20 */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <netcdf.h>
24
25 /*
26 * Include this header so SPERR compression parameters can be encoded.
27 */
28 #include "../include/h5z-sperr.h"
29
30 /* We are writing 2D data, a 12 x 12 grid. */
31 #define NDIMS 2
32 #define NX 12
33 #define NY 12
34
35 /* Handle errors by printing an error message and exiting with a
36 * non-zero status. */
37 #define ERRCODE 2
38 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
39
40 int
41 main()
42 {
43 int ncid, x_dimid, y_dimid, varid;
44 int dimids[NDIMS];
45 size_t chunks[NDIMS];
46 int deflate = 1, deflate_level = 1;
47 float data_out[NX][NY];
48 int x, y, retval;
49
50 /* Create some pretend data. If this wasn't an example program, we
51 * would have some real data to write, for example, model output. */
52 for (x = 0; x < NX; x++)
53 for (y = 0; y < NY; y++)
54 data_out[x][y] = (float)x * NY + y;
55
56 /* Create the file. The NC_NETCDF4 parameter tells netCDF to create
57 * a file in netCDF-4/HDF5 standard. */
58 if ((retval = nc_create("simple_xy_nc4.nc", NC_NETCDF4, &ncid)))
59 ERR(retval);
60
61 /* Define the dimensions. */
62 if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
63 ERR(retval);
64 if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
65 ERR(retval);
66
67 /* Set up variable data. */
68 dimids[0] = x_dimid;
69 dimids[1] = y_dimid;
70 chunks[0] = NX;
71 chunks[1] = NY;
72
73 /* Define the variable. */
74 if ((retval = nc_def_var(ncid, "data", NC_FLOAT, NDIMS, dimids, &varid)))
75 ERR(retval);
76 if ((retval = nc_def_var_chunking(ncid, varid, 0, &chunks[0])))
77 ERR(retval);
78
79 /*
80 * Encode SPERR compression parameters:
81 * 1 == fixed bitrate mode
82 * 6.4 == target bitrate
83 * 0 == no rank swaps
84 * 32028 == registered ID for the SPERR filter.
85 */
86 unsigned int cd_values = H5Z_SPERR_make_cd_values(1, 6.4, 0);
87 if ((retval = nc_def_var_filter(ncid, varid, 32028, 1, &cd_values)))
88 ERR(retval);
89
90 /*
91 * One can also apply another lossless filter, but please make sure that
92 * 1) they are only applied AFTER the SPERR filter, and
93 * 2) the byte shuffle feature is turned off (NC_NOSHUFFLE).
94 */
95 if ((retval = nc_def_var_deflate(ncid, varid, NC_NOSHUFFLE, deflate, deflate_level)))
96 ERR(retval);
97
98 /* No need to explicitly end define mode for netCDF-4 files. Write
99 * the pretend data to the file. */
100 if ((retval = nc_put_var_float(ncid, varid, &data_out[0][0])))
101 ERR(retval);
102
103 /* Close the file. */
104 if ((retval = nc_close(ncid)))
105 ERR(retval);
106
107 printf("*** SUCCESS writing example file simple_xy_nc4.nc!\n");
108 return 0;
109 }