NetCDF  4.6.3
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
simple_xy_nc4_wr.c
Go to the documentation of this file.
1 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <netcdf.h>
18 
19 /* This is the name of the data file we will create. */
20 #define FILE_NAME "simple_xy_nc4.nc"
21 
22 /* We are writing 2D data, a 6 x 12 grid. */
23 #define NDIMS 2
24 #define NX 6
25 #define NY 12
26 
27 /* Handle errors by printing an error message and exiting with a
28  * non-zero status. */
29 #define ERRCODE 2
30 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
31 
32 int
33 main()
34 {
35  int ncid, x_dimid, y_dimid, varid;
36  int dimids[NDIMS];
37  size_t chunks[NDIMS];
38  int shuffle, deflate, deflate_level;
39  int data_out[NX][NY];
40  int x, y, retval;
41 
42  /* Set chunking, shuffle, and deflate. */
43  shuffle = NC_SHUFFLE;
44  deflate = 1;
45  deflate_level = 1;
46 
47  /* Create some pretend data. If this wasn't an example program, we
48  * would have some real data to write, for example, model output. */
49  for (x = 0; x < NX; x++)
50  for (y = 0; y < NY; y++)
51  data_out[x][y] = x * NY + y;
52 
53  /* Create the file. The NC_NETCDF4 parameter tells netCDF to create
54  * a file in netCDF-4/HDF5 standard. */
55  if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
56  ERR(retval);
57 
58  /* Define the dimensions. */
59  if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
60  ERR(retval);
61  if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
62  ERR(retval);
63 
64  /* Set up variable data. */
65  dimids[0] = x_dimid;
66  dimids[1] = y_dimid;
67  chunks[0] = NX/4;
68  chunks[1] = NY/4;
69 
70  /* Define the variable. */
71  if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
72  dimids, &varid)))
73  ERR(retval);
74  if ((retval = nc_def_var_chunking(ncid, varid, 0, &chunks[0])))
75  ERR(retval);
76  if ((retval = nc_def_var_deflate(ncid, varid, shuffle, deflate,
77  deflate_level)))
78  ERR(retval);
79 
80  /* No need to explicitly end define mode for netCDF-4 files. Write
81  * the pretend data to the file. */
82  if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
83  ERR(retval);
84 
85  /* Close the file. */
86  if ((retval = nc_close(ncid)))
87  ERR(retval);
88 
89  printf("*** SUCCESS writing example file simple_xy_nc4.nc!\n");
90  return 0;
91 }
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:207
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 compression settings for a netCDF-4/HDF5 variable.
Definition: dvar.c:407
Main header file for the C API.
int nc_put_var_int(int ncid, int varid, const int *op)
Write an entire variable with one call.
Definition: dvarput.c:961
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1470
#define NC_INT
signed 4 byte integer
Definition: netcdf.h:38
EXTERNL int nc_def_var_chunking(int ncid, int varid, int storage, const size_t *chunksizesp)
Define chunking parameters for a variable.
Definition: dvar.c:545
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
Definition: netcdf.h:151
#define NC_SHUFFLE
Control the HDF5 shuffle filter.
Definition: netcdf.h:311
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition: dfile.c:562

Return to the Main Unidata NetCDF page.
Generated on Sat Apr 6 2019 08:19:00 for NetCDF. NetCDF is a Unidata library.