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::Ptr
PartitionList
 
- 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 626 of file Field3DFile.h.

Member Enumeration Documentation

Enumerator
OverwriteMode 
FailOnExisting 

Definition at line 632 of file Field3DFile.h.

Constructor & Destructor Documentation

Field3DOutputFile::Field3DOutputFile ( )

Definition at line 1199 of file Field3DFile.cpp.

1200 {
1201  // Empty
1202 }
Field3DOutputFile::~Field3DOutputFile ( )
virtual

Definition at line 1206 of file Field3DFile.cpp.

1207 {
1208 
1209 }

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 654 of file Field3DFile.h.

656  { 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 660 of file Field3DFile.h.

662  { 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 1511 of file Field3DFile.h.

1514 {
1515  return writeLayer<Data_T>(partitionName, layerName, false, field);
1516 }
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 1522 of file Field3DFile.h.

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

1523 {
1524  if (layer->name.size() == 0) {
1525  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeScalarLayer: "
1526  "Tried to write a scalar layer with no name");
1527  return false;
1528  }
1529  if (layer->attribute.size() == 0) {
1530  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeScalarLayer: "
1531  "Tried to write a scalar layer with no attribute name");
1532  return false;
1533  }
1534  return writeScalarLayer<Data_T>(layer->name, layer->attribute, layer);
1535 }
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
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 1542 of file Field3DFile.h.

1545 {
1546  return writeLayer<FIELD3D_VEC3_T<Data_T> >(partitionName, layerName,
1547  true, field);
1548 }
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 1555 of file Field3DFile.h.

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

1556 {
1557  if (layer->name.size() == 0) {
1558  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeVectorLayer: "
1559  "Tried to write a vector layer with no name");
1560  return false;
1561  }
1562  if (layer->attribute.size() == 0) {
1563  Msg::print(Msg::SevWarning, "Field3DOutputFile::writeVectorLayer: "
1564  "Tried to write a vector layer with no attribute name");
1565  return false;
1566  }
1567  return writeVectorLayer<Data_T>(layer->name, layer->attribute, layer);
1568 }
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
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 1215 of file Field3DFile.cpp.

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

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

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

Definition at line 1455 of file Field3DFile.cpp.

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

1456 {
1457 
1458  // Add metadata group and write it out
1459  H5ScopedGcreate metadataGroup(m_file, "field3d_global_metadata");
1460  if (metadataGroup.id() < 0) {
1461  Msg::print(Msg::SevWarning, "Error creating group: file metadata");
1462  return false;
1463  }
1464  if (!writeMetadata(metadataGroup.id())) {
1465  Msg::print(Msg::SevWarning, "Error writing file metadata.");
1466  return false;
1467  }
1468 
1469  return true;
1470 }
bool writeMetadata(hid_t metadataGroup, FieldBase::Ptr layer)
Writes metadata for this layer.
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:150
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:384
bool Field3DOutputFile::writeGroupMembership ( )

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

Definition at line 1475 of file Field3DFile.cpp.

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

1476 {
1477  using namespace std;
1478  using namespace Hdf5Util;
1479 
1480  if (!m_groupMembership.size())
1481  return true;
1482 
1483  H5ScopedGcreate group(m_file, "field3d_group_membership");
1484  if (group < 0) {
1486  "Error creating field3d_group_membership group.");
1487  return false;
1488  }
1489 
1490  if (!writeAttribute(group, "is_field3d_group_membership", "1")) {
1492  "Failed to write field3d_group_membership attribute.");
1493  return false;
1494  }
1495 
1496  std::map<std::string, std::string>::const_iterator iter =
1497  m_groupMembership.begin();
1498  std::map<std::string, std::string>::const_iterator iEnd =
1499  m_groupMembership.end();
1500 
1501  for (; iter != iEnd; ++iter) {
1502  if (!writeAttribute(group, iter->first, iter->second)) {
1504  "Failed to write groupMembership string: "+ iter->first);
1505  return false;
1506  }
1507  }
1508 
1509  return true;
1510 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:150
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:384
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:397
std::string Field3DOutputFile::incrementPartitionName ( std::string &  pname)

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

