NetCDF  4.9.2
simple_xy_nc4_wr.c
Go to the documentation of this file.
1 /* Copyright 2019 University Corporation for Atmospheric
2  Research/Unidata. See COPYRIGHT file for conditions of use. */
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <netcdf.h>
24 
25 /* This is the name of the data file we will create. */
26 #define FILE_NAME "simple_xy_nc4.nc"
27 
28 /* We are writing 2D data, a 6 x 12 grid. */
29 #define NDIMS 2
30 #define NX 6
31 #define NY 12
32 
33 /* Handle errors by printing an error message and exiting with a
34  * non-zero status. */
35 #define ERRCODE 2
36 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
37 
38 int
39 main()
40 {
41  int ncid, x_dimid, y_dimid, varid;
42  int dimids[NDIMS];
43  size_t chunks[NDIMS];
44  int shuffle, deflate, deflate_level;
45  int data_out[NX][NY];
46  int x, y, retval;
47 
48  /* Set chunking, shuffle, and deflate. */
49  shuffle = NC_SHUFFLE;
50  deflate = 1;
51  deflate_level = 1;
52 
53  /* Create some pretend data. If this wasn't an example program, we
54  * would have some real data to write, for example, model output. */
55  for (x = 0; x < NX; x++)
56  for (y = 0; y < NY; y++)
57  data_out[x][y] = x * NY + y;
58 
59  /* Create the file. The NC_NETCDF4 parameter tells netCDF to create
60  * a file in netCDF-4/HDF5 standard. */
61  if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
62  ERR(retval);
63 
64  /* Define the dimensions. */
65  if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
66  ERR(retval);
67  if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
68  ERR(retval);
69 
70  /* Set up variable data. */
71  dimids[0] = x_dimid;
72  dimids[1] = y_dimid;
73  chunks[0] = NX/4;
74  chunks[1] = NY/4;
75 
76  /* Define the variable. */
77  if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
78  dimids, &varid)))
79  ERR(retval);
80  if ((retval = nc_def_var_chunking(ncid, varid, 0, &chunks[0])))
81  ERR(retval);
82  if ((retval = nc_def_var_deflate(ncid, varid, shuffle, deflate,
83  deflate_level)))
84  ERR(retval);
85 
86  /* No need to explicitly end define mode for netCDF-4 files. Write
87  * the pretend data to the file. */
88  if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
89  ERR(retval);
90 
91  /* Close the file. */
92  if ((retval = nc_close(ncid)))
93  ERR(retval);
94 
95  printf("*** SUCCESS writing example file simple_xy_nc4.nc!\n");
96  return 0;
97 }
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1302
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition: dfile.c:400
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition: ddim.c:121
EXTERNL int nc_def_var_deflate(int ncid, int varid, int shuffle, int deflate, int deflate_level)
Set the zlib compression and shuffle settings for a variable in an netCDF/HDF5 file.
Definition: dvar.c:461
int nc_put_var_int(int ncid, int varid, const int *op)
Write an entire variable with one call.
Definition: dvarput.c:950
EXTERNL int nc_def_var(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
Definition: dvar.c:214
EXTERNL int nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
Define storage and, if chunked storage is used, chunking parameters for a variable.
Definition: dvar.c:729
Main header file for the C API.
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
Definition: netcdf.h:153
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
#define NC_SHUFFLE
Control the HDF5 shuffle filter.
Definition: netcdf.h:326