File: tst_files4.c

package info (click to toggle)
netcdf 1%3A4.4.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 96,828 kB
  • ctags: 15,369
  • sloc: ansic: 163,650; sh: 9,294; yacc: 2,457; makefile: 1,208; lex: 1,161; xml: 173; f90: 7; fortran: 6; awk: 2
file content (126 lines) | stat: -rw-r--r-- 3,851 bytes parent folder | download
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
/* 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 file from user-reported error. This code based on an
   ncgen output.

   $Id: tst_files4.c,v 1.4 2010/05/18 12:30:05 ed Exp $
*/

#include <config.h>
#include <stdio.h>
#include <nc_tests.h>
#include "err_macros.h"
#include "netcdf.h"
#include <string.h>

#define FILE_NAME "tst_files4.nc"
#define SNAPSHOT_LEN NC_UNLIMITED
#define AXIS_LEN 3
#define PARTICLE_LEN 256
#define SLICE_LEN 2
#define NDIMS1 1
#define NDIMS4 4
#define CLASSICAL "classical"

int
main() {/* create data.nc */

    int  ncid;
    int classical_grp;

    /* dimension ids */
    int snapshot_dim;
    int axis_dim;
    int particle_dim;
    int slice_dim;


    size_t snapshot_len = SNAPSHOT_LEN;
    size_t axis_len = AXIS_LEN;
    size_t particle_len = PARTICLE_LEN;
    size_t slice_len = SLICE_LEN;

    int snapshot_id;
    int position_id;
    int axis_id;

    printf("\n*** Testing NetCDF-4 with user-supplied sample file.\n");
    printf("*** testing creation of sample file...");
    {
       double data[SLICE_LEN * PARTICLE_LEN * AXIS_LEN];
       size_t start[4] = {0, 0, 0, 0};
       size_t count[4] = {1, SLICE_LEN, PARTICLE_LEN, AXIS_LEN};
       int dimids[NDIMS4], grpid;
       int ndims_in, nvars_in, natts_in, ngrps_in;
       int unlimdimid;
       char name_in[NC_MAX_NAME + 1];
       int i;

       /* Sample data. */
       for (i = 0; i < SLICE_LEN * PARTICLE_LEN * AXIS_LEN; i++)
	  data[i] = 42.42;

       if (nc_create(FILE_NAME, NC_CLOBBER|NC_NETCDF4, &ncid)) ERR;
       if (nc_def_grp(ncid, CLASSICAL, &classical_grp)) ERR;

       /* define dimensions */
       if (nc_def_dim(classical_grp, "snapshot", snapshot_len, &snapshot_dim)) ERR;
       if (nc_def_dim(classical_grp, "axis", axis_len, &axis_dim)) ERR;
       if (nc_def_dim(classical_grp, "particle", particle_len, &particle_dim)) ERR;
       if (nc_def_dim(classical_grp, "slice", slice_len, &slice_dim)) ERR;

       dimids[0] = snapshot_dim;
       dimids[1] = slice_dim;
       dimids[2] = particle_dim;
       dimids[3] = axis_dim;
       if (nc_def_var(classical_grp, "position", NC_DOUBLE, NDIMS4,
		      dimids, &position_id)) ERR;

       /* First write some position data. */
       if (nc_put_vara_double(classical_grp, position_id,
			      start, count, data)) ERR;

       /* Now define some coordinate variables. */
       if (nc_def_var(classical_grp, "snapshot", NC_INT, NDIMS1,
		      &snapshot_dim, &snapshot_id)) ERR;
       if (nc_def_var(classical_grp, "axis", NC_CHAR, NDIMS1,
		      &axis_dim, &axis_id)) ERR;

       /* Check some stuff. */
       if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid)) ERR;
       if (ndims_in != 0 || nvars_in != 0 || natts_in != 0 || unlimdimid != -1) ERR;
       if (nc_inq_grps(ncid, &ngrps_in, &grpid)) ERR;
       if (ngrps_in != 1) ERR;
       if (nc_inq_grpname(grpid, name_in)) ERR;
       if (strcmp(name_in, CLASSICAL)) ERR;
       if (nc_inq(classical_grp, &ndims_in, &nvars_in, &natts_in, &unlimdimid)) ERR;
       if (ndims_in != 4 || nvars_in != 3 || natts_in != 0 || unlimdimid != 0) ERR;


       if (nc_close(ncid)) ERR;

       if (nc_open(FILE_NAME, 0, &ncid)) ERR;
       if (nc_close(ncid)) ERR;
    }
    SUMMARIZE_ERR;
#ifdef EXTRA_TESTS
    printf("*** testing opening of many files...");
    {
       int i, ncid;

       for (i = 0; i < 32768; i++)
       {
	  if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR_RET;
	  if (nc_close(ncid)) ERR_RET;
       }
       if (nc_create(FILE_NAME, 0, &ncid)) ERR_RET;
       if (nc_def_var(ncid, "blah", NC_CHAR, 0, NULL, NULL)) ERR;
       if (nc_close(ncid)) ERR_RET;
       /*printf("last ncid: %d\n", ncid);*/
    }
    SUMMARIZE_ERR;
#endif
    FINAL_RESULTS;
}