forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc4info.c
289 lines (265 loc) · 7.73 KB
/
nc4info.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* @file
* @internal Add provenance info for netcdf-4 files.
*
* Copyright 2010, UCAR/Unidata See netcdf/COPYRIGHT file for copying
* and redistribution conditions.
* @author Dennis Heimbigner
*/
#include "config.h"
#include "hdf5internal.h"
#define HDF5_MAX_NAME 1024 /**< HDF5 max name. */
/** @internal Check NetCDF return code. */
#define NCHECK(expr) {if((expr)!=NC_NOERR) {goto done;}}
/** @internal Check HDF5 return code. */
#define HCHECK(expr) {if((expr)<0) {ncstat = NC_EHDFERR; goto done;}}
struct NCPROPINFO globalpropinfo; /**< Global property info. */
/**
* @internal Initialize file info.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner
*/
int
NC4_fileinfo_init(void)
{
int stat = NC_NOERR;
unsigned major,minor,release;
/* Build nc properties */
memset((void*)&globalpropinfo,0,sizeof(globalpropinfo));
globalpropinfo.version = NCPROPS_VERSION;
globalpropinfo.netcdfver[0] = '\0';
globalpropinfo.hdf5ver[0] = '\0';
stat = NC4_hdf5get_libversion(&major,&minor,&release);
if(stat) goto done;
snprintf(globalpropinfo.hdf5ver,sizeof(globalpropinfo.hdf5ver),
"%1u.%1u.%1u",major,minor,release);
strncpy(globalpropinfo.netcdfver,PACKAGE_VERSION,sizeof(globalpropinfo.netcdfver));
done:
return stat;
}
/**
* @internal Parse file properties.
*
* @param ncprops Property info.
* @param text Text properties.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner
*/
static int
NC4_properties_parse(struct NCPROPINFO* ncprops, const char* text)
{
int ret = NC_NOERR;
size_t len;
char* p;
char* propdata = NULL;
ncprops->version = 0;
ncprops->hdf5ver[0] = '\0';
ncprops->netcdfver[0] = '\0';
len = strlen(text);
if(len == 0) return NC_NOERR;
propdata = (char*)malloc(len+1);
if(propdata == NULL) return NC_ENOMEM;
memcpy(propdata,text,len+1);
propdata[len] = '\0'; /* guarantee */
/* Walk and fill in ncinfo */
p = propdata;
while(*p) {
char* name = p;
char* value = NULL;
char* q = strchr(p,'=');
if(q == NULL)
{ret = NC_EINVAL; goto done;}
*q++ = '\0';
value = p = q;
q = strchr(p,NCPROPSSEP);
if(q == NULL) q = (p+strlen(p)); else* q++ = '\0';
p = q;
if(value != NULL) {
if(strcmp(name,NCPVERSION) == 0) {
int v = atoi(value);
if(v < 0) v = 0;
ncprops->version = v;
} else if(strcmp(name,NCPNCLIBVERSION) == 0)
strncpy(ncprops->netcdfver,value,sizeof(ncprops->netcdfver)-1);
else if(strcmp(name,NCPHDF5LIBVERSION) == 0)
strncpy(ncprops->hdf5ver,value,sizeof(ncprops->hdf5ver)-1);
/* else ignore */
}
}
/* Guarantee null term */
ncprops->netcdfver[sizeof(ncprops->netcdfver)-1] = '\0';
ncprops->hdf5ver[sizeof(ncprops->hdf5ver)-1] = '\0';
done:
if(propdata != NULL) free(propdata);
return ret;
}
/**
* @internal Get properties attribure.
*
* @param h5 Pointer to HDF5 file info struct.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner
*/
static int
NC4_get_propattr(NC_FILE_INFO_T* h5)
{
int ncstat = NC_NOERR;
size_t size;
H5T_class_t t_class;
hid_t grp = -1;
hid_t attid = -1;
hid_t aspace = -1;
hid_t atype = -1;
hid_t ntype = -1;
char* text = NULL;
/* Get root group */
grp = h5->root_grp->hdf_grpid; /* get root group */
/* Try to extract the NCPROPS attribute */
if(H5Aexists(grp,NCPROPS) > 0) { /* Does exist */
attid = H5Aopen_name(grp, NCPROPS);
aspace = H5Aget_space(attid); /* dimensions of attribute data */
atype = H5Aget_type(attid);
/* Verify that atype and size */
t_class = H5Tget_class(atype);
if(t_class != H5T_STRING) {ncstat = NC_EATTMETA; goto done;}
size = H5Tget_size(atype);
if(size == 0) goto done;
text = (char*)malloc(size+1);
if(text == NULL)
{ncstat = NC_ENOMEM; goto done;}
HCHECK((ntype = H5Tget_native_type(atype, H5T_DIR_ASCEND)));
HCHECK((H5Aread(attid, ntype, text)));
/* Make sure its null terminated */
text[size] = '\0';
/* Try to parse text */
ncstat = NC4_properties_parse(&h5->fileinfo->propattr,text);
}
done:
if(attid >= 0) HCHECK((H5Aclose(attid)));
if(aspace >= 0) HCHECK((H5Sclose(aspace)));
if(ntype >= 0) HCHECK((H5Tclose(ntype)));
if(atype >= 0) HCHECK((H5Tclose(atype)));
if(text != NULL) free(text);
return ncstat;
}
/**
* @internal Write the properties attribute to file.
*
* @param h5 Pointer to HDF5 file info struct.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner
*/
int
NC4_put_propattr(NC_FILE_INFO_T* h5)
{
int ncstat = NC_NOERR;
hid_t grp = -1;
hid_t attid = -1;
hid_t aspace = -1;
hid_t atype = -1;
char* text = NULL;
/* Get root group */
grp = h5->root_grp->hdf_grpid; /* get root group */
/* See if the NCPROPS attribute exists */
if(H5Aexists(grp,NCPROPS) == 0) { /* Does not exist */
ncstat = NC4_buildpropinfo(&h5->fileinfo->propattr,&text);
if(text == NULL || ncstat != NC_NOERR) {
goto done;
}
/* Create a datatype to refer to. */
HCHECK((atype = H5Tcopy(H5T_C_S1)));
HCHECK((H5Tset_cset(atype, H5T_CSET_ASCII)));
HCHECK((H5Tset_size(atype, strlen(text)+1))); /*keep nul term */
HCHECK((aspace = H5Screate(H5S_SCALAR)));
HCHECK((attid = H5Acreate(grp, NCPROPS, atype, aspace, H5P_DEFAULT)));
HCHECK((H5Awrite(attid, atype, text)));
}
done:
if(text != NULL) {
free(text);
text = NULL;
}
if(attid >= 0) HCHECK((H5Aclose(attid)));
if(aspace >= 0) HCHECK((H5Sclose(aspace)));
if(atype >= 0) HCHECK((H5Tclose(atype)));
return ncstat;
}
/**
* @internal
*
* @param h5 Pointer to HDF5 file info struct.
* @param init Initialization.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner
*/
int
NC4_get_fileinfo(NC_FILE_INFO_T* h5, struct NCPROPINFO* init)
{
int ncstat = NC_NOERR;
/* Allocate the fileinfo in h5 */
h5->fileinfo = (struct NCFILEINFO*)calloc(1,sizeof(struct NCFILEINFO));
if(h5->fileinfo == NULL)
{ncstat = NC_ENOMEM; goto done;}
/* Get superblock version */
NCHECK((ncstat = NC4_hdf5get_superblock(h5,&h5->fileinfo->superblockversion)));
/* Get properties attribute if not already defined */
if(init == NULL) {
NCHECK((ncstat = NC4_get_propattr(h5)));
} else { /*init != NULL*/
h5->fileinfo->propattr = *init; /* Initialize */
}
done:
return ncstat;
}
/**
* @internal Build properties attribute.
*
* @param info Properties info.
* @param propdatap Pointer that gets properties data.
*
* @return ::NC_NOERR No error.
* @author Dennis Heimbigner
*/
int
NC4_buildpropinfo(struct NCPROPINFO* info,char** propdatap)
{
size_t total;
char* propdata = NULL;
if(info == NULL || info->version == 0) return NC_EINVAL;
if(propdatap == NULL)
return NC_NOERR;
*propdatap = NULL;
/* compute attribute length */
total = 0;
total += strlen(NCPVERSION);
total += strlen("=00000000");
if(strlen(info->netcdfver) > 0) {
total += 1; /*|NCPROPSEP|*/
total += strlen(NCPNCLIBVERSION);
total += strlen("=");
total += strlen(info->netcdfver);
}
if(strlen(info->hdf5ver) > 0) {
total += 1; /*|NCPROPSEP|*/
total += strlen(NCPHDF5LIBVERSION);
total += strlen("=");
total += strlen(info->hdf5ver);
}
propdata = (char*)malloc(total+1);
if(propdata == NULL)
return NC_ENOMEM;
snprintf(propdata,total+1,
"%s=%d|%s=%s|%s=%s",
NCPVERSION,info->version,
NCPNCLIBVERSION,info->netcdfver,
NCPHDF5LIBVERSION,info->hdf5ver);
/* Force null termination */
propdata[total] = '\0';
*propdatap = propdata;
return NC_NOERR;
}