Skip to content

Commit 95467e0

Browse files
committed
Rewrite attribute getting and setting
+ Logicals attributes written as 1 byte type + Support default values + No longer needs H5LT routines
1 parent 5dc8e62 commit 95467e0

23 files changed

+1519
-65
lines changed

Makefile.am

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ dat1EncodeSubscript.c \
146146
dat1ExportDims.c \
147147
dat1FixNameCell.c \
148148
dat1FreeLoc.c \
149+
dat1GetAttr.c \
150+
dat1GetAttrBool.c \
151+
dat1GetAttrHdsdims.c \
152+
dat1GetAttrInt.c \
153+
dat1GetAttrString.c \
149154
dat1GetBounds.c \
150155
dat1GetFullName.c \
151156
dat1H5EtoEMS.c \
@@ -158,6 +163,11 @@ dat1New.c \
158163
dat1NewPrim.c \
159164
dat1RetrieveContainer.c \
160165
dat1RetrieveIdentifier.c \
166+
dat1SetAttr.c \
167+
dat1SetAttrBool.c \
168+
dat1SetAttrHdsdims.c \
169+
dat1SetAttrInt.c \
170+
dat1SetAttrString.c \
161171
dat1SetStructureDims.c \
162172
dat1Type.c \
163173
dat1TypeInfo.c \

dat1.h

+42
Original file line numberDiff line numberDiff line change
@@ -330,5 +330,47 @@ hds1CountFiles();
330330
int
331331
hds1CountLocators( size_t ncomp, char **comps, hdsbool_t skip_scratch_root, int * status );
332332

333+
void
334+
dat1SetAttrString( hid_t obj_id, const char * attrname,
335+
const char * value, int * status );
336+
337+
void
338+
dat1SetAttr( hid_t obj_id, const char * attrname, hid_t attrtype,
339+
size_t nvals, const void * value, int * status );
340+
341+
void
342+
dat1SetAttrInt( hid_t obj_id, const char * attrname,
343+
int value, int * status );
344+
345+
void
346+
dat1SetAttrBool( hid_t obj_id, const char * attrname,
347+
hdsbool_t value, int * status );
348+
349+
void
350+
dat1SetAttrHdsdims( hid_t obj_id, const char * attrname,
351+
int nvals, const hdsdim values[], int * status );
352+
353+
hdsbool_t
354+
dat1GetAttr( hid_t obj_id, const char * attrname, hid_t attrtype,
355+
size_t maxvals, void * values, size_t *actvals, int * status );
356+
357+
hdsbool_t
358+
dat1GetAttrBool( hid_t objid, const char * attrname, hdsbool_t usedef,
359+
hdsbool_t defval, int *status);
360+
361+
void
362+
dat1GetAttrHdsdims( hid_t objid, const char * attrname, hdsbool_t usedef,
363+
size_t ndefault, const hdsdim defdims[], size_t maxvals,
364+
hdsdim values[], size_t *actvals, int *status);
365+
366+
int
367+
dat1GetAttrInt( hid_t objid, const char * attrname, hdsbool_t usedef,
368+
int defval, int *status);
369+
370+
void
371+
dat1GetAttrString( hid_t objid, const char * attrname, hdsbool_t usedef,
372+
const char * defval, char * attrval, size_t attrvallen,
373+
int *status);
374+
333375
/* DAT1_H_INCLUDED */
334376
#endif

dat1CreateStructureCell.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
*/
9191

9292
#include "hdf5.h"
93-
#include "hdf5_hl.h"
9493

9594
#include "ems.h"
9695
#include "sae_par.h"
@@ -122,10 +121,10 @@ dat1CreateStructureCell( hid_t group_id, size_t index, const char * typestr, con
122121
);
123122

124123
/* Actual data type of the structure/group must be stored in an attribute */
125-
CALLHDFQ( H5LTset_attribute_string( cellgroup_id, ".", HDS__ATTR_STRUCT_TYPE, typestr ) );
124+
dat1SetAttrString( cellgroup_id, HDS__ATTR_STRUCT_TYPE, typestr, status );
126125

127126
/* Also store the number of dimensions */
128-
CALLHDFQ( H5LTset_attribute_int( cellgroup_id, ".", HDS__ATTR_STRUCT_NDIMS, &scalardims, 1 ) );
127+
dat1SetAttrInt( cellgroup_id, HDS__ATTR_STRUCT_NDIMS, scalardims, status );
129128

130129
CLEANUP:
131130
if (*status != SAI__OK) {

dat1GetAttr.c

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
*+
3+
* Name:
4+
* dat1GetAttr
5+
6+
* Purpose:
7+
* Retrieve values of specified data type from an HDF5 attribute
8+
9+
* Language:
10+
* Starlink ANSI C
11+
12+
* Type of Module:
13+
* Library routine
14+
15+
* Invocation:
16+
* exists = dat1GetAttr( hid_t obj_id, const char * attrname, hid_t attrtype,
17+
* size_t maxvals, void * values, size_t *actvals, int * status );
18+
19+
* Arguments:
20+
* obj_id = hid_t (Given)
21+
* HDF5 object to associate with attribute.
22+
* attrname = const char * (Given)
23+
* Name of attribute.
24+
* attrtype = hid_t (Given)
25+
* Memory data type of attribute.
26+
* maxvals = size_t (Given)
27+
* Maximum number of values that can be retrieved.
28+
* values = void * (Returned)
29+
* Buffer to retrieve attribute values. maxvals elements (not bytes).
30+
* actvals = size_t * (Returned)
31+
* Number of elements retrieved from attribute. Can be NULL.
32+
* status = int* (Given and Returned)
33+
* Pointer to global status.
34+
35+
* Description:
36+
* Retrieve values of a specified data type from the HDF5 attribute.
37+
* An error will be triggered if the size of the attribute dataspace
38+
* exceeds maxvals.
39+
40+
* Returned Value:
41+
* Returns 1 if an attribute was found, 0 if an attribute was
42+
* not found. The absence of an attribute is not an error and it is
43+
* up to the caller to decide how to react.
44+
45+
* Authors:
46+
* TIMJ: Tim Jenness (Cornell)
47+
* {enter_new_authors_here}
48+
49+
* History:
50+
* 2014-11-17 (TIMJ):
51+
* Initial version
52+
* {enter_further_changes_here}
53+
54+
* Copyright:
55+
* Copyright (C) 2014 Cornell University
56+
* All Rights Reserved.
57+
58+
* Licence:
59+
* Redistribution and use in source and binary forms, with or
60+
* without modification, are permitted provided that the following
61+
* conditions are met:
62+
*
63+
* - Redistributions of source code must retain the above copyright
64+
* notice, this list of conditions and the following disclaimer.
65+
*
66+
* - Redistributions in binary form must reproduce the above
67+
* copyright notice, this list of conditions and the following
68+
* disclaimer in the documentation and/or other materials
69+
* provided with the distribution.
70+
*
71+
* - Neither the name of the {organization} nor the names of its
72+
* contributors may be used to endorse or promote products
73+
* derived from this software without specific prior written
74+
* permission.
75+
*
76+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
77+
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
78+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
79+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
80+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
81+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
84+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
85+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
87+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
88+
* THE POSSIBILITY OF SUCH DAMAGE.
89+
90+
* Bugs:
91+
* {note_any_bugs_here}
92+
*-
93+
*/
94+
95+
#include "hdf5.h"
96+
97+
#include "ems.h"
98+
#include "sae_par.h"
99+
100+
#include "hds1.h"
101+
#include "dat1.h"
102+
#include "hds.h"
103+
104+
#include "dat_err.h"
105+
106+
hdsbool_t
107+
dat1GetAttr( hid_t obj_id, const char * attrname, hid_t attrtype,
108+
size_t maxvals, void * values, size_t *actvals, int * status ) {
109+
110+
hid_t attribute_id = 0;
111+
hid_t attr_dataspace_id = 0;
112+
113+
if (*status != SAI__OK) return HDS_FALSE;
114+
115+
/* Check for existance and return immediately if not there */
116+
if (!H5Aexists(obj_id, attrname)) return HDS_FALSE;
117+
118+
if (!values) {
119+
*status = DAT__FATAL;
120+
emsRepf("dat1GetAttr", "Can not retrieve attribute '%s' into a null pointer"
121+
" (possible programming error)", status, attrname);
122+
abort();
123+
return HDS_TRUE;
124+
}
125+
126+
/* Get the attribute object */
127+
CALLHDF( attribute_id,
128+
H5Aopen( obj_id, attrname, H5P_DEFAULT ),
129+
DAT__HDF5E,
130+
emsRepf("dat1GetAttr_1", "Error retrieving attribute named %s",
131+
status, attrname)
132+
);
133+
134+
/* Retrieve the underlying dataspace */
135+
CALLHDF( attr_dataspace_id,
136+
H5Aget_space( attribute_id ),
137+
DAT__HDF5E,
138+
emsRepf("dat1GetAttr_2", "Error retrieving dataspace from attribute named %s",
139+
status, attrname)
140+
);
141+
142+
{
143+
size_t nelem = 1;
144+
hsize_t dims[DAT__MXDIM];
145+
int rank;
146+
rank = H5Sget_simple_extent_ndims( attr_dataspace_id );
147+
if (rank > DAT__MXDIM) {
148+
if (*status == SAI__OK) {
149+
*status = DAT__DIMIN;
150+
emsRepf("dat1GetAttr_3", "Can not have more than %d dimensions in an HDS attribute, got %d",
151+
status, DAT__MXDIM, rank );
152+
}
153+
goto CLEANUP;
154+
}
155+
if (rank > 0) { /* not a scalar */
156+
int i;
157+
CALLHDF(rank,
158+
H5Sget_simple_extent_dims( attr_dataspace_id, dims, NULL),
159+
DAT__HDF5E,
160+
emsRepf("dat1GetAttr_4", "Error retrieving dimensions of attribute %s", status, attrname)
161+
);
162+
for (i=0; i<rank; i++) {
163+
nelem *= dims[i];
164+
}
165+
}
166+
if (nelem > maxvals) {
167+
*status = DAT__DIMIN;
168+
emsRepf("dat1GetAttr_4", "Supplied buffer to small to retrieve %zu values from attribute %s",
169+
status, nelem, attrname);
170+
goto CLEANUP;
171+
}
172+
if (actvals) *actvals = nelem;
173+
}
174+
175+
/* Now copy out the data */
176+
CALLHDFQ(H5Aread( attribute_id, attrtype, values ));
177+
178+
CLEANUP:
179+
if (attribute_id > 0) H5Aclose(attribute_id);
180+
if (attr_dataspace_id > 0) H5Sclose(attr_dataspace_id);
181+
182+
return HDS_TRUE;
183+
}

0 commit comments

Comments
 (0)