NetCDF  4.6.3
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
sfc_pres_temp_wr.c
Go to the documentation of this file.
1 
13 #include <stdio.h>
14 #include <string.h>
15 #include <netcdf.h>
16 
17 /* This is the name of the data file we will create. */
18 #define FILE_NAME "sfc_pres_temp.nc"
19 
20 /* We are writing 2D data, a 6 x 12 lat-lon grid. We will need two
21  * netCDF dimensions. */
22 #define NDIMS 2
23 #define NLAT 6
24 #define NLON 12
25 #define LAT_NAME "latitude"
26 #define LON_NAME "longitude"
27 
28 /* Names of things. */
29 #define PRES_NAME "pressure"
30 #define TEMP_NAME "temperature"
31 #define UNITS "units"
32 #define DEGREES_EAST "degrees_east"
33 #define DEGREES_NORTH "degrees_north"
34 
35 /* These are used to construct some example data. */
36 #define SAMPLE_PRESSURE 900
37 #define SAMPLE_TEMP 9.0
38 #define START_LAT 25.0
39 #define START_LON -125.0
40 
41 /* Handle errors by printing an error message and exiting with a
42  * non-zero status. */
43 #define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
44 
45 int
46 main()
47 {
48  int ncid, lon_dimid, lat_dimid, pres_varid, temp_varid;
49 
50 /* In addition to the latitude and longitude dimensions, we will also
51  create latitude and longitude netCDF variables which will hold the
52  actual latitudes and longitudes. Since they hold data about the
53  coordinate system, the netCDF term for these is: "coordinate
54  variables." */
55  int lat_varid, lon_varid;
56 
57  int dimids[NDIMS];
58 
59  /* We will write surface temperature and pressure fields. */
60  float pres_out[NLAT][NLON];
61  float temp_out[NLAT][NLON];
62  float lats[NLAT], lons[NLON];
63 
64  /* It's good practice for each netCDF variable to carry a "units"
65  * attribute. */
66  char pres_units[] = "hPa";
67  char temp_units[] = "celsius";
68 
69  /* Loop indexes. */
70  int lat, lon;
71 
72  /* Error handling. */
73  int retval;
74 
75  /* Create some pretend data. If this wasn't an example program, we
76  * would have some real data to write, for example, model
77  * output. */
78  for (lat = 0; lat < NLAT; lat++)
79  lats[lat] = START_LAT + 5.*lat;
80  for (lon = 0; lon < NLON; lon++)
81  lons[lon] = START_LON + 5.*lon;
82 
83  for (lat = 0; lat < NLAT; lat++)
84  for (lon = 0; lon < NLON; lon++)
85  {
86  pres_out[lat][lon] = SAMPLE_PRESSURE + (lon * NLAT + lat);
87  temp_out[lat][lon] = SAMPLE_TEMP + .25 * (lon * NLAT + lat);
88  }
89 
90  /* Create the file. */
91  if ((retval = nc_create(FILE_NAME, NC_CLOBBER, &ncid)))
92  ERR(retval);
93 
94  /* Define the dimensions. */
95  if ((retval = nc_def_dim(ncid, LAT_NAME, NLAT, &lat_dimid)))
96  ERR(retval);
97  if ((retval = nc_def_dim(ncid, LON_NAME, NLON, &lon_dimid)))
98  ERR(retval);
99 
100  /* Define coordinate netCDF variables. They will hold the
101  coordinate information, that is, the latitudes and longitudes. A
102  varid is returned for each.*/
103  if ((retval = nc_def_var(ncid, LAT_NAME, NC_FLOAT, 1, &lat_dimid,
104  &lat_varid)))
105  ERR(retval);
106  if ((retval = nc_def_var(ncid, LON_NAME, NC_FLOAT, 1, &lon_dimid,
107  &lon_varid)))
108  ERR(retval);
109 
110  /* Define units attributes for coordinate vars. This attaches a
111  text attribute to each of the coordinate variables, containing
112  the units. Note that we are not writing a trailing NULL, just
113  "units", because the reading program may be fortran which does
114  not use null-terminated strings. In general it is up to the
115  reading C program to ensure that it puts null-terminators on
116  strings where necessary.*/
117  if ((retval = nc_put_att_text(ncid, lat_varid, UNITS,
118  strlen(DEGREES_NORTH), DEGREES_NORTH)))
119  ERR(retval);
120  if ((retval = nc_put_att_text(ncid, lon_varid, UNITS,
121  strlen(DEGREES_EAST), DEGREES_EAST)))
122  ERR(retval);
123 
124  /* Define the netCDF variables. The dimids array is used to pass
125  the dimids of the dimensions of the variables.*/
126  dimids[0] = lat_dimid;
127  dimids[1] = lon_dimid;
128  if ((retval = nc_def_var(ncid, PRES_NAME, NC_FLOAT, NDIMS,
129  dimids, &pres_varid)))
130  ERR(retval);
131  if ((retval = nc_def_var(ncid, TEMP_NAME, NC_FLOAT, NDIMS,
132  dimids, &temp_varid)))
133  ERR(retval);
134 
135  /* Define units attributes for vars. */
136  if ((retval = nc_put_att_text(ncid, pres_varid, UNITS,
137  strlen(pres_units), pres_units)))
138  ERR(retval);
139  if ((retval = nc_put_att_text(ncid, temp_varid, UNITS,
140  strlen(temp_units), temp_units)))
141  ERR(retval);
142 
143  /* End define mode. */
144  if ((retval = nc_enddef(ncid)))
145  ERR(retval);
146 
147  /* Write the coordinate variable data. This will put the latitudes
148  and longitudes of our data grid into the netCDF file. */
149  if ((retval = nc_put_var_float(ncid, lat_varid, &lats[0])))
150  ERR(retval);
151  if ((retval = nc_put_var_float(ncid, lon_varid, &lons[0])))
152  ERR(retval);
153 
154  /* Write the pretend data. This will write our surface pressure and
155  surface temperature data. The arrays of data are the same size
156  as the netCDF variables we have defined. */
157  if ((retval = nc_put_var_float(ncid, pres_varid, &pres_out[0][0])))
158  ERR(retval);
159  if ((retval = nc_put_var_float(ncid, temp_varid, &temp_out[0][0])))
160  ERR(retval);
161 
162  /* Close the file. */
163  if ((retval = nc_close(ncid)))
164  ERR(retval);
165 
166  printf("*** SUCCESS writing example file sfc_pres_temp.nc!\n");
167  return 0;
168 }
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
Main header file for the C API.
EXTERNL int nc_put_att_text(int ncid, int varid, const char *name, size_t len, const char *op)
Write a text attribute.
Definition: dattput.c:139
EXTERNL int nc_close(int ncid)
Close an open netCDF dataset.
Definition: dfile.c:1470
#define NC_CLOBBER
Destroy existing file.
Definition: netcdf.h:126
int nc_put_var_float(int ncid, int varid, const float *op)
Write an entire variable with one call.
Definition: dvarput.c:973
EXTERNL int nc_enddef(int ncid)
Leave define mode.
Definition: dfile.c:1191
#define NC_FLOAT
single precision floating point number
Definition: netcdf.h:40
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.