Field3D
Field3DOutputFile Class Reference

Provides writing of .f3d (internally, hdf5) files. More...

#include <Field3DFile.h>

Inheritance diagram for Field3DOutputFile:
Field3DFileBase

Public Types

enum  CreateMode { OverwriteMode, FailOnExisting }
 
- Public Types inherited from Field3DFileBase
typedef std::map< std::string, std::string > GroupMembershipMap
 

Public Member Functions

bool create (const std::string &filename, CreateMode cm=OverwriteMode)
 Creates a .f3d file on disk. More...
 
template<class Data_T >
File::Partition::Ptr createNewPartition (const std::string &partitionName, const std::string &layerName, typename Field< Data_T >::Ptr field)
 create newPartition given the input config More...
 
std::string incrementPartitionName (std::string &pname)
 increment the partition or make it zero if there's not an integer suffix More...
 
bool writeGlobalMetadata ()
 This routine is call if you want to write out global metadata to disk. More...
 
bool writeGroupMembership ()
 This routine is called just before closing to write out any group membership to disk. More...
 
Constructors & destructor
 Field3DOutputFile ()
 
virtual ~Field3DOutputFile ()
 
Writing layer to disk
template<class Data_T >
bool writeScalarLayer (const std::string &layerName, typename Field< Data_T >::Ptr layer)
 Writes a scalar layer to the "Default" partition. More...
 
template<class Data_T >
bool writeVectorLayer (const std::string &layerName, typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr layer)
 Writes a vector layer to the "Default" partition. More...
 
template<class Data_T >
bool writeScalarLayer (const std::string &partitionName, const std::string &layerName, typename Field< Data_T >::Ptr layer)
 Writes a layer to a specific partition. The partition will be created if not specified. More...
 
template<class Data_T >
bool writeScalarLayer (typename Field< Data_T >::Ptr layer)
 Writes a layer to a specific partition. The field name and attribute name are used for partition and layer, respectively. More...
 
template<class Data_T >
bool writeVectorLayer (const std::string &partitionName, const std::string &layerName, typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr layer)
 Writes a layer to a specific partition. The partition will be created if not specified. More...
 
template<class Data_T >
bool writeVectorLayer (typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr layer)
 Writes a layer to a specific partition. The field name and attribute name are used for partition and layer, respectively. More...
 
- Public Member Functions inherited from Field3DFileBase
void clear ()
 Clear the data structures and close the file. More...
 
bool close ()
 Closes the file. No need to call this unless you specifically want to close the file early. It will close once the File object goes out of scope. More...
 
FieldMetadata< Field3DFileBase > & metadata ()
 accessor to the m_metadata class More...
 
const FieldMetadata< Field3DFileBase > & metadata () const
 Read only access to the m_metadata class. More...
 
virtual void metadataHasChanged (const std::string &)
 This function should implemented by concrete classes to get the callback when metadata changes. More...
 
 Field3DFileBase ()
 
virtual ~Field3DFileBase ()=0
 Pure virtual destructor to ensure we never instantiate this class. More...
 
void getPartitionNames (std::vector< std::string > &names) const
 Gets the names of all the partitions in the file. More...
 
void getScalarLayerNames (std::vector< std::string > &names, const std::string &partitionName) const
 Gets the names of all the scalar layers in a given partition. More...
 
void getVectorLayerNames (std::vector< std::string > &names, const std::string &partitionName) const
 Gets the names of all the vector layers in a given partition. More...
 
File::Partition::Ptr getPartition (const std::string &partitionName) const
 Returns a pointer to the given partition. More...
 
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 mappings get the same name but each subsequent differing mapping gets a new, separate name. More...
 
std::string removeUniqueId (const std::string &partitionName) const
 Strips any unique identifiers from the partition name and returns the original name. More...
 
void addGroupMembership (const GroupMembershipMap &groupMembers)
 Add to the group membership. More...
 
void printHierarchy () const
 

Private Member Functions

