-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathtst_fileinfo.c
215 lines (174 loc) · 6.26 KB
/
tst_fileinfo.c
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* This is part of the netCDF package. Copyright 2018 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use. See www.unidata.ucar.edu for more info.
*/
/*
Test _NCProperties and other special attributes
*/
#include "config.h"
#ifdef HAVE_UNISTD_H
#include "unistd.h"
#endif
#include <hdf5.h>
#include "nc_tests.h"
#include "err_macros.h"
#include "netcdf.h"
#include "nc4internal.h"
#define NC4FILE "nc4_fileinfo.nc"
#define HDFFILE "hdf5_fileinfo.hdf"
#define INT_ATT_NAME "int_attr"
#define INT_VAR_NAME "int_var"
#define GROUPNAME "subgroup"
#define DIMNAME "time"
/*
Effective cdl:
netcdf nc4_fileinfo {
dimensions:
time = 4;
variables:
int :int_attr;
int int_var;
int int_var:int_attr;
char int_var:_NCProperties;
int time(time);
group subgroup: {
int :int_attr;
char :_NCProperties;
}
}
*/
int
main(int argc, char **argv)
{
printf("\n*** Testing 'Fileinfo attributes.\n");
{
hid_t fileid;
hid_t fcplid;
hid_t scalar_spaceid;
printf("*** creating test file using HDF5 directly %s...", HDFFILE);
/* Create scalar dataspace */
if((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
/* Set creation ordering for file, so we can revise its contents later */
if((fcplid = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
if(H5Pset_link_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
if(H5Pset_attr_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR;
/* Create new file, using default properties */
if((fileid = H5Fcreate(HDFFILE, H5F_ACC_TRUNC, fcplid, H5P_DEFAULT)) < 0) ERR;
/* Close file creation property list */
if(H5Pclose(fcplid) < 0) ERR;
/* Add attributes to root group */
{
hid_t scalar_spaceid = -1;
hid_t attid = -1;
/* Create scalar dataspace */
if((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR;
/* Create attribute with native integer datatype on object */
if((attid = H5Acreate2(fileid, INT_ATT_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR;
if(H5Aclose(attid) < 0) ERR;
/* Clean up objects created */
if(H5Sclose(scalar_spaceid) < 0) ERR;
}
/* Close rest */
if(H5Sclose(scalar_spaceid) < 0) ERR;
if(H5Fclose(fileid) < 0) ERR;
}
{
int root, grpid, varid, stat, natts, id;
int data = 17;
const char* sdata = "text";
char ncprops[8192];
size_t len;
int dimid;
nc_type xtype;
char name[NC_MAX_NAME];
printf("\n*** creating netcdf-4 test file using netCDF %s...", NC4FILE);
if(nc_create(NC4FILE,NC_WRITE|NC_CLOBBER|NC_NETCDF4,&root)!=0) ERR;
/* Create global attribute */
if(nc_put_att_int(root,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
/* Create global variable */
if(nc_def_var(root,INT_VAR_NAME,NC_INT,0,NULL,&varid)!=0) ERR;
/* Create attribute on var */
if(nc_put_att_int(root,varid,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
/* Create global subgroup */
if(nc_def_grp(root,GROUPNAME,&grpid)!=0) ERR;
/* Create global attribute in the group */
if(nc_put_att_int(grpid,NC_GLOBAL,INT_ATT_NAME,NC_INT,1,&data)!=0) ERR;
/* Create var + dimension to cause e.g. dimscales to appear */
if(nc_def_dim(root,DIMNAME,(size_t)4,&dimid)!=0) ERR;
if(nc_def_var(root,DIMNAME,NC_INT,1,&dimid,&varid)!=0) ERR; /* same name */
/* Close, then re-open */
if(nc_close(root)) ERR;
if(nc_open(NC4FILE,NC_WRITE|NC_NETCDF4,&root)!=0) ERR;
/* Is all invisible attributes actually invisible vis-a-vis nc_inq? */
if(nc_inq(root,NULL,NULL,&natts,NULL)!=0) ERR;
if(natts != 1) ERR;
/* Now, fiddle with the NCPROPS attribute */
/* Get its metadata */
if(nc_inq_att(root,NC_GLOBAL,NCPROPS,&xtype,&len)!=0) ERR;
if(xtype != NC_CHAR) ERR;
/* Read in two ways */
if(nc_get_att_text(root,NC_GLOBAL,NCPROPS,ncprops)!=0) ERR;
if(strlen(ncprops) != len) ERR;
/* Attempt to get attribute metadata piecemeal; some will fail */
id = -1;
stat = nc_inq_attid(root,NC_GLOBAL,NCPROPS,&id);
if(stat == NC_NOERR) ERR;
stat = nc_inq_attname(root,NC_GLOBAL,id,name);
if(stat == NC_NOERR) ERR;
if(nc_inq_atttype(root,NC_GLOBAL,NCPROPS,&xtype)!=0) ERR;
if(xtype != NC_CHAR) ERR;
if(nc_inq_attlen(root,NC_GLOBAL,NCPROPS,&len)!=0) ERR;
if(len != strlen(ncprops)) ERR;
/*Overwrite _NCProperties root attribute; should fail */
stat = nc_put_att_text(root,NC_GLOBAL,NCPROPS,strlen(sdata),sdata);
if(stat == NC_NOERR) ERR;
/* Delete; should fail */
stat = nc_del_att(root,NC_GLOBAL,NCPROPS);
if(stat != NC_ENOTATT) ERR;
/* Ditto _SuperblockVersion */
/* Get its metadata */
if(nc_inq_att(root,NC_GLOBAL,SUPERBLOCKATT,&xtype,&len)!=0) ERR;
if(xtype != NC_INT) ERR;
if(len != 1) ERR;
if(nc_get_att_int(root,NC_GLOBAL,SUPERBLOCKATT,&data)!=0) ERR;
/* Attempt to get attribute metadata piecemeal */
stat = nc_inq_attid(root,NC_GLOBAL,SUPERBLOCKATT,&id);
if(stat == NC_NOERR) ERR;
stat = nc_inq_attname(root,NC_GLOBAL,id,name);
if(stat == NC_NOERR) ERR;
if(nc_inq_atttype(root,NC_GLOBAL,SUPERBLOCKATT,&xtype)!=0) ERR;
if(xtype != NC_INT) ERR;
if(nc_inq_attlen(root,NC_GLOBAL,SUPERBLOCKATT,&len)!=0) ERR;
if(len != 1) ERR;
/*Overwrite; should fail */
stat = nc_put_att_int(root,NC_GLOBAL,NCPROPS,NC_INT,1,&data);
if(stat == NC_NOERR) ERR;
/* Delete; should fail */
stat = nc_del_att(root,NC_GLOBAL,SUPERBLOCKATT);
if(stat == NC_NOERR) ERR;
/* Ditto _IsNetcdf4 */
/* Get its metadata */
if(nc_inq_att(root,NC_GLOBAL,ISNETCDF4ATT,&xtype,&len)!=0) ERR;
if(xtype != NC_INT) ERR;
if(len != 1) ERR;
if(nc_get_att_int(root,NC_GLOBAL,ISNETCDF4ATT,&data)!=0) ERR;
/* Attempt to get attribute metadata piecemeal */
stat = nc_inq_attid(root,NC_GLOBAL,ISNETCDF4ATT,&id);
if(stat == NC_NOERR) ERR;
stat = nc_inq_attname(root,NC_GLOBAL,id,name);
if(stat == NC_NOERR) ERR;
if(nc_inq_atttype(root,NC_GLOBAL,ISNETCDF4ATT,&xtype)!=0) ERR;
if(xtype != NC_INT) ERR;
if(nc_inq_attlen(root,NC_GLOBAL,ISNETCDF4ATT,&len)!=0) ERR;
if(len != 1) ERR;
/*Overwrite; should fail */
stat = nc_put_att_int(root,NC_GLOBAL,ISNETCDF4ATT,NC_INT,1,&data);
if(stat == NC_NOERR) ERR;
/* Delete; should fail */
stat = nc_del_att(root,NC_GLOBAL,ISNETCDF4ATT);
if(stat == NC_NOERR) ERR;
if(nc_close(root)!=0) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}