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;
245 : name(nm), parentName(par), components(cpt)
281 const std::string &partitionName)
const;
284 const std::string &partitionName)
const;
300 const std::string &layerName,
305 std::string
removeUniqueId(
const std::string &partitionName)
const;
454 template <
class Data_T>
459 template <
class Data_T>
462 const std::string &layerName)
const;
468 template <
class Data_T>
473 template <
class Data_T>
476 const std::string &layerName)
const;
480 template <
template <
typename T>
class Field_T,
class Data_T>
481 typename Field_T<Data_T>::Vec
486 template <
template <
typename T>
class Field_T,
class Data_T>
487 typename Field_T<Data_T>::Vec
489 const std::string &layerName)
const;
493 template <
template <
typename T>
class Field_T,
class Data_T>
494 typename Field_T<Data_T>::Vec
499 template <
template <
typename T>
class Field_T,
class Data_T>
500 typename Field_T<Data_T>::Vec
502 const std::string &layerName)
const;
514 template <
class Data_T>
517 const std::string &layerName,
518 bool isVectorLayer)
const;
525 template <
class Data_T>
534 template <
class Data_T>
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>
574 const std::string &layerName)
const;
578 template <
class Data_T>
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;
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>
668 const std::string &layerName,
673 template <
class Data_T>
678 template <
class Data_T>
680 const std::string &layerName,
681 typename Field<FIELD3D_VEC3_T<Data_T> >::Ptr layer);
685 template <
class Data_T>
704 template <
class Data_T>
707 const std::string &layerName,
719 template <
class Data_T>
720 bool writeLayer(
const std::string &partitionName,
721 const std::string &layerName,
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;
780 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
781 std::vector<std::string> layers;
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;
817 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
818 std::vector<std::string> layers;
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;
852 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
853 std::vector<std::string> layers;
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;
888 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
889 std::vector<std::string> layers;
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) {
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;
1145 bool foundPartition =
false;
1147 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1149 foundPartition =
true;
1150 if (isVectorLayer) {
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) {
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;
1237 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1238 std::vector<std::string> layers;
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;
1272 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1273 std::vector<std::string> layers;
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
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);
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");
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.
VectorLayerList m_vectorLayers
The vector-valued layers belonging to this partition.
std::vector< Layer > ScalarLayerList
bool writeGroupMembership()
This routine is called just before closing to write out any group membership to disk.
bool writeGlobalMetadata()
This routine is call if you want to write out global metadata to disk.
Contains utility functions and classes for Hdf5 files.
void addScalarLayer(const File::Layer &layer)
Adds a scalar layer.
boost::intrusive_ptr< Partition > Ptr
Namespace for Exception objects.
void getIntVectorLayerNames(std::vector< std::string > &names, const std::string &intPartitionName) const
Gets the names of all the vector layers in a given partition, but assumes that partition name is the ...
bool writeMetadata(hid_t metadataGroup, FieldBase::Ptr layer)
Writes metadata for this layer.
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
void clear()
Clear the data structures and close the file.
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.
bool create(const std::string &filename, CreateMode cm=OverwriteMode)
Creates a .f3d file on disk.
std::vector< Layer > VectorLayerList
void getIntScalarLayerNames(std::vector< std::string > &names, const std::string &intPartitionName) const
Gets the names of all the scalar layers in a given partition, but assumes that partition name is the ...
File::Partition::Ptr partition(const std::string &partitionName)
Returns a pointer to the given partition.
static DataTypeEnum typeEnum()
std::string intPartitionName(const std::string &partitionName, const std::string &layerName, FieldRes::Ptr field)
Returns a unique partition name given the requested name. This ensures that partitions with matching ...
void getIntPartitionNames(std::vector< std::string > &names) const
Gets the names of all the -internal- partitions in the file.
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...
std::string removeUniqueId(const std::string &partitionName) const
Strips any unique identifiers from the partition name and returns the original name.
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)
std::string makeIntPartitionName(const std::string &partitionsName, int i) const
Makes an internal partition name given the external partition name. Effectively just tacks on ...
void addGroupMembership(const GroupMembershipMap &groupMembers)
Add to the group membership.
void printHierarchy() const
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.
int numIntPartitions(const std::string &partitionName) const
Returns the number of internal partitions for a given partition name.
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 writeMapping(hid_t partitionLocation, FieldMapping::Ptr mapping)
Writes the mapping to the given hdf5 node. Mappings are assumed to be light-weight enough to be store...
void closeInternal()
Closes the file if open.
void getScalarLayerNames(std::vector< std::string > &names) const
Gets all the scalar layer names.
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...
virtual ~Field3DFileBase()=0
Pure virtual destructor to ensure we never instantiate this class.
const File::Layer * vectorLayer(const std::string &name) const
Finds a vector layer.
boost::intrusive_ptr< EmptyField > Ptr
virtual ~Field3DOutputFile()
bool close()
Closes the file. No need to call this unless you specifically want to close the file early...
void getPartitionNames(std::vector< std::string > &names) const
Gets the names of all the partitions in the file.
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 File::Layer * scalarLayer(const std::string &name) const
Finds a scalar layer.
const FieldMetadata< Field3DFileBase > & metadata() const
Read only access to the m_metadata class.
void getVectorLayerNames(std::vector< std::string > &names) const
Gets all the vector layer names.
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.
void operator=(const Field3DFileBase &)
Contains the ClassFactory class for registering Field3D classes.
FieldIO::Ptr createFieldIO(const std::string &className) const
Instances an IO object by name.
void getScalarLayerNames(std::vector< std::string > &names, const std::string &partitionName) const
Gets the names of all the scalar layers in a given partition.
File::Partition::Ptr getPartition(const std::string &partitionName) const
Returns a pointer to the given partition.
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 std::string className() const
void getVectorLayerNames(std::vector< std::string > &names, const std::string &partitionName) const
Gets the names of all the vector layers in a given partition.
std::string incrementPartitionName(std::string &pname)
increment the partition or make it zero if there's not an integer suffix
virtual void metadataHasChanged(const std::string &)
This function should implemented by concrete classes to get the callback when metadata changes...
void addVectorLayer(const File::Layer &layer)
Adds a vector layer.
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.