Skip to content

Commit d32987a

Browse files
committed
No longer use an attribute for structure dimensionality
If the dimensionality attribute is missing the structure is scalar. Else read the dimensionality attribute and the number of elements in that tells you the dimensionality. This saves one attribute per HDF5 group.
1 parent 4316945 commit d32987a

7 files changed

+125
-34
lines changed

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ dat1GetBounds.c \
155155
dat1Getenv.c \
156156
dat1GetFullName.c \
157157
dat1GetParentID.c \
158+
dat1GetStructureDims.c \
158159
dat1H5EtoEMS.c \
159160
dat1ImportDims.c \
160161
dat1ImportFloc.c \

dat1.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ typedef enum {
135135
/* Names of attributes */
136136
#define HDS__ATTR_DEFINED "HDS_DATASET_IS_DEFINED?"
137137
#define HDS__ATTR_STRUCT_TYPE "CLASS"
138-
#define HDS__ATTR_STRUCT_NDIMS "HDS_STRUCTURE_NDIMS"
139138
#define HDS__ATTR_STRUCT_DIMS "HDS_STRUCTURE_DIMS"
140139

141140
/* Private definition of the HDS locator struct */
@@ -393,5 +392,8 @@ int dat1Annul( HDSLoc *locator, int * status );
393392

394393
hid_t dat1GetParentID( hid_t objid, hdsbool_t allow_root, int *status );
395394

395+
int
396+
dat1GetStructureDims( const HDSLoc * locator, int maxdims, hdsdim dims[], int *status );
397+
396398
/* DAT1_H_INCLUDED */
397399
#endif

dat1CreateStructureCell.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,10 @@ dat1CreateStructureCell( hid_t group_id, size_t index, const char * typestr, con
120120
emsRepf("dat1New_4", "Error creating structure/group '%s'", status, parentstr)
121121
);
122122

123-
/* Actual data type of the structure/group must be stored in an attribute */
123+
/* Actual data type of the structure/group must be stored in an attribute.
124+
Do not need to store dimensions as each cell is itself scalar. */
124125
dat1SetAttrString( cellgroup_id, HDS__ATTR_STRUCT_TYPE, typestr, status );
125126

126-
/* Also store the number of dimensions */
127-
dat1SetAttrInt( cellgroup_id, HDS__ATTR_STRUCT_NDIMS, scalardims, status );
128-
129127
CLEANUP:
130128
if (*status != SAI__OK) {
131129
if (cellgroup_id > 0) {

dat1GetBounds.c

+5-13
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
* History:
4444
* 2014-09-15 (TIMJ):
4545
* Initial version
46+
* 2014-11-21 (TIMJ):
47+
* Use dat1GetStructDims
4648
* {enter_further_changes_here}
4749
4850
* Copyright:
@@ -127,22 +129,12 @@ dat1GetBounds( const HDSLoc * locator, hdsdim lower[DAT__MXDIM],
127129
}
128130

129131
if (dat1IsStructure( locator, status ) ) {
130-
/* Assume scalar structure if we are missing the attribute */
131-
rank = dat1GetAttrInt( locator->group_id, HDS__ATTR_STRUCT_NDIMS, HDS_TRUE, 0, status );
132+
133+
/* Query the dimensions of the structure */
134+
rank = dat1GetStructureDims( locator, DAT__MXDIM, upper, status );
132135

133136
if (rank > 0) {
134137
int i;
135-
size_t actvals = 0; /* sanity check */
136-
dat1GetAttrHdsdims( locator->group_id, HDS__ATTR_STRUCT_DIMS, HDS_FALSE,
137-
0, NULL, DAT__MXDIM, upper, &actvals, status );
138-
139-
if (rank != (int)actvals) {
140-
*status = DAT__DIMIN;
141-
emsRepf("datshape_1b", "datBounds: Inconsistency in object dimensions of structure (%d != %zu)",
142-
status, rank, actvals);
143-
goto CLEANUP;
144-
}
145-
146138
for (i=0; i<rank; i++) {
147139
lower[i] = 1;
148140
}

dat1GetStructureDims.c

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
*+
3+
* Name:
4+
* dat1GetStructureDims
5+
6+
* Purpose:
7+
* Obtains dimensions of a structure
8+
9+
* Language:
10+
* Starlink ANSI C
11+
12+
* Type of Module:
13+
* Library routine
14+
15+
* Invocation:
16+
* rank = dat1GetStructureDims( const HDSLoc * locator, int maxdims, hdsdim dims[],
17+
* int *status );
18+
19+
* Arguments:
20+
* locator = const HDSLoc * (Given)
21+
* Locator to structure object.
22+
* maxdims = int (Given)
23+
* Allocated size of dims[]
24+
* dims = hdsdim (Given & Returned)
25+
* On exit will contain the dimensions of the structure. Will not
26+
* be touched if rank is 0. Only rank elements will be stored.
27+
* status = int* (Given and Returned)
28+
* Pointer to global status.
29+
30+
* Description:
31+
* Calculates the dimensionality or rank of a structure and returns
32+
* the appropriate dimensions.
33+
34+
* Returned Value:
35+
* rank = int
36+
* Dimensionality of the structure. Can be 0 for a scalar structure.
37+
38+
* Authors:
39+
* TIMJ: Tim Jenness (Cornell)
40+
* {enter_new_authors_here}
41+
42+
* Notes:
43+
*
44+
45+
* History:
46+
* 2014-11-21 (TIMJ):
47+
* Initial version
48+
* {enter_further_changes_here}
49+
50+
* Copyright:
51+
* Copyright (C) 2014 Cornell University
52+
* All Rights Reserved.
53+
54+
* Licence:
55+
* Redistribution and use in source and binary forms, with or
56+
* without modification, are permitted provided that the following
57+
* conditions are met:
58+
*
59+
* - Redistributions of source code must retain the above copyright
60+
* notice, this list of conditions and the following disclaimer.
61+
*
62+
* - Redistributions in binary form must reproduce the above
63+
* copyright notice, this list of conditions and the following
64+
* disclaimer in the documentation and/or other materials
65+
* provided with the distribution.
66+
*
67+
* - Neither the name of the {organization} nor the names of its
68+
* contributors may be used to endorse or promote products
69+
* derived from this software without specific prior written
70+
* permission.
71+
*
72+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
73+
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
74+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
75+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
76+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
78+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
79+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
80+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
81+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
83+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
84+
* THE POSSIBILITY OF SUCH DAMAGE.
85+
86+
* Bugs:
87+
* {note_any_bugs_here}
88+
*-
89+
*/
90+
91+
#include "hdf5.h"
92+
93+
#include "ems.h"
94+
#include "sae_par.h"
95+
96+
#include "hds1.h"
97+
#include "dat1.h"
98+
#include "hds.h"
99+
100+
int
101+
dat1GetStructureDims( const HDSLoc * locator, int maxdims, hdsdim dims[], int *status ) {
102+
103+
size_t actdims = 0;
104+
if (*status != SAI__OK) return actdims;
105+
if (!H5Aexists(locator->group_id, HDS__ATTR_STRUCT_DIMS)) return actdims;
106+
107+
dat1GetAttrHdsdims( locator->group_id, HDS__ATTR_STRUCT_DIMS, HDS_FALSE,
108+
0, NULL, maxdims, dims, &actdims, status );
109+
return actdims;
110+
}

dat1New.c

-3
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ dat1New( const HDSLoc *locator,
177177
/* Actual data type of the structure/group must be stored in an attribute */
178178
dat1SetAttrString( group_id, HDS__ATTR_STRUCT_TYPE, groupstr, status );
179179

180-
/* Also store the number of dimensions */
181-
dat1SetAttrInt( group_id, HDS__ATTR_STRUCT_NDIMS, ndim, status );
182-
183180
if (ndim > 0) {
184181
/* HDF5 can not define an array of structures so we create a collection
185182
of groups below the parent group. */

datCell.c

+4-13
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ datCell(const HDSLoc *locator1, int ndim, const hdsdim subs[],
152152
hdsdim groupsub[DAT__MXDIM];
153153

154154
if (locator1->vectorized > 0) {
155-
155+
hdsdim structdims[DAT__MXDIM];
156156
/* If this locator is vectorized then the name will be incorrect
157157
if we naively calculate the name. */
158-
/* Assume scalar structure if we are missing the attribute */
159-
rank = dat1GetAttrInt( locator1->group_id, HDS__ATTR_STRUCT_NDIMS, HDS_TRUE, 0, status );
158+
159+
/* Get the dimensionality */
160+
rank = dat1GetStructureDims( locator1, DAT__MXDIM, structdims, status );
160161

161162
if (rank == 0) {
162163
/* So the group is really a scalar so we just need to clone the
@@ -169,16 +170,6 @@ datCell(const HDSLoc *locator1, int ndim, const hdsdim subs[],
169170

170171
} else if (rank > 1) {
171172
/* Map vectorized index to underlying dimensionality */
172-
size_t actvals;
173-
hdsdim structdims[DAT__MXDIM];
174-
dat1GetAttrHdsdims( locator1->group_id, HDS__ATTR_STRUCT_DIMS, HDS_FALSE,
175-
0, NULL, DAT__MXDIM, structdims, &actvals, status );
176-
if (rank != (int)actvals) {
177-
*status = DAT__DIMIN;
178-
emsRepf("datshape_1b", "datCell: Inconsistency in object dimensions of structure (%d != %zu)",
179-
status, rank, actvals);
180-
goto CLEANUP;
181-
}
182173
dat1Index2Coords( subs[0], rank, structdims, groupsub, status );
183174
ndim = rank;
184175
} else {

0 commit comments

Comments
 (0)