1 /* This is part of the netCDF package. Copyright 2008 University
2 Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
3 conditions of use. See www.unidata.ucar.edu for more info.
4 */
5
6 /*
7 Test _NCProperties and other special attributes
8 */
9
10 #include "config.h"
11 #ifdef HAVE_UNISTD_H
12 #include "unistd.h"
13 #endif
14
15 #include <hdf5.h>
16 #include "nc_tests.h"
17 #include "err_macros.h"
18 #include "netcdf.h"
19 #include "nc4internal.h"
20
21 #define NC4FILE "nc4_fileinfo.nc"
22 #define HDFFILE "hdf5_fileinfo.hdf"
23 #define INT_ATT_NAME "int_attr"
24 #define INT_VAR_NAME "int_var"
25 #define GROUPNAME "subgroup"
26 #define DIMNAME "time"
27
28 /*
29 Effective cdl:
30
31 netcdf nc4_fileinfo {
32 dimensions:
33 time = 4;
34 variables:
35 int :int_attr;
36 int int_var;
37 int int_var:int_attr;
38 char int_var:_NCProperties;
39 int time(time);
40 group subgroup: {
41 int :int_attr;
42 char :_NCProperties;
43 }
44 }
45 */
46
47 int
main(int argc,char ** argv)48 main(int argc, char **argv)
49 {
50 printf("\n*** Testing 'Fileinfo attributes.\n");
51
52 {
53 hid_t fileid;
54 hid_t fcplid;
55 hid_t scalar_spaceid;
56
57 printf("*** creating test file using HDF5 directly %s...", HDFFILE);
58
59 /* Create scalar dataspace */
60 if((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
61
62 /* Set creation ordering for file, so we can revise its contents later */
63 if((fcplid = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
64 if(H5Pset_link_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
65 if(H5Pset_attr_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
66
67 /* Create new file, using default properties */
68 if((fileid = H5Fcreate(HDFFILE, H5F_ACC_TRUNC, fcplid, H5P_DEFAULT)) < 0) ERR;
69 /* Close file creation property list */
70 if(H5Pclose(fcplid) < 0) ERR;
71
72 /* Add attributes to root group */
73 {
74 hid_t scalar_spaceid = -1;
75 hid_t attid = -1;
76
77 /* Create scalar dataspace */
78 if((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
79
80 /* Create attribute with native integer datatype on object */
81 if((attid = H5Acreate2(fileid, INT_ATT_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR;
82 if(H5Aclose(attid) < 0) ERR;
83
84 /* Clean up objects created */
85 if(H5Sclose(scalar_spaceid) < 0) ERR;
86 }
87
88 /* Close rest */
89 if(H5Sclose(scalar_spaceid) < 0) ERR;
90 if(H5Fclose(fileid) < 0) ERR;
91 }
92
93 {
94 int root, grpid, varid, stat, natts, id;
95 int data = 17;
96 const char* sdata = "text";
97 char ncprops[8192];
98 size_t len;
99 int dimid;
100 nc_type xtype;
101 char name[NC_MAX_NAME];
102
103 printf("\n*** creating netcdf-4 test file using netCDF %s...", NC4FILE);
104
105 if(nc_create(NC4FILE,NC_WRITE|NC_CLOBBER|NC_NETCDF4,&root)!=0) ERR;
106 /* Create global attribute */
107 if(nc_put_att_int(root,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
108 /* Create global variable */
109 if(nc_def_var(root,INT_VAR_NAME,NC_INT,0,NULL,&varid)!=0) ERR;
110 /* Create attribute on var */
111 if(nc_put_att_int(root,varid,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
112 /* Create global subgroup */
113 if(nc_def_grp(root,GROUPNAME,&grpid)!=0) ERR;
114 /* Create global attribute in the group */
115 if(nc_put_att_int(grpid,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
116 /* Create var + dimension to cause e.g. dimscales to appear */
117 if(nc_def_dim(root,DIMNAME,(size_t)4,&dimid)!=0) ERR;
118 if(nc_def_var(root,DIMNAME,NC_INT,1,&dimid,&varid)!=0) ERR; /* same name */
119 /* Close, then re-open */
120 if(nc_close(root)) ERR;
121 if(nc_open(NC4FILE,NC_WRITE|NC_NETCDF4,&root)!=0) ERR;
122
123 /* Is all invisible attributes actually invisible vis-a-vis nc_inq? */
124 if(nc_inq(root,NULL,NULL,&natts,NULL)!=0) ERR;
125 if(natts != 1) ERR;
126
127 /* Now, fiddle with the NCPROPS attribute */
128
129 /* Get its metadata */
130 if(nc_inq_att(root,NC_GLOBAL,NCPROPS,&xtype,&len)!=0) ERR;
131 if(xtype != NC_CHAR) ERR;
132
133 /* Read in two ways */
134 if(nc_get_att_text(root,NC_GLOBAL,NCPROPS,ncprops)!=0) ERR;
135 if(strlen(ncprops) != len) ERR;
136
137 /* Attempt to get attribute metadata piecemeal; some will fail */
138 id = -1;
139 stat = nc_inq_attid(root,NC_GLOBAL,NCPROPS,&id);
140 if(stat == NC_NOERR) ERR;
141 stat = nc_inq_attname(root,NC_GLOBAL,id,name);
142 if(stat == NC_NOERR) ERR;
143 if(nc_inq_atttype(root,NC_GLOBAL,NCPROPS,&xtype)!=0) ERR;
144 if(xtype != NC_CHAR) ERR;
145 if(nc_inq_attlen(root,NC_GLOBAL,NCPROPS,&len)!=0) ERR;
146 if(len != strlen(ncprops)) ERR;
147
148 /*Overwrite _NCProperties root attribute; should fail */
149 stat = nc_put_att_text(root,NC_GLOBAL,NCPROPS,strlen(sdata),sdata);
150 if(stat == NC_NOERR) ERR;
151
152 /* Delete; should fail */
153 stat = nc_del_att(root,NC_GLOBAL,NCPROPS);
154 if(stat != NC_ENOTATT) ERR;
155
156 /* Ditto _SuperblockVersion */
157
158 /* Get its metadata */
159 if(nc_inq_att(root,NC_GLOBAL,SUPERBLOCKATT,&xtype,&len)!=0) ERR;
160 if(xtype != NC_INT) ERR;
161 if(len != 1) ERR;
162
163 if(nc_get_att_int(root,NC_GLOBAL,SUPERBLOCKATT,&data)!=0) ERR;
164
165 /* Attempt to get attribute metadata piecemeal */
166 stat = nc_inq_attid(root,NC_GLOBAL,SUPERBLOCKATT,&id);
167 if(stat == NC_NOERR) ERR;
168 stat = nc_inq_attname(root,NC_GLOBAL,id,name);
169 if(stat == NC_NOERR) ERR;
170 if(nc_inq_atttype(root,NC_GLOBAL,SUPERBLOCKATT,&xtype)!=0) ERR;
171 if(xtype != NC_INT) ERR;
172 if(nc_inq_attlen(root,NC_GLOBAL,SUPERBLOCKATT,&len)!=0) ERR;
173 if(len != 1) ERR;
174
175 /*Overwrite; should fail */
176 stat = nc_put_att_int(root,NC_GLOBAL,NCPROPS,NC_INT,1,&data);
177 if(stat == NC_NOERR) ERR;
178
179 /* Delete; should fail */
180 stat = nc_del_att(root,NC_GLOBAL,SUPERBLOCKATT);
181 if(stat == NC_NOERR) ERR;
182
183 /* Ditto _IsNetcdf4 */
184
185 /* Get its metadata */
186 if(nc_inq_att(root,NC_GLOBAL,ISNETCDF4ATT,&xtype,&len)!=0) ERR;
187 if(xtype != NC_INT) ERR;
188 if(len != 1) ERR;
189
190 if(nc_get_att_int(root,NC_GLOBAL,ISNETCDF4ATT,&data)!=0) ERR;
191
192 /* Attempt to get attribute metadata piecemeal */
193 stat = nc_inq_attid(root,NC_GLOBAL,ISNETCDF4ATT,&id);
194 if(stat == NC_NOERR) ERR;
195 stat = nc_inq_attname(root,NC_GLOBAL,id,name);
196 if(stat == NC_NOERR) ERR;
197 if(nc_inq_atttype(root,NC_GLOBAL,ISNETCDF4ATT,&xtype)!=0) ERR;
198 if(xtype != NC_INT) ERR;
199 if(nc_inq_attlen(root,NC_GLOBAL,ISNETCDF4ATT,&len)!=0) ERR;
200 if(len != 1) ERR;
201
202 /*Overwrite; should fail */
203 stat = nc_put_att_int(root,NC_GLOBAL,ISNETCDF4ATT,NC_INT,1,&data);
204 if(stat == NC_NOERR) ERR;
205
206 /* Delete; should fail */
207 stat = nc_del_att(root,NC_GLOBAL,ISNETCDF4ATT);
208 if(stat == NC_NOERR) ERR;
209
210 if(nc_close(root)!=0) ERR;
211 }
212
213 SUMMARIZE_ERR;
214 FINAL_RESULTS;
215 }
216