template<class Data_T >
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. More...
 
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 stored as plain attributes under a group. More...
 
bool writeMetadata (hid_t metadataGroup, FieldBase::Ptr layer)
 Writes metadata for this layer. More...
 
bool writeMetadata (hid_t metadataGroup)
 Writes metadata for this file. More...
 

Additional Inherited Members

- Protected Types inherited from Field3DFileBase
typedef std::map< std::string, int > PartitionCountMap
 
typedef std::vector< File::Partition::PtrPartitionList
 
- Protected Member Functions inherited from Field3DFileBase
void closeInternal ()
 Closes the file if open. More...
 
File::Partition::Ptr partition (const std::string &partitionName)
 Returns a pointer to the given partition. More...
 
File::Partition::Ptr partition (const std::string &partitionName) const
 Returns a pointer to the given partition. More...
 
void getIntPartitionNames (std::vector< std::string > &names) const
 Gets the names of all the -internal- partitions in the file. More...
 
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 -internal- partition name. More...
 
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 -internal- partition name. More...
 
int numIntPartitions (const std::string &partitionName) const
 Returns the number of internal partitions for a given partition name. More...
 
std::string makeIntPartitionName (const std::string &partitionsName, int i) const
 Makes an internal partition name given the external partition name. Effectively just tacks on .X to the name, where X is the number. More...
 
- Protected Attributes inherited from Field3DFileBase
hid_t m_file
 The hdf5 id of the current file. Will be -1 if no file is open. More...
 
GroupMembershipMap m_groupMembership
 Keeps track of group membership for each layer of partition name. The key is the "group" and the value is a space separated list of "partitionName.0:Layer1 partitionName.1:Layer0 ...". More...
 
std::vector< LayerInfom_layerInfo
 This stores layer info. More...
 
FieldMetadata< Field3DFileBasem_metadata
 metadata More...
 
PartitionCountMap m_partitionCount
 Contains a counter for each partition name. This is used to keep multiple fields with the same name unique in the file. More...
 
std::vector< std::string > m_partitionNames
 This stores partition names. More...
 
PartitionList m_partitions
 Vector of partitions. More...
 

Detailed Description

Provides writing of .f3d (internally, hdf5) files.

Refer to using_files for examples of how to use this in your code.

Note
We distinguish between scalar and vector layers even though both are templated. A scalarField<float> layer is interchangeable with a scalarField<double> (conceptually) but not with a scalar<V3f>, and thus not with vectorField<float>.

Definition at line 764 of file Field3DFile.h.

Member Enumeration Documentation

Enumerator
OverwriteMode 
FailOnExisting 

Definition at line 770 of file Field3DFile.h.

Constructor & Destructor Documentation

Field3DOutputFile::Field3DOutputFile ( )

Definition at line 1206 of file Field3DFile.cpp.

1207 {
1208  // Empty
1209 }
Field3DOutputFile::~Field3DOutputFile ( )
virtual

Definition at line 1213 of file Field3DFile.cpp.

1214 {
1215 
1216 }

Member Function Documentation

template<class Data_T >
bool Field3DOutputFile::writeScalarLayer ( const std::string &  layerName,
typename Field< Data_T >::Ptr  layer 
)
inline

Writes a scalar layer to the "Default" partition.

Definition at line 792 of file Field3DFile.h.

794  { return writeScalarLayer<Data_T>(layerName, std::string("default"), layer); }
template<class Data_T >
bool Field3DOutputFile::writeVectorLayer ( const std::string &  layerName,
typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr  layer 
)
inline

Writes a vector layer to the "Default" partition.

Definition at line 798 of file Field3DFile.h.

Referenced by writeScalarLayer(), and writeVectorLayer().

800  { return writeVectorLayer<Data_T>(layerName, std::string("default"), layer); }
template<class Data_T >
bool Field3DOutputFile::writeScalarLayer ( const std::string &  partitionName,
const std::string &  layerName,
typename Field< Data_T >::Ptr  layer 
)

