47 #ifndef _INCLUDED_Field3D_Field3DFile_H_ 48 #define _INCLUDED_Field3D_Field3DFile_H_ 58 #include <boost/intrusive_ptr.hpp> 83 template <
class Data_T>
85 readField(
const std::string &className, hid_t layerGroup,
86 const std::string &filename,
const std::string &layerPath);
149 typedef boost::intrusive_ptr<Partition>
Ptr;
150 typedef boost::intrusive_ptr<const Partition>
CPtr;
174 virtual std::string className()
const;
188 const File::Layer* vectorLayer(
const std::string &name)
const;
191 void getScalarLayerNames(std::vector<std::string> &names)
const;
193 void getVectorLayerNames(std::vector<std::string> &names)
const;
245 : name(nm), parentName(par), components(cpt)
278 void getPartitionNames(std::vector<std::string> &names)
const;
280 void getScalarLayerNames(std::vector<std::string> &names,
281 const std::string &partitionName)
const;
283 void getVectorLayerNames(std::vector<std::string> &names,
284 const std::string &partitionName)
const;
289 {
return partition(partitionName); }
299 std::string intPartitionName(
const std::string &partitionName,
300 const std::string &layerName,
305 std::string removeUniqueId(
const std::string &partitionName)
const;
308 void addGroupMembership(
const GroupMembershipMap &groupMembers);
316 {
return m_metadata; }
320 {
return m_metadata; }
332 void printHierarchy()
const;
349 void closeInternal();
358 void getIntPartitionNames(std::vector<std::string> &names)
const;
361 void getIntScalarLayerNames(std::vector<std::string> &names,
362 const std::string &intPartitionName)
const;
365 void getIntVectorLayerNames(std::vector<std::string> &names,
366 const std::string &intPartitionName)
const;
369 int numIntPartitions(
const std::string &partitionName)
const;
373 std::string makeIntPartitionName(
const std::string &partitionsName,
454 template <
class Data_T>
456 readScalarLayers(
const std::string &layerName = std::string(
""))
const;
459 template <
class Data_T>
461 readScalarLayers(
const std::string &partitionName,
462 const std::string &layerName)
const;
468 template <
class Data_T>
470 readVectorLayers(
const std::string &layerName = std::string(
""))
const;
473 template <
class Data_T>
475 readVectorLayers(
const std::string &partitionName,
476 const std::string &layerName)
const;
480 template <
template <
typename T>
class Field_T,
class Data_T>
481 typename Field_T<Data_T>::Vec
482 readScalarLayersAs(
const std::string &layerName = std::string(
""))
const;
486 template <
template <
typename T>
class Field_T,
class Data_T>
487 typename Field_T<Data_T>::Vec
488 readScalarLayersAs(
const std::string &partitionName,
489 const std::string &layerName)
const;
493 template <
template <
typename T>
class Field_T,
class Data_T>
494 typename Field_T<Data_T>::Vec
495 readVectorLayersAs(
const std::string &layerName = std::string(
""))
const;
499 template <
template <
typename T>
class Field_T,
class Data_T>
500 typename Field_T<Data_T>::Vec
501 readVectorLayersAs(
const std::string &partitionName,
502 const std::string &layerName)
const;
514 template <
class Data_T>
516 readProxyLayer(
const std::string &partitionName,
517 const std::string &layerName,
518 bool isVectorLayer)
const;
525 template <
class Data_T>
527 readProxyScalarLayers(
const std::string &
name = std::string(
""))
const;
534 template <
class Data_T>
536 readProxyVectorLayers(
const std::string &
name = std::string(
""))
const;
544 bool open(
const std::string &filename);
552 herr_t parsePartition(hid_t loc_id,
const std::string partitionName);
555 herr_t parseLayer(hid_t loc_id,
const std::string &partitionName,
556 const std::string &layerName);
571 template <
class Data_T>
573 readScalarLayer(
const std::string &intPartitionName,
574 const std::string &layerName)
const;
578 template <
class Data_T>
580 readVectorLayer(
const std::string &intPartitionName,
581 const std::string &layerName)
const;
585 template <
class Data_T>
587 readLayer(
const std::string &intPartitionName,
588 const std::string &layerName,
589 bool isVectorLayer)
const;
592 bool readPartitionAndLayerInfo();
598 bool readMetadata(hid_t metadata_id);
653 template <
class Data_T>
656 {
return writeScalarLayer<Data_T>(layerName, std::string(
"default"), layer); }
659 template <
class Data_T>
661 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
662 {
return writeVectorLayer<Data_T>(layerName, std::string(
"default"), layer); }
666 template <
class Data_T>
667 bool writeScalarLayer(
const std::string &partitionName,
668 const std::string &layerName,
673 template <
class Data_T>
678 template <
class Data_T>
679 bool writeVectorLayer(
const std::string &partitionName,
680 const std::string &layerName,
681 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer);
685 template <
class Data_T>
686 bool writeVectorLayer(
typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer);
691 bool create(
const std::string &filename,
CreateMode cm = OverwriteMode);
694 bool writeGlobalMetadata();
698 bool writeGroupMembership();
701 std::string incrementPartitionName(std::string &pname);
704 template <
class Data_T>
706 createNewPartition(
const std::string &partitionName,
707 const std::string &layerName,
719 template <
class Data_T>
720 bool writeLayer(
const std::string &partitionName,
721 const std::string &layerName,
729 bool writeMetadata(hid_t metadataGroup);
753 const H5L_info_t *linfo,
void *opdata);
758 herr_t
parseLayers(hid_t loc_id,
const char *partitionName,
759 const H5L_info_t *linfo,
void *opdata);
767 template <
class Data_T>
777 std::vector<std::string> parts;
778 getIntPartitionNames(parts);
780 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
781 std::vector<std::string> layers;
782 getIntScalarLayerNames(layers, *p);
783 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
785 if ((name.length() == 0) || (*l == name)) {
786 FieldPtr mf = readScalarLayer<Data_T>(*p, *l);
799 template <
class Data_T>
802 const std::string &layerName)
const 811 if ((layerName.length() == 0) || (partitionName.length() == 0))
814 std::vector<std::string> parts;
815 getIntPartitionNames(parts);
817 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
818 std::vector<std::string> layers;
819 getIntScalarLayerNames(layers, *p);
820 if (removeUniqueId(*p) == partitionName) {
821 for (vector<string>::iterator l = layers.begin();
822 l != layers.end(); ++l) {
824 if (*l == layerName) {
825 FieldPtr mf = readScalarLayer<Data_T>(*p, *l);
838 template <
class Data_T>
849 std::vector<std::string> parts;
850 getIntPartitionNames(parts);
852 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
853 std::vector<std::string> layers;
854 getIntVectorLayerNames(layers, *p);
855 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
857 if ((name.length() == 0) || (*l == name)) {
858 FieldPtr mf = readVectorLayer<Data_T>(*p, *l);
870 template <
class Data_T>
873 const std::string &layerName)
const 882 if ((layerName.length() == 0) || (partitionName.length() == 0))
885 std::vector<std::string> parts;
886 getIntPartitionNames(parts);
888 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
889 std::vector<std::string> layers;
890 getIntVectorLayerNames(layers, *p);
891 if (removeUniqueId(*p) == partitionName) {
892 for (vector<string>::iterator l = layers.begin();
893 l != layers.end(); ++l) {
895 if (*l == layerName) {
896 FieldPtr mf = readVectorLayer<Data_T>(*p, *l);
909 template <
class Data_T>
912 const std::string &layerName,
913 bool isVectorLayer)
const 915 using namespace boost;
932 l = part->vectorLayer(layerName);
934 l = part->scalarLayer(layerName);
944 if (layerGroup.id() < 0) {
952 if (!
readAttribute(layerGroup.id(),
"class_name", className)) {
961 field = readField<Data_T>(className, layerGroup.id(), m_filename, layerPath);
964 #if 0 // This isn't really an error 972 string metadataPath = layerPath +
"/metadata";
974 if (metadataGroup.id() > 0) {
975 readMetadata(metadataGroup.id(), field);
979 field->
name = removeUniqueId(intPartitionName);
988 template <
template <
typename T>
class Field_T,
class Data_T>
989 typename Field_T<Data_T>::Vec
993 typedef typename Field_T<Data_T>::Vec TypedFieldList;
997 originals = readScalarLayers<Data_T>(layerName);
1000 TypedFieldList output;
1001 typename FieldList::iterator i = originals.begin();
1002 for (; i != originals.end(); ++i) {
1003 typename Field_T<Data_T>::Ptr targetField;
1006 output.push_back(targetField);
1008 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
1009 newTarget->name = (*i)->name;
1010 newTarget->attribute = (*i)->attribute;
1011 newTarget->copyMetadata(*i);
1012 newTarget->copyFrom(*i);
1013 output.push_back(newTarget);
1022 template <
template <
typename T>
class Field_T,
class Data_T>
1023 typename Field_T<Data_T>::Vec
1025 const std::string &layerName)
const 1028 typedef typename Field_T<Data_T>::Vec TypedFieldList;
1031 FieldList originals;
1032 originals = readScalarLayers<Data_T>(partitionName, layerName);
1035 TypedFieldList output;
1036 typename FieldList::iterator i = originals.begin();
1037 for (; i != originals.end(); ++i) {
1038 typename Field_T<Data_T>::Ptr targetField;
1041 output.push_back(targetField);
1043 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
1044 newTarget->name = (*i)->name;
1045 newTarget->attribute = (*i)->attribute;
1046 newTarget->copyMetadata(**i);
1047 newTarget->copyFrom(*i);
1048 output.push_back(newTarget);
1057 template <
template <
typename T>
class Field_T,
class Data_T>
1058 typename Field_T<Data_T>::Vec
1062 typedef typename Field_T<Data_T>::Vec TypedFieldList;
1065 FieldList originals;
1066 originals = readVectorLayers<Data_T>(layerName);
1069 TypedFieldList output;
1070 typename FieldList::iterator i = originals.begin();
1071 for (; i != originals.end(); ++i) {
1072 typename Field_T<Data_T>::Ptr targetField;
1075 output.push_back(targetField);
1077 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
1078 newTarget->name = (*i)->name;
1079 newTarget->attribute = (*i)->attribute;
1080 newTarget->copyMetadata(*i);
1081 newTarget->copyFrom(*i);
1082 output.push_back(newTarget);
1091 template <
template <
typename T>
class Field_T,
class Data_T>
1092 typename Field_T<Data_T>::Vec
1094 const std::string &layerName)
const 1097 typedef typename Field_T<Data_T>::Vec TypedFieldList;
1100 FieldList originals;
1101 originals = readVectorLayers<Data_T>(partitionName, layerName);
1104 TypedFieldList output;
1105 typename FieldList::iterator i = originals.begin();
1106 for (; i != originals.end(); ++i) {
1107 typename Field_T<Data_T>::Ptr targetField;
1110 output.push_back(targetField);
1112 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
1113 newTarget->name = (*i)->name;
1114 newTarget->attribute = (*i)->attribute;
1115 newTarget->copyMetadata(*i);
1116 newTarget->copyFrom(*i);
1117 output.push_back(newTarget);
1126 template <
class Data_T>
1129 const std::string &layerName,
1130 bool isVectorLayer)
const 1132 using namespace boost;
1133 using namespace std;
1139 if ((layerName.length() == 0) || (partitionName.length() == 0))
1142 std::vector<std::string> parts, layers;
1143 getIntPartitionNames(parts);
1145 bool foundPartition =
false;
1147 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1148 if (removeUniqueId(*p) == partitionName) {
1149 foundPartition =
true;
1150 if (isVectorLayer) {
1151 getIntVectorLayerNames(layers, *p);
1153 getIntScalarLayerNames(layers, *p);
1155 for (vector<string>::iterator l = layers.begin();
1156 l != layers.end(); ++l) {
1157 if (*l == layerName) {
1167 layer = part->vectorLayer(layerName);
1169 layer = part->scalarLayer(layerName);
1175 string layerPath = layer->
parent +
"/" + layer->
name;
1177 if (layerGroup.id() < 0) {
1179 + layerName +
" in .f3d file ");
1183 Box3i extents, dataW;
1184 if (!
readAttribute(layerGroup,
"extents", 6, extents.min.x)) {
1187 if (!
readAttribute(layerGroup,
"data_window", 6, dataW.min.x)) {
1192 field->
setSize(extents, dataW);
1195 string metadataPath = layerPath +
"/metadata";
1197 if (metadataGroup.id() > 0) {
1198 readMetadata(metadataGroup.id(), field);
1203 field->
name = partitionName;
1207 output.push_back(field);
1213 if (!foundPartition) {
1223 template <
class Data_T>
1227 using namespace std;
1230 typedef std::vector<FieldPtr> FieldList;
1234 std::vector<std::string> parts;
1235 getPartitionNames(parts);
1237 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1238 std::vector<std::string> layers;
1239 getScalarLayerNames(layers, *p);
1240 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1242 if ((name.length() == 0) || (*l == name)) {
1243 FieldList f = readProxyLayer<Data_T>(*p, *l,
false);
1244 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1258 template <
class Data_T>
1262 using namespace std;
1265 typedef std::vector<FieldPtr> FieldList;
1269 std::vector<std::string> parts;
1270 getPartitionNames(parts);
1272 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1273 std::vector<std::string> layers;
1274 getVectorLayerNames(layers, *p);
1275 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1277 if ((name.length() == 0) || (*l == name)) {
1278 FieldList f = readProxyLayer<Data_T>(*p, *l,
true);
1279 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1293 template <
class Data_T>
1296 const std::string &layerName)
const 1298 return readLayer<Data_T>(intPartitionName, layerName,
false);
1303 template <
class Data_T>
1306 const std::string &layerName)
const 1308 return readLayer<FIELD3D_VEC3_T<Data_T> >(intPartitionName, layerName,
true);
1315 template <
class Data_T>
1318 const std::string &layerName,
1322 using namespace Exc;
1326 newPart->name = partitionName;
1329 if (partGroup.id() < 0) {
1331 "Error creating partition: " + newPart->name);
1335 m_partitions.push_back(newPart);
1344 if (!writeMapping(partGroup.id(), field->
mapping())) {
1346 "writeMapping returned false for an unknown reason ");
1350 catch (WriteMappingException &e) {
1357 "Unknown error when writing mapping for partition: " 1365 part->mapping = field->
mapping();
1369 if (!
writeAttribute(partGroup.id(),
"is_field3d_partition",
"1")) {
1379 template <
class Data_T>
1382 const std::string &layerName,
1386 using namespace std;
1387 using namespace Exc;
1392 "Called writeLayer with null pointer. Ignoring...");
1398 "Attempting to write layer without opening file first. ");
1402 string partitionName = intPartitionName(userPartitionName, layerName, field);
1409 part = createNewPartition<Data_T>(partitionName,layerName,field);
1416 "Couldn't add layer \"" + layerName +
"\" to partition \"" 1417 + partitionName +
"\" because the layer's mapping is null.");
1423 if (!isVectorLayer) {
1424 if (part->scalarLayer(layerName)) {
1426 std::string newPartitionName = incrementPartitionName(partitionName);
1427 part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1432 if (part->vectorLayer(layerName)) {
1434 std::string newPartitionName = incrementPartitionName(partitionName);
1435 part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1442 if (!part->mapping) {
1449 if (!field->
mapping()->isIdentical(part->mapping)) {
1451 +
"\" to partition \"" + partitionName
1452 +
"\" because mapping doesn't match");
1457 H5ScopedGopen partGroup(m_file, part->name.c_str(), H5P_DEFAULT);
1462 layer.
name = layerName;
1463 layer.parent = partitionName;
1468 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1470 if (layerGroup.id() < 0) {
1476 if (!
writeAttribute(layerGroup.id(),
"class_type",
"field3d_layer")) {
1483 if (metadataGroup.id() < 0) {
1487 if (!writeMetadata(metadataGroup.id(), field)) {
1500 part->addVectorLayer(layer);
1502 part->addScalarLayer(layer);
1509 template <
class Data_T>
1512 const std::string &layerName,
1515 return writeLayer<Data_T>(partitionName, layerName,
false, field);
1520 template <
class Data_T>
1524 if (layer->
name.size() == 0) {
1526 "Tried to write a scalar layer with no name");
1531 "Tried to write a scalar layer with no attribute name");
1534 return writeScalarLayer<Data_T>(layer->
name, layer->
attribute, layer);
1539 template <
class Data_T>
1543 const std::string &layerName,
1544 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr field)
1546 return writeLayer<FIELD3D_VEC3_T<Data_T> >(partitionName, layerName,
1552 template <
class Data_T>
1555 (
typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
1557 if (layer->name.size() == 0) {
1559 "Tried to write a vector layer with no name");
1562 if (layer->attribute.size() == 0) {
1564 "Tried to write a vector layer with no attribute name");
1567 return writeVectorLayer<Data_T>(layer->name, layer->attribute, layer);
1574 template <
class Data_T>
1577 const std::string &filename,
const std::string &layerPath)
1593 FieldBase::Ptr field = io->read(layerGroup, filename, layerPath, typeEnum);
#define FIELD3D_NAMESPACE_HEADER_CLOSE
Field_T::Ptr field_dynamic_cast(RefBase::Ptr field)
Dynamic cast that uses string-comparison in order to be safe even after an object crosses a shared li...
Namespace for file I/O specifics.
File::Partition::Ptr getPartition(const std::string &partitionName) const
Returns a pointer to the given partition.
VectorLayerList m_vectorLayers
The vector-valued layers belonging to this partition.
std::vector< Layer > ScalarLayerList
Contains utility functions and classes for Hdf5 files.
boost::intrusive_ptr< Partition > Ptr
Namespace for Exception objects.
std::map< std::string, int > PartitionCountMap
Field< Data_T >::Ptr readField(const std::string &className, hid_t layerGroup, const std::string &filename, const std::string &layerPath)
This function creates a FieldIO instance based on className which then reads the field data from laye...
DEFINE_FIELD_RTTI_CONCRETE_CLASS
FieldMetadata< Field3DFileBase > m_metadata
metadata
std::map< std::string, std::string > GroupMembershipMap
bool writeFieldMapping(hid_t mappingGroup, FieldMapping::Ptr mapping)
This function creates a FieldMappingIO instance based on mapping->className() which then writes Field...
Provides writing of .f3d (internally, hdf5) files.
std::string name
Name of the partition.
boost::intrusive_ptr< FieldBase > Ptr
static ClassFactory & singleton()
}
RefBase base
Convenience typedef for referring to base class.
Contains the EmptyField class.
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
boost::intrusive_ptr< FieldRes > Ptr
static const char * classType()
bool writeScalarLayer(const std::string &layerName, typename Field< Data_T >::Ptr layer)
Writes a scalar layer to the "Default" partition.
std::vector< Layer > VectorLayerList
static DataTypeEnum typeEnum()
Contains various utility functions for Hdf5.
Scoped object - creates a group on creation and closes it on destruction.
PartitionCountMap m_partitionCount
Contains a counter for each partition name. This is used to keep multiple fields with the same name u...
ScalarLayerList m_scalarLayers
The scalar-valued layers belonging to this partition.
std::vector< std::string > m_partitionNames
This stores partition names.
boost::intrusive_ptr< FieldMapping > Ptr
void setSize(const V3i &size)
Resizes the object.
std::string name
The name of the layer (always available)
const FieldMetadata< Field3DFileBase > & metadata() const
Read only access to the m_metadata class.
FieldIO::Ptr createFieldIO(const std::string &className) const
Instances an IO object by name.
bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
void setMapping(FieldMapping::Ptr mapping)
Sets the field's mapping.
bool writeLayer(const std::string &partitionName, const std::string &layerName, bool isVectorLayer, typename Field< Data_T >::Ptr layer)
Performs the actual writing of the layer to disk.
PartitionList m_partitions
Vector of partitions.
FieldMapping::Ptr readFieldMapping(hid_t mappingGroup)
This function creates a FieldMappingIO instance based on className read from mappingGroup location wh...
This subclass of Field does not store any data.
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
std::vector< Ptr > Vec
This is a convenience typedef for the list that Field3DInputFile::readScalarLayers() and Field3DInput...
boost::intrusive_ptr< EmptyField > Ptr
boost::intrusive_ptr< const Partition > CPtr
File::Partition::Ptr createNewPartition(const std::string &partitionName, const std::string &layerName, typename Field< Data_T >::Ptr field)
create newPartition given the input config
LayerInfo(std::string par, std::string nm, int cpt)
std::vector< LayerInfo > m_layerInfo
This stores layer info.
FieldMetadata< Field3DFileBase > & metadata()
accessor to the m_metadata class
std::string attribute
Optional name of the attribute the field represents.
Contains Field, WritableField and ResizableField classes.
Contains the ClassFactory class for registering Field3D classes.
bool writeField(hid_t layerGroup, FieldBase::Ptr field)
This function creates a FieldIO instance based on field->className() which then writes the field data...
boost::intrusive_ptr< FieldIO > Ptr
FieldMapping::Ptr mapping
Pointer to the mapping object.
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
GroupMembershipMap m_groupMembership
Keeps track of group membership for each layer of partition name. The key is the "group" and the valu...
std::vector< File::Partition::Ptr > PartitionList
Scoped object - opens a group on creation and closes it on destruction.
virtual void metadataHasChanged(const std::string &)
This function should implemented by concrete classes to get the callback when metadata changes...
std::string parent
The name of the parent partition. We need this in order to open its group.
boost::intrusive_ptr< Field > Ptr
FieldMapping::Ptr mapping()
Returns a pointer to the mapping.
bool writeVectorLayer(const std::string &layerName, typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr layer)
Writes a vector layer to the "Default" partition.
std::string name
Optional name of the field.