2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #include "H5Dataset.hxx"
14 #include "H5DataFactory.hxx"
15 #include "H5Attribute.hxx"
16 #include "H5HardLink.hxx"
18 namespace org_modules_hdf5
21 void H5Dataset::init()
23 dataset = H5Dopen2(getParent().getH5Id(), name.c_str(), H5P_DEFAULT);
26 throw H5Exception(__LINE__, __FILE__, _("Cannot open the given dataset %s."), name.c_str());
30 H5Dataset::H5Dataset(H5Object & _parent, const std::string & _name) : H5Object(_parent, _name), dataset((hid_t) - 1)
35 H5Dataset::H5Dataset(H5Object & _parent, hid_t _dataset, const std::string & _name) : H5Object(_parent, _name), dataset(_dataset)
40 H5Dataset::~H5Dataset()
48 H5Data & H5Dataset::getData()
50 return H5DataFactory::getData(*this, dataset, 0, 0, false);
53 H5Data & H5Dataset::getData(H5Dataspace & space, hsize_t * dims)
55 return H5DataFactory::getData(*this, dataset, &space, dims, false);
58 H5Dataspace & H5Dataset::getSpace()
60 hid_t space = H5Dget_space(dataset);
63 throw H5Exception(__LINE__, __FILE__, _("Cannot get the dataspace associated with dataset named %s."), name.c_str());
66 return *new H5Dataspace(*this, space);
69 H5Type & H5Dataset::getDataType()
71 hid_t type = H5Dget_type(dataset);
74 throw H5Exception(__LINE__, __FILE__, _("Cannot get the dataspace associated with dataset named %s."), name.c_str());
77 return *new H5Type(*this, type);
80 H5Dataset::H5Layout & H5Dataset::getLayout()
82 hid_t plist = H5Dget_create_plist(dataset);
83 H5D_layout_t layout = H5Pget_layout(plist);
84 H5Dataset::H5Layout * obj = 0;
89 obj = new H5Dataset::H5CompactLayout(*this);
92 obj = new H5Dataset::H5ContiguousLayout(*this);
95 obj = new H5Dataset::H5ChunkedLayout(*this);
99 throw H5Exception(__LINE__, __FILE__, _("Invalid layout"));
107 void H5Dataset::getAccessibleAttribute(const std::string & _name, const int pos, void * pvApiCtx) const
110 std::string lower(_name);
114 H5Attribute * attr = new H5Attribute(*const_cast<H5Dataset *>(this), _name);
115 attr->createOnScilabStack(pos, pvApiCtx);
119 catch (const H5Exception & e) { }
121 std::transform(_name.begin(), _name.end(), lower.begin(), tolower);
123 if (lower == "attributes")
125 const H5AttributesList & attrs = const_cast<H5Dataset *>(this)->getAttributes();
126 attrs.createOnScilabStack(pos, pvApiCtx);
130 else if (lower == "type")
132 const H5Type & type = const_cast<H5Dataset *>(this)->getDataType();
133 type.createOnScilabStack(pos, pvApiCtx);
137 else if (lower == "dataspace")
139 const H5Dataspace & space = const_cast<H5Dataset *>(this)->getSpace();
140 space.createOnScilabStack(pos, pvApiCtx);
144 else if (lower == "data")
146 const H5Data & data = const_cast<H5Dataset *>(this)->getData();
147 data.toScilab(pvApiCtx, pos);
149 if (!data.isReference())
156 else if (lower == "layout")
158 const H5Dataset::H5Layout & layout = const_cast<H5Dataset *>(this)->getLayout();
159 layout.createOnScilabStack(pos, pvApiCtx);
164 H5Object::getAccessibleAttribute(_name, pos, pvApiCtx);
167 std::string H5Dataset::dump(std::map<haddr_t, std::string> & alreadyVisited, const unsigned int indentLevel) const
169 std::ostringstream os;
170 haddr_t addr = this->getAddr();
171 std::map<haddr_t, std::string>::iterator it = alreadyVisited.find(addr);
172 if (it != alreadyVisited.end())
174 os << H5Object::getIndentString(indentLevel) << "DATASET \"" << getName() << "\" {" << std::endl
175 << H5Object::getIndentString(indentLevel + 1) << "HARDLINK \"" << it->second << "\"" << std::endl
176 << H5Object::getIndentString(indentLevel) << "}" << std::endl;
182 alreadyVisited.insert(std::pair<haddr_t, std::string>(addr, getCompletePath()));
185 const H5Type & type = const_cast<H5Dataset *>(this)->getDataType();
186 const H5Dataspace & space = const_cast<H5Dataset *>(this)->getSpace();
187 const H5AttributesList & attrs = const_cast<H5Dataset *>(this)->getAttributes();
188 const H5Data & data = const_cast<H5Dataset *>(this)->getData();
189 const H5Dataset::H5Layout & layout = const_cast<H5Dataset *>(this)->getLayout();
191 os << H5Object::getIndentString(indentLevel) << "DATASET \"" << getName() << "\" {" << std::endl
192 << type.dump(alreadyVisited, indentLevel + 1)
193 << space.dump(alreadyVisited, indentLevel + 1)
194 << layout.dump(alreadyVisited, indentLevel + 1)
195 << data.dump(alreadyVisited, indentLevel + 1)
196 << attrs.dump(alreadyVisited, indentLevel + 1)
197 << H5Object::getIndentString(indentLevel) << "}" << std::endl;
208 void H5Dataset::printLsInfo(std::ostringstream & os) const
210 const H5Dataspace & space = const_cast<H5Dataset *>(this)->getSpace();
211 std::vector<unsigned int> dims = space.getDims();
212 std::string str(getName());
213 H5Object::getResizedString(str);
215 os << str << "Dataset {";
217 if (dims.size() == 0)
223 for (unsigned int i = 0; i < dims.size() - 1; i++)
225 os << dims[i] << ", ";
227 os << dims[dims.size() - 1] << "}";
235 std::string H5Dataset::ls() const
237 std::ostringstream os;
243 void H5Dataset::ls(std::vector<std::string> & name, std::vector<std::string> & type) const
247 opdata.parent = const_cast<H5Dataset *>(this);
252 err = H5Aiterate2(dataset, H5_INDEX_NAME, H5_ITER_INC, &idx, H5Object::getLsAttributes, &opdata);
255 throw H5Exception(__LINE__, __FILE__, _("Cannot list dataset attributes."));
259 std::string H5Dataset::toString(const unsigned int indentLevel) const
261 std::ostringstream os;
262 std::string indentString = H5Object::getIndentString(indentLevel + 1);
263 const H5Type & type = const_cast<H5Dataset *>(this)->getDataType();
264 const H5AttributesList & attrs = const_cast<H5Dataset *>(this)->getAttributes();
266 os << H5Object::getIndentString(indentLevel) << "HDF5 Dataset" << std::endl
267 << indentString << _("Filename") << ": " << getParent().getFile().getFileName() << std::endl
268 << indentString << _("Name") << ": " << getName() << std::endl
269 << indentString << _("Path") << ": " << getCompletePath() << std::endl
270 << indentString << _("Elements type") << ": " << type.getTypeName() << std::endl
271 << indentString << _("Attributes") << ": [1 x " << attrs.getSize() << "]";
279 hid_t H5Dataset::create(H5Object & loc, const std::string & name, const hid_t type, const hid_t targettype, const hid_t space, void * data)
282 hid_t dataset = H5Dcreate2(loc.getH5Id(), name.c_str(), targettype, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
285 throw H5Exception(__LINE__, __FILE__, _("Cannot create a new dataset."));
288 err = H5Dwrite(dataset, type, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
291 throw H5Exception(__LINE__, __FILE__, _("Cannot write data in the dataset."));