Writes a layer to a specific partition. The partition will be created if not specified.

Definition at line 1576 of file Field3DFile.h.

1579 {
1580  return writeLayer<Data_T>(partitionName, layerName, false, field);
1581 }
template<class Data_T >
bool Field3DOutputFile::writeScalarLayer ( typename Field< Data_T >::Ptr  layer)

Writes a layer to a specific partition. The field name and attribute name are used for partition and layer, respectively.

Definition at line 1587 of file Field3DFile.h.

References FieldBase::attribute, FieldBase::name, Msg::print(), Msg::SevWarning, and writeVectorLayer().

1588 {
1589  if (layer->name.size() == 0) {
1590  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeScalarLayer: "
1591  "Tried to write a scalar layer with no name");
1592  return false;
1593  }
1594  if (layer->attribute.size() == 0) {
1595  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeScalarLayer: "
1596  "Tried to write a scalar layer with no attribute name");
1597  return false;
1598  }
1599  return writeScalarLayer<Data_T>(layer->name, layer->attribute, layer);
1600 }
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
template<class Data_T >
bool Field3DOutputFile::writeVectorLayer ( const std::string &  partitionName,
const std::string &  layerName,
typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr  layer 
)

Writes a layer to a specific partition. The partition will be created if not specified.

Definition at line 1607 of file Field3DFile.h.

References writeVectorLayer().

1610 {
1611  return writeLayer<FIELD3D_VEC3_T<Data_T> >(partitionName, layerName,
1612  true, field);
1613 }
template<class Data_T >
bool Field3DOutputFile::writeVectorLayer ( typename Field< FIELD3D_VEC3_T< Data_T > >::Ptr  layer)

Writes a layer to a specific partition. The field name and attribute name are used for partition and layer, respectively.

Definition at line 1620 of file Field3DFile.h.

References Msg::print(), and Msg::SevWarning.

1621 {
1622  if (layer->name.size() == 0) {
1623  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeVectorLayer: "
1624  "Tried to write a vector layer with no name");
1625  return false;
1626  }
1627  if (layer->attribute.size() == 0) {
1628  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeVectorLayer: "
1629  "Tried to write a vector layer with no attribute name");
1630  return false;
1631  }
1632  return writeVectorLayer<Data_T>(layer->name, layer->attribute, layer);
1633 }
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
bool Field3DOutputFile::create ( const std::string &  filename,
CreateMode  cm = OverwriteMode 
)

Creates a .f3d file on disk.

Todo:
If the file can't be created hdf5 spits out an ugly error msg, we should make sure that doesn't happen.

Definition at line 1222 of file Field3DFile.cpp.

References Field3DFileBase::closeInternal(), g_hdf5Mutex, Field3DFileBase::m_file, Msg::print(), Msg::SevWarning, and Hdf5Util::writeAttribute().

1223 {
1224  GlobalLock lock(g_hdf5Mutex);
1225 
1226  closeInternal();
1227 
1228  bool success = true;
1229 
1230  try {
1231 
1232  hid_t faid = H5Pcreate(H5P_FILE_ACCESS);
1233  H5Pset_libver_bounds(faid, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST);
1234 
1235  // Create new file
1236  switch (cm) {
1237  case OverwriteMode:
1238  m_file = H5Fcreate(filename.c_str(),
1239  H5F_ACC_TRUNC, H5P_DEFAULT, faid);
1240  break;
1241  case FailOnExisting:
1242  m_file = H5Fcreate(filename.c_str(),
1243  H5F_ACC_EXCL, H5P_DEFAULT, faid);
1244  break;
1245  }
1246 
1247  // Check that file was created
1248  if (m_file < 0)
1249  throw ErrorCreatingFileException(filename);
1250 
1251  // Create a version attribute on the root node
1252  if (!writeAttribute(m_file, k_versionAttrName, 3,
1253  k_currentFileVersion[0])) {
1254  Msg::print(Msg::SevWarning, "Adding version number.");
1255  closeInternal();
1256  return false;
1257  }
1258 
1259  }
1260  catch (ErrorCreatingFileException &e) {
1261  Msg::print(Msg::SevWarning, "Couldn't create file: " + string(e.what()) );
1262  success = false;
1263  }
1264  catch (WriteAttributeException &e) {
1265  Msg::print(Msg::SevWarning, "In file : " + filename +
1266  " - Couldn't add attribute " + string(e.what()) );
1267  success = false;
1268  }
1269  catch (...) {
1271  "Unknown error when creating file: " + filename );
1272  success = false;
1273  }
1274 
1275  return success;
1276 }
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
void closeInternal()
Closes the file if open.
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:387
bool Field3DOutputFile::writeGlobalMetadata ( )

