Loading...
Searching...
No Matches
simple_xy_nc4_wr.c
/* Copyright 2019 University Corporation for Atmospheric
Research/Unidata. See COPYRIGHT file for conditions of use. */
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
/* This is the name of the data file we will create. */
#define FILE_NAME "simple_xy_nc4.nc"
/* We are writing 2D data, a 6 x 12 grid. */
#define NDIMS 2
#define NX 6
#define NY 12
/* Handle errors by printing an error message and exiting with a
* non-zero status. */
#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
int
main()
{
int ncid, x_dimid, y_dimid, varid;
int dimids[NDIMS];
size_t chunks[NDIMS];
int shuffle, deflate, deflate_level;
int data_out[NX][NY];
int x, y, retval;
/* Set chunking, shuffle, and deflate. */
shuffle = NC_SHUFFLE;
deflate = 1;
deflate_level = 1;
/* Create some pretend data. If this wasn't an example program, we
* would have some real data to write, for example, model output. */
for (x = 0; x < NX; x++)
for (y = 0; y < NY; y++)
data_out[x][y] = x * NY + y;
/* Create the file. The NC_NETCDF4 parameter tells netCDF to create
* a file in netCDF-4/HDF5 standard. */
if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
ERR(retval);
/* Define the dimensions. */
if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
ERR(retval);
if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
ERR(retval);
/* Set up variable data. */
dimids[0] = x_dimid;
dimids[1] = y_dimid;
chunks[0] = NX/4;
chunks[1] = NY/4;
/* Define the variable. */
if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
dimids, &varid)))
ERR(retval);
if ((retval = nc_def_var_chunking(ncid, varid, 0, &chunks[0])))
ERR(retval);
if ((retval = nc_def_var_deflate(ncid, varid, shuffle, deflate,
deflate_level)))
ERR(retval);
/* No need to explicitly end define mode for netCDF-4 files. Write
* the pretend data to the file. */
if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
ERR(retval);
/* Close the file. */
if ((retval = nc_close(ncid)))
ERR(retval);
printf("*** SUCCESS writing example file simple_xy_nc4.nc!\n");
return 0;
}
nc_close
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition dfile.c:1300
nc_create
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition dfile.c:398
nc_def_dim
EXTERNL int nc_def_dim(int ncid, const char *name, size_t len, int *idp)
Define a new dimension.
Definition ddim.c:121
nc_def_var_deflate
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
nc_def_var
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
nc_def_var_chunking
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
netcdf.h
Main header file for the C API.
NC_NETCDF4
#define NC_NETCDF4
Use netCDF-4/HDF5 format.
Definition netcdf.h:162
NC_INT
#define NC_INT
signed 4 byte integer
Definition netcdf.h:38
NC_SHUFFLE
#define NC_SHUFFLE
Control the HDF5 shuffle filter.
Definition netcdf.h:336