File: tst_sync.c

package info (click to toggle)
netcdf 1%3A4.1.3-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,032 kB
  • sloc: ansic: 169,389; fortran: 17,742; sh: 13,203; cpp: 10,960; f90: 7,903; yacc: 2,832; xml: 2,129; makefile: 2,027; lex: 1,210
file content (95 lines) | stat: -rw-r--r-- 3,309 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* This is part of the netCDF package.  Copyright 2011 University
   Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
   conditions of use.

   Test netcdf-4 syncs. 
*/

#include <nc_tests.h>

#define FILE_NAME "tst_sync.nc"
#define DIM_NAME "x"
#define VAR1_NAME "var1"
#define VAR2_NAME "var2"

int
main(int argc, char **argv)
{
   printf("\n*** Testing netcdf-4 variable syncing.\n");
   printf("**** testing that sync works in netCDF-4...");
   {
      int ncid, var1_id, var2_id, var3_id, dimid;
      int ndims, nvars, natts, unlimdimid, dimid_in;
      nc_type xtype_in;
      char name_in[NC_MAX_NAME + 1];

      /* Create a file with one dim, and two vars that use it. For
       * fun, do a sync between the def_var calls. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, 1, &dimid)) ERR;
      if (nc_def_var(ncid, VAR1_NAME, NC_FLOAT, 1, &dimid, &var1_id)) ERR;
      if (nc_sync(ncid)) ERR;
      if (nc_def_var(ncid, VAR2_NAME, NC_FLOAT, 1, &dimid, &var2_id)) ERR;
      
      /* Now define a coordinate variable for the dimension. */
      if (nc_def_var(ncid, DIM_NAME, NC_FLOAT, 1, &dimid, &var3_id)) ERR;
      if (nc_close(ncid)) ERR;

      /* Reopen the file and check it. */
      if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
      if (nc_inq(ncid, &ndims, &nvars, &natts, &unlimdimid)) ERR;
      if (ndims != 1 || nvars != 3 || natts != 0 || unlimdimid != -1) ERR;
      if (nc_inq_var(ncid, 2, name_in, &xtype_in, &ndims, &dimid_in, &natts)) ERR;
      if (strcmp(name_in, DIM_NAME) || xtype_in != NC_FLOAT || ndims != 1 || 
	  dimid_in != 0 || natts != 0) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** testing that sync works in netCDF-4, with test contributed by Jeff W....");
   {
      int ncid, var1_id, var2_id, var3_id, dimid;

      /* Create a file with one dim and two variables. */
      if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, 1, &dimid)) ERR;
      if (nc_def_var(ncid, VAR1_NAME, NC_FLOAT, 1, &dimid, &var1_id)) ERR;
      if (nc_def_var(ncid, VAR2_NAME, NC_FLOAT, 1,&dimid, &var2_id)) ERR;

      /* Sync the file and add a coordinate variable. */
      if (nc_sync(ncid)) ERR;
      if (nc_def_var(ncid, DIM_NAME, NC_FLOAT, 1, &dimid, &var3_id)) ERR;

      /* Close the file. */
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   printf("**** testing that sync works in netCDF-4, with test contributed by Jeff W....");
   {
      int ncid, var1_id, var2_id, var3_id, dimid;
      float xx;

      if (nc_create(FILE_NAME, NC_NETCDF4 | NC_CLOBBER, &ncid)) ERR;
      if (nc_def_dim(ncid, DIM_NAME, 1, &dimid)) ERR;
      if (nc_def_var(ncid, VAR1_NAME, NC_FLOAT, 1, &dimid, &var1_id)) ERR;
      xx = 1.0;
      if (nc_put_var_float(ncid, var1_id, &xx)) ERR;
      if (nc_sync(ncid)) ERR;
      if (nc_def_var(ncid, VAR2_NAME, NC_FLOAT, 1,&dimid, &var2_id)) ERR;
      xx = 2.0;
      if (nc_put_var_float(ncid, var2_id, &xx)) ERR;
      if (nc_sync(ncid)) ERR;
      if (nc_def_var(ncid, DIM_NAME, NC_FLOAT, 1, &dimid, &var3_id)) ERR;
      xx = 3.0;
      if (nc_put_var_float(ncid, var3_id, &xx)) ERR;
      if (nc_sync(ncid)) ERR;
      if (nc_close(ncid)) ERR;
   }
   SUMMARIZE_ERR;
   FINAL_RESULTS;
}