This routine is call if you want to write out global metadata to disk.

Definition at line 1466 of file Field3DFile.cpp.

References g_hdf5Mutex, Hdf5Util::H5Base::id(), Field3DFileBase::m_file, Msg::print(), and Msg::SevWarning.

1467 {
1468  GlobalLock lock(g_hdf5Mutex);
1469 
1470  // Add metadata group and write it out
1471  H5ScopedGcreate metadataGroup(m_file, "field3d_global_metadata");
1472  if (metadataGroup.id() < 0) {
1473  Msg::print(Msg::SevWarning, "Error creating group: file metadata");
1474  return false;
1475  }
1476  if (!writeMetadata(metadataGroup.id())) {
1477  Msg::print(Msg::SevWarning, "Error writing file metadata.");
1478  return false;
1479  }
1480 
1481  return true;
1482 }
bool writeMetadata(hid_t metadataGroup, FieldBase::Ptr layer)
Writes metadata for this layer.
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:165
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:387
bool Field3DOutputFile::writeGroupMembership ( )

This routine is called just before closing to write out any group membership to disk.

Definition at line 1487 of file Field3DFile.cpp.

References g_hdf5Mutex, Field3DFileBase::m_file, Field3DFileBase::m_groupMembership, Msg::print(), Msg::SevWarning, and Hdf5Util::writeAttribute().

1488 {
1489  using namespace std;
1490  using namespace Hdf5Util;
1491 
1492  GlobalLock lock(g_hdf5Mutex);
1493 
1494  if (!m_groupMembership.size())
1495  return true;
1496 
1497  H5ScopedGcreate group(m_file, "field3d_group_membership");
1498  if (group < 0) {
1500  "Error creating field3d_group_membership group.");
1501  return false;
1502  }
1503 
1504  if (!writeAttribute(group, "is_field3d_group_membership", "1")) {
1506  "Failed to write field3d_group_membership attribute.");
1507  return false;
1508  }
1509 
1510  std::map<std::string, std::string>::const_iterator iter =
1511  m_groupMembership.begin();
1512  std::map<std::string, std::string>::const_iterator iEnd =
1513  m_groupMembership.end();
1514 
1515  for (; iter != iEnd; ++iter) {
1516  if (!writeAttribute(group, iter->first, iter->second)) {
1518  "Failed to write groupMembership string: "+ iter->first);
1519  return false;
1520  }
1521  }
1522 
1523  return true;
1524 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:165
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:387
GroupMembershipMap m_groupMembership
Keeps track of group membership for each layer of partition name. The key is the "group" and the valu...
Definition: Field3DFile.h:400
std::string Field3DOutputFile::incrementPartitionName ( std::string &  pname)

increment the partition or make it zero if there's not an integer suffix

Definition at line 1529 of file Field3DFile.cpp.

References Field3DFileBase::m_partitionCount, Field3DFileBase::makeIntPartitionName(), and Field3DFileBase::removeUniqueId().

