47 #ifndef _INCLUDED_Field3D_Field3DFileHDF5_H_
48 #define _INCLUDED_Field3D_Field3DFileHDF5_H_
58 #include <boost/intrusive_ptr.hpp>
84 template <
class Data_T>
86 readField(
const std::string &className, hid_t layerGroup,
87 const std::string &filename,
const std::string &layerPath);
152 typedef boost::intrusive_ptr<Partition>
Ptr;
153 typedef boost::intrusive_ptr<const Partition>
CPtr;
177 virtual std::string className()
const;
194 void getScalarLayerNames(std::vector<std::string> &names)
const;
196 void getVectorLayerNames(std::vector<std::string> &names)
const;
251 : name(nm), parentName(par), components(cpt)
284 void getPartitionNames(std::vector<std::string> &names)
const;
286 void getScalarLayerNames(std::vector<std::string> &names,
287 const std::string &partitionName)
const;
289 void getVectorLayerNames(std::vector<std::string> &names,
290 const std::string &partitionName)
const;
295 {
return partition(partitionName); }
305 std::string intPartitionName(
const std::string &partitionName,
306 const std::string &layerName,
311 std::string removeUniqueId(
const std::string &partitionName)
const;
314 void addGroupMembership(
const GroupMembershipMap &groupMembers);
322 {
return m_metadata; }
326 {
return m_metadata; }
338 void printHierarchy()
const;
355 void closeInternal();
364 void getIntPartitionNames(std::vector<std::string> &names)
const;
367 void getIntScalarLayerNames(std::vector<std::string> &names,
368 const std::string &intPartitionName)
const;
371 void getIntVectorLayerNames(std::vector<std::string> &names,
372 const std::string &intPartitionName)
const;
375 int numIntPartitions(
const std::string &partitionName)
const;
379 std::string makeIntPartitionName(
const std::string &partitionsName,
463 template <
class Data_T>
465 readScalarLayers(
const std::string &layerName = std::string(
""))
const;
468 template <
class Data_T>
470 readScalarLayers(
const std::string &partitionName,
471 const std::string &layerName)
const;
477 template <
class Data_T>
479 readVectorLayers(
const std::string &layerName = std::string(
""))
const;
482 template <
class Data_T>
484 readVectorLayers(
const std::string &partitionName,
485 const std::string &layerName)
const;
489 template <
template <
typename T>
class Field_T,
class Data_T>
490 typename Field_T<Data_T>::Vec
494 typedef typename Field_T<Data_T>::Vec TypedFieldList;
498 originals = readScalarLayers<Data_T>(layerName);
501 TypedFieldList output;
502 typename FieldList::iterator i = originals.begin();
503 for (; i != originals.end(); ++i) {
504 typename Field_T<Data_T>::Ptr targetField;
507 output.push_back(targetField);
509 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
510 newTarget->name = (*i)->name;
511 newTarget->attribute = (*i)->attribute;
512 newTarget->copyMetadata(**i);
513 newTarget->copyFrom(*i);
514 output.push_back(newTarget);
523 template <
template <
typename T>
class Field_T,
class Data_T>
524 typename Field_T<Data_T>::Vec
526 const std::string &layerName)
const
529 typedef typename Field_T<Data_T>::Vec TypedFieldList;
533 originals = readScalarLayers<Data_T>(partitionName, layerName);
536 TypedFieldList output;
537 typename FieldList::iterator i = originals.begin();
538 for (; i != originals.end(); ++i) {
539 typename Field_T<Data_T>::Ptr targetField;
542 output.push_back(targetField);
544 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
545 newTarget->name = (*i)->name;
546 newTarget->attribute = (*i)->attribute;
547 newTarget->copyMetadata(**i);
548 newTarget->copyFrom(*i);
549 output.push_back(newTarget);
558 template <
template <
typename T>
class Field_T,
class Data_T>
559 typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec
562 typedef Field_T<FIELD3D_VEC3_T<Data_T> > TypedVField;
564 typedef typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec TypedFieldList;
568 originals = readVectorLayers<Data_T>(layerName);
571 TypedFieldList output;
572 typename FieldList::iterator i = originals.begin();
573 for (; i != originals.end(); ++i) {
574 typename TypedVField::Ptr targetField;
575 targetField = field_dynamic_cast<TypedVField>(*i);
577 output.push_back(targetField);
579 typename TypedVField::Ptr newTarget(
new TypedVField);
580 newTarget->name = (*i)->name;
581 newTarget->attribute = (*i)->attribute;
582 newTarget->copyMetadata(**i);
583 newTarget->copyFrom(*i);
584 output.push_back(newTarget);
593 template <
template <
typename T>
class Field_T,
class Data_T>
594 typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec
596 const std::string &layerName)
const
598 typedef Field_T<FIELD3D_VEC3_T<Data_T> > TypedVField;
600 typedef typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec TypedFieldList;
604 originals = readVectorLayers<Data_T>(partitionName, layerName);
607 TypedFieldList output;
608 typename FieldList::iterator i = originals.begin();
609 for (; i != originals.end(); ++i) {
610 typename TypedVField::Ptr targetField;
611 targetField = field_dynamic_cast<TypedVField>(*i);
613 output.push_back(targetField);
615 typename TypedVField::Ptr newTarget(
new TypedVField);
616 newTarget->name = (*i)->name;
617 newTarget->attribute = (*i)->attribute;
618 newTarget->copyMetadata(**i);
619 newTarget->copyFrom(*i);
620 output.push_back(newTarget);
637 template <
class Data_T>
639 readProxyLayer(
const std::string &partitionName,
640 const std::string &layerName,
641 bool isVectorLayer)
const;
647 template <
class Data_T>
649 readProxyLayer(hid_t location,
const std::string &name,
650 const std::string &attribute,
658 template <
class Data_T>
660 readProxyScalarLayers(
const std::string &name = std::string(
""))
const;
667 template <
class Data_T>
669 readProxyVectorLayers(
const std::string &name = std::string(
""))
const;
677 bool open(
const std::string &filename);
685 herr_t parsePartition(hid_t loc_id,
const std::string partitionName);
688 herr_t parseLayer(hid_t loc_id,
const std::string &partitionName,
689 const std::string &layerName);
704 template <
class Data_T>
707 const std::string &layerName)
const;
711 template <
class Data_T>
714 const std::string &layerName)
const;
718 template <
class Data_T>
721 const std::string &layerName,
722 bool isVectorLayer)
const;
725 bool readPartitionAndLayerInfo();
731 bool readMetadata(hid_t metadata_id);
794 template <
class Data_T>
797 {
return writeScalarLayer<Data_T>(layerName, std::string(
"default"), layer); }
800 template <
class Data_T>
802 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
803 {
return writeVectorLayer<Data_T>(layerName, std::string(
"default"), layer); }
807 template <
class Data_T>
809 const std::string &layerName,
814 template <
class Data_T>
819 template <
class Data_T>
821 const std::string &layerName,
822 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer);
826 template <
class Data_T>
845 template <
class Data_T>
848 const std::string &layerName,
860 template <
class Data_T>
861 bool writeLayer(
const std::string &partitionName,
862 const std::string &layerName,
894 const H5L_info_t *linfo,
void *opdata);
900 const H5L_info_t *linfo,
void *opdata);
908 template <
class Data_T>
918 std::vector<std::string> parts;
921 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
922 std::vector<std::string> layers;
924 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
926 if ((name.length() == 0) || (*l == name)) {
927 FieldPtr mf = readScalarLayer<Data_T>(*p, *l);
940 template <
class Data_T>
943 const std::string &layerName)
const
952 if ((layerName.length() == 0) || (partitionName.length() == 0))
955 std::vector<std::string> parts;
958 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
959 std::vector<std::string> layers;
962 for (vector<string>::iterator l = layers.begin();
963 l != layers.end(); ++l) {
965 if (*l == layerName) {
966 FieldPtr mf = readScalarLayer<Data_T>(*p, *l);
979 template <
class Data_T>
990 std::vector<std::string> parts;
993 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
994 std::vector<std::string> layers;
996 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
998 if ((name.length() == 0) || (*l == name)) {
999 FieldPtr mf = readVectorLayer<Data_T>(*p, *l);
1011 template <
class Data_T>
1014 const std::string &layerName)
const
1016 using namespace std;
1023 if ((layerName.length() == 0) || (partitionName.length() == 0))
1026 std::vector<std::string> parts;
1029 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1030 std::vector<std::string> layers;
1033 for (vector<string>::iterator l = layers.begin();
1034 l != layers.end(); ++l) {
1036 if (*l == layerName) {
1037 FieldPtr mf = readVectorLayer<Data_T>(*p, *l);
1050 template <
class Data_T>
1053 const std::string &layerName,
1054 bool isVectorLayer)
const
1056 using namespace boost;
1057 using namespace std;
1077 l = part->vectorLayer(layerName);
1079 l = part->scalarLayer(layerName);
1086 string layerPath = l->
parent +
"/" + l->
name;
1089 if (layerGroup.id() < 0) {
1091 +
" in .f3d file ");
1097 if (!
readAttribute(layerGroup.id(),
"class_name", className)) {
1120 field = readField<Data_T>(className, layerGroup.id(),
m_filename, layerPath);
1123 #if 0 // This isn't really an error
1134 string metadataPath = layerPath +
"/metadata";
1136 if (metadataGroup.id() > 0) {
1155 template <
class Data_T>
1158 const std::string &layerName,
1159 bool isVectorLayer)
const
1161 using namespace boost;
1162 using namespace std;
1170 if ((layerName.length() == 0) || (partitionName.length() == 0))
1173 std::vector<std::string> parts, layers;
1176 bool foundPartition =
false;
1178 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1180 foundPartition =
true;
1181 if (isVectorLayer) {
1186 for (vector<string>::iterator l = layers.begin();
1187 l != layers.end(); ++l) {
1188 if (*l == layerName) {
1198 layer = part->vectorLayer(layerName);
1200 layer = part->scalarLayer(layerName);
1206 string layerPath = layer->
parent +
"/" + layer->
name;
1208 if (layerGroup.id() < 0) {
1210 + layerName +
" in .f3d file ");
1216 readProxyLayer<Data_T>(layerGroup, partitionName, layerName,
1222 if (mipGroup.
id() >= 0)
1227 output.push_back(field);
1233 if (!foundPartition) {
1243 template <
class Data_T>
1246 const std::string &name,
1247 const std::string &attribute,
1250 using namespace boost;
1251 using namespace std;
1259 Box3i extents, dataW;
1260 if (!
readAttribute(location,
"extents", 6, extents.min.x)) {
1263 if (!
readAttribute(location,
"data_window", 6, dataW.min.x)) {
1269 field->
setSize(extents, dataW);
1273 if (metadataGroup.
id() > 0) {
1287 template <
class Data_T>
1291 using namespace std;
1294 typedef std::vector<FieldPtr> FieldList;
1298 std::vector<std::string> parts;
1301 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1302 std::vector<std::string> layers;
1304 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1306 if ((name.length() == 0) || (*l == name)) {
1307 FieldList f = readProxyLayer<Data_T>(*p, *l,
false);
1308 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1322 template <
class Data_T>
1326 using namespace std;
1329 typedef std::vector<FieldPtr> FieldList;
1333 std::vector<std::string> parts;
1336 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1337 std::vector<std::string> layers;
1339 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1341 if ((name.length() == 0) || (*l == name)) {
1342 FieldList f = readProxyLayer<Data_T>(*p, *l,
true);
1343 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1357 template <
class Data_T>
1360 const std::string &layerName)
const
1367 template <
class Data_T>
1370 const std::string &layerName)
const
1372 return readLayer<FIELD3D_VEC3_T<Data_T> >(
intPartitionName, layerName,
true);
1379 template <
class Data_T>
1382 const std::string & ,
1386 using namespace Exc;
1392 newPart->name = partitionName;
1395 if (partGroup.id() < 0) {
1397 "Error creating partition: " + newPart->name);
1410 if (!writeMapping(partGroup.id(), field->
mapping())) {
1412 "writeMapping returned false for an unknown reason ");
1416 catch (WriteMappingException &e) {
1423 "Unknown error when writing mapping for partition: "
1431 part->mapping = field->
mapping();
1435 if (!
writeAttribute(partGroup.id(),
"is_field3d_partition",
"1")) {
1445 template <
class Data_T>
1448 const std::string &layerName,
1452 using namespace std;
1453 using namespace Exc;
1460 "Called writeLayer with null pointer. Ignoring...");
1466 "Attempting to write layer without opening file first. ");
1470 string partitionName =
intPartitionName(userPartitionName, layerName, field);
1477 part = createNewPartition<Data_T>(partitionName,layerName,field);
1484 "Couldn't add layer \"" + layerName +
"\" to partition \""
1485 + partitionName +
"\" because the layer's mapping is null.");
1491 if (!isVectorLayer) {
1492 if (part->scalarLayer(layerName)) {
1494 std::string newPartitionName = incrementPartitionName(partitionName);
1495 part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1500 if (part->vectorLayer(layerName)) {
1502 std::string newPartitionName = incrementPartitionName(partitionName);
1503 part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1510 if (!part->mapping) {
1517 if (!field->
mapping()->isIdentical(part->mapping)) {
1519 +
"\" to partition \"" + partitionName
1520 +
"\" because mapping doesn't match");
1530 layer.
name = layerName;
1531 layer.parent = partitionName;
1536 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1538 if (layerGroup.id() < 0) {
1544 if (!
writeAttribute(layerGroup.id(),
"class_type",
"field3d_layer")) {
1551 if (metadataGroup.id() < 0) {
1555 if (!writeMetadata(metadataGroup.id(), field)) {
1568 part->addVectorLayer(layer);
1570 part->addScalarLayer(layer);
1577 template <
class Data_T>
1580 const std::string &layerName,
1583 return writeLayer<Data_T>(partitionName, layerName,
false, field);
1588 template <
class Data_T>
1592 if (layer->
name.size() == 0) {
1594 "Tried to write a scalar layer with no name");
1599 "Tried to write a scalar layer with no attribute name");
1602 return writeScalarLayer<Data_T>(layer->
name, layer->
attribute, layer);
1607 template <
class Data_T>
1611 const std::string &layerName,
1612 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr field)
1614 return writeLayer<FIELD3D_VEC3_T<Data_T> >(partitionName, layerName,
1620 template <
class Data_T>
1623 (
typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
1625 if (layer->name.size() == 0) {
1627 "Tried to write a vector layer with no name");
1630 if (layer->attribute.size() == 0) {
1632 "Tried to write a vector layer with no attribute name");
1635 return writeVectorLayer<Data_T>(layer->name, layer->attribute, layer);
1642 template <
class Data_T>
1645 const std::string &filename,
const std::string &layerPath)
1660 FieldBase::Ptr field = io->read(layerGroup, filename, layerPath, typeEnum);