Field3D
|
00001 //----------------------------------------------------------------------------// 00002 00003 /* 00004 * Copyright (c) 2009 Sony Pictures Imageworks Inc 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the 00017 * distribution. Neither the name of Sony Pictures Imageworks nor the 00018 * names of its contributors may be used to endorse or promote 00019 * products derived from this software without specific prior written 00020 * permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00031 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00032 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00033 * OF THE POSSIBILITY OF SUCH DAMAGE. 00034 */ 00035 00036 //----------------------------------------------------------------------------// 00037 00044 //----------------------------------------------------------------------------// 00045 00046 #include "Hdf5Util.h" 00047 00048 #include <iostream> 00049 #include <vector> 00050 00051 //----------------------------------------------------------------------------// 00052 00053 using namespace std; 00054 00055 //----------------------------------------------------------------------------// 00056 00057 FIELD3D_NAMESPACE_OPEN 00058 00059 using namespace Exc; 00060 00061 namespace Hdf5Util { 00062 00063 //----------------------------------------------------------------------------// 00064 // Implementations 00065 //----------------------------------------------------------------------------// 00066 00067 bool 00068 readAttribute(hid_t location, const string& attrName, string& value) 00069 { 00070 H5T_class_t typeClass; 00071 H5A_info_t attrInfo; 00072 hsize_t strLen; 00073 00074 if (H5Aexists(location, attrName.c_str()) < 1) 00075 throw MissingAttributeException("Couldn't find attribute " + attrName); 00076 00077 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT); 00078 H5ScopedAget_space attrSpace(attr); 00079 H5ScopedAget_type attrType(attr); 00080 00081 if (H5Aget_info(attr, &attrInfo) < 0) { 00082 throw MissingAttributeException("Couldn't get attribute info " + attrName); 00083 } else { 00084 strLen = attrInfo.data_size; 00085 } 00086 00087 typeClass = H5Tget_class(attrType); 00088 00089 if (typeClass != H5T_STRING) 00090 throw MissingAttributeException("Bad attribute type class for " + attrName); 00091 00092 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND); 00093 00094 std::vector<char> tempString(strLen + 1); 00095 00096 if (H5Aread(attr, nativeType, &tempString[0]) < 0) 00097 throw MissingAttributeException("Couldn't read attribute " + attrName); 00098 00099 value = string(&tempString[0]); 00100 00101 return true; 00102 00103 } 00104 00105 //----------------------------------------------------------------------------// 00106 00107 bool 00108 readAttribute(hid_t location, const string& attrName, 00109 unsigned int attrSize, int &value) 00110 { 00111 H5T_class_t typeClass; 00112 00113 if (H5Aexists(location, attrName.c_str()) < 1) 00114 throw MissingAttributeException("Couldn't find attribute " + attrName); 00115 00116 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT); 00117 H5ScopedAget_space attrSpace(attr); 00118 H5ScopedAget_type attrType(attr); 00119 00120 if (H5Sget_simple_extent_ndims(attrSpace) != 1) 00121 throw MissingAttributeException("Bad attribute rank for attribute " + 00122 attrName); 00123 00124 hsize_t dims[1]; 00125 H5Sget_simple_extent_dims(attrSpace, dims, NULL); 00126 00127 if (dims[0] != attrSize) 00128 throw MissingAttributeException("Invalid attribute size for attribute " + 00129 attrName); 00130 00131 typeClass = H5Tget_class(attrType); 00132 00133 if (typeClass != H5T_INTEGER) 00134 throw MissingAttributeException("Bad attribute type class for " + 00135 attrName); 00136 00137 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND); 00138 00139 if (H5Aread(attr, nativeType, &value) < 0) 00140 throw MissingAttributeException("Couldn't read attribute " + attrName); 00141 00142 return true; 00143 00144 } 00145 00146 //----------------------------------------------------------------------------// 00147 00148 bool 00149 readAttribute(hid_t location, const string& attrName, 00150 unsigned int attrSize, float &value) 00151 { 00152 H5T_class_t typeClass; 00153 00154 if (H5Aexists(location, attrName.c_str()) < 1) 00155 throw MissingAttributeException("Couldn't find attribute " + attrName); 00156 00157 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT); 00158 H5ScopedAget_space attrSpace(attr); 00159 H5ScopedAget_type attrType(attr); 00160 00161 if (H5Sget_simple_extent_ndims(attrSpace) != 1) 00162 throw MissingAttributeException("Bad attribute rank for attribute " + 00163 attrName); 00164 00165 hsize_t dims[1]; 00166 H5Sget_simple_extent_dims(attrSpace, dims, NULL); 00167 00168 if (dims[0] != attrSize) 00169 throw MissingAttributeException("Invalid attribute size for attribute " + 00170 attrName); 00171 00172 typeClass = H5Tget_class(attrType); 00173 00174 if (typeClass != H5T_FLOAT) 00175 throw MissingAttributeException("Bad attribute type class for " + 00176 attrName); 00177 00178 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND); 00179 00180 if (H5Aread(attr, nativeType, &value) < 0) 00181 throw MissingAttributeException("Couldn't read attribute " + attrName); 00182 00183 return true; 00184 } 00185 00186 //----------------------------------------------------------------------------// 00187 00188 bool 00189 readAttribute(hid_t location, const string& attrName, 00190 unsigned int attrSize, double &value) 00191 { 00192 H5T_class_t typeClass; 00193 00194 if (H5Aexists(location, attrName.c_str()) < 0) 00195 throw MissingAttributeException("Couldn't find attribute " + attrName); 00196 00197 H5ScopedAopen attr(location, attrName.c_str(), H5P_DEFAULT); 00198 H5ScopedAget_space attrSpace(attr); 00199 H5ScopedAget_type attrType(attr); 00200 00201 if (H5Sget_simple_extent_ndims(attrSpace) != 1) 00202 throw MissingAttributeException("Bad attribute rank for attribute " + 00203 attrName); 00204 00205 hsize_t dims[1]; 00206 H5Sget_simple_extent_dims(attrSpace, dims, NULL); 00207 00208 if (dims[0] != attrSize) 00209 throw MissingAttributeException("Invalid attribute size for attribute " + 00210 attrName); 00211 00212 typeClass = H5Tget_class(attrType); 00213 00214 if (typeClass != H5T_FLOAT) 00215 throw MissingAttributeException("Bad attribute type class for " + 00216 attrName); 00217 00218 H5ScopedTget_native_type nativeType(attrType, H5T_DIR_ASCEND); 00219 00220 if (H5Aread(attr, nativeType, &value) < 0) 00221 throw MissingAttributeException("Couldn't read attribute " + attrName); 00222 00223 return true; 00224 } 00225 00226 //----------------------------------------------------------------------------// 00227 00228 bool 00229 writeAttribute(hid_t location, const string& attrName, const string &value) 00230 { 00231 hid_t attr = -1; 00232 hid_t attrSpace; 00233 hid_t attrType; 00234 00235 bool success = true; 00236 00237 attrSpace = H5Screate(H5S_SCALAR); 00238 if (attrSpace == -1) 00239 success = false; 00240 00241 attrType = H5Tcopy(H5T_C_S1); 00242 if (attrType == -1) 00243 success = false; 00244 00245 if (value.size()) { 00246 // if the string is null the following will return error 00247 // which we don't want. 00248 if (success && H5Tset_size(attrType, value.size()) == -1){ 00249 success = false; 00250 } 00251 } 00252 00253 if (success) { 00254 H5Tset_strpad(attrType, H5T_STR_NULLTERM); 00255 attr = H5Acreate(location, attrName.c_str(), attrType, attrSpace, 00256 H5P_DEFAULT, H5P_DEFAULT); 00257 } 00258 00259 if (attr == -1) { 00260 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName); 00261 success = false; 00262 } 00263 00264 if (success && H5Awrite(attr, attrType, value.c_str()) == -1) { 00265 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName); 00266 success = false; 00267 } 00268 00269 H5Aclose(attr); 00270 H5Tclose(attrType); 00271 H5Sclose(attrSpace); 00272 00273 return success; 00274 00275 } 00276 00277 //----------------------------------------------------------------------------// 00278 00279 bool 00280 writeAttribute(hid_t location, const string &attrName, 00281 unsigned int attrSize, const int &value) 00282 { 00283 hid_t attr; 00284 hid_t attrSpace; 00285 hsize_t dims[1]; 00286 00287 dims[0] = attrSize; 00288 00289 attrSpace = H5Screate(H5S_SIMPLE); 00290 if (attrSpace < 0) 00291 return false; 00292 00293 if (H5Sset_extent_simple(attrSpace, 1, dims, NULL) < 0) 00294 return false; 00295 00296 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_INT, 00297 attrSpace, H5P_DEFAULT, H5P_DEFAULT); 00298 if (attr < 0) { 00299 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName); 00300 H5Aclose(attr); 00301 H5Sclose(attrSpace); 00302 return false; 00303 } 00304 00305 if (H5Awrite(attr, H5T_NATIVE_INT, &value) < 0) { 00306 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName); 00307 H5Aclose(attr); 00308 H5Sclose(attrSpace); 00309 return false; 00310 } 00311 00312 H5Aclose(attr); 00313 H5Sclose(attrSpace); 00314 00315 return true; 00316 } 00317 00318 //----------------------------------------------------------------------------// 00319 00320 bool 00321 writeAttribute(hid_t location, const string& attrName, 00322 unsigned int attrSize, const float &value) 00323 { 00324 hid_t attr; 00325 hid_t attrSpace; 00326 hsize_t dims[1]; 00327 00328 dims[0] = attrSize; 00329 00330 attrSpace = H5Screate(H5S_SIMPLE); 00331 if (attrSpace < 0) 00332 return false; 00333 00334 if (H5Sset_extent_simple(attrSpace, 1, dims, NULL) < 0) 00335 return false; 00336 00337 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_FLOAT, 00338 attrSpace, H5P_DEFAULT, H5P_DEFAULT); 00339 if (attr < 0) { 00340 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName); 00341 H5Aclose(attr); 00342 H5Sclose(attrSpace); 00343 return false; 00344 } 00345 00346 if (H5Awrite(attr, H5T_NATIVE_FLOAT, &value) < 0) { 00347 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName); 00348 H5Aclose(attr); 00349 H5Sclose(attrSpace); 00350 return false; 00351 } 00352 00353 H5Aclose(attr); 00354 H5Sclose(attrSpace); 00355 00356 return true; 00357 } 00358 00359 //----------------------------------------------------------------------------// 00360 00361 bool 00362 writeAttribute(hid_t location, const string& attrName, 00363 unsigned int attrSize, const double &value) 00364 { 00365 hid_t attr; 00366 hid_t attrSpace; 00367 hsize_t dims[1]; 00368 00369 dims[0] = attrSize; 00370 00371 attrSpace = H5Screate(H5S_SIMPLE); 00372 if (attrSpace < 0) 00373 return false; 00374 00375 if (H5Sset_extent_simple(attrSpace, 1, dims, NULL) < 0) 00376 return false; 00377 00378 attr = H5Acreate(location, attrName.c_str(), H5T_NATIVE_DOUBLE, 00379 attrSpace, H5P_DEFAULT, H5P_DEFAULT); 00380 if (attr < 0) { 00381 Msg::print(Msg::SevWarning, "Error creating attribute: " + attrName); 00382 H5Aclose(attr); 00383 H5Sclose(attrSpace); 00384 return false; 00385 } 00386 00387 if (H5Awrite(attr, H5T_NATIVE_DOUBLE, &value) < 0) { 00388 Msg::print(Msg::SevWarning, "Error writing attribute: " + attrName); 00389 H5Aclose(attr); 00390 H5Sclose(attrSpace); 00391 return false; 00392 } 00393 00394 H5Aclose(attr); 00395 H5Sclose(attrSpace); 00396 00397 return true; 00398 } 00399 00400 //----------------------------------------------------------------------------// 00401 00402 bool checkHdf5Gzip() 00403 { 00404 htri_t avail = H5Zfilter_avail(H5Z_FILTER_DEFLATE); 00405 if (!avail) 00406 return false; 00407 00408 unsigned int filter_info; 00409 herr_t status = H5Zget_filter_info (H5Z_FILTER_DEFLATE, &filter_info); 00410 00411 if (status < 0) 00412 return false; 00413 00414 if (!(filter_info & H5Z_FILTER_CONFIG_ENCODE_ENABLED) || 00415 !(filter_info & H5Z_FILTER_CONFIG_DECODE_ENABLED)) { 00416 return false; 00417 } 00418 00419 return true; 00420 } 00421 00422 //----------------------------------------------------------------------------// 00423 00424 } // namespace Hdf5Util 00425 00426 //----------------------------------------------------------------------------// 00427 00428 FIELD3D_NAMESPACE_SOURCE_CLOSE 00429 00430 //----------------------------------------------------------------------------//