1530 {
1531  std::string myPartitionName = removeUniqueId(partitionName);
1532  int nextIdx = -1;
1533  if (m_partitionCount.find(myPartitionName) != m_partitionCount.end()) {
1534  nextIdx = ++m_partitionCount[myPartitionName];
1535  } else {
1536  nextIdx = 0;
1537  m_partitionCount[myPartitionName] = 0;
1538  }
1539 
1540  return makeIntPartitionName(myPartitionName, nextIdx);
1541 }
PartitionCountMap m_partitionCount
Contains a counter for each partition name. This is used to keep multiple fields with the same name u...
Definition: Field3DFile.h:395
std::string removeUniqueId(const std::string &partitionName) const
Strips any unique identifiers from the partition name and returns the original name.
std::string makeIntPartitionName(const std::string &partitionsName, int i) const
Makes an internal partition name given the external partition name. Effectively just tacks on ...
template<class Data_T >
File::Partition::Ptr Field3DOutputFile::createNewPartition ( const std::string &  partitionName,
const std::string &  layerName,
typename Field< Data_T >::Ptr  field 
)

create newPartition given the input config

Todo:
We should probably remove the group on disk if we can't write the mapping

Definition at line 1378 of file Field3DFile.h.

References g_hdf5Mutex, FieldRes::mapping(), Msg::print(), Msg::SevWarning, and Hdf5Util::writeAttribute().

1381 {
1382  using namespace Hdf5Util;
1383  using namespace Exc;
1384 
1385  GlobalLock lock(g_hdf5Mutex);
1386 
1388 
1389  newPart->name = partitionName;
1390 
1391  H5ScopedGcreate partGroup(m_file, newPart->name.c_str());
1392  if (partGroup.id() < 0) {
1394  "Error creating partition: " + newPart->name);
1395  return File::Partition::Ptr();
1396  }
1397 
1398  m_partitions.push_back(newPart);
1399 
1400  // Pick up new pointer
1401  File::Partition::Ptr part = partition(partitionName);
1402 
1403  // Add mapping group to the partition
1406  try {
1407  if (!writeMapping(partGroup.id(), field->mapping())) {
1409  "writeMapping returned false for an unknown reason ");
1410  return File::Partition::Ptr();
1411  }
1412  }
1413  catch (WriteMappingException &e) {
1414  Msg::print(Msg::SevWarning, "Couldn't write mapping for partition: "
1415  + partitionName);
1416  return File::Partition::Ptr();
1417  }
1418  catch (...) {
1420  "Unknown error when writing mapping for partition: "
1421  + partitionName);
1422  return File::Partition::Ptr();
1423  }
1424 
1425  // Set the mapping of the partition. Since all layers share their
1426  // partition's mapping, we can just pick this first one. All subsequent
1427  // additions to the same partition are checked to have the same mapping
1428  part->mapping = field->mapping();
1429 
1430  // Tag node as partition
1431  // Create a version attribute on the root node
1432  if (!writeAttribute(partGroup.id(), "is_field3d_partition", "1")) {
1433  Msg::print(Msg::SevWarning, "Adding partition string.");
1434  return File::Partition::Ptr();
1435  }
1436 
1437  return part;
1438 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
boost::intrusive_ptr< Partition > Ptr
Definition: Field3DFile.h:152
Namespace for Exception objects.
Definition: Exception.h:57
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
File::Partition::Ptr partition(const std::string &partitionName)
Returns a pointer to the given partition.
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:165
PartitionList m_partitions
Vector of partitions.
Definition: Field3DFile.h:389
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...
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:387
bool Field3DOutputFile::writeMapping ( hid_t  partitionLocation,
FieldMapping::Ptr  mapping 
)
private

Writes the mapping to the given hdf5 node. Mappings are assumed to be light-weight enough to be stored as plain attributes under a group.

Definition at line 1280 of file Field3DFile.cpp.

References g_hdf5Mutex, Hdf5Util::H5Base::id(), Msg::print(), Msg::SevWarning, and writeFieldMapping().

