File: tst_put_vars.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,921 bytes parent folder | download | duplicates (2)
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
/*! Test added as part of JIRA ticket NCF-326.

  The test was provided by Ellen Johnson at Mathworks.

  See https://bugtracking.unidata.ucar.edu/browse/NCF-326
  for more information.
*/
#include <stdio.h>
#include <string.h>
#include <netcdf.h>

#define FILE_NAME "tst_put_vars.nc"

/* 3D matrix, 6 x 4 x 3 */

#define NDIMS 3
#define X_LEN 6
#define Y_LEN 4
#define Z_LEN 3

/* Handle errors by printing an error message */
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); return 2;}
int
main()
{
    size_t start[NDIMS] = {0, 0, 0};
    size_t count[NDIMS] = {X_LEN, Y_LEN, Z_LEN};
    ptrdiff_t stride[NDIMS] = {1, 1, 1};
    float mydata[X_LEN * Y_LEN * Z_LEN];
    int i;
    int retval;

    int ncid, varid;
    int dimids[NDIMS];

    for (i = 0; i < (X_LEN * Y_LEN * Z_LEN); i++)
        mydata[i] = i;

    /* create the file in NetCDF-4 format */
    if ((retval = nc_create(FILE_NAME, NC_NETCDF4, &ncid)))
        ERR(retval);

    /* define dimensions */
    if ((retval = nc_def_dim(ncid, "time", X_LEN, &dimids[0])))
        ERR(retval);
    if ((retval = nc_def_dim(ncid, "lat", Y_LEN, &dimids[1])))
        ERR(retval);
    if ((retval = nc_def_dim(ncid, "lon", NC_UNLIMITED, &dimids[2])))
        ERR(retval);

   /* define the variable */
    if ((retval = nc_def_var(ncid, "data", NC_FLOAT, NDIMS, dimids, &varid)))
        ERR(retval);

    /* end define mode */
    if ((retval = nc_enddef(ncid)))
        ERR(retval);

    /* This will fail. */
    if ((retval = nc_put_vars_float(ncid, varid, NULL, count, stride,
                                    mydata)) != NC_EINVALCOORDS)
        ERR(99);

    /* write data */
    if ((retval = nc_put_vars_float(ncid, varid, start, count, stride, mydata)))
        ERR(retval);

    /* close the file */
    if ((retval = nc_close(ncid)))
        ERR(retval);

    printf("\n\n*** SUCCESS writing example file %s!\n", FILE_NAME);
    return 0;
}