Definition at line 1515 of file Field3DFile.cpp.

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

1516 {
1517  std::string myPartitionName = removeUniqueId(partitionName);
1518  int nextIdx = -1;
1519  if (m_partitionCount.find(myPartitionName) != m_partitionCount.end()) {
1520  nextIdx = ++m_partitionCount[myPartitionName];
1521  } else {
1522  nextIdx = 0;
1523  m_partitionCount[myPartitionName] = 0;
1524  }
1525 
1526  return makeIntPartitionName(myPartitionName, nextIdx);
1527 }
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:392
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 1317 of file Field3DFile.h.

References Field3DFileBase::m_file, Field3DFileBase::m_partitions, FieldRes::mapping(), Field3DFileBase::partition(), Msg::print(), Msg::SevWarning, and Hdf5Util::writeAttribute().

1320 {
1321  using namespace Hdf5Util;
1322  using namespace Exc;
1323 
1325 
1326  newPart->name = partitionName;
1327 
1328  H5ScopedGcreate partGroup(m_file, newPart->name.c_str());
1329  if (partGroup.id() < 0) {
1331  "Error creating partition: " + newPart->name);
1332  return File::Partition::Ptr();
1333  }
1334 
1335  m_partitions.push_back(newPart);
1336 
1337  // Pick up new pointer
1338  File::Partition::Ptr part = partition(partitionName);
1339 
1340  // Add mapping group to the partition
1343  try {
1344  if (!writeMapping(partGroup.id(), field->mapping())) {
1346  "writeMapping returned false for an unknown reason ");
1347  return File::Partition::Ptr();
1348  }
1349  }
1350  catch (WriteMappingException &e) {
1351  Msg::print(Msg::SevWarning, "Couldn't write mapping for partition: "
1352  + partitionName);
1353  return File::Partition::Ptr();
1354  }
1355  catch (...) {
1357  "Unknown error when writing mapping for partition: "
1358  + partitionName);
1359  return File::Partition::Ptr();
1360  }
1361 
1362  // Set the mapping of the partition. Since all layers share their
1363  // partition's mapping, we can just pick this first one. All subsequent
1364  // additions to the same partition are checked to have the same mapping
1365  part->mapping = field->mapping();
1366 
1367  // Tag node as partition
1368  // Create a version attribute on the root node
1369  if (!writeAttribute(partGroup.id(), "is_field3d_partition", "1")) {
1370  Msg::print(Msg::SevWarning, "Adding partition string.");
1371  return File::Partition::Ptr();
1372  }
1373 
1374  return part;
1375 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
boost::intrusive_ptr< Partition > Ptr
Definition: Field3DFile.h:149
Namespace for Exception objects.
Definition: Exception.h:57
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
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:150
PartitionList m_partitions
Vector of partitions.
Definition: Field3DFile.h:386
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...
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:384
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 1271 of file Field3DFile.cpp.

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

1273 {
1274  try {
1275  // Make a group under the partition to store the mapping data
1276  H5ScopedGcreate mappingGroup(partitionGroup, k_mappingStr);
1277  if (mappingGroup.id() < 0)
1278  throw CreateGroupException(k_mappingStr);
1279  // Let FieldMappingIO handle the rest
1280  if (!writeFieldMapping(mappingGroup.id(), mapping))
1281  throw WriteMappingException(k_mappingStr);
1282  }
1283  catch (CreateGroupException &e) {
1284  Msg::print(Msg::SevWarning, "Couldn't create group: " + string(e.what()) );
1285  throw WriteMappingException(k_mappingStr);
1286  }
1287  return true;
1288 }
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
Scoped object - creates a group on creation and closes it on destruction.
Definition: Hdf5Util.h:150
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 1381 of file Field3DFile.h.

References Field3DFileBase::intPartitionName(), Field3DFileBase::m_file, FieldRes::mapping(), File::Layer::name, Field3DFileBase::partition(), Msg::print(), Msg::SevWarning, Hdf5Util::writeAttribute(), and writeField().