1282 {
1283  GlobalLock lock(g_hdf5Mutex);
1284 
1285  try {
1286  // Make a group under the partition to store the mapping data
1287  H5ScopedGcreate mappingGroup(partitionGroup, k_mappingStr);
1288  if (mappingGroup.id() < 0)
1289  throw CreateGroupException(k_mappingStr);
1290  // Let FieldMappingIO handle the rest
1291  if (!writeFieldMapping(mappingGroup.id(), mapping))
1292  throw WriteMappingException(k_mappingStr);
1293  }
1294  catch (CreateGroupException &e) {
1295  Msg::print(Msg::SevWarning, "Couldn't create group: " + string(e.what()) );
1296  throw WriteMappingException(k_mappingStr);
1297  }
1298  return true;
1299 }
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:165
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
bool writeFieldMapping(hid_t mappingGroup, FieldMapping::Ptr mapping)
This function creates a FieldMappingIO instance based on mapping->className() which then writes Field...
template<class Data_T >
bool Field3DOutputFile::writeLayer ( const std::string &  partitionName,
const std::string &  layerName,
bool  isVectorLayer,
typename Field< Data_T >::Ptr  layer 
)
private

Performs the actual writing of the layer to disk.

Definition at line 1444 of file Field3DFile.h.

References g_hdf5Mutex, FieldRes::mapping(), File::Layer::name, Msg::print(), Msg::SevWarning, Hdf5Util::writeAttribute(), and writeField().

