File: tst_coords3.c

package info (click to toggle)
netcdf-parallel 1%3A4.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 101,668 kB
  • sloc: ansic: 200,241; sh: 10,807; yacc: 2,522; makefile: 1,306; lex: 1,153; xml: 173; awk: 2
file content (476 lines) | stat: -rw-r--r-- 18,686 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* This is part of the netCDF package. Copyright 2010 University
   Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
   conditions of use.

   Test netcdf-4 coordinate variables and dimensions with an example
   from the CF conventions.

 Here's the example from the CF conventions:
dimensions:
  xc = 128 ;
  yc = 64 ;
  lev = 18 ;
variables:
  float T(lev,yc,xc) ;
    T:long_name = "temperature" ;
    T:units = "K" ;
    T:coordinates = "lon lat" ;
  float xc(xc) ;
    xc:axis = "X" ;
    xc:long_name = "x-coordinate in Cartesian system" ;
    xc:units = "m" ;
  float yc(yc) ;
    yc:axis = "Y" ;
    yc:long_name = "y-coordinate in Cartesian system" ;
    yc:units = "m" ;
  float lev(lev) ;
    lev:long_name = "pressure level" ;
    lev:units = "hPa" ;
  float lon(yc,xc) ;
    lon:long_name = "longitude" ;
    lon:units = "degrees_east" ;
  float lat(yc,xc) ;
    lat:long_name = "latitude" ;
    lat:units = "degrees_north" ;

   $Id: tst_coords3.c,v 1.1 2010/03/30 19:42:24 ed Exp $
*/

#include <nc_tests.h>
#include "err_macros.h"
#include "netcdf.h"

#define FILE_NAME "tst_coords3.nc"

#define NDIMS 3
#define NVARS 6
#define XC_NAME "xc"
#define YC_NAME "yc"
#define LEV_NAME "lev"
#define T_NAME "T"
#define LON_NAME "lon"
#define LAT_NAME "lat"
#define XC_LEN 128
#define YC_LEN 64
#define LEV_LEN 18
#define DATA_NDIMS 3
#define COORD_NDIMS 2

/* For the attributes. */
#define LONG_NAME "long_name"
#define TEMPERATURE "temperature"
#define UNITS "units"
#define KELVIN "K"
#define COORDINATES_NAME "coordinates"
#define LONLAT_COORDINATES "lon lat"
#define AXIS "axis"
#define X_NAME "X"
#define X_LONG_NAME "x-coordinate in Cartesian system"
#define METER "m"
#define Y_NAME "Y"
#define Y_LONG_NAME "y-coordinate in Cartesian system"
#define LEV_LONG_NAME "pressure level"
#define LON_LONG_NAME "longitude"
#define LAT_LONG_NAME "latitude"
#define DEGREES_EAST "degrees_east"
#define DEGREES_NORTH "degrees_north"
#define HPA "hPa"
#define SAMPLE_VALUE 0.5

int
check_cf_data(int ncid)
{
   float temp_in[YC_LEN][XC_LEN];
   size_t start[DATA_NDIMS] = {0, 0, 0}, count[DATA_NDIMS] = {1, YC_LEN, XC_LEN};
   float xc_in[XC_LEN], yc_in[YC_LEN];
   int x, y;

   /* Get the record we wrote. */
   if (nc_get_vara_float(ncid, 0, start, count, (float *)temp_in)) ERR_RET;
   for (y = 0; y < YC_LEN; y++)
      for (x = 0; x < XC_LEN; x++)
	 if (temp_in[y][x] != SAMPLE_VALUE) ERR_RET;

   /* Get the XC data. */
   if (nc_get_var_float(ncid, 1, xc_in)) ERR_RET;
   for (x = 0; x < XC_LEN; x++)
      if (xc_in[x] != SAMPLE_VALUE) ERR_RET;

   /* Get the YC data. */
   if (nc_get_var_float(ncid, 2, yc_in)) ERR_RET;
   for (y = 0; y < YC_LEN; y++)
      if (yc_in[y] != SAMPLE_VALUE) ERR_RET;

   return 0;
}

int
check_cf_metadata(int ncid)
{

   int nvars_in, varids_in[NVARS];
   int nvars, ndims, ngatts, unlimdimid;
   int ndims_in, natts_in, dimids_in[NDIMS];
   char var_name_in[NC_MAX_NAME + 1], dim_name_in[NC_MAX_NAME + 1];
   size_t len_in;
   nc_type xtype_in;
   int v;
   char att_text_in[NC_MAX_NAME + 1];

   /* Right number of objects? */
   if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR_RET;
   if (nvars != NVARS || ndims != NDIMS || ngatts != 0 || unlimdimid != -1) ERR_RET;

   /* Right number of varids? */
   if (nc_inq_varids(ncid, &nvars_in, varids_in)) ERR_RET;
   if (nvars_in != NVARS) ERR_RET;
   for (v = 0; v < NVARS; v++)
      if (varids_in[v] != v) ERR_RET;

   /* Right number of dimids? */
   if (nc_inq_dimids(ncid, &ndims_in, dimids_in, 0)) ERR_RET;
   if (ndims_in != NDIMS) ERR_RET;

   /* Check dimensions. */
   if (nc_inq_dim(ncid, 0, dim_name_in, &len_in)) ERR;
   if (strcmp(dim_name_in, XC_NAME) || len_in != XC_LEN) ERR;
   if (nc_inq_dim(ncid, 1, dim_name_in, &len_in)) ERR;
   if (strcmp(dim_name_in, YC_NAME) || len_in != YC_LEN) ERR;
   if (nc_inq_dim(ncid, 2, dim_name_in, &len_in)) ERR;
   if (strcmp(dim_name_in, LEV_NAME) || len_in != LEV_LEN) ERR;

   /* Check variable T. */
   if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR_RET;
   if (strcmp(var_name_in, T_NAME) || xtype_in != NC_FLOAT || ndims_in != NDIMS ||
       dimids_in[0] != 2 || dimids_in[1] != 1 || dimids_in[2] != 0 || natts_in != 3) ERR_RET;
   if (nc_get_att_text(ncid, 0, LONG_NAME, att_text_in)) ERR;
   if (strncmp(att_text_in, TEMPERATURE, strlen(TEMPERATURE))) ERR;
   if (nc_get_att_text(ncid, 0, UNITS, att_text_in)) ERR;
   if (strncmp(att_text_in, KELVIN, strlen(KELVIN))) ERR;
   if (nc_get_att_text(ncid, 0, COORDINATES_NAME, att_text_in)) ERR;
   if (strncmp(att_text_in, LONLAT_COORDINATES, strlen(LONLAT_COORDINATES))) ERR;

   /* Check variable xc. */
   if (nc_inq_var(ncid, 1, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR_RET;
   if (strcmp(var_name_in, XC_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
       dimids_in[0] != 0 || natts_in != 3) ERR_RET;
   if (nc_get_att_text(ncid, 1, AXIS, att_text_in)) ERR;
   if (strncmp(att_text_in, X_NAME, strlen(X_NAME))) ERR;
   if (nc_get_att_text(ncid, 1, LONG_NAME, att_text_in)) ERR;
   if (strncmp(att_text_in, X_LONG_NAME, strlen(X_LONG_NAME))) ERR;
   if (nc_get_att_text(ncid, 1, UNITS, att_text_in)) ERR;
   if (strncmp(att_text_in, METER, strlen(METER))) ERR;

   /* Check variable yc. */
   if (nc_inq_var(ncid, 2, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR_RET;
   if (strcmp(var_name_in, YC_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
       dimids_in[0] != 1 || natts_in != 3) ERR_RET;
   if (nc_get_att_text(ncid, 2, AXIS, att_text_in)) ERR;
   if (strncmp(att_text_in, Y_NAME, strlen(Y_NAME))) ERR;
   if (nc_get_att_text(ncid, 2, LONG_NAME, att_text_in)) ERR;
   if (strncmp(att_text_in, Y_LONG_NAME, strlen(Y_LONG_NAME))) ERR;
   if (nc_get_att_text(ncid, 2, UNITS, att_text_in)) ERR;
   if (strncmp(att_text_in, METER, strlen(METER))) ERR;

   /* Check variable lev. */
   if (nc_inq_var(ncid, 3, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR_RET;
   if (strcmp(var_name_in, LEV_NAME) || xtype_in != NC_FLOAT || ndims_in != 1 ||
       dimids_in[0] != 2 || natts_in != 2) ERR_RET;
   if (nc_get_att_text(ncid, 3, LONG_NAME, att_text_in)) ERR;
   if (strncmp(att_text_in, LEV_LONG_NAME, strlen(LEV_LONG_NAME))) ERR;
   if (nc_get_att_text(ncid, 3, UNITS, att_text_in)) ERR;
   if (strncmp(att_text_in, HPA, strlen(HPA))) ERR;

   /* Check variable lon. */
   if (nc_inq_var(ncid, 4, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR_RET;
   if (strcmp(var_name_in, LON_NAME) || xtype_in != NC_FLOAT || ndims_in != COORD_NDIMS ||
       dimids_in[0] != 1 || dimids_in[1] != 0 || natts_in != 2) ERR_RET;
   if (nc_get_att_text(ncid, 4, LONG_NAME, att_text_in)) ERR;
   if (strncmp(att_text_in, LON_LONG_NAME, strlen(LON_LONG_NAME))) ERR;
   if (nc_get_att_text(ncid, 4, UNITS, att_text_in)) ERR;
   if (strncmp(att_text_in, DEGREES_EAST, strlen(DEGREES_EAST))) ERR;

   /* Check variable lat. */
   if (nc_inq_var(ncid, 5, var_name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR_RET;
   if (strcmp(var_name_in, LAT_NAME) || xtype_in != NC_FLOAT || ndims_in != COORD_NDIMS ||
       dimids_in[0] != 1 || dimids_in[1] != 0 || natts_in != 2) ERR_RET;
   if (nc_get_att_text(ncid, 5, LONG_NAME, att_text_in)) ERR;
   if (strncmp(att_text_in, LAT_LONG_NAME, strlen(LAT_LONG_NAME))) ERR;
   if (nc_get_att_text(ncid, 5, UNITS, att_text_in)) ERR;
   if (strncmp(att_text_in, DEGREES_NORTH, strlen(DEGREES_NORTH))) ERR;

   return 0;
}

int
main(int argc, char **argv)
{
   printf("\n*** Testing with CF example http://cf-pcmdi.llnl.gov/documents/cf-conventions/1.4/ch05s02.html....\n");
   printf("**** simple test with only metadata");
   {
      int ncid, dimids[NDIMS], varids[NVARS], data_dimids[DATA_NDIMS];
      int coord_dimids[COORD_NDIMS];

      /* Create a netcdf-4 file. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* Define dimensions. */
      if (nc_def_dim(ncid, XC_NAME, XC_LEN, &dimids[0])) ERR;
      if (nc_def_dim(ncid, YC_NAME, YC_LEN, &dimids[1])) ERR;
      if (nc_def_dim(ncid, LEV_NAME, LEV_LEN, &dimids[2])) ERR;

      /* Define variables. Start with T. */
      data_dimids[0] = dimids[2];
      data_dimids[1] = dimids[1];
      data_dimids[2] = dimids[0];
      if (nc_def_var(ncid, T_NAME, NC_FLOAT, DATA_NDIMS, data_dimids, &varids[0])) ERR;
      if (nc_put_att_text(ncid, varids[0], LONG_NAME, strlen(TEMPERATURE),
			  TEMPERATURE)) ERR;
      if (nc_put_att_text(ncid, varids[0], UNITS, strlen(KELVIN), KELVIN)) ERR;
      if (nc_put_att_text(ncid, varids[0], COORDINATES_NAME, strlen(LONLAT_COORDINATES),
			  LONLAT_COORDINATES)) ERR;

      /* Define xc variable. */
      if (nc_def_var(ncid, XC_NAME, NC_FLOAT, 1, &dimids[0], &varids[1])) ERR;
      if (nc_put_att_text(ncid, varids[1], AXIS, strlen(X_NAME), X_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[1], LONG_NAME, strlen(X_LONG_NAME),
			  X_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[1], UNITS, strlen(METER), METER)) ERR;

      /* Define yc variable. */
      if (nc_def_var(ncid, YC_NAME, NC_FLOAT, 1, &dimids[1], &varids[2])) ERR;
      if (nc_put_att_text(ncid, varids[2], AXIS, strlen(Y_NAME), Y_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[2], LONG_NAME, strlen(Y_LONG_NAME),
			  Y_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[2], UNITS, strlen(METER), METER)) ERR;

      /* Define lev variable. */
      if (nc_def_var(ncid, LEV_NAME, NC_FLOAT, 1, &dimids[2], &varids[3])) ERR;
      if (nc_put_att_text(ncid, varids[3], LONG_NAME, strlen(LEV_LONG_NAME),
			  LEV_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[3], UNITS, strlen(HPA), HPA)) ERR;

      /* Define lon variable. */
      coord_dimids[0] = dimids[1];
      coord_dimids[1] = dimids[0];
      if (nc_def_var(ncid, LON_NAME, NC_FLOAT, COORD_NDIMS, coord_dimids, &varids[4])) ERR;
      if (nc_put_att_text(ncid, varids[4], LONG_NAME, strlen(LON_LONG_NAME),
			  LON_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[4], UNITS, strlen(DEGREES_EAST), DEGREES_EAST)) ERR;

      /* Define lat variable. */
      if (nc_def_var(ncid, LAT_NAME, NC_FLOAT, COORD_NDIMS, coord_dimids, &varids[5])) ERR;
      if (nc_put_att_text(ncid, varids[5], LONG_NAME, strlen(LAT_LONG_NAME),
			  LAT_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[5], UNITS, strlen(DEGREES_NORTH), DEGREES_NORTH)) ERR;

      /* Check the metadata. */
      if (check_cf_metadata(ncid)) ERR;

      /* Close up. */
      if (nc_close(ncid)) ERR;

      /* Open the file and check the order of variables and dimensions. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;

      /* Check the metadata. */
      if (check_cf_metadata(ncid)) ERR;

      /* Close up. */
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** test with data writes...");
   {
      int ncid, dimids[NDIMS], varids[NVARS], data_dimids[DATA_NDIMS];
      int coord_dimids[COORD_NDIMS];
      float temp[YC_LEN][XC_LEN], xc[XC_LEN], yc[YC_LEN];
      size_t start[DATA_NDIMS] = {0, 0, 0}, count[DATA_NDIMS] = {1, YC_LEN, XC_LEN};
      int x, y;

      /* Initialize some fake data. */
      for (y = 0; y < YC_LEN; y++)
	 for (x = 0; x < XC_LEN; x++)
	    temp[y][x] = SAMPLE_VALUE;
      for (x = 0; x < XC_LEN; x++)
	 xc[x] = SAMPLE_VALUE;
      for (y = 0; y < YC_LEN; y++)
	 yc[y] = SAMPLE_VALUE;

      /* Create a netcdf-4 file. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* Define dimensions. */
      if (nc_def_dim(ncid, XC_NAME, XC_LEN, &dimids[0])) ERR;
      if (nc_def_dim(ncid, YC_NAME, YC_LEN, &dimids[1])) ERR;
      if (nc_def_dim(ncid, LEV_NAME, LEV_LEN, &dimids[2])) ERR;

      /* Define variables. Start with T. */
      data_dimids[0] = dimids[2];
      data_dimids[1] = dimids[1];
      data_dimids[2] = dimids[0];
      if (nc_def_var(ncid, T_NAME, NC_FLOAT, DATA_NDIMS, data_dimids, &varids[0])) ERR;
      if (nc_put_att_text(ncid, varids[0], LONG_NAME, strlen(TEMPERATURE),
			  TEMPERATURE)) ERR;
      if (nc_put_att_text(ncid, varids[0], UNITS, strlen(KELVIN), KELVIN)) ERR;
      if (nc_put_att_text(ncid, varids[0], COORDINATES_NAME, strlen(LONLAT_COORDINATES),
			  LONLAT_COORDINATES)) ERR;

      /* Define xc variable. */
      if (nc_def_var(ncid, XC_NAME, NC_FLOAT, 1, &dimids[0], &varids[1])) ERR;
      if (nc_put_att_text(ncid, varids[1], AXIS, strlen(X_NAME), X_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[1], LONG_NAME, strlen(X_LONG_NAME),
			  X_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[1], UNITS, strlen(METER), METER)) ERR;

      /* Define yc variable. */
      if (nc_def_var(ncid, YC_NAME, NC_FLOAT, 1, &dimids[1], &varids[2])) ERR;
      if (nc_put_att_text(ncid, varids[2], AXIS, strlen(Y_NAME), Y_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[2], LONG_NAME, strlen(Y_LONG_NAME),
			  Y_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[2], UNITS, strlen(METER), METER)) ERR;

      /* Define lev variable. */
      if (nc_def_var(ncid, LEV_NAME, NC_FLOAT, 1, &dimids[2], &varids[3])) ERR;
      if (nc_put_att_text(ncid, varids[3], LONG_NAME, strlen(LEV_LONG_NAME),
			  LEV_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[3], UNITS, strlen(HPA), HPA)) ERR;

      /* Define lon variable. */
      coord_dimids[0] = dimids[1];
      coord_dimids[1] = dimids[0];
      if (nc_def_var(ncid, LON_NAME, NC_FLOAT, COORD_NDIMS, coord_dimids, &varids[4])) ERR;
      if (nc_put_att_text(ncid, varids[4], LONG_NAME, strlen(LON_LONG_NAME),
			  LON_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[4], UNITS, strlen(DEGREES_EAST), DEGREES_EAST)) ERR;

      /* Define lat variable. */
      if (nc_def_var(ncid, LAT_NAME, NC_FLOAT, COORD_NDIMS, coord_dimids, &varids[5])) ERR;
      if (nc_put_att_text(ncid, varids[5], LONG_NAME, strlen(LAT_LONG_NAME),
			  LAT_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[5], UNITS, strlen(DEGREES_NORTH), DEGREES_NORTH)) ERR;

      /* Write some data to T. */
      if (nc_put_vara_float(ncid, 0, start, count, (const float *)temp)) ERR;
      /* Write data to XC. */
      if (nc_put_var_float(ncid, 1, xc)) ERR;
      /* Write data to YC. */
      if (nc_put_var_float(ncid, 2, yc)) ERR;

      /* Check the metadata and data. */
      if (check_cf_metadata(ncid)) ERR;
      if (check_cf_data(ncid)) ERR;

      /* Close up. */
      if (nc_close(ncid)) ERR;

      /* Open the file and check the order of variables and dimensions. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;

      /* Check the metadata and data. */
      if (check_cf_metadata(ncid)) ERR;
      if (check_cf_data(ncid)) ERR;

      /* Close up. */
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** test with data writes without enddefs...");
   {
      int ncid, dimids[NDIMS], varids[NVARS], data_dimids[DATA_NDIMS];
      int coord_dimids[COORD_NDIMS];
      float temp[YC_LEN][XC_LEN], xc[XC_LEN], yc[YC_LEN];
      size_t start[DATA_NDIMS] = {0, 0, 0}, count[DATA_NDIMS] = {1, YC_LEN, XC_LEN};
      int x, y;

      /* Initialize some fake data. */
      for (y = 0; y < YC_LEN; y++)
	 for (x = 0; x < XC_LEN; x++)
	    temp[y][x] = SAMPLE_VALUE;
      for (x = 0; x < XC_LEN; x++)
	 xc[x] = SAMPLE_VALUE;
      for (y = 0; y < YC_LEN; y++)
	 yc[y] = SAMPLE_VALUE;

      /* Create a netcdf-4 file. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;

      /* Define dimensions. */
      if (nc_def_dim(ncid, XC_NAME, XC_LEN, &dimids[0])) ERR;
      if (nc_def_dim(ncid, YC_NAME, YC_LEN, &dimids[1])) ERR;
      if (nc_def_dim(ncid, LEV_NAME, LEV_LEN, &dimids[2])) ERR;

      /* Define variables. Start with T. */
      data_dimids[0] = dimids[2];
      data_dimids[1] = dimids[1];
      data_dimids[2] = dimids[0];
      if (nc_def_var(ncid, T_NAME, NC_FLOAT, DATA_NDIMS, data_dimids, &varids[0])) ERR;
      if (nc_put_att_text(ncid, varids[0], LONG_NAME, strlen(TEMPERATURE),
			  TEMPERATURE)) ERR;
      if (nc_put_att_text(ncid, varids[0], UNITS, strlen(KELVIN), KELVIN)) ERR;
      if (nc_put_att_text(ncid, varids[0], COORDINATES_NAME, strlen(LONLAT_COORDINATES),
			  LONLAT_COORDINATES)) ERR;

      /* Write some data to T. */
      if (nc_put_vara_float(ncid, 0, start, count, (const float *)temp)) ERR;

      /* Define xc variable. */
      if (nc_def_var(ncid, XC_NAME, NC_FLOAT, 1, &dimids[0], &varids[1])) ERR;
      if (nc_put_att_text(ncid, varids[1], AXIS, strlen(X_NAME), X_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[1], LONG_NAME, strlen(X_LONG_NAME),
			  X_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[1], UNITS, strlen(METER), METER)) ERR;

      /* Write data to XC. */
      if (nc_put_var_float(ncid, 1, xc)) ERR;

      /* Define yc variable. */
      if (nc_def_var(ncid, YC_NAME, NC_FLOAT, 1, &dimids[1], &varids[2])) ERR;
      if (nc_put_att_text(ncid, varids[2], AXIS, strlen(Y_NAME), Y_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[2], LONG_NAME, strlen(Y_LONG_NAME),
			  Y_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[2], UNITS, strlen(METER), METER)) ERR;

      /* Write data to YC. */
      if (nc_put_var_float(ncid, 2, yc)) ERR;

      /* Define lev variable. */
      if (nc_def_var(ncid, LEV_NAME, NC_FLOAT, 1, &dimids[2], &varids[3])) ERR;
      if (nc_put_att_text(ncid, varids[3], LONG_NAME, strlen(LEV_LONG_NAME),
			  LEV_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[3], UNITS, strlen(HPA), HPA)) ERR;

      /* Define lon variable. */
      coord_dimids[0] = dimids[1];
      coord_dimids[1] = dimids[0];
      if (nc_def_var(ncid, LON_NAME, NC_FLOAT, COORD_NDIMS, coord_dimids, &varids[4])) ERR;
      if (nc_put_att_text(ncid, varids[4], LONG_NAME, strlen(LON_LONG_NAME),
			  LON_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[4], UNITS, strlen(DEGREES_EAST), DEGREES_EAST)) ERR;

      /* Define lat variable. */
      if (nc_def_var(ncid, LAT_NAME, NC_FLOAT, COORD_NDIMS, coord_dimids, &varids[5])) ERR;
      if (nc_put_att_text(ncid, varids[5], LONG_NAME, strlen(LAT_LONG_NAME),
			  LAT_LONG_NAME)) ERR;
      if (nc_put_att_text(ncid, varids[5], UNITS, strlen(DEGREES_NORTH), DEGREES_NORTH)) ERR;

      /* Check the metadata and data. */
      if (check_cf_metadata(ncid)) ERR;
      if (check_cf_data(ncid)) ERR;

      /* Close up. */
      if (nc_close(ncid)) ERR;

      /* Open the file and check the order of variables and dimensions. */
      if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;

      /* Check the metadata and data. */
      if (check_cf_metadata(ncid)) ERR;
      if (check_cf_data(ncid)) ERR;

      /* Close up. */
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}