|
| 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