1448 {
1449  using namespace std;
1450  using namespace Exc;
1451  using namespace Hdf5Util;
1452 
1453  GlobalLock lock(g_hdf5Mutex);
1454 
1455  if (!field) {
1457  "Called writeLayer with null pointer. Ignoring...");
1458  return false;
1459  }
1460 
1461  if (m_file < 0) {
1463  "Attempting to write layer without opening file first. ");
1464  return false;
1465  }
1466 
1467  string partitionName = intPartitionName(userPartitionName, layerName, field);
1468 
1469  // See if the partition already exists or if we need to make it ---
1470 
1471  File::Partition::Ptr part = partition(partitionName);
1472 
1473  if (!part) {
1474  part = createNewPartition<Data_T>(partitionName,layerName,field);
1475  if (!part)
1476  return false;
1477  } else {
1478 
1479  if (!field->mapping()) {
1481  "Couldn't add layer \"" + layerName + "\" to partition \""
1482  + partitionName + "\" because the layer's mapping is null.");
1483  return false;
1484  }
1485 
1486  // If the partition already existed, we need to make sure that the layer
1487  // doesn't also exist
1488  if (!isVectorLayer) {
1489  if (part->scalarLayer(layerName)) {
1490  //need to create a new partition and then add the layer to that
1491  std::string newPartitionName = incrementPartitionName(partitionName);
1492  part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1493  if (!part)
1494  return false;
1495  }
1496  } else {
1497  if (part->vectorLayer(layerName)) {
1498  //need to create a new partition and then add the layer to that
1499  std::string newPartitionName = incrementPartitionName(partitionName);
1500  part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1501  if (!part)
1502  return false;
1503  }
1504  }
1505  }
1506 
1507  if (!part->mapping) {
1508  Msg::print(Msg::SevWarning, "Severe error - partition mapping is null: "
1509  + partitionName);
1510  return false;
1511  }
1512 
1513  // Check that the mapping matches what's already in the Partition
1514  if (!field->mapping()->isIdentical(part->mapping)) {
1515  Msg::print(Msg::SevWarning, "Couldn't add layer \"" + layerName
1516  + "\" to partition \"" + partitionName
1517  + "\" because mapping doesn't match");
1518  return false;
1519  }
1520 
1521  // Open the partition
1522  H5ScopedGopen partGroup(m_file, part->name.c_str(), H5P_DEFAULT);
1523 
1524  // Build a Layer object ---
1525 
1526  File::Layer layer;
1527  layer.name = layerName;
1528  layer.parent = partitionName;
1529 
1530  // Add Layer to file ---
1531 
1532  H5ScopedGcreate layerGroup(partGroup.id(), layerName.c_str(),
1533  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1534 
1535  if (layerGroup.id() < 0) {
1536  Msg::print(Msg::SevWarning, "Error creating layer: " + layerName);
1537  return false;
1538  }
1539 
1540  // Tag as layer
1541  if (!writeAttribute(layerGroup.id(), "class_type", "field3d_layer")) {
1542  Msg::print(Msg::SevWarning, "Error adding layer string.");
1543  return false;
1544  }
1545 
1546  // Add metadata group and write it out
1547  H5ScopedGcreate metadataGroup(layerGroup.id(), "metadata");
1548  if (metadataGroup.id() < 0) {
1549  Msg::print(Msg::SevWarning, "Error creating group: metadata");
1550  return false;
1551  }
1552  if (!writeMetadata(metadataGroup.id(), field)) {
1553  Msg::print(Msg::SevWarning, "Error writing metadata.");
1554  return false;
1555  }
1556 
1557  if (!writeField(layerGroup.id(), field)) {
1558  Msg::print(Msg::SevWarning, "Error writing layer: " + layer.name);
1559  return false;
1560  }
1561 
1562  // Add layer to partition ---
1563 
1564  if (isVectorLayer)
1565  part->addVectorLayer(layer);
1566  else
1567  part->addScalarLayer(layer);
1568 
1569  return true;
1570 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
boost::intrusive_ptr< Partition > Ptr
Definition: Field3DFile.h:152
Namespace for Exception objects.
Definition: Exception.h:57
bool writeMetadata(hid_t metadataGroup, FieldBase::Ptr layer)
Writes metadata for this layer.
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
File::Partition::Ptr partition(const std::string &partitionName)
Returns a pointer to the given partition.
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 ...
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:165
std::string name
The name of the layer (always available)
Definition: Field3DFile.h:124
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:387
Scoped object - opens a group on creation and closes it on destruction.
Definition: Hdf5Util.h:194
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...
std::string incrementPartitionName(std::string &pname)
increment the partition or make it zero if there&#39;s not an integer suffix
bool Field3DOutputFile::writeMetadata ( hid_t  metadataGroup,
FieldBase::Ptr  layer 
)
private

Writes metadata for this layer.

Definition at line 1303 of file Field3DFile.cpp.

References FieldMetadata< CallBack_T >::floatMetadata(), FieldMetadata< CallBack_T >::intMetadata(), Msg::print(), Msg::SevWarning, FieldMetadata< CallBack_T >::strMetadata(), FieldMetadata< CallBack_T >::vecFloatMetadata(), FieldMetadata< CallBack_T >::vecIntMetadata(), and Hdf5Util::writeAttribute().

1304 {
1305  using namespace Hdf5Util;
1306 
1307  {
1309  field->metadata().strMetadata().begin();
1311  field->metadata().strMetadata().end();
1312  for (; i != end; ++i) {
1313  if (!writeAttribute(metadataGroup, i->first, i->second))
1314  {
1315  Msg::print(Msg::SevWarning, "Writing attribute " + i->first );
1316  return false;
1317  }
1318  }
1319  }
1320 
1321  {
1323  field->metadata().intMetadata().begin();
1325  field->metadata().intMetadata().end();
1326  for (; i != end; ++i) {
1327  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1328  {
1329  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1330  return false;
1331  }
1332  }
1333  }
1334 
1335  {
1337  field->metadata().floatMetadata().begin();
1339  field->metadata().floatMetadata().end();
1340  for (; i != end; ++i) {
1341  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1342  {
1343  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1344  return false;
1345  }
1346  }
1347  }
1348 
1349  {
1351  field->metadata().vecIntMetadata().begin();
1353  field->metadata().vecIntMetadata().end();
1354  for (; i != end; ++i) {
1355  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1356  {
1357  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1358  return false;
1359  }
1360  }
1361  }
1362 
1363  {
1365  field->metadata().vecFloatMetadata().begin();
1367  field->metadata().vecFloatMetadata().end();
1368  for (; i != end; ++i) {
1369  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1370  {
1371  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1372  return false;
1373  }
1374  }
1375 
1376  }
1377 
1378  return true;
1379 
1380 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
V3f vecFloatMetadata(const std::string &name, const V3f &defaultVal) const
Tries to retrieve a V3f metadata value. Returns the specified default value if no metadata was found...
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
float floatMetadata(const std::string &name, const float defaultVal) const
Tries to retrieve a float metadata value. Returns the specified default value if no metadata was foun...
int intMetadata(const std::string &name, const int defaultVal) const
Tries to retrieve an int metadata value. Returns the specified default value if no metadata was found...
V3i vecIntMetadata(const std::string &name, const V3i &defaultVal) const
Tries to retrieve a V3i metadata value. Returns the specified default value if no metadata was found...
std::string strMetadata(const std::string &name, const std::string &defaultVal) const
Tries to retrieve a string metadata value. Returns the specified default value if no metadata was fou...
bool Field3DOutputFile::writeMetadata ( hid_t  metadataGroup)
private

Writes metadata for this file.

Definition at line 1384 of file Field3DFile.cpp.

References FieldMetadata< CallBack_T >::floatMetadata(), FieldMetadata< CallBack_T >::intMetadata(), Field3DFileBase::metadata(), Msg::print(), Msg::SevWarning, FieldMetadata< CallBack_T >::strMetadata(), FieldMetadata< CallBack_T >::vecFloatMetadata(), FieldMetadata< CallBack_T >::vecIntMetadata(), and Hdf5Util::writeAttribute().

1385 {
1386  using namespace Hdf5Util;
1387 
1388  {
1390  metadata().strMetadata().begin();
1392  metadata().strMetadata().end();
1393  for (; i != end; ++i) {
1394  if (!writeAttribute(metadataGroup, i->first, i->second))
1395  {
1396  Msg::print(Msg::SevWarning, "Writing attribute " + i->first );
1397  return false;
1398  }
1399  }
1400  }
1401 
1402  {
1404  metadata().intMetadata().begin();
1406  metadata().intMetadata().end();
1407  for (; i != end; ++i) {
1408  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1409  {
1410  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1411  return false;
1412  }
1413  }
1414  }
1415 
1416  {
1418  metadata().floatMetadata().begin();
1420  metadata().floatMetadata().end();
1421  for (; i != end; ++i) {
1422  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1423  {
1424  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1425  return false;
1426  }
1427  }
1428  }
1429 
1430  {
1432  metadata().vecIntMetadata().begin();
1434  metadata().vecIntMetadata().end();
1435  for (; i != end; ++i) {
1436  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1437  {
1438  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1439  return false;
1440  }
1441  }
1442  }
1443 
1444  {
1446  metadata().vecFloatMetadata().begin();
1448  metadata().vecFloatMetadata().end();
1449  for (; i != end; ++i) {
1450  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1451  {
1452  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1453  return false;
1454  }
1455  }
1456 
1457  }
1458 
1459  return true;
1460 
1461 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
V3f vecFloatMetadata(const std::string &name, const V3f &defaultVal) const
Tries to retrieve a V3f metadata value. Returns the specified default value if no metadata was found...
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
float floatMetadata(const std::string &name, const float defaultVal) const
Tries to retrieve a float metadata value. Returns the specified default value if no metadata was foun...
int intMetadata(const std::string &name, const int defaultVal) const
Tries to retrieve an int metadata value. Returns the specified default value if no metadata was found...
FieldMetadata< Field3DFileBase > & metadata()
accessor to the m_metadata class
Definition: Field3DFile.h:318
V3i vecIntMetadata(const std::string &name, const V3i &defaultVal) const
Tries to retrieve a V3i metadata value. Returns the specified default value if no metadata was found...
std::string strMetadata(const std::string &name, const std::string &defaultVal) const
Tries to retrieve a string metadata value. Returns the specified default value if no metadata was fou...

The documentation for this class was generated from the following files: