static intn test_named_vars(void) { char sds_name[20]; float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9}; float32 sds2_data[2][3] = {{0.1, 2.3, 4.5}, {4.5, 6.7, 8.9}}; int32 dimsize[1], dimsize2[2]; int32 sds_id, sds1_id, sds2_id, sds3_id, sds4_id, sds5_id; int32 file_id, dim_id, index; int32 start=0, stride=1, stat; int32 start2[2]={0,0}, stride2[2]={1,1}; int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5]; int32 array_rank; int32 n_datasets, n_file_attrs, n_local_attrs, n_vars=0; float32 out_data2[2][3]; intn datanum, ranknum, status =0, idx, idx1, idx2; intn is_coordvar=FALSE; hdf_varlist_t *allvars, *varlistp; intn num_errs = 0; /* number of errors so far */ char line[40]; char contents[7][40]={ "#0 SDS 2-dim 'Common Name'", "#1 SDS 2-dim 'Common Name'", "#2 SDS 1-dim 'One Dimension'", "#3 Coordinate 1-dim 'Common Name'", "#4 SDS 1-dim 'One Dimension'", "#5 SDS 1-dim 'Another Name'", "#6 Coordinate 1-dim 'Another Name'"}; file_id = SDstart(FILE3, DFACC_CREATE); CHECK(file_id, FAIL, "SDstart"); dimsize2[0] = 2; dimsize2[1] = 3; /* Create first COMMON_NAME data set. */ sds1_id = SDcreate(file_id, COMMON_NAME, DFNT_FLOAT32, 2, dimsize2); CHECK(sds1_id, FAIL, "SDcreate"); status = SDendaccess(sds1_id); CHECK(status, FAIL, "SDendaccess"); /* Create second COMMON_NAME data set. */ sds2_id = SDcreate(file_id, COMMON_NAME, DFNT_FLOAT32, 2, dimsize2); CHECK(sds2_id, FAIL, "SDcreate"); status = SDendaccess(sds2_id); CHECK(status, FAIL, "SDendaccess"); dimsize[0] = 5; sds3_id = SDcreate(file_id, ONEDIM_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds3_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as the previous 2 datasets */ dim_id = SDgetdimid(sds3_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, COMMON_NAME); CHECK(status, FAIL, "SDsetdimname"); /* Get file info and verify that there are 3 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 3, "SDfileinfo"); /* Write values to the dimension COMMON_NAME (same name as first 2 datasets) */ status = SDsetdimscale (dim_id, dimsize[0], DFNT_INT32, scale1); CHECK(status, FAIL, "SDsetdimscale"); /* Get file info and verify that there are 4 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 4, "SDfileinfo"); dimsize[0] = 8; sds4_id = SDcreate(file_id, ONEDIM_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds4_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as the previous 2 datasets */ dim_id = SDgetdimid(sds4_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, ANOTHER_NAME); CHECK(status, FAIL, "SDsetdimname"); sds5_id = SDcreate(file_id, ANOTHER_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds5_id, FAIL, "SDcreate"); /* Get file info and verify that there are 6 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 6, "SDfileinfo"); status = SDsetattr(dim_id, ATTR1_NAME, DFNT_CHAR8, ATTR1_LEN, ATTR1_VAL); CHECK(status, FAIL, "SDsetattr"); /* Get file info and verify that there are 7 datasets in the file */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 7, "SDfileinfo"); /* Verify again that the number of datasets in the file is 7 */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 7, "SDfileinfo"); /* There are 3 variables of name COMMON_NAME */ status = SDgetnumvars_byname(file_id, COMMON_NAME, &n_vars); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_vars, 3, "SDfileinfo"); allvars = (hdf_varlist_t *)HDmalloc(n_vars * sizeof(hdf_varlist_t)); status = SDnametoindices(file_id, COMMON_NAME, allvars); CHECK(status, FAIL, "SDfileinfo"); /* Compare file contents with predefined text to verify */ for (idx = 0; idx < n_datasets; idx++) { sds_id = SDselect(file_id, idx); CHECK(sds_id, FAIL, "SDselect"); status = SDgetinfo(sds_id, sds_name, &array_rank, NULL, NULL, NULL); CHECK(status, FAIL, "SDgetinfo"); is_coordvar = SDiscoordvar(sds_id); if (is_coordvar) sprintf(line,"#%d Coordinate %d-dim '%s'\n", idx, array_rank, sds_name); else sprintf(line,"#%d SDS %d-dim '%s'\n", idx, array_rank, sds_name); if (strncmp(contents[idx], line, strlen(contents[idx])) != 0) { fprintf(stderr, "File contents are incorrect in testing variable types at variable of index %d\n", idx); } } status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_named_vars */
static intn test_dim1_SDS1(void) { char sds_name[20]; float32 sds1_data[] = {0.1, 2.3, 4.5, 6.7, 8.9}; float32 out_data[5]; int32 dimsize[1]; int32 sds_id, file_id, dim_id, index; int32 start=0, stride=1; int32 scale1 [5] = {101,102,103,104,105}, scale1_out[5]; int32 num_type, array_rank, count; int32 n_datasets, n_file_attrs, n_local_attrs, n_vars = 0; intn datanum, ranknum, status =0, i, idx, idx1, idx2; hdf_varlist_t* var_list; intn is_coord = FALSE; char attr_name[H4_MAX_NC_NAME], attr_values[80]; intn num_errs = 0; /* number of errors so far */ file_id = SDstart(FILE1, DFACC_CREATE); CHECK(file_id, FAIL, "SDstart"); /* Create a one-dim dataset named VAR1_NAME, of type DFNT_FLOAT32. */ dimsize[0] = 5; sds_id = SDcreate(file_id, VAR1_NAME, DFNT_FLOAT32, 1, dimsize); CHECK(sds_id, FAIL, "SDcreate"); /* Set the dimension name to be the same as its dataset. */ dim_id = SDgetdimid(sds_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); status = SDsetdimname(dim_id, VAR1_NAME); /* status = SDsetdimname(dim_id, VAR1_NAME); */ CHECK(status, FAIL, "SDsetdimname"); /* Get file info and verify that there is 1 dataset in the file. */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 1, "SDfileinfo"); /* Set an attribute to dimension VAR1_NAME. */ status = SDsetattr(dim_id, ATTR1_NAME, DFNT_CHAR8, ATTR1_LEN, ATTR1_VAL); CHECK(status, FAIL, "SDsetattr"); /* Set an attribute to dataset VAR1_NAME. */ status = SDsetattr(sds_id, ATTR2_NAME, DFNT_CHAR8, ATTR2_LEN, ATTR2_VAL); CHECK(status, FAIL, "SDsetattr"); /* Get file info and verify that there are 2 datasets in the file: 1 SDS and 1 coordinate variable (because of SDsetattr dim) */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 2, "SDfileinfo"); /* Write data to the SDS */ status = SDwritedata(sds_id, &start, &stride, dimsize, (VOIDP)sds1_data); CHECK(status, FAIL, "SDwritedata"); /* Close dataset and file. */ status = SDendaccess(sds_id); CHECK(status, FAIL, "SDendaccess"); status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Open the file again to check its data */ file_id = SDstart(FILE1, DFACC_RDWR); CHECK(file_id, FAIL, "SDstart"); /* Check variable type and attributes of each element in the file */ /* Get the number of variables of name VAR1_NAME */ status = SDgetnumvars_byname(file_id, VAR1_NAME, &n_vars); if (n_vars == 1) { /* Get index of dataset VAR1_NAME */ index = SDnametoindex(file_id, VAR1_NAME); CHECK(index, FAIL, "SDnametoindex"); } else { /* Get the list of all variables of named VAR1_NAME */ var_list = (hdf_varlist_t *)HDmalloc(n_vars * sizeof(hdf_varlist_t)); status = SDnametoindices(file_id, VAR1_NAME, var_list); /* In this case, the first variable is a dataset */ for (idx = 0; idx < n_vars; idx++) { if (var_list[idx].var_type == IS_SDSVAR) { index = var_list[idx].var_index; VERIFY(index, 0, "SDnametoindices"); } } } sds_id = SDselect(file_id, index); CHECK(sds_id, FAIL, "SDselect"); /* Verify that this variable is a dataset. */ is_coord = SDiscoordvar(sds_id); VERIFY(is_coord, FALSE, "SDiscoordvar"); /* Read and verify the information of the SDS' first attribute. */ status = SDattrinfo(sds_id, 0, attr_name, &num_type, &count); CHECK(status, FAIL, "SDattrinfo"); VERIFY(count, ATTR2_LEN, "SDattrinfo"); VERIFY(HDstrncmp(attr_name, ATTR2_NAME, 14), 0, "SDattrinfo"); /* Read and verify the values of the SDS' first attribute. */ status = SDreadattr(sds_id, 0, attr_values); CHECK(status, FAIL, "SDreadattr"); if (HDstrncmp(attr_values, ATTR2_VAL, ATTR2_LEN) != 0) { fprintf(stderr, "Unmatched attribute values for SDS %s: is <%s>, should be <%s>\n", VAR1_NAME, attr_values, ATTR2_VAL); num_errs++; } /* Get access to the SDS' first dimension. */ dim_id = SDgetdimid(sds_id, 0); CHECK(dim_id, FAIL, "SDgetdimid"); /* Read and verify the information of the dimension's first attribute. */ status = SDattrinfo(dim_id, 0, attr_name, &num_type, &count); CHECK(status, FAIL, "SDattrinfo"); VERIFY(count, 19, "SDattrinfo"); VERIFY(HDstrncmp(attr_name, ATTR1_NAME, 21), 0, "SDattrinfo"); /* Read and verify the values of the dimension's first attribute. */ status = SDreadattr(dim_id, 0, attr_values); CHECK(status, FAIL, "SDreadattr"); if (HDstrncmp(attr_values, ATTR1_VAL, ATTR1_LEN) != 0) { fprintf(stderr, "Unmatched attribute values for dimension %s: is <%s>, should be <%s>\n", VAR1_NAME, attr_values, ATTR1_VAL); num_errs++; } /* Verify again that the number of datasets in the file is 2, 1 SDS and 1 coordinate variable */ status = SDfileinfo(file_id, &n_datasets, &n_file_attrs); CHECK(status, FAIL, "SDfileinfo"); VERIFY(n_datasets, 2, "SDfileinfo"); VERIFY(n_file_attrs, 0, "SDfileinfo"); /* Read and verify the dataset's data */ status = SDreaddata (sds_id, &start, NULL, dimsize, &out_data); CHECK(status, FAIL, "SDreaddata"); for (idx1 = 0; idx1 < dimsize[0]; idx1++) if (out_data[idx1] != sds1_data[idx1]) { fprintf(stderr, "Read value (%f) differs from written (%f) at [%d]\n", out_data[idx1], sds1_data[idx1], idx1); num_errs++; } /* Close dataset and file. */ status = SDendaccess(sds_id); CHECK(status, FAIL, "SDendaccess"); status = SDend(file_id); CHECK(status, FAIL, "SDend"); /* Return the number of errors that's been kept track of so far */ return num_errs; } /* test_dim1_SDS1 */
/* Opens an HDF file and reads all its SDS metadata, returning an SDSInfo * structure containing this metadata. Returns NULL on error. */ SDSInfo *open_h4_sds(const char *path) { int i, status; int sd_id = SDstart(path, DFACC_READ); CHECK_HDF_ERROR(path, sd_id); // get dataset and global att counts int32 n_datasets, n_global_atts; status = SDfileinfo(sd_id, &n_datasets, &n_global_atts); CHECK_HDF_ERROR(path, status); SDSInfo *sds = NEW0(SDSInfo); sds->path = xstrdup(path); sds->type = SDS_HDF4_FILE; sds->id = sd_id; // read global attributes sds->gatts = read_attributes(path, sd_id, n_global_atts); // read variables ('datasets') for (i = 0; i < n_datasets; i++) { int sds_id = SDselect(sd_id, i); CHECK_HDF_ERROR(path, sds_id); char buf[_H4_MAX_SDS_NAME + 1]; memset(buf, 0, sizeof(buf)); int32 rank, dim_sizes[H4_MAX_VAR_DIMS], type, natts; status = SDgetinfo(sds_id, buf, &rank, dim_sizes, &type, &natts); CHECK_HDF_ERROR(path, status); SDSVarInfo *var = NEW0(SDSVarInfo); var->name = xstrdup(buf); var->type = h4_to_sdstype(type); var->iscoord = SDiscoordvar(sds_id); var->ndims = rank; var->dims = read_dimensions(sds, sds_id, rank, dim_sizes); var->atts = read_attributes(path, sds_id, natts); var->id = i; // actually the sds_index comp_coder_t comp_type; comp_info c_info; status = SDgetcompinfo(sds_id, &comp_type, &c_info); CHECK_HDF_ERROR(path, status); switch (comp_type) { case COMP_CODE_NONE: var->compress = 0; break; case COMP_CODE_DEFLATE: var->compress = c_info.deflate.level; break; default: // any other compression method is 'worth' 1 imo *trollface* // better than claiming 0 to the user var->compress = 1; break; } var->sds = sds; var->next = sds->vars; sds->vars = var; status = SDendaccess(sds_id); CHECK_HDF_ERROR(path, status); } sds->vars = (SDSVarInfo *)list_reverse((List *)sds->vars); sds->dims = (SDSDimInfo *)list_reverse((List *)sds->dims); sds->funcs = &h4_funcs; return sds; }