1 /* This is part of the netCDF package.  Copyright 2018-2011 University
2    Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
3    conditions of use.
4 
5    Test HDF5 file code. These are not intended to be exhaustive tests,
6    but they use HDF5 the same way that netCDF-4 does, so if these
7    tests don't work, than netCDF-4 won't work either.
8 
9 */
10 
11 #include "h5_err_macros.h"
12 #include <hdf5.h>
13 #include <H5DSpublic.h>
14 #include <ncdimscale.h>
15 
16 #define FILE_NAME "tst_h_dimscales4.h5"
17 #define DIMSCALE_NAME "AAA_dimscale"
18 #define VAR1_NAME "Watson"
19 #define VAR2_NAME "Holmes"
20 #define VAR3_NAME "Moriarty"
21 #define NDIMS 1
22 #define DIM_LEN 1
23 #define NAME_ATTRIBUTE "dimscale_name_attribute"
24 #define DIMSCALE_LABEL "dimscale_label"
25 #define STR_LEN 255
26 #define NC_MAX_NAME STR_LEN
27 #define NC_EHDFERR 255
28 #define DIM_WITHOUT_VARIABLE "This is a netCDF dimension but not a netCDF variable."
29 
30 struct nc_hdf5_link_info
31 {
32    char name[STR_LEN];
33    H5I_type_t obj_type;
34 };
35 
alien_visitor(hid_t did,unsigned dim,hid_t dsid,void * visitor_data)36 herr_t alien_visitor(hid_t did, unsigned dim, hid_t dsid,
37 		     void *visitor_data)
38 {
39    H5G_stat_t statbuf;
40    HDF5_OBJID_T *objid = visitor_data;
41 
42    /* Get more info on the dimscale object.*/
43    if (H5Gget_objinfo(dsid, ".", 1, &statbuf) < 0) ERR;
44    objid->fileno[0] = statbuf.fileno[0];
45    objid->objno[0] = statbuf.objno[0];
46    objid->fileno[1] = statbuf.fileno[1];
47    objid->objno[1] = statbuf.objno[1];
48 
49    return 0;
50 }
51 
52 int
main()53 main()
54 {
55    printf("\n*** Checking HDF5 dimscales detach.\n");
56    printf("*** Creating a file with two vars with one dimension scale...");
57    {
58       hid_t fileid, grpid, spaceid, var1_id, var2_id, dimscaleid;
59       hid_t fcpl_id, fapl_id, create_propid, access_propid;
60       hsize_t dims[NDIMS] = {DIM_LEN};
61       char dimscale_wo_var[STR_LEN];
62       float data = 42;
63 
64       /* Create file. */
65       if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR;
66       if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG)) ERR;
67       if (H5Pset_cache(fapl_id, 0, CHUNK_CACHE_NELEMS, CHUNK_CACHE_SIZE,
68 		       CHUNK_CACHE_PREEMPTION) < 0) ERR;
69       if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR;
70       if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR;
71       if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
72 					       H5P_CRT_ORDER_INDEXED)) < 0) ERR;
73       if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED |
74 					       H5P_CRT_ORDER_INDEXED)) < 0) ERR;
75       if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR;
76       if (H5Pclose(fapl_id) < 0) ERR;
77       if (H5Pclose(fcpl_id) < 0) ERR;
78       if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR;
79 
80       /* Create dimension scale. */
81       if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
82       if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED|
83 				     H5P_CRT_ORDER_INDEXED) < 0) ERR;
84       if ((spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR;
85       if ((dimscaleid = H5Dcreate1(grpid, DIMSCALE_NAME, H5T_IEEE_F32BE,
86 				   spaceid, create_propid)) < 0) ERR;
87       if (H5Sclose(spaceid) < 0) ERR;
88       if (H5Pclose(create_propid) < 0) ERR;
89       sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, DIM_LEN);
90       if (H5DSset_scale(dimscaleid, dimscale_wo_var) < 0) ERR;
91 
92       /* Create a variable that uses this dimension scale. */
93       if ((access_propid = H5Pcreate(H5P_DATASET_ACCESS)) < 0) ERR;
94       if (H5Pset_chunk_cache(access_propid, CHUNK_CACHE_NELEMS,
95    			     CHUNK_CACHE_SIZE, CHUNK_CACHE_PREEMPTION) < 0) ERR;
96       if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
97       if (H5Pset_fill_value(create_propid, H5T_NATIVE_FLOAT, &data) < 0) ERR;
98       if (H5Pset_layout(create_propid, H5D_CONTIGUOUS) < 0) ERR;
99       if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED|
100 				     H5P_CRT_ORDER_INDEXED) < 0) ERR;
101       if ((spaceid = H5Screate_simple(NDIMS, dims, dims)) < 0) ERR;
102       if ((var1_id = H5Dcreate2(grpid, VAR1_NAME, H5T_NATIVE_FLOAT, spaceid,
103 				H5P_DEFAULT, create_propid, access_propid)) < 0) ERR;
104       if (H5Pclose(create_propid) < 0) ERR;
105       if (H5Pclose(access_propid) < 0) ERR;
106       if (H5Sclose(spaceid) < 0) ERR;
107       if (H5DSattach_scale(var1_id, dimscaleid, 0) < 0) ERR;
108 
109       /* Create another variable that uses this dimension scale. */
110       if ((access_propid = H5Pcreate(H5P_DATASET_ACCESS)) < 0) ERR;
111       if (H5Pset_chunk_cache(access_propid, CHUNK_CACHE_NELEMS,
112    			     CHUNK_CACHE_SIZE, CHUNK_CACHE_PREEMPTION) < 0) ERR;
113       if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR;
114       if (H5Pset_fill_value(create_propid, H5T_NATIVE_FLOAT, &data) < 0) ERR;
115       if (H5Pset_layout(create_propid, H5D_CONTIGUOUS) < 0) ERR;
116       if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED|
117 				     H5P_CRT_ORDER_INDEXED) < 0) ERR;
118       if ((spaceid = H5Screate_simple(NDIMS, dims, dims)) < 0) ERR;
119       if ((var2_id = H5Dcreate2(grpid, VAR2_NAME, H5T_NATIVE_FLOAT, spaceid,
120 				H5P_DEFAULT, create_propid, access_propid)) < 0) ERR;
121       if (H5Pclose(create_propid) < 0) ERR;
122       if (H5Pclose(access_propid) < 0) ERR;
123       if (H5Sclose(spaceid) < 0) ERR;
124       if (H5DSattach_scale(var2_id, dimscaleid, 0) < 0) ERR;
125 
126       /* Now detach the scales and remove the dimscale. This doesn't
127        * work if I reverse the order of the statements. */
128       if (H5DSdetach_scale(var2_id, dimscaleid, 0) < 0) ERR;
129       if (H5DSdetach_scale(var1_id, dimscaleid, 0) < 0) ERR;
130 
131       /* Fold up our tents. */
132       if (H5Dclose(var1_id) < 0) ERR;
133       if (H5Dclose(dimscaleid) < 0) ERR;
134       if (H5Gclose(grpid) < 0) ERR;
135       if (H5Fclose(fileid) < 0) ERR;
136    }
137    SUMMARIZE_ERR;
138    FINAL_RESULTS;
139 }
140 
141