53 #include <boost/tokenizer.hpp>
54 #include <boost/utility.hpp>
61 #include "OgIAttribute.h"
62 #include "OgIDataset.h"
64 #include "OgOAttribute.h"
65 #include "OgODataset.h"
90 const std::string k_mappingStr(
"mapping");
91 const std::string k_partitionName(
"partition");
92 const std::string k_versionAttrName(
"version_number");
93 const std::string k_classNameAttrName(
"class_name");
94 const std::string k_mappingTypeAttrName(
"mapping_type");
102 int k_minFileVersion[2] = { 0, 0 };
106 std::vector<std::string> makeUnique(std::vector<std::string> vec)
108 std::vector<string> ret;
109 std::sort(vec.begin(), vec.end());
110 std::vector<std::string>::iterator newEnd =
111 std::unique(vec.begin(), vec.end());
112 ret.resize(std::distance(vec.begin(), newEnd));
113 std::copy(vec.begin(), newEnd, ret.begin());
121 class print : std::unary_function<T, void>
127 void operator()(
const T& x)
const
129 for (
int i = 0; i < indent; i++)
131 std::cout << x << std::endl;
143 void checkFile(
const std::string &filename)
147 throw NoSuchFileException(filename);
153 bool isSupportedFileVersion(
const int fileVersion[3],
154 const int minVersion[2])
156 stringstream currentVersionStr;
157 currentVersionStr << k_currentFileVersion[0] <<
"."
158 << k_currentFileVersion[1] <<
"."
159 << k_currentFileVersion[2];
160 stringstream fileVersionStr;
161 fileVersionStr << fileVersion[0] <<
"."
162 << fileVersion[1] <<
"."
164 stringstream minVersionStr;
165 minVersionStr << minVersion[0] <<
"."
168 if (fileVersion[0] > k_currentFileVersion[0] ||
169 (fileVersion[0] == k_currentFileVersion[0] &&
170 fileVersion[1] > k_currentFileVersion[1])) {
172 " is higher than the current version " +
173 currentVersionStr.str());
177 if (fileVersion[0] < minVersion[0] ||
178 (fileVersion[0] == minVersion[0] &&
179 fileVersion[1] < minVersion[1])) {
181 " is lower than the minimum supported version " +
182 minVersionStr.str());
195 mappingGroup.findAttribute<
string>(k_mappingTypeAttrName);
196 if (!mappingAttr.isValid()) {
201 const std::string className = mappingAttr.value();
238 return io->write(layerGroup, field);
247 template <
class Data_T>
249 readField(
const std::string &className,
const OgIGroup &layerGroup,
250 const std::string &filename,
const std::string &layerPath)
263 OgDataType typeEnum = OgawaTypeTraits<Data_T>::typeEnum();
264 FieldBase::Ptr field = io->read(layerGroup, filename, layerPath, typeEnum);
283 bool readMeta(
const OgIGroup &group,
FieldMetadata &metadata)
286 std::vector<std::string> attrs = group.attributeNames();
288 for (
size_t i = 0, end = attrs.size(); i < end; ++i) {
292 if (attr.isValid()) {
299 if (attr.isValid()) {
306 if (attr.isValid()) {
313 if (attr.isValid()) {
320 if (attr.isValid()) {
343 std::string Partition::className()
const
345 return k_partitionName;
351 Partition::addLayer(
const Layer &layer)
353 m_layers.push_back(layer);
359 Partition::layer(
const std::string &name)
const
361 for (LayerList::const_iterator i = m_layers.begin();
362 i != m_layers.end(); ++i) {
363 if (i->name == name) {
373 Partition::getLayerNames(std::vector<std::string> &names)
const
377 for (LayerList::const_iterator i = m_layers.begin();
378 i != m_layers.end(); ++i) {
379 names.push_back(i->name);
385 OgOGroup& Partition::group()
const
392 void Partition::setGroup(boost::shared_ptr<OgOGroup> ptr)
423 const std::string & ,
427 for (PartitionList::const_iterator i =
m_partitions.begin();
430 if ((**i).mapping->isIdentical(field->mapping())) {
455 if ((**i).name == partitionName)
467 for (PartitionList::const_iterator i =
m_partitions.begin();
469 if ((**i).name == partitionName)
481 size_t pos = partitionName.rfind(
".");
482 if (pos == partitionName.npos) {
483 return partitionName;
485 return partitionName.substr(0, pos);
501 vector<string> tempNames;
503 for (PartitionList::const_iterator i =
m_partitions.begin();
508 names = makeUnique(tempNames);
515 const string &partitionName)
const
518 m_hdf5Base->getScalarLayerNames(names, partitionName);
530 part->getLayerNames(names);
533 names = makeUnique(names);
540 const string &partitionName)
const
543 m_hdf5Base->getVectorLayerNames(names, partitionName);
555 part->getLayerNames(names);
558 names = makeUnique(names);
568 for (PartitionList::const_iterator i =
m_partitions.begin();
570 names.push_back((**i).name);
578 const string &intPartitionName)
const
591 part->getLayerNames(names);
598 const string &intPartitionName)
const
611 part->getLayerNames(names);
648 for (PartitionList::const_iterator i =
m_partitions.begin();
650 string name = (**i).name;
651 size_t pos = name.rfind(
".");
652 if (pos != name.npos) {
653 if (name.substr(0, pos) == partitionName) {
668 return partitionName +
"." + boost::lexical_cast<std::string>(i);
681 GroupMembershipMap::const_iterator i = groupMembers.begin();
682 GroupMembershipMap::const_iterator end = groupMembers.end();
684 for (; i != end; ++i) {
685 GroupMembershipMap::iterator foundGroupIter =
729 m_archive.reset(
new Alembic::Ogawa::IArchive(filename));
735 if (
m_hdf5->open(filename)) {
739 throw NoSuchFileException(filename);
750 if (!version.isValid()) {
751 throw OgIAttributeException(
"Missing version attribute.");
753 int fileVersion[3] = { version.value()[0],
755 version.value()[2] };
756 if (!isSupportedFileVersion(fileVersion, k_minFileVersion)) {
757 stringstream versionStr;
758 versionStr << fileVersion[0] <<
"."
759 << fileVersion[1] <<
"."
761 throw UnsupportedVersionException(versionStr.str());
764 catch (OgIAttributeException &e) {
771 const OgIGroup metadataGroup =
m_root->findGroup(
"field3d_global_metadata");
772 if (metadataGroup.isValid()) {
778 "Unknown error when reading file metadata ");
787 catch (MissingGroupException &e) {
789 throw BadFileHierarchyException(filename);
791 catch (ReadMappingException &e) {
794 throw BadFileHierarchyException(filename);
799 throw BadFileHierarchyException(filename);
803 "Unknown error when reading file hierarchy. ");
804 throw BadFileHierarchyException(filename);
807 catch (NoSuchFileException &e) {
809 +
string(e.what()) );
812 catch (MissingAttributeException &e) {
814 "In file: " + filename +
" - "
815 +
string(e.what()) );
818 catch (UnsupportedVersionException &e) {
820 "In file: " + filename +
" - File version can not be read: "
824 catch (BadFileHierarchyException &) {
826 "In file: " + filename +
" - Bad file hierarchy. ");
831 "In file: " + filename +
" Unknown exception ");
847 std::vector<std::string> groups =
m_root->groupNames();
851 for (std::vector<std::string>::const_iterator i = groups.begin(),
852 end = groups.end(); i != end; ++i) {
854 const std::string &name = *i;
856 if (name ==
"field3d_global_metadata") {
870 const std::string &name = (**i).name;
872 const OgIGroup partitionGroup =
m_root->findGroup(name);
873 if (!partitionGroup.isValid()) {
877 const OgIGroup mappingGroup = partitionGroup.findGroup(k_mappingStr);
878 if (!mappingGroup.isValid()) {
886 throw ReadMappingException((**i).name);
890 (**i).mapping = mapping;
895 for (PartitionList::const_iterator i =
m_partitions.begin();
898 const std::string &partitionName = (**i).name;
900 const OgIGroup partitionGroup =
m_root->findGroup(partitionName);
901 if (!partitionGroup.isValid()) {
906 groups = partitionGroup.groupNames();
907 for (std::vector<std::string>::const_iterator l = groups.begin(),
908 lEnd = groups.end(); l != lEnd; ++l) {
910 const std::string layerName = *l;
912 if (layerName == k_mappingStr) {
918 layer.
parent = partitionName;
920 partition(partitionName)->addLayer(layer);
932 return readMeta(metadataGroup, field->metadata());
939 return readMeta(metadataGroup,
metadata());
980 m_archive.reset(
new Alembic::Ogawa::OArchive(filename));
993 k_currentFileVersion);
1004 const std::string className = mapping->className();
1008 OgOGroup mappingGroup(partitionGroup, k_mappingStr);
1020 return io->write(mappingGroup, mapping);
1023 catch (OgOGroupException &e) {
1025 throw WriteMappingException(k_mappingStr);
1036 FieldMetadata::StrMetadata::const_iterator i =
1037 field->metadata().strMetadata().begin();
1038 FieldMetadata::StrMetadata::const_iterator end =
1039 field->metadata().strMetadata().end();
1040 for (; i != end; ++i) {
1044 catch (OgOAttributeException &e) {
1053 FieldMetadata::IntMetadata::const_iterator i =
1054 field->metadata().intMetadata().begin();
1055 FieldMetadata::IntMetadata::const_iterator end =
1056 field->metadata().intMetadata().end();
1057 for (; i != end; ++i) {
1061 catch (OgOAttributeException &e) {
1070 FieldMetadata::FloatMetadata::const_iterator i =
1071 field->metadata().floatMetadata().begin();
1072 FieldMetadata::FloatMetadata::const_iterator end =
1073 field->metadata().floatMetadata().end();
1074 for (; i != end; ++i) {
1078 catch (OgOAttributeException &e) {
1087 FieldMetadata::VecIntMetadata::const_iterator i =
1088 field->metadata().vecIntMetadata().begin();
1089 FieldMetadata::VecIntMetadata::const_iterator end =
1090 field->metadata().vecIntMetadata().end();
1091 for (; i != end; ++i) {
1095 catch (OgOAttributeException &e) {
1104 FieldMetadata::VecFloatMetadata::const_iterator i =
1105 field->metadata().vecFloatMetadata().begin();
1106 FieldMetadata::VecFloatMetadata::const_iterator end =
1107 field->metadata().vecFloatMetadata().end();
1108 for (; i != end; ++i) {
1112 catch (OgOAttributeException &e) {
1130 FieldMetadata::StrMetadata::const_iterator i =
1132 FieldMetadata::StrMetadata::const_iterator end =
1134 for (; i != end; ++i) {
1138 catch (OgOAttributeException &e) {
1147 FieldMetadata::IntMetadata::const_iterator i =
1149 FieldMetadata::IntMetadata::const_iterator end =
1151 for (; i != end; ++i) {
1155 catch (OgOAttributeException &e) {
1164 FieldMetadata::FloatMetadata::const_iterator i =
1166 FieldMetadata::FloatMetadata::const_iterator end =
1168 for (; i != end; ++i) {
1172 catch (OgOAttributeException &e) {
1181 FieldMetadata::VecIntMetadata::const_iterator i =
1183 FieldMetadata::VecIntMetadata::const_iterator end =
1185 for (; i != end; ++i) {
1189 catch (OgOAttributeException &e) {
1198 FieldMetadata::VecFloatMetadata::const_iterator i =
1200 FieldMetadata::VecFloatMetadata::const_iterator end =
1202 for (; i != end; ++i) {
1206 catch (OgOAttributeException &e) {
1224 return m_hdf5->writeGlobalMetadata();
1227 OgOGroup ogMetadata(*
m_root,
"field3d_global_metadata");
1242 return m_hdf5->writeGroupMembership();
1249 using namespace std;
1258 "Error creating field3d_group_membership group.");
1262 if (!
writeAttribute(group,
"is_field3d_group_membership",
"1")) {
1264 "Failed to write field3d_group_membership attribute.");
1268 std::map<std::string, std::string>::const_iterator iter =
1270 std::map<std::string, std::string>::const_iterator iEnd =
1273 for (; iter != iEnd; ++iter) {
1276 "Failed to write groupMembership string: "+ iter->first);
1310 for (PartitionList::const_iterator i =
m_partitions.begin();
1312 cout <<
"Name: " << (**i).name << endl;
1314 cout <<
" Mapping: " << (**i).mapping->className() << endl;
1316 cout <<
" Mapping: NULL" << endl;
1317 cout <<
" Layers: " << endl;
1318 vector<string> names;
1319 (**i).getLayerNames(names);
1320 for_each(names.begin(), names.end(), print<string>(4));
1331 struct __stat64 statbuf;
1332 return (_stat64(filename.c_str(), &statbuf) != -1);
1334 struct stat statbuf;
1335 return (stat(filename.c_str(), &statbuf) != -1);
1343 const std::string & ,
1346 using namespace Exc;
1349 newPart->name = partitionName;
1351 boost::shared_ptr<OgOGroup> ogPartition(
new OgOGroup(*
m_root, newPart->name));
1352 newPart->setGroup(ogPartition);
1363 "writeMapping returned false for an unknown reason ");
1367 catch (WriteMappingException &e) {
1374 "Unknown error when writing mapping for partition: "
1382 part->mapping = field->mapping();
1395 template <
class Data_T>
1397 const std::string &layerName,
1405 "Called writeLayer with null pointer. Ignoring...");
1412 "Attempting to write layer without opening file first.");
1417 string partitionName =
intPartitionName(userPartitionName, layerName, field);
1433 "Couldn't add layer \"" + layerName +
"\" to partition \""
1434 + partitionName +
"\" because the layer's mapping is null.");
1439 if (part->layer(layerName)) {
1452 if (!part->mapping) {
1459 if (!field->
mapping()->isIdentical(part->mapping)) {
1461 +
"\" to partition \"" + partitionName
1462 +
"\" because mapping doesn't match");
1468 OgOGroup &ogPartition = part->group();
1473 layer.
name = layerName;
1474 layer.
parent = partitionName;
1478 OgOGroup ogLayer(ogPartition, layerName);
1484 OgOGroup ogMetadata(ogLayer,
"metadata");
1494 part->addLayer(layer);
1501 template <
class Data_T>
1504 const std::string &layerName)
const
1519 const File::Layer *layer = part->layer(layerName);
1527 if (!partitionGroup.isValid()) {
1534 const OgIGroup layerGroup = partitionGroup.findGroup(layerName);
1535 if (!layerGroup.isValid()) {
1542 string layerPath = layer->
parent +
"/" + layer->
name;
1545 className = layerGroup.findAttribute<
string>(
"class_name").value();
1547 catch (OgIAttributeException &e) {
1556 FieldPtr cachedField = cache.
getCachedField(m_filename, layerPath);
1565 field = readField<Data_T>(className, layerGroup, m_filename, layerPath);
1573 const OgIGroup metadataGroup = layerGroup.findGroup(
"metadata");
1574 if (metadataGroup.isValid()) {
1575 readMetadata(metadataGroup, field);
1585 cache.
cacheField(field, m_filename, layerPath);
1593 template <
class Data_T>
1604 std::vector<std::string> parts;
1607 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1608 vector<std::string> layers;
1610 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1612 if ((name.length() == 0) || (*l == name)) {
1613 FieldPtr mf = readLayer<Data_T>(*p, *l);
1626 template <
class Data_T>
1629 const std::string &layerName)
const
1631 using namespace std;
1638 if ((layerName.length() == 0) || (partitionName.length() == 0))
1641 std::vector<std::string> parts;
1644 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1645 std::vector<std::string> layers;
1648 for (vector<string>::iterator l = layers.begin();
1649 l != layers.end(); ++l) {
1651 if (*l == layerName) {
1652 FieldPtr mf = readLayer<Data_T>(*p, *l);
1665 template <
class Data_T>
1668 const std::string &name,
1669 const std::string &attribute,
1672 using namespace boost;
1673 using namespace std;
1677 const std::string extentsMinStr(
"extents_min");
1678 const std::string extentsMaxStr(
"extents_max");
1679 const std::string dataWindowMinStr(
"data_window_min");
1680 const std::string dataWindowMaxStr(
"data_window_max");
1682 Box3i extents, dataW;
1687 location.findAttribute<
veci32_t>(extentsMinStr);
1689 location.findAttribute<
veci32_t>(extentsMaxStr);
1690 if (!extMinAttr.isValid()) {
1691 throw MissingAttributeException(
"Couldn't find attribute " +
1694 if (!extMaxAttr.isValid()) {
1695 throw MissingAttributeException(
"Couldn't find attribute " +
1699 extents.min = extMinAttr.value();
1700 extents.max = extMaxAttr.value();
1705 location.findAttribute<
veci32_t>(dataWindowMinStr);
1707 location.findAttribute<
veci32_t>(dataWindowMaxStr);
1708 if (!dwMinAttr.isValid()) {
1709 throw MissingAttributeException(
"Couldn't find attribute " +
1712 if (!dwMaxAttr.isValid()) {
1713 throw MissingAttributeException(
"Couldn't find attribute " +
1717 dataW.min = dwMinAttr.value();
1718 dataW.max = dwMaxAttr.value();
1722 field->
setSize(extents, dataW);
1725 OgIGroup metadataGroup = location.findGroup(
"metadata");
1726 if (metadataGroup.isValid()) {
1727 readMetadata(metadataGroup, field);
1740 template <
class Data_T>
1743 const std::string &layerName,
1744 bool isVectorLayer)
const
1746 using namespace boost;
1747 using namespace std;
1751 return m_hdf5->readProxyLayer<Data_T>(partitionName, layerName,
1758 if ((layerName.length() == 0) || (partitionName.length() == 0))
1761 std::vector<std::string> parts, layers;
1764 bool foundPartition =
false;
1766 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1768 foundPartition =
true;
1769 if (isVectorLayer) {
1774 for (vector<string>::iterator l = layers.begin();
1775 l != layers.end(); ++l) {
1776 if (*l == layerName) {
1785 if (isVectorLayer) {
1786 layer = part->layer(layerName);
1788 layer = part->layer(layerName);
1795 string layerPath = layer->
parent +
"/" + layer->
name;
1797 if (!parent.isValid()) {
1799 + layerPath +
" in .f3d file ");
1802 OgIGroup layerGroup = parent.findGroup(layer->
name);
1803 if (!layerGroup.isValid()) {
1805 + layerPath +
" in .f3d file ");
1811 readProxyLayer<Data_T>(layerGroup, partitionName, layerName,
1816 OgIGroup mipGroup = layerGroup.findGroup(
"mip_levels");
1817 if (mipGroup.isValid()) {
1819 mipGroup.findAttribute<uint32_t>(
"levels");
1820 if (levelsAttr.isValid()) {
1821 numLevels = levelsAttr.value();
1827 output.push_back(field);
1833 if (!foundPartition) {
1843 template <
class Data_T>
1847 using namespace std;
1850 typedef std::vector<FieldPtr> FieldList;
1854 std::vector<std::string> parts;
1857 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1858 std::vector<std::string> layers;
1860 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1862 if ((name.length() == 0) || (*l == name)) {
1863 FieldList f = readProxyLayer<Data_T>(*p, *l,
false);
1864 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1878 template <
class Data_T>
1882 using namespace std;
1885 typedef std::vector<FieldPtr> FieldList;
1889 std::vector<std::string> parts;
1892 for (vector<string>::iterator p = parts.begin(); p != parts.end(); ++p) {
1893 std::vector<std::string> layers;
1895 for (vector<string>::iterator l = layers.begin(); l != layers.end(); ++l) {
1897 if ((name.length() == 0) || (*l == name)) {
1898 FieldList f = readProxyLayer<Data_T>(*p, *l,
true);
1899 for (
typename FieldList::iterator i = f.begin(); i != f.end(); ++i) {
1915 #define FIELD3D_INSTANTIATION_WRITELAYER(type) \
1917 bool Field3DOutputFile::writeLayer<type> \
1918 (const std::string &, const std::string &, Field<type>::Ptr ); \
1931 #define FIELD3D_INSTANTIATION_READLAYER(type) \
1934 Field3DInputFile::readLayer<type> \
1935 (const std::string &, const std::string &) const; \
1937 FIELD3D_INSTANTIATION_READLAYER(
float16_t);
1938 FIELD3D_INSTANTIATION_READLAYER(
float32_t);
1939 FIELD3D_INSTANTIATION_READLAYER(
float64_t);
1940 FIELD3D_INSTANTIATION_READLAYER(
vec16_t);
1941 FIELD3D_INSTANTIATION_READLAYER(
vec32_t);
1942 FIELD3D_INSTANTIATION_READLAYER(
vec64_t);
1948 #define FIELD3D_INSTANTIATION_READLAYERS1(type) \
1951 Field3DInputFile::readLayers<type>(const std::string &name) const; \
1962 #define FIELD3D_INSTANTIATION_READLAYERS2(type) \
1965 Field3DInputFile::readLayers<type>(const std::string &partitionName, \
1966 const std::string &layerName) const; \
1977 #define FIELD3D_INSTANTIATION_READPROXYLAYER(type) \
1979 EmptyField<type>::Vec \
1980 Field3DInputFile::readProxyLayer<type>(const std::string &partitionName, \
1981 const std::string &layerName, \
1982 bool isVectorLayer) const \
1993 #define FIELD3D_INSTANTIATION_READPROXYSCALARLAYER(type) \
1995 EmptyField<type>::Vec \
1996 Field3DInputFile::readProxyScalarLayers<type> \
1997 (const std::string &name) const \
2008 #define FIELD3D_INSTANTIATION_READPROXYVECTORLAYER(type) \
2010 EmptyField<type>::Vec \
2011 Field3DInputFile::readProxyVectorLayers<type> \
2012 (const std::string &name) const \