File: simple.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 (74 lines) | stat: -rw-r--r-- 1,842 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
/* This example program is part of Unidata's netCDF library for
   scientific data access. 

   How about a short, but meaningful, netCDF program?

   Ed Hartnett, 6/19/4
   $Id: simple.c,v 1.1 2004/07/26 14:04:42 ed Exp $
*/

#include <netcdf.h>
#include <stdio.h>
#include <string.h>

/* This macro handles errors by outputting a message to stdout and
   then exiting. */
#define NC_EXAMPLE_ERROR 2 /* This is the exit code for failure. */
#define BAIL(e) do { \
printf("Bailing out in file %s, line %d, error:%s.\n", \
__FILE__, __LINE__, nc_strerror(e)); \
return NC_EXAMPLE_ERROR; \
} while (0) 

#define NUMDIMS 2
#define NUMVARS 1
#define CELSIUS "celsius"
#define LAT_LEN 3
#define LON_LEN 2

int
main()
{
   int ncid, temp_varid, dimids[NUMDIMS];
   float temp[LAT_LEN][LON_LEN], *fp;
   int i, res; 

   /* Create a bunch of phoney data so we have something to write in
      the example file. */
   for (fp=(float *)temp, i=0; i<LAT_LEN*LON_LEN; i++)
      *fp++ = 10. + i/10.;

   /* Create the netCDF file. */
   if ((res = nc_create("short.nc", NC_CLOBBER, &ncid)))
      BAIL(res);

   /* Define dimensions. */
   if ((res = nc_def_dim(ncid, "latitude", LAT_LEN, dimids)))
      BAIL(res);
   if ((res = nc_def_dim(ncid, "longitude", LON_LEN, &dimids[1])))
      BAIL(res);
   
   /* Define the variable. */
   if ((res = nc_def_var(ncid, "sfc_temp", NC_FLOAT, NUMDIMS, 
			 dimids, &temp_varid)))
      BAIL(res);
   
   /* We'll store the units. */
   if ((res = nc_put_att_text(ncid, temp_varid, "units", 
			      strlen(CELSIUS), CELSIUS)))
      BAIL(res);

   /* We're finished defining metadata. */
   if ((res = nc_enddef(ncid)))
      BAIL(res);

   if ((res = nc_put_var_float(ncid, temp_varid, (float *)temp)))
      BAIL(res);

   /* We're done! */
   if ((res = nc_close(ncid)))
      BAIL(res);

   return 0;
}