Changeset 1711 for trunk


Ignore:
Timestamp:
24/10/11 16:43:58 (7 months ago)
Author:
Janik Zikovsky
Message:

Refs #294: Added some methods for writing extendible data sets to the C++ API.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/cpp/NeXusFile.cpp

    r1654 r1711  
    370370 
    371371 
     372 
     373template <typename NumT> 
     374void File::writeExtendibleData(const string& name, vector<NumT>& value) 
     375{ 
     376  // Use a default chunk size of 4096 bytes. TODO: Is this optimal? 
     377  writeExtendibleData(name, value, 4096); 
     378} 
     379 
     380template <typename NumT> 
     381void File::writeExtendibleData(const string& name, vector<NumT>& value, const int64_t chunk) 
     382{ 
     383  vector<int64_t> dims(1, NX_UNLIMITED); 
     384  vector<int64_t> chunk_dims(1, chunk); 
     385  // Use chunking without using compression 
     386  this->makeCompData(name, getType<NumT>(), dims, NONE, chunk_dims, true ); 
     387  this->putSlab(value, int64_t(0), int64_t(value.size())); 
     388  this->closeData(); 
     389} 
     390 
     391template <typename NumT> 
     392void File::writeExtendibleData(const string& name, vector<NumT>& value, 
     393    vector<int64_t>& dims, std::vector<int64_t> & chunk) 
     394{ 
     395  // Create the data with unlimited 0th dimensions 
     396  std::vector<int64_t> unlim_dims(dims); 
     397  unlim_dims[0] = NX_UNLIMITED; 
     398  // Use chunking without using compression 
     399  this->makeCompData(name, getType<NumT>(), unlim_dims, NONE, chunk, true ); 
     400  // And put that slab of that of that given size in there 
     401  std::vector<int64_t> start( dims.size(), 0 ); 
     402  this->putSlab(value, start, dims); 
     403  this->closeData(); 
     404 
     405} 
     406 
     407 
     408template <typename NumT> 
     409void File::writeUpdatedData(const std::string& name, std::vector<NumT>& value) 
     410{ 
     411  this->openData(name); 
     412  this->putSlab(value, int64_t(0), int64_t(value.size())); 
     413  this->closeData(); 
     414} 
     415 
     416template <typename NumT> 
     417void File::writeUpdatedData(const std::string& name, std::vector<NumT>& value, 
     418                            std::vector<int64_t>& dims) 
     419{ 
     420  this->openData(name); 
     421  std::vector<int64_t> start( dims.size(), 0 ); 
     422  this->putSlab(value, start, dims); 
     423  this->closeData(); 
     424} 
     425 
     426 
    372427void File::makeCompData(const string& name, const NXnumtype type, 
    373428                        const vector<int>& dims, const NXcompression comp, 
     
    807862  this->closeData(); 
    808863} 
     864 
     865template <typename NumT> 
     866void File::readData(const std::string & dataName, NumT & data) 
     867{ 
     868  std::vector<NumT> dataVector; 
     869  this->openData(dataName); 
     870  this->getData(dataVector); 
     871  if (dataVector.size() > 0) 
     872    data = dataVector[0]; 
     873  this->closeData(); 
     874} 
     875 
    809876void File::readData(const std::string & dataName, std::string& data) 
    810877{ 
     
    13471414 
    13481415template 
     1416NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<float>& value); 
     1417template 
     1418NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<double>& value); 
     1419template 
     1420NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int8_t>& value); 
     1421template 
     1422NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint8_t>& value); 
     1423template 
     1424NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int16_t>& value); 
     1425template 
     1426NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint16_t>& value); 
     1427template 
     1428NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int32_t>& value); 
     1429template 
     1430NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint32_t>& value); 
     1431template 
     1432NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int64_t>& value); 
     1433template 
     1434NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint64_t>& value); 
     1435template 
     1436NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<char>& value); 
     1437 
     1438template 
     1439NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<float>& value, const int64_t chunk); 
     1440template 
     1441NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<double>& value, const int64_t chunk); 
     1442template 
     1443NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int8_t>& value, const int64_t chunk); 
     1444template 
     1445NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint8_t>& value, const int64_t chunk); 
     1446template 
     1447NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int16_t>& value, const int64_t chunk); 
     1448template 
     1449NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint16_t>& value, const int64_t chunk); 
     1450template 
     1451NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int32_t>& value, const int64_t chunk); 
     1452template 
     1453NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint32_t>& value, const int64_t chunk); 
     1454template 
     1455NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int64_t>& value, const int64_t chunk); 
     1456template 
     1457NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint64_t>& value, const int64_t chunk); 
     1458template 
     1459NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<char>& value, const int64_t chunk); 
     1460 
     1461template 
     1462NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<float>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1463template 
     1464NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<double>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1465template 
     1466NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int8_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1467template 
     1468NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint8_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1469template 
     1470NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int16_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1471template 
     1472NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint16_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1473template 
     1474NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int32_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1475template 
     1476NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint32_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1477template 
     1478NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<int64_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1479template 
     1480NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<uint64_t>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1481template 
     1482NXDLL_EXPORT void File::writeExtendibleData(const string& name, std::vector<char>& value, std::vector<int64_t> & dims, std::vector<int64_t> & chunk); 
     1483 
     1484template 
     1485NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<float>& value); 
     1486template 
     1487NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<double>& value); 
     1488template 
     1489NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int8_t>& value); 
     1490template 
     1491NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint8_t>& value); 
     1492template 
     1493NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int16_t>& value); 
     1494template 
     1495NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint16_t>& value); 
     1496template 
     1497NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int32_t>& value); 
     1498template 
     1499NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint32_t>& value); 
     1500template 
     1501NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int64_t>& value); 
     1502template 
     1503NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint64_t>& value); 
     1504template 
     1505NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<char>& value); 
     1506 
     1507template 
     1508NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<float>& value, std::vector<int64_t> & dims); 
     1509template 
     1510NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<double>& value, std::vector<int64_t> & dims); 
     1511template 
     1512NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int8_t>& value, std::vector<int64_t> & dims); 
     1513template 
     1514NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint8_t>& value, std::vector<int64_t> & dims); 
     1515template 
     1516NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int16_t>& value, std::vector<int64_t> & dims); 
     1517template 
     1518NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint16_t>& value, std::vector<int64_t> & dims); 
     1519template 
     1520NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int32_t>& value, std::vector<int64_t> & dims); 
     1521template 
     1522NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint32_t>& value, std::vector<int64_t> & dims); 
     1523template 
     1524NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<int64_t>& value, std::vector<int64_t> & dims); 
     1525template 
     1526NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<uint64_t>& value, std::vector<int64_t> & dims); 
     1527template 
     1528NXDLL_EXPORT void File::writeUpdatedData(const string& name, vector<char>& value, std::vector<int64_t> & dims); 
     1529 
     1530template 
    13491531NXDLL_EXPORT void File::writeCompData(const string & name, const vector<float> & value, 
    13501532                                    const vector<int> & dims, const NXcompression comp, 
     
    14951677template 
    14961678NXDLL_EXPORT void File::readData(const std::string & dataName, vector<char>& data); 
     1679 
     1680template 
     1681NXDLL_EXPORT void File::readData(const std::string & dataName, float& data); 
     1682template 
     1683NXDLL_EXPORT void File::readData(const std::string & dataName, double& data); 
     1684template 
     1685NXDLL_EXPORT void File::readData(const std::string & dataName, int8_t& data); 
     1686template 
     1687NXDLL_EXPORT void File::readData(const std::string & dataName, uint8_t& data); 
     1688template 
     1689NXDLL_EXPORT void File::readData(const std::string & dataName, int16_t& data); 
     1690template 
     1691NXDLL_EXPORT void File::readData(const std::string & dataName, uint16_t& data); 
     1692template 
     1693NXDLL_EXPORT void File::readData(const std::string & dataName, int32_t& data); 
     1694template 
     1695NXDLL_EXPORT void File::readData(const std::string & dataName, uint32_t& data); 
     1696template 
     1697NXDLL_EXPORT void File::readData(const std::string & dataName, int64_t& data); 
     1698template 
     1699NXDLL_EXPORT void File::readData(const std::string & dataName, uint64_t& data); 
    14971700 
    14981701template 
  • trunk/bindings/cpp/NeXusFile.hpp

    r1710 r1711  
    332332                   const std::vector<int>& dims); 
    333333 
     334 
     335    /** Create a 1D data field with an unlimited dimension, insert the data, and close the data. 
     336     * 
     337     * \tparam NumT numeric data type of \a value 
     338     * \param name :: The name of the field to create. 
     339     * \param value :: The vector to put into the file. 
     340     */ 
     341    template <typename NumT> 
     342    void writeExtendibleData(const std::string& name, std::vector<NumT>& value); 
     343 
     344    /** Create a 1D data field with an unlimited dimension, insert the data, and close the data. 
     345     * 
     346     * \tparam NumT numeric data type of \a value 
     347     * \param name :: The name of the field to create. 
     348     * \param value :: The vector to put into the file. 
     349     * \param chunkSize :: chunk size to use when writing 
     350     */ 
     351    template <typename NumT> 
     352    void writeExtendibleData(const std::string& name, std::vector<NumT>& value, const int64_t chunk); 
     353 
     354    /** Create a 1D data field with an unlimited dimension, insert the data, and close the data. 
     355     * 
     356     * \tparam NumT numeric data type of \a value 
     357     * \param name :: The name of the field to create. 
     358     * \param value :: The vector to put into the file. 
     359     * \param dims :: The dimensions of the data. 
     360     * \param chunk :: chunk size to use when writing 
     361     */ 
     362    template <typename NumT> 
     363    void writeExtendibleData(const std::string& name, std::vector<NumT>& value, 
     364                             std::vector<int64_t>& dims, std::vector<int64_t> & chunk); 
     365 
     366 
     367    /** Updates the data written into an already-created 
     368     * data vector. If the data was created as extendible, it will be resized. 
     369     * 
     370     * \tparam NumT numeric data type of \a value 
     371     * \param name :: The name of the field to create. 
     372     * \param value :: The vector to put into the file. 
     373     */ 
     374    template <typename NumT> 
     375    void writeUpdatedData(const std::string& name, std::vector<NumT>& value); 
     376 
     377    /** Updates the data written into an already-created 
     378     * data vector. If the data was created as extendible, it will be resized. 
     379     * 
     380     * \tparam NumT numeric data type of \a value 
     381     * \param name :: The name of the field to create. 
     382     * \param value :: The vector to put into the file. 
     383     * \param dims :: The dimensions of the data. 
     384     */ 
     385    template <typename NumT> 
     386    void writeUpdatedData(const std::string& name, std::vector<NumT>& value, 
     387                          std::vector<int64_t>& dims); 
     388 
    334389    /** 
    335390     * \copydoc makeCompData(const std::string&, const NXnumtype, 
     
    581636    void readData(const std::string & dataName, std::vector<NumT>& data); 
    582637 
    583     /** Put data into the supplied vector. The vector does not need to 
     638    /** Put data into the supplied value. 
     639     * 
     640     * The named data object is opened, loaded, then closed. 
     641     * 
     642     * \param dataName :: name of the data to open. 
     643     * \param data :: Where to put the data. 
     644     * \tparam NumT numeric data type of \a data 
     645     */ 
     646    template <typename NumT> 
     647    void readData(const std::string & dataName, NumT & data); 
     648 
     649    /** Put data into the supplied string. The vector does not need to 
    584650     * be the correct size, just the correct type as it is resized to 
    585651     * the appropriate value. 
     
    589655     * @param dataName :: name of the data to open. 
    590656     * @param data :: Where to put the data. 
    591      * \tparam NumT numeric data type of \a data 
    592657     */ 
    593658    void readData(const std::string & dataName, std::string & data); 
  • trunk/test/napi_test_cpp.cxx

    r1596 r1711  
    130130  cdims.push_back(20); 
    131131  file.writeCompData("comp_data", comp_array, array_dims, NeXus::LZW, cdims); 
     132 
     133  // ---------- Test write Extendible Data -------------------------- 
     134  std::vector<int> data(10, 123); 
     135  file.makeGroup("extendible_data", "NXdata", 1); 
     136  file.writeExtendibleData("mydata1", data); 
     137  file.writeExtendibleData("mydata2", data, 1000); 
     138  std::vector<int64_t> dims(2); 
     139  dims[0] = 5; 
     140  dims[1] = 2; 
     141  std::vector<int64_t> chunk(2, 2); 
     142  file.writeExtendibleData("my2Ddata", data, dims, chunk); 
     143  file.putAttr("string_attrib", "some short string"); 
     144 
     145  // Data vector can grow 
     146  for (size_t i=0; i<6; i++) 
     147    data.push_back(456); 
     148  data[0]=789; 
     149  file.writeUpdatedData("mydata1", data); 
     150 
     151  dims[0] = 8; 
     152  dims[1] = 2; 
     153  file.writeUpdatedData("my2Ddata", data, dims); 
     154 
     155  // Data vector can also shrink! 
     156  data.clear(); 
     157  data.resize(5, 234); 
     158  file.writeUpdatedData("mydata2", data); 
     159 
     160  // Exit the group 
     161  file.closeGroup(); 
     162  // ---------- End Test write Extendible Data -------------------------- 
    132163 
    133164  // simple flush test 
     
    493524        NeXus::File file(fname); 
    494525        multimap<string, string> *map = file.getTypeMap(); 
    495         int mapsize = 21; 
     526        int mapsize = 25; 
    496527        // HDF4 does not have int64 capability, so resulting map is one shorter than HDF5 and XML files 
    497528        if (fname == string("napi_test_cpp.hdf")) { 
Note: See TracChangeset for help on using the changeset viewer.