2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2018-07-16 18:50:15 +08:00
|
|
|
|
2016-05-04 11:17:06 +08:00
|
|
|
#include "config.h"
|
2018-07-12 21:05:21 +08:00
|
|
|
#include "hdf5internal.h"
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
#define HDF5_MAX_NAME 1024 /**< HDF5 max name. */
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/** @internal Check NetCDF return code. */
|
2016-05-04 11:17:06 +08:00
|
|
|
#define NCHECK(expr) {if((expr)!=NC_NOERR) {goto done;}}
|
2017-12-05 03:21:14 +08:00
|
|
|
|
|
|
|
/** @internal Check HDF5 return code. */
|
2016-05-04 11:17:06 +08:00
|
|
|
#define HCHECK(expr) {if((expr)<0) {ncstat = NC_EHDFERR; goto done;}}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
struct NCPROPINFO globalpropinfo; /**< Global property info. */
|
2016-05-04 11:17:06 +08:00
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @internal Initialize file info.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-05-04 11:17:06 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @internal Parse file properties.
|
|
|
|
*
|
|
|
|
* @param ncprops Property info.
|
|
|
|
* @param text Text properties.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-05-04 11:17:06 +08:00
|
|
|
static int
|
2016-08-08 23:24:19 +08:00
|
|
|
NC4_properties_parse(struct NCPROPINFO* ncprops, const char* text)
|
2016-05-04 11:17:06 +08:00
|
|
|
{
|
2016-08-08 23:24:19 +08:00
|
|
|
int ret = NC_NOERR;
|
2016-05-04 11:17:06 +08:00
|
|
|
size_t len;
|
|
|
|
char* p;
|
2016-08-08 23:24:19 +08:00
|
|
|
char* propdata = NULL;
|
2016-05-04 11:17:06 +08:00
|
|
|
|
|
|
|
ncprops->version = 0;
|
|
|
|
ncprops->hdf5ver[0] = '\0';
|
|
|
|
ncprops->netcdfver[0] = '\0';
|
|
|
|
|
2016-08-08 23:24:19 +08:00
|
|
|
len = strlen(text);
|
2016-05-04 11:17:06 +08:00
|
|
|
if(len == 0) return NC_NOERR;
|
2016-08-08 23:24:19 +08:00
|
|
|
propdata = (char*)malloc(len+1);
|
|
|
|
if(propdata == NULL) return NC_ENOMEM;
|
|
|
|
memcpy(propdata,text,len+1);
|
|
|
|
propdata[len] = '\0'; /* guarantee */
|
2016-05-04 11:17:06 +08:00
|
|
|
|
|
|
|
/* Walk and fill in ncinfo */
|
|
|
|
p = propdata;
|
|
|
|
while(*p) {
|
2016-09-15 06:11:22 +08:00
|
|
|
char* name = p;
|
|
|
|
char* value = NULL;
|
|
|
|
char* q = strchr(p,'=');
|
|
|
|
if(q == NULL)
|
2016-08-08 23:24:19 +08:00
|
|
|
{ret = NC_EINVAL; goto done;}
|
2016-09-15 06:11:22 +08:00
|
|
|
*q++ = '\0';
|
|
|
|
value = p = q;
|
|
|
|
q = strchr(p,NCPROPSSEP);
|
|
|
|
if(q == NULL) q = (p+strlen(p)); else* q++ = '\0';
|
|
|
|
p = q;
|
|
|
|
if(value != NULL) {
|
2016-05-04 11:17:06 +08:00
|
|
|
if(strcmp(name,NCPVERSION) == 0) {
|
2016-09-15 06:11:22 +08:00
|
|
|
int v = atoi(value);
|
|
|
|
if(v < 0) v = 0;
|
|
|
|
ncprops->version = v;
|
2016-05-04 11:17:06 +08:00
|
|
|
} else if(strcmp(name,NCPNCLIBVERSION) == 0)
|
2016-09-15 06:11:22 +08:00
|
|
|
strncpy(ncprops->netcdfver,value,sizeof(ncprops->netcdfver)-1);
|
2016-05-04 11:17:06 +08:00
|
|
|
else if(strcmp(name,NCPHDF5LIBVERSION) == 0)
|
2016-09-15 06:11:22 +08:00
|
|
|
strncpy(ncprops->hdf5ver,value,sizeof(ncprops->hdf5ver)-1);
|
2016-05-04 11:17:06 +08:00
|
|
|
/* else ignore */
|
2016-09-15 06:11:22 +08:00
|
|
|
}
|
2016-05-04 11:17:06 +08:00
|
|
|
}
|
|
|
|
/* Guarantee null term */
|
|
|
|
ncprops->netcdfver[sizeof(ncprops->netcdfver)-1] = '\0';
|
|
|
|
ncprops->hdf5ver[sizeof(ncprops->hdf5ver)-1] = '\0';
|
2016-08-08 23:24:19 +08:00
|
|
|
done:
|
|
|
|
if(propdata != NULL) free(propdata);
|
|
|
|
return ret;
|
2016-05-04 11:17:06 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @internal Get properties attribure.
|
|
|
|
*
|
|
|
|
* @param h5 Pointer to HDF5 file info struct.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-05-04 11:17:06 +08:00
|
|
|
static int
|
2018-06-22 21:08:09 +08:00
|
|
|
NC4_get_propattr(NC_FILE_INFO_T* h5)
|
2016-05-04 11:17:06 +08:00
|
|
|
{
|
|
|
|
int ncstat = NC_NOERR;
|
|
|
|
size_t size;
|
2016-09-15 06:11:22 +08:00
|
|
|
H5T_class_t t_class;
|
2016-05-04 11:17:06 +08:00
|
|
|
hid_t grp = -1;
|
|
|
|
hid_t attid = -1;
|
|
|
|
hid_t aspace = -1;
|
|
|
|
hid_t atype = -1;
|
|
|
|
hid_t ntype = -1;
|
2016-08-08 23:24:19 +08:00
|
|
|
char* text = NULL;
|
2016-05-04 11:17:06 +08:00
|
|
|
|
|
|
|
/* Get root group */
|
|
|
|
grp = h5->root_grp->hdf_grpid; /* get root group */
|
|
|
|
/* Try to extract the NCPROPS attribute */
|
2016-05-28 00:08:01 +08:00
|
|
|
if(H5Aexists(grp,NCPROPS) > 0) { /* Does exist */
|
|
|
|
attid = H5Aopen_name(grp, NCPROPS);
|
2016-05-04 11:17:06 +08:00
|
|
|
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);
|
2016-08-08 23:24:19 +08:00
|
|
|
if(size == 0) goto done;
|
|
|
|
text = (char*)malloc(size+1);
|
|
|
|
if(text == NULL)
|
|
|
|
{ncstat = NC_ENOMEM; goto done;}
|
2016-05-04 11:17:06 +08:00
|
|
|
HCHECK((ntype = H5Tget_native_type(atype, H5T_DIR_ASCEND)));
|
|
|
|
HCHECK((H5Aread(attid, ntype, text)));
|
2016-08-09 11:54:23 +08:00
|
|
|
/* Make sure its null terminated */
|
|
|
|
text[size] = '\0';
|
2016-05-04 11:17:06 +08:00
|
|
|
/* Try to parse text */
|
2016-08-08 23:24:19 +08:00
|
|
|
ncstat = NC4_properties_parse(&h5->fileinfo->propattr,text);
|
2016-09-15 06:11:22 +08:00
|
|
|
}
|
2016-05-04 11:17:06 +08:00
|
|
|
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)));
|
2016-08-08 23:24:19 +08:00
|
|
|
if(text != NULL) free(text);
|
2016-05-04 11:17:06 +08:00
|
|
|
return ncstat;
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @internal Write the properties attribute to file.
|
|
|
|
*
|
|
|
|
* @param h5 Pointer to HDF5 file info struct.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-05-04 11:17:06 +08:00
|
|
|
int
|
2018-06-22 21:08:09 +08:00
|
|
|
NC4_put_propattr(NC_FILE_INFO_T* h5)
|
2016-05-04 11:17:06 +08:00
|
|
|
{
|
|
|
|
int ncstat = NC_NOERR;
|
|
|
|
hid_t grp = -1;
|
|
|
|
hid_t attid = -1;
|
|
|
|
hid_t aspace = -1;
|
|
|
|
hid_t atype = -1;
|
2016-09-15 06:20:43 +08:00
|
|
|
char* text = NULL;
|
2016-05-04 11:17:06 +08:00
|
|
|
|
|
|
|
/* Get root group */
|
|
|
|
grp = h5->root_grp->hdf_grpid; /* get root group */
|
|
|
|
/* See if the NCPROPS attribute exists */
|
2016-05-28 00:08:01 +08:00
|
|
|
if(H5Aexists(grp,NCPROPS) == 0) { /* Does not exist */
|
2016-09-15 06:20:43 +08:00
|
|
|
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)));
|
2016-05-04 11:17:06 +08:00
|
|
|
}
|
2016-09-15 06:20:43 +08:00
|
|
|
done:
|
2017-06-07 16:45:32 +08:00
|
|
|
if(text != NULL) {
|
|
|
|
free(text);
|
2017-06-16 02:09:54 +08:00
|
|
|
text = NULL;
|
2016-09-15 06:20:43 +08:00
|
|
|
}
|
2016-09-15 06:16:06 +08:00
|
|
|
|
2016-05-04 11:17:06 +08:00
|
|
|
if(attid >= 0) HCHECK((H5Aclose(attid)));
|
|
|
|
if(aspace >= 0) HCHECK((H5Sclose(aspace)));
|
|
|
|
if(atype >= 0) HCHECK((H5Tclose(atype)));
|
|
|
|
return ncstat;
|
|
|
|
}
|
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*
|
|
|
|
* @param h5 Pointer to HDF5 file info struct.
|
|
|
|
* @param init Initialization.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-05-04 11:17:06 +08:00
|
|
|
int
|
2018-06-22 21:08:09 +08:00
|
|
|
NC4_get_fileinfo(NC_FILE_INFO_T* h5, struct NCPROPINFO* init)
|
2016-05-04 11:17:06 +08:00
|
|
|
{
|
|
|
|
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:
|
2016-09-15 06:11:22 +08:00
|
|
|
return ncstat;
|
2016-05-04 11:17:06 +08:00
|
|
|
}
|
2016-08-08 23:24:19 +08:00
|
|
|
|
2017-12-05 03:21:14 +08:00
|
|
|
/**
|
|
|
|
* @internal Build properties attribute.
|
|
|
|
*
|
|
|
|
* @param info Properties info.
|
|
|
|
* @param propdatap Pointer that gets properties data.
|
|
|
|
*
|
|
|
|
* @return ::NC_NOERR No error.
|
|
|
|
* @author Dennis Heimbigner
|
|
|
|
*/
|
2016-08-08 23:24:19 +08:00
|
|
|
int
|
|
|
|
NC4_buildpropinfo(struct NCPROPINFO* info,char** propdatap)
|
|
|
|
{
|
|
|
|
size_t total;
|
2016-08-09 11:54:23 +08:00
|
|
|
char* propdata = NULL;
|
2016-08-08 23:24:19 +08:00
|
|
|
|
|
|
|
if(info == NULL || info->version == 0) return NC_EINVAL;
|
|
|
|
if(propdatap == NULL)
|
2016-09-15 06:26:08 +08:00
|
|
|
return NC_NOERR;
|
2016-09-15 06:11:22 +08:00
|
|
|
*propdatap = NULL;
|
2016-08-08 23:24:19 +08:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
2016-08-09 11:54:23 +08:00
|
|
|
propdata = (char*)malloc(total+1);
|
|
|
|
if(propdata == NULL)
|
2016-08-08 23:24:19 +08:00
|
|
|
return NC_ENOMEM;
|
2016-08-09 11:54:23 +08:00
|
|
|
snprintf(propdata,total+1,
|
2016-09-15 06:26:08 +08:00
|
|
|
"%s=%d|%s=%s|%s=%s",
|
2016-08-08 23:24:19 +08:00
|
|
|
NCPVERSION,info->version,
|
|
|
|
NCPNCLIBVERSION,info->netcdfver,
|
|
|
|
NCPHDF5LIBVERSION,info->hdf5ver);
|
2016-08-09 11:54:23 +08:00
|
|
|
/* Force null termination */
|
|
|
|
propdata[total] = '\0';
|
2016-09-15 06:26:08 +08:00
|
|
|
*propdatap = propdata;
|
|
|
|
|
2016-08-08 23:24:19 +08:00
|
|
|
return NC_NOERR;
|
|
|
|
}
|