File: tst_rename2.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 (140 lines) | stat: -rw-r--r-- 4,524 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
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
/*
 * Test more renames of vars and dims.
 *
 * Ed Hartnett
 */

#include "nc_tests.h"
#include "err_macros.h"

#define TEST_NAME "tst_rename"
#define LAT "lat"
#define LON "lon"
#define LEV "lev"
#define DIM_X "x"
#define DIM_Y "y"
#define DIM_Z "z"

#define DIM1_LEN 4
#define NDIM1 1
#define NDIM3 3
#define NUM_ENDDEF_SETTINGS 2

int
main(int argc, char **argv)
{
#define NUM_FORMATS 2
   int formats[NUM_FORMATS] = {NC_FORMAT_NETCDF4, NC_FORMAT_NETCDF4_CLASSIC};
   int format;

   fprintf(stderr,"*** Testing more renames\n");

   for (format = 0; format < NUM_FORMATS; format++)
   {
      fprintf(stderr,"*** test renaming 3 dimensions with format %d...",
              formats[format]);
      {
         char filename[NC_MAX_NAME + 1];
         int ncid, dimid[NDIM3];
         int dimid_in;
         int enddef_setting;

         if (nc_set_default_format(formats[format], NULL)) ERR;

         for (enddef_setting = 0; enddef_setting < NUM_ENDDEF_SETTINGS;
              enddef_setting++)
         {
            sprintf(filename, "%s_%d_%d.nc", TEST_NAME, formats[format],
                    enddef_setting);
            
            /* Create file with three dims. */
            if (nc_create(filename, 0, &ncid)) ERR;
            if (nc_def_dim(ncid, LAT, DIM1_LEN, &dimid[0])) ERR;
            if (nc_def_dim(ncid, LON, DIM1_LEN, &dimid[1])) ERR;
            if (nc_def_dim(ncid, LEV, DIM1_LEN, &dimid[2])) ERR;

            if (enddef_setting)
            {
               if (nc_enddef(ncid)) ERR;
               if (nc_redef(ncid)) ERR;
            }
            
            /* Rename the dimensions. */
            if (nc_rename_dim(ncid, 0, DIM_X)) ERR;
            if (nc_rename_dim(ncid, 1, DIM_Y)) ERR;
            if (nc_rename_dim(ncid, 2, DIM_Z)) ERR;
            
            /* Close the file. */
            if (nc_close(ncid)) ERR;
            
            /* Reopen the file and check. */
            if (nc_open(filename, NC_NOWRITE, &ncid)) ERR;
            if (nc_inq_dimid(ncid, DIM_X, &dimid_in)) ERR;
            if (dimid_in != 0) ERR;
            if (nc_inq_dimid(ncid, DIM_Y, &dimid_in)) ERR;
            if (dimid_in != 1) ERR;
            if (nc_inq_dimid(ncid, DIM_Z, &dimid_in)) ERR;
            if (dimid_in != 2) ERR;
            if (nc_close(ncid)) ERR;
         } /* next enddef setting */
      }
      SUMMARIZE_ERR;

      fprintf(stderr,"*** test renaming 3 dims with coord data format %d...",
              formats[format]);
      {
         char filename[NC_MAX_NAME + 1];
         int ncid, dimid[NDIM3], varid[NDIM3];
         int dimid_in;
         int lat_data[DIM1_LEN] = {0, 1, 2, 3};
         int lon_data[DIM1_LEN] = {0, 10, 20, 30};
         int lev_data[DIM1_LEN] = {0, 100, 200, 300};

         if (nc_set_default_format(formats[format], NULL)) ERR;

         sprintf(filename, "%s_data_%d.nc", TEST_NAME, formats[format]);
         
         /* Create file with three dims. */
         if (nc_create(filename, 0, &ncid)) ERR;
         if (nc_def_dim(ncid, LAT, DIM1_LEN, &dimid[0])) ERR;
         if (nc_def_dim(ncid, LON, DIM1_LEN, &dimid[1])) ERR;
         if (nc_def_dim(ncid, LEV, DIM1_LEN, &dimid[2])) ERR;

         /* Define coordinate data vars. */
         if (nc_def_var(ncid, LAT, NC_INT, NDIM1, &dimid[0], &varid[0])) ERR;
         if (nc_def_var(ncid, LON, NC_INT, NDIM1, &dimid[1], &varid[1])) ERR;
         if (nc_def_var(ncid, LEV, NC_INT, NDIM1, &dimid[2], &varid[2])) ERR;

         if (nc_enddef(ncid)) ERR;

         if (nc_put_var(ncid, 0, lat_data)) ERR;
         if (nc_put_var(ncid, 1, lon_data)) ERR;
         if (nc_put_var(ncid, 2, lev_data)) ERR;

         if (nc_close(ncid)) ERR;
         if (nc_open(filename, NC_WRITE, &ncid)) ERR;
         if (nc_redef(ncid)) ERR;
         
         /* Rename the dimensions. */
         if (nc_rename_dim(ncid, 0, DIM_X)) ERR;
         if (nc_rename_dim(ncid, 1, DIM_Y)) ERR;
         if (nc_rename_dim(ncid, 2, DIM_Z)) ERR;
         
         /* Close the file. */
         if (nc_close(ncid)) ERR;
         
         /* Reopen the file and check. */
         if (nc_open(filename, NC_NOWRITE, &ncid)) ERR;
         if (nc_inq_dimid(ncid, DIM_X, &dimid_in)) ERR;
         if (dimid_in != 0) ERR;
         if (nc_inq_dimid(ncid, DIM_Y, &dimid_in)) ERR;
         if (dimid_in != 1) ERR;
         if (nc_inq_dimid(ncid, DIM_Z, &dimid_in)) ERR;
         if (dimid_in != 2) ERR;
         if (nc_close(ncid)) ERR;
      }
      SUMMARIZE_ERR;
      
   } /* next format */
   FINAL_RESULTS;
}