47 #ifndef _INCLUDED_Field3D_Field3DFile_H_ 48 #define _INCLUDED_Field3D_Field3DFile_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;
191 const File::Layer* vectorLayer(
const std::string &name)
const;
194 void getScalarLayerNames(std::vector<std::string> &names)
const;
196 void getVectorLayerNames(std::vector<std::string> &names)
const;
248 : name(nm), parentName(par), components(cpt)
281 void getPartitionNames(std::vector<std::string> &names)
const;
283 void getScalarLayerNames(std::vector<std::string> &names,
284 const std::string &partitionName)
const;
286 void getVectorLayerNames(std::vector<std::string> &names,
287 const std::string &partitionName)
const;
292 {
return partition(partitionName); }
302 std::string intPartitionName(
const std::string &partitionName,
303 const std::string &layerName,
308 std::string removeUniqueId(
const std::string &partitionName)
const;
311 void addGroupMembership(
const GroupMembershipMap &groupMembers);
319 {
return m_metadata; }
323 {
return m_metadata; }
335 void printHierarchy()
const;
352 void closeInternal();
361 void getIntPartitionNames(std::vector<std::string> &names)
const;
364 void getIntScalarLayerNames(std::vector<std::string> &names,
365 const std::string &intPartitionName)
const;
368 void getIntVectorLayerNames(std::vector<std::string> &names,
369 const std::string &intPartitionName)
const;
372 int numIntPartitions(
const std::string &partitionName)
const;
376 std::string makeIntPartitionName(
const std::string &partitionsName,
457 template <
class Data_T>
459 readScalarLayers(
const std::string &layerName = std::string(
""))
const;
462 template <
class Data_T>
464 readScalarLayers(
const std::string &partitionName,
465 const std::string &layerName)
const;
471 template <
class Data_T>
473 readVectorLayers(
const std::string &layerName = std::string(
""))
const;
476 template <
class Data_T>
478 readVectorLayers(
const std::string &partitionName,
479 const std::string &layerName)
const;
483 template <
template <
typename T>
class Field_T,
class Data_T>
484 typename Field_T<Data_T>::Vec
488 typedef typename Field_T<Data_T>::Vec TypedFieldList;
492 originals = readScalarLayers<Data_T>(layerName);
495 TypedFieldList output;
496 typename FieldList::iterator i = originals.begin();
497 for (; i != originals.end(); ++i) {
498 typename Field_T<Data_T>::Ptr targetField;
501 output.push_back(targetField);
503 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
504 newTarget->name = (*i)->name;
505 newTarget->attribute = (*i)->attribute;
506 newTarget->copyMetadata(**i);
507 newTarget->copyFrom(*i);
508 output.push_back(newTarget);
517 template <
template <
typename T>
class Field_T,
class Data_T>
518 typename Field_T<Data_T>::Vec
520 const std::string &layerName)
const 523 typedef typename Field_T<Data_T>::Vec TypedFieldList;
527 originals = readScalarLayers<Data_T>(partitionName, layerName);
530 TypedFieldList output;
531 typename FieldList::iterator i = originals.begin();
532 for (; i != originals.end(); ++i) {
533 typename Field_T<Data_T>::Ptr targetField;
536 output.push_back(targetField);
538 typename Field_T<Data_T>::Ptr newTarget(
new Field_T<Data_T>);
539 newTarget->name = (*i)->name;
540 newTarget->attribute = (*i)->attribute;
541 newTarget->copyMetadata(**i);
542 newTarget->copyFrom(*i);
543 output.push_back(newTarget);
552 template <
template <
typename T>
class Field_T,
class Data_T>
553 typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec
556 typedef Field_T<FIELD3D_VEC3_T<Data_T> > TypedVField;
558 typedef typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec TypedFieldList;
562 originals = readVectorLayers<Data_T>(layerName);
565 TypedFieldList output;
566 typename FieldList::iterator i = originals.begin();
567 for (; i != originals.end(); ++i) {
568 typename TypedVField::Ptr targetField;
571 output.push_back(targetField);
573 typename TypedVField::Ptr newTarget(
new TypedVField);
574 newTarget->name = (*i)->name;
575 newTarget->attribute = (*i)->attribute;
576 newTarget->copyMetadata(**i);
577 newTarget->copyFrom(*i);
578 output.push_back(newTarget);
587 template <
template <
typename T>
class Field_T,
class Data_T>
588 typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec
590 const std::string &layerName)
const 592 typedef Field_T<FIELD3D_VEC3_T<Data_T> > TypedVField;
594 typedef typename Field_T<FIELD3D_VEC3_T<Data_T> >::Vec TypedFieldList;
598 originals = readVectorLayers<Data_T>(partitionName, layerName);
601 TypedFieldList output;
602 typename FieldList::iterator i = originals.begin();
603 for (; i != originals.end(); ++i) {
604 typename TypedVField::Ptr targetField;
607 output.push_back(targetField);
609 typename TypedVField::Ptr newTarget(
new TypedVField);
610 newTarget->name = (*i)->name;
611 newTarget->attribute = (*i)->attribute;
612 newTarget->copyMetadata(**i);
613 newTarget->copyFrom(*i);
614 output.push_back(newTarget);
631 template <
class Data_T>
633 readProxyLayer(
const std::string &partitionName,
634 const std::string &layerName,
635 bool isVectorLayer)
const;
641 template <
class Data_T>
643 readProxyLayer(hid_t location,
const std::string &
name,
644 const std::string &attribute,
652 template <
class Data_T>
654 readProxyScalarLayers(
const std::string &name = std::string(
""))
const;
661 template <
class Data_T>
663 readProxyVectorLayers(
const std::string &name = std::string(
""))
const;
671 bool open(
const std::string &filename);
679 herr_t parsePartition(hid_t loc_id,
const std::string partitionName);
682 herr_t parseLayer(hid_t loc_id,
const std::string &partitionName,
683 const std::string &layerName);
698 template <
class Data_T>
700 readScalarLayer(
const std::string &intPartitionName,
701 const std::string &layerName)
const;
705 template <
class Data_T>
707 readVectorLayer(
const std::string &intPartitionName,
708 const std::string &layerName)
const;
712 template <
class Data_T>
714 readLayer(
const std::string &intPartitionName,
715 const std::string &layerName,
716 bool isVectorLayer)
const;
719 bool readPartitionAndLayerInfo();
725 bool readMetadata(hid_t metadata_id);
791 template <
class Data_T>
794 {
return writeScalarLayer<Data_T>(layerName, std::string(
"default"), layer); }
797 template <
class Data_T>
799 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
800 {
return writeVectorLayer<Data_T>(layerName, std::string(
"default"), layer); }
804 template <
class Data_T>
805 bool writeScalarLayer(
const std::string &partitionName,
806 const std::string &layerName,
811 template <
class Data_T>
816 template <
class Data_T>
817 bool writeVectorLayer(
const std::string &partitionName,
818 const std::string &layerName,
819 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer);
823 template <
class Data_T>
824 bool writeVectorLayer(
typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer);
829 bool create(
const std::string &filename,
CreateMode cm = OverwriteMode);
832 bool writeGlobalMetadata();
836 bool writeGroupMembership();
839 std::string incrementPartitionName(std::string &pname);
842 template <
class Data_T>
844 createNewPartition(
const std::string &partitionName,
845 const std::string &layerName,
857 template <
class Data_T>
858 bool writeLayer(
const std::string &partitionName,
859 const std::string &layerName,
867 bool writeMetadata(hid_t metadataGroup);
891 const H5L_info_t *linfo,
void *opdata);
897 const H5L_info_t *linfo,
void *opdata);
905 template <
class Data_T>
915 std::vector<std::string> parts;
916 getIntPartitionNames(parts);
918 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
919 std::vector<std::string> layers;
920 getIntScalarLayerNames(layers, *p);
921 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
923 if ((name.length() == 0) || (*l == name)) {
924 FieldPtr mf = readScalarLayer<Data_T>(*p, *l);
937 template <
class Data_T>
940 const std::string &layerName)
const 949 if ((layerName.length() == 0) || (partitionName.length() == 0))
952 std::vector<std::string> parts;
953 getIntPartitionNames(parts);
955 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
956 std::vector<std::string> layers;
957 getIntScalarLayerNames(layers, *p);
958 if (removeUniqueId(*p) == partitionName) {
959 for (vector<string>::iterator l = layers.begin();
960 l != layers.end(); ++l) {
962 if (*l == layerName) {
963 FieldPtr mf = readScalarLayer<Data_T>(*p, *l);
976 template <
class Data_T>
987 std::vector<std::string> parts;
988 getIntPartitionNames(parts);
990 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
991 std::vector<std::string> layers;
992 getIntVectorLayerNames(layers, *p);
993 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
995 if ((name.length() == 0) || (*l == name)) {
996 FieldPtr mf = readVectorLayer<Data_T>(*p, *l);
1008 template <
class Data_T>
1011 const std::string &layerName)
const 1013 using namespace std;
1020 if ((layerName.length() == 0) || (partitionName.length() == 0))
1023 std::vector<std::string> parts;
1024 getIntPartitionNames(parts);
1026 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1027 std::vector<std::string> layers;
1028 getIntVectorLayerNames(layers, *p);
1029 if (removeUniqueId(*p) == partitionName) {
1030 for (vector<string>::iterator l = layers.begin();
1031 l != layers.end(); ++l) {
1033 if (*l == layerName) {
1034 FieldPtr mf = readVectorLayer<Data_T>(*p, *l);
1047 template <
class Data_T>
1050 const std::string &layerName,
1051 bool isVectorLayer)
const 1053 using namespace boost;
1054 using namespace std;
1074 l = part->vectorLayer(layerName);
1076 l = part->scalarLayer(layerName);
1083 string layerPath = l->
parent +
"/" + l->
name;
1086 if (layerGroup.id() < 0) {
1088 +
" in .f3d file ");
1094 if (!
readAttribute(layerGroup.id(),
"class_name", className)) {
1103 FieldPtr cachedField = cache.
getCachedField(m_filename, layerPath);
1117 field = readField<Data_T>(className, layerGroup.id(), m_filename, layerPath);
1120 #if 0 // This isn't really an error 1131 string metadataPath = layerPath +
"/metadata";
1133 if (metadataGroup.id() > 0) {
1134 readMetadata(metadataGroup.id(), field);
1138 field->
name = removeUniqueId(intPartitionName);
1144 cache.
cacheField(field, m_filename, layerPath);
1152 template <
class Data_T>
1155 const std::string &layerName,
1156 bool isVectorLayer)
const 1158 using namespace boost;
1159 using namespace std;
1167 if ((layerName.length() == 0) || (partitionName.length() == 0))
1170 std::vector<std::string> parts, layers;
1171 getIntPartitionNames(parts);
1173 bool foundPartition =
false;
1175 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1176 if (removeUniqueId(*p) == partitionName) {
1177 foundPartition =
true;
1178 if (isVectorLayer) {
1179 getIntVectorLayerNames(layers, *p);
1181 getIntScalarLayerNames(layers, *p);
1183 for (vector<string>::iterator l = layers.begin();
1184 l != layers.end(); ++l) {
1185 if (*l == layerName) {
1195 layer = part->vectorLayer(layerName);
1197 layer = part->scalarLayer(layerName);
1203 string layerPath = layer->
parent +
"/" + layer->
name;
1205 if (layerGroup.id() < 0) {
1207 + layerName +
" in .f3d file ");
1213 readProxyLayer<Data_T>(layerGroup, partitionName, layerName,
1219 if (mipGroup.
id() >= 0)
1224 output.push_back(field);
1230 if (!foundPartition) {
1240 template <
class Data_T>
1243 const std::string &
name,
1244 const std::string &attribute,
1247 using namespace boost;
1248 using namespace std;
1256 Box3i extents, dataW;
1257 if (!
readAttribute(location,
"extents", 6, extents.min.x)) {
1260 if (!
readAttribute(location,
"data_window", 6, dataW.min.x)) {
1266 field->
setSize(extents, dataW);
1270 if (metadataGroup.
id() > 0) {
1271 readMetadata(metadataGroup.
id(), field);
1284 template <
class Data_T>
1288 using namespace std;
1291 typedef std::vector<FieldPtr> FieldList;
1295 std::vector<std::string> parts;
1296 getPartitionNames(parts);
1298 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1299 std::vector<std::string> layers;
1300 getScalarLayerNames(layers, *p);
1301 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1303 if ((name.length() == 0) || (*l == name)) {
1304 FieldList f = readProxyLayer<Data_T>(*p, *l,
false);
1305 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1319 template <
class Data_T>
1323 using namespace std;
1326 typedef std::vector<FieldPtr> FieldList;
1330 std::vector<std::string> parts;
1331 getPartitionNames(parts);
1333 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1334 std::vector<std::string> layers;
1335 getVectorLayerNames(layers, *p);
1336 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1338 if ((name.length() == 0) || (*l == name)) {
1339 FieldList f = readProxyLayer<Data_T>(*p, *l,
true);
1340 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1354 template <
class Data_T>
1357 const std::string &layerName)
const 1359 return readLayer<Data_T>(intPartitionName, layerName,
false);
1364 template <
class Data_T>
1367 const std::string &layerName)
const 1369 return readLayer<FIELD3D_VEC3_T<Data_T> >(intPartitionName, layerName,
true);
1376 template <
class Data_T>
1379 const std::string & ,
1383 using namespace Exc;
1389 newPart->name = partitionName;
1392 if (partGroup.id() < 0) {
1394 "Error creating partition: " + newPart->name);
1398 m_partitions.push_back(newPart);
1407 if (!writeMapping(partGroup.id(), field->
mapping())) {
1409 "writeMapping returned false for an unknown reason ");
1413 catch (WriteMappingException &e) {
1420 "Unknown error when writing mapping for partition: " 1428 part->mapping = field->
mapping();
1432 if (!
writeAttribute(partGroup.id(),
"is_field3d_partition",
"1")) {
1442 template <
class Data_T>
1445 const std::string &layerName,
1449 using namespace std;
1450 using namespace Exc;
1457 "Called writeLayer with null pointer. Ignoring...");
1463 "Attempting to write layer without opening file first. ");
1467 string partitionName = intPartitionName(userPartitionName, layerName, field);
1474 part = createNewPartition<Data_T>(partitionName,layerName,field);
1481 "Couldn't add layer \"" + layerName +
"\" to partition \"" 1482 + partitionName +
"\" because the layer's mapping is null.");
1488 if (!isVectorLayer) {
1489 if (part->scalarLayer(layerName)) {
1491 std::string newPartitionName = incrementPartitionName(partitionName);
1492 part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1497 if (part->vectorLayer(layerName)) {
1499 std::string newPartitionName = incrementPartitionName(partitionName);
1500 part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1507 if (!part->mapping) {
1514 if (!field->
mapping()->isIdentical(part->mapping)) {
1516 +
"\" to partition \"" + partitionName
1517 +
"\" because mapping doesn't match");
1522 H5ScopedGopen partGroup(m_file, part->name.c_str(), H5P_DEFAULT);
1527 layer.
name = layerName;
1528 layer.parent = partitionName;
1533 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1535 if (layerGroup.id() < 0) {
1541 if (!
writeAttribute(layerGroup.id(),
"class_type",
"field3d_layer")) {
1548 if (metadataGroup.id() < 0) {
1552 if (!writeMetadata(metadataGroup.id(), field)) {
1565 part->addVectorLayer(layer);
1567 part->addScalarLayer(layer);
1574 template <
class Data_T>
1577 const std::string &layerName,
1580 return writeLayer<Data_T>(partitionName, layerName,
false, field);
1585 template <
class Data_T>
1589 if (layer->
name.size() == 0) {
1591 "Tried to write a scalar layer with no name");
1596 "Tried to write a scalar layer with no attribute name");
1599 return writeScalarLayer<Data_T>(layer->
name, layer->
attribute, layer);
1604 template <
class Data_T>
1608 const std::string &layerName,
1609 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr field)
1611 return writeLayer<FIELD3D_VEC3_T<Data_T> >(partitionName, layerName,
1617 template <
class Data_T>
1620 (
typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer)
1622 if (layer->name.size() == 0) {
1624 "Tried to write a vector layer with no name");
1627 if (layer->attribute.size() == 0) {
1629 "Tried to write a vector layer with no attribute name");
1632 return writeVectorLayer<Data_T>(layer->name, layer->attribute, layer);
1639 template <
class Data_T>
1642 const std::string &filename,
const std::string &layerPath)
1658 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.
VectorLayerList m_vectorLayers
The vector-valued layers belonging to this partition.
std::vector< Layer > ScalarLayerList
Contains utility functions and classes for Hdf5 files.
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
FieldMetadata< FieldBase > & metadata()
accessor to the m_metadata class
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
Provides writing of .f3d (internally, hdf5) files.
std::string name
Name of the partition.
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
boost::intrusive_ptr< FieldBase > Ptr
Contains the FieldCache class.
static ClassFactory & singleton()
}
boost::recursive_mutex::scoped_lock GlobalLock
RefBase base
Convenience typedef for referring to base class.
Contains the EmptyField class.
FIELD3D_API 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
FieldPtr getCachedField(const std::string &filename, const std::string &layerPath)
Checks the cache for a previously loaded field.
FIELD3D_API bool writeFieldMapping(hid_t mappingGroup, FieldMapping::Ptr mapping)
This function creates a FieldMappingIO instance based on mapping->className() which then writes Field...
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.
bool fileExists(const std::string &filename)
checks to see if a file/directory exists or not
std::vector< std::string > m_partitionNames
This stores partition names.
boost::intrusive_ptr< FieldMapping > Ptr
void setSize(const V3i &size)
Resizes the object.
void cacheField(FieldPtr field, const std::string &filename, const std::string &layerPath)
Adds the given field to the cache.
std::string name
The name of the layer (always available)
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.
This subclass of Field does not store any data.
std::vector< Ptr > Vec
This is a convenience typedef for the list that Field3DInputFile::readScalarLayers() and Field3DInput...
static const char * staticClassType()
boost::intrusive_ptr< EmptyField > Ptr
FIELD3D_API FieldMapping::Ptr readFieldMapping(hid_t mappingGroup)
This function creates a FieldMappingIO instance based on className read from mappingGroup location wh...
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
const FieldMetadata< Field3DFileBase > & metadata() const
Read only access to the m_metadata class.
static FieldCache & singleton()
Returns a reference to the FieldCache singleton.
LayerInfo(std::string par, std::string nm, int cpt)
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
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.
FieldIO::Ptr createFieldIO(const std::string &className) const
Instances an IO object by name.
File::Partition::Ptr getPartition(const std::string &partitionName) const
Returns a pointer to the given partition.
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.
FIELD3D_API bool writeField(hid_t layerGroup, FieldBase::Ptr field)
This function creates a FieldIO instance based on field->className() which then writes the field data...
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.
hid_t id() const
Query the hid_t value.