1385 {
1386  using namespace std;
1387  using namespace Exc;
1388  using namespace Hdf5Util;
1389 
1390  if (!field) {
1392  "Called writeLayer with null pointer. Ignoring...");
1393  return false;
1394  }
1395 
1396  if (m_file < 0) {
1398  "Attempting to write layer without opening file first. ");
1399  return false;
1400  }
1401 
1402  string partitionName = intPartitionName(userPartitionName, layerName, field);
1403 
1404  // See if the partition already exists or if we need to make it ---
1405 
1406  File::Partition::Ptr part = partition(partitionName);
1407 
1408  if (!part) {
1409  part = createNewPartition<Data_T>(partitionName,layerName,field);
1410  if (!part)
1411  return false;
1412  } else {
1413 
1414  if (!field->mapping()) {
1416  "Couldn't add layer \"" + layerName + "\" to partition \""
1417  + partitionName + "\" because the layer's mapping is null.");
1418  return false;
1419  }
1420 
1421  // If the partition already existed, we need to make sure that the layer
1422  // doesn't also exist
1423  if (!isVectorLayer) {
1424  if (part->scalarLayer(layerName)) {
1425  //need to create a new partition and then add the layer to that
1426  std::string newPartitionName = incrementPartitionName(partitionName);
1427  part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1428  if (!part)
1429  return false;
1430  }
1431  } else {
1432  if (part->vectorLayer(layerName)) {
1433  //need to create a new partition and then add the layer to that
1434  std::string newPartitionName = incrementPartitionName(partitionName);
1435  part = createNewPartition<Data_T>(newPartitionName,layerName,field);
1436  if (!part)
1437  return false;
1438  }
1439  }
1440  }
1441 
1442  if (!part->mapping) {
1443  Msg::print(Msg::SevWarning, "Severe error - partition mapping is null: "
1444  + partitionName);
1445  return false;
1446  }
1447 
1448  // Check that the mapping matches what's already in the Partition
1449  if (!field->mapping()->isIdentical(part->mapping)) {
1450  Msg::print(Msg::SevWarning, "Couldn't add layer \"" + layerName
1451  + "\" to partition \"" + partitionName
1452  + "\" because mapping doesn't match");
1453  return false;
1454  }
1455 
1456  // Open the partition
1457  H5ScopedGopen partGroup(m_file, part->name.c_str(), H5P_DEFAULT);
1458 
1459  // Build a Layer object ---
1460 
1461  File::Layer layer;
1462  layer.name = layerName;
1463  layer.parent = partitionName;
1464 
1465  // Add Layer to file ---
1466 
1467  H5ScopedGcreate layerGroup(partGroup.id(), layerName.c_str(),
1468  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1469 
1470  if (layerGroup.id() < 0) {
1471  Msg::print(Msg::SevWarning, "Error creating layer: " + layerName);
1472  return false;
1473  }
1474 
1475  // Tag as layer
1476  if (!writeAttribute(layerGroup.id(), "class_type", "field3d_layer")) {
1477  Msg::print(Msg::SevWarning, "Error adding layer string.");
1478  return false;
1479  }
1480 
1481  // Add metadata group and write it out
1482  H5ScopedGcreate metadataGroup(layerGroup.id(), "metadata");
1483  if (metadataGroup.id() < 0) {
1484  Msg::print(Msg::SevWarning, "Error creating group: metadata");
1485  return false;
1486  }
1487  if (!writeMetadata(metadataGroup.id(), field)) {
1488  Msg::print(Msg::SevWarning, "Error writing metadata.");
1489  return false;
1490  }
1491 
1492  if (!writeField(layerGroup.id(), field)) {
1493  Msg::print(Msg::SevWarning, "Error writing layer: " + layer.name);
1494  return false;
1495  }
1496 
1497  // Add layer to partition ---
1498 
1499  if (isVectorLayer)
1500  part->addVectorLayer(layer);
1501  else
1502  part->addScalarLayer(layer);
1503 
1504  return true;
1505 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
boost::intrusive_ptr< Partition > Ptr
Definition: Field3DFile.h:149
Namespace for Exception objects.
Definition: Exception.h:57
bool writeMetadata(hid_t metadataGroup, FieldBase::Ptr layer)
Writes metadata for this layer.
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
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:150
std::string name
The name of the layer (always available)
Definition: Field3DFile.h:121
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
bool writeField(hid_t layerGroup, FieldBase::Ptr field)
This function creates a FieldIO instance based on field->className() which then writes the field data...
hid_t m_file
The hdf5 id of the current file. Will be -1 if no file is open.
Definition: Field3DFile.h:384
Scoped object - opens a group on creation and closes it on destruction.
Definition: Hdf5Util.h:176
std::string incrementPartitionName(std::string &pname)
increment the partition or make it zero if there's not an integer suffix
bool Field3DOutputFile::writeMetadata ( hid_t  metadataGroup,
FieldBase::Ptr  layer 
)
private

Writes metadata for this layer.

Definition at line 1292 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().

1293 {
1294  using namespace Hdf5Util;
1295 
1296  {
1298  field->metadata().strMetadata().begin();
1300  field->metadata().strMetadata().end();
1301  for (; i != end; ++i) {
1302  if (!writeAttribute(metadataGroup, i->first, i->second))
1303  {
1304  Msg::print(Msg::SevWarning, "Writing attribute " + i->first );
1305  return false;
1306  }
1307  }
1308  }
1309 
1310  {
1312  field->metadata().intMetadata().begin();
1314  field->metadata().intMetadata().end();
1315  for (; i != end; ++i) {
1316  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1317  {
1318  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1319  return false;
1320  }
1321  }
1322  }
1323 
1324  {
1326  field->metadata().floatMetadata().begin();
1328  field->metadata().floatMetadata().end();
1329  for (; i != end; ++i) {
1330  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1331  {
1332  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1333  return false;
1334  }
1335  }
1336  }
1337 
1338  {
1340  field->metadata().vecIntMetadata().begin();
1342  field->metadata().vecIntMetadata().end();
1343  for (; i != end; ++i) {
1344  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1345  {
1346  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1347  return false;
1348  }
1349  }
1350  }
1351 
1352  {
1354  field->metadata().vecFloatMetadata().begin();
1356  field->metadata().vecFloatMetadata().end();
1357  for (; i != end; ++i) {
1358  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1359  {
1360  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1361  return false;
1362  }
1363  }
1364 
1365  }
1366 
1367  return true;
1368 
1369 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
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...
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
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...
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
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 1373 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().

1374 {
1375  using namespace Hdf5Util;
1376 
1377  {
1379  metadata().strMetadata().begin();
1381  metadata().strMetadata().end();
1382  for (; i != end; ++i) {
1383  if (!writeAttribute(metadataGroup, i->first, i->second))
1384  {
1385  Msg::print(Msg::SevWarning, "Writing attribute " + i->first );
1386  return false;
1387  }
1388  }
1389  }
1390 
1391  {
1393  metadata().intMetadata().begin();
1395  metadata().intMetadata().end();
1396  for (; i != end; ++i) {
1397  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1398  {
1399  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1400  return false;
1401  }
1402  }
1403  }
1404 
1405  {
1407  metadata().floatMetadata().begin();
1409  metadata().floatMetadata().end();
1410  for (; i != end; ++i) {
1411  if (!writeAttribute(metadataGroup, i->first, 1, i->second))
1412  {
1413  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1414  return false;
1415  }
1416  }
1417  }
1418 
1419  {
1421  metadata().vecIntMetadata().begin();
1423  metadata().vecIntMetadata().end();
1424  for (; i != end; ++i) {
1425  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1426  {
1427  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1428  return false;
1429  }
1430  }
1431  }
1432 
1433  {
1435  metadata().vecFloatMetadata().begin();
1437  metadata().vecFloatMetadata().end();
1438  for (; i != end; ++i) {
1439  if (!writeAttribute(metadataGroup, i->first, 3, i->second.x))
1440  {
1441  Msg::print(Msg::SevWarning, "Writing attribute " + i->first);
1442  return false;
1443  }
1444  }
1445 
1446  }
1447 
1448  return true;
1449 
1450 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:76
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...
void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:62
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...
bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
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:315
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: