1 : /******************************************************************************
2 : * $Id: gh5_convenience.cpp 17985 2009-11-10 13:39:46Z rouault $
3 : *
4 : * Project: Hierarchical Data Format Release 5 (HDF5)
5 : * Purpose: HDF5 convenience functions.
6 : * Author: Frank Warmerdam <warmerdam@pobox.com>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "gh5_convenience.h"
31 :
32 : CPL_CVSID("$Id: gh5_convenience.cpp 17985 2009-11-10 13:39:46Z rouault $");
33 :
34 : /************************************************************************/
35 : /* GH5_FetchAttribute(CPLString) */
36 : /************************************************************************/
37 :
38 1 : bool GH5_FetchAttribute( hid_t loc_id, const char *pszAttrName,
39 : CPLString &osResult, bool bReportError )
40 :
41 : {
42 1 : hid_t hAttr = H5Aopen_name( loc_id, pszAttrName );
43 :
44 1 : osResult.clear();
45 :
46 1 : if( hAttr < 0 )
47 : {
48 0 : if( bReportError )
49 : CPLError( CE_Failure, CPLE_AppDefined,
50 : "Attempt to read attribute %s failed, not found.",
51 0 : pszAttrName );
52 0 : return false;
53 : }
54 :
55 1 : hid_t hAttrTypeID = H5Aget_type( hAttr );
56 1 : hid_t hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT );
57 :
58 1 : if( H5Tget_class( hAttrNativeType ) == H5T_STRING )
59 : {
60 1 : int nAttrSize = H5Tget_size( hAttrTypeID );
61 1 : char *pachBuffer = (char *) CPLCalloc(nAttrSize+1,1);
62 1 : H5Aread( hAttr, hAttrNativeType, pachBuffer );
63 :
64 1 : osResult = pachBuffer;
65 1 : CPLFree( pachBuffer );
66 :
67 1 : return true;
68 : }
69 :
70 : else
71 : {
72 0 : if( bReportError )
73 : CPLError( CE_Failure, CPLE_AppDefined,
74 : "Attribute %s of unsupported type for conversion to string.",
75 0 : pszAttrName );
76 :
77 0 : return false;
78 : }
79 : }
80 :
81 : /************************************************************************/
82 : /* GH5_FetchAttribute(double) */
83 : /************************************************************************/
84 :
85 6 : bool GH5_FetchAttribute( hid_t loc_id, const char *pszAttrName,
86 : double &dfResult, bool bReportError )
87 :
88 : {
89 6 : hid_t hAttr = H5Aopen_name( loc_id, pszAttrName );
90 :
91 6 : dfResult = 0.0;
92 6 : if( hAttr < 0 )
93 : {
94 0 : if( bReportError )
95 : CPLError( CE_Failure, CPLE_AppDefined,
96 : "Attempt to read attribute %s failed, not found.",
97 0 : pszAttrName );
98 0 : return false;
99 : }
100 :
101 6 : hid_t hAttrTypeID = H5Aget_type( hAttr );
102 6 : hid_t hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT );
103 :
104 : /* -------------------------------------------------------------------- */
105 : /* Confirm that we have a single element value. */
106 : /* -------------------------------------------------------------------- */
107 :
108 6 : hid_t hAttrSpace = H5Aget_space( hAttr );
109 : hsize_t anSize[64];
110 6 : int nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, anSize, NULL );
111 :
112 6 : int i, nAttrElements = 1;
113 :
114 6 : for( i=0; i < nAttrDims; i++ ) {
115 0 : nAttrElements *= anSize[i];
116 : }
117 :
118 6 : if( nAttrElements != 1 )
119 : {
120 0 : if( bReportError )
121 : CPLError( CE_Failure, CPLE_AppDefined,
122 : "Attempt to read attribute %s failed, count=%d, not 1.",
123 0 : pszAttrName, nAttrElements );
124 0 : return false;
125 : }
126 :
127 : /* -------------------------------------------------------------------- */
128 : /* Read the value. */
129 : /* -------------------------------------------------------------------- */
130 6 : void *buf = (void *)CPLMalloc( H5Tget_size( hAttrNativeType ));
131 6 : H5Aread( hAttr, hAttrNativeType, buf );
132 :
133 : /* -------------------------------------------------------------------- */
134 : /* Translate to double. */
135 : /* -------------------------------------------------------------------- */
136 6 : if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) )
137 0 : dfResult = *((int *) buf);
138 6 : else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) )
139 6 : dfResult = *((float *) buf);
140 0 : else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) )
141 0 : dfResult = *((double *) buf);
142 : else
143 : {
144 0 : if( bReportError )
145 : CPLError( CE_Failure, CPLE_AppDefined,
146 : "Attribute %s of unsupported type for conversion to double.",
147 0 : pszAttrName );
148 0 : CPLFree( buf );
149 0 : return false;
150 : }
151 :
152 6 : CPLFree( buf );
153 :
154 6 : return true;
155 : }
156 :
157 : /************************************************************************/
158 : /* GH5_GetDataType() */
159 : /* */
160 : /* Transform HDF5 datatype to GDAL datatype */
161 : /************************************************************************/
162 3 : GDALDataType GH5_GetDataType(hid_t TypeID)
163 : {
164 3 : if( H5Tequal( H5T_NATIVE_CHAR, TypeID ) )
165 0 : return GDT_Byte;
166 3 : else if( H5Tequal( H5T_NATIVE_UCHAR, TypeID ) )
167 0 : return GDT_Byte;
168 3 : else if( H5Tequal( H5T_NATIVE_SHORT, TypeID ) )
169 0 : return GDT_Int16;
170 3 : else if( H5Tequal( H5T_NATIVE_USHORT, TypeID ) )
171 0 : return GDT_UInt16;
172 3 : else if( H5Tequal( H5T_NATIVE_INT, TypeID ) )
173 0 : return GDT_Int32;
174 3 : else if( H5Tequal( H5T_NATIVE_UINT, TypeID ) )
175 0 : return GDT_UInt32;
176 3 : else if( H5Tequal( H5T_NATIVE_LONG, TypeID ) )
177 : {
178 : if( sizeof(long) == 4 )
179 0 : return GDT_Int32;
180 : else
181 : return GDT_Unknown;
182 : }
183 3 : else if( H5Tequal( H5T_NATIVE_ULONG, TypeID ) )
184 : {
185 : if( sizeof(unsigned long) == 4 )
186 0 : return GDT_UInt32;
187 : else
188 : return GDT_Unknown;
189 : }
190 3 : else if( H5Tequal( H5T_NATIVE_FLOAT, TypeID ) )
191 3 : return GDT_Float32;
192 0 : else if( H5Tequal( H5T_NATIVE_DOUBLE, TypeID ) )
193 0 : return GDT_Float64;
194 0 : else if( H5Tequal( H5T_NATIVE_LLONG, TypeID ) )
195 0 : return GDT_Unknown;
196 0 : else if( H5Tequal( H5T_NATIVE_ULLONG, TypeID ) )
197 0 : return GDT_Unknown;
198 0 : else if( H5Tequal( H5T_NATIVE_DOUBLE, TypeID ) )
199 0 : return GDT_Unknown;
200 :
201 0 : return GDT_Unknown;
202 : }
203 :
|