NetCDF 4.10.1
Loading...
Searching...
No Matches
simple_xy_wr.c
Go to the documentation of this file.
1/* Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
3 2015, 2016, 2017, 2018 University Corporation for Atmospheric
4 Research/Unidata. See COPYRIGHT file for conditions of use. */
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <netcdf.h>
22
23/* This is the name of the data file we will create. */
24#define FILE_NAME "simple_xy.nc"
25
26/* We are writing 2D data, a 6 x 12 grid. */
27#define NDIMS 2
28#define NX 6
29#define NY 12
30
31/* Handle errors by printing an error message and exiting with a
32 * non-zero status. */
33#define ERRCODE 2
34#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}
35
36int
37main()
38{
39 /* When we create netCDF variables and dimensions, we get back an
40 * ID for each one. */
41 int ncid, x_dimid, y_dimid, varid;
42 int dimids[NDIMS];
43
44 /* This is the data array we will write. It will be filled with a
45 * progression of numbers for this example. */
46 int data_out[NX][NY];
47
48 /* Loop indexes, and error handling. */
49 int x, y, retval;
50
51 /* Create some pretend data. If this wasn't an example program, we
52 * would have some real data to write, for example, model
53 * output. */
54 for (x = 0; x < NX; x++)
55 for (y = 0; y < NY; y++)
56 data_out[x][y] = x * NY + y;
57
58 /* Always check the return code of every netCDF function call. In
59 * this example program, any retval which is not equal to NC_NOERR
60 * (0) will cause the program to print an error message and exit
61 * with a non-zero return code. */
62
63 /* Create the file. The NC_CLOBBER parameter tells netCDF to
64 * overwrite this file, if it already exists.*/
65 if ((retval = nc_create(FILE_NAME, NC_CLOBBER, &ncid)))
66 ERR(retval);
67
68 /* Define the dimensions. NetCDF will hand back an ID for each. */
69 if ((retval = nc_def_dim(ncid, "x", NX, &x_dimid)))
70 ERR(retval);
71 if ((retval = nc_def_dim(ncid, "y", NY, &y_dimid)))
72 ERR(retval);
73
74 /* The dimids array is used to pass the IDs of the dimensions of
75 * the variable. */
76 dimids[0] = x_dimid;
77 dimids[1] = y_dimid;
78
79 /* Define the variable. The type of the variable in this case is
80 * NC_INT (4-byte integer). */
81 if ((retval = nc_def_var(ncid, "data", NC_INT, NDIMS,
82 dimids, &varid)))
83 ERR(retval);
84
85 /* End define mode. This tells netCDF we are done defining
86 * metadata. */
87 if ((retval = nc_enddef(ncid)))
88 ERR(retval);
89
90 /* Write the pretend data to the file. Although netCDF supports
91 * reading and writing subsets of data, in this case we write all
92 * the data in one operation. */
93 if ((retval = nc_put_var_int(ncid, varid, &data_out[0][0])))
94 ERR(retval);
95
96 /* Close the file. This frees up any internal netCDF resources
97 * associated with the file, and flushes any buffers. */
98 if ((retval = nc_close(ncid)))
99 ERR(retval);
100
101 printf("*** SUCCESS writing example file simple_xy.nc!\n");
102 return 0;
103}
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition dfile.c:1330
EXTERNL int nc_create(const char *path, int cmode, int *ncidp)
Create a new netCDF file.
Definition dfile.c:428
EXTERNL int nc_enddef(int ncid)
Leave define mode.
Definition dfile.c:1057
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(int ncid, const char *name, nc_type xtype, int ndims, const int *dimidsp, int *varidp)
Define a new variable.
Definition dvar.c:216
Main header file for the C API.
#define NC_INT
signed 4 byte integer
Definition netcdf.h:38
#define NC_CLOBBER
Destroy existing file.
Definition netcdf.h:138