1186

Writing 2-D array int[n][m] to HDF5 file using Visual C++

I'm just getting started with HDF5 and would appreciate some advice on the following.

I have a 2-d array: data[][] passed into a method. The method looks like:

void WriteData( int data[48][100], int sizes[48])

The size of the data is not actually 48 x 100 but rather 48 x sizes[i]. I.e. each row could be a different length! In one simple case I'm dealing with, all rows are the same size (but not 100), so you can say that the array is 48 X sizes[0].

How best to write this to HDF5?

I have some working code where I loop through 0 to 48 and create a new dataset for each row.

Something like:

for (int i = 0; i < 48; i++) { hsize_t dsSize[2]; dsSize[0] = 48; dsSize[1] = sizes[0]; // use sizes[i] in most general case // Create the Data Space DataSpace dataSpace = DataSpace(2, dsSize); DataSet dataSet = group.createDataSet(dataSetName, intDataType, dataSpace); dataSet.write(data[i], intDataType); }

Is there a way to write the data all at once in one DataSet? Perhaps one solution for the simpler case of all rows the same length, and another for the ragged rows?

I've tried a few things to no avail. I called dataSet.write(data, intDataType), i.e. I threw the whole array at it. I seemed to get garbage in the file, I suspect because the array the data is stored in is actually 48x100 and I only need a small part of that.

It occurred to me that I could maybe use double ptrs int** or vector> but I'm stuck on that. As far as I can tell, "write" need a void* ptr. Also, I'd like the file to "look correct". I.e. one giant row with all rows of data is not desirable, if I must go that route, someone would need to communicate a slick way to store the info that would allow me to read the data back in from file (perhaps store row lengths as attributes?).

Perhaps my real problem is finding C++ examples of non-trivial use cases.

Any help is much appreciated.

Dave

Answer1:

Here is how you can do it using variable length arrays if your data is a vector of vectors (which seems to make sense for your use case):

void WriteData(const std::vector< std::vector<int> >& data) { hsize_t dim(data.size()); H5::DataSpace dspace(1, &dim); H5::VarLenType dtype(H5::PredType::NATIVE_INT); H5::DataSet dset(group.createDataSet(dataSetName, dtype, dspace)); hvl_t vl[dim]; for (hsize_t i = 0; i < dim; ++i) { vl[i].len = data[i].size(); vl[i].p = &data[i][0]; } dset.write(vl, dtype); }

Recommend

  • c# List to Lambda Expression with starter example: Refactor to handle the List
  • How to deal with SpiderWebPlot in JFreeChart?
  • ASP.NET Gridview Paging Problem
  • Fancy dynamic list in Android: TableLayout vs ListView
  • Keep same scale in different graphs ggplot2
  • Stacked Bar Chart with percentage composition inside the Bar and total above the Bar in JFreeChart
  • Can you use DataSet and DataTables in a Portable Class Library
  • How to return DataSet (xsd) in WCF
  • Django model inheritance, filtering models
  • How do I alternate colors in Flat List (React Native)
  • Django rest serializer Breaks when data exists
  • Change Inet root folder for iis 7
  • How to delay loading a property with linq to sql external mapping?
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • R - Combining Columns to String Based on Logical Match
  • How do I change content of ComboFieldEditor?
  • Splitting given String into two variables - php
  • Read text file and split every line in MSBuild
  • Counter field in MS Access, how to generate?
  • Is there a amazon webstore API for customers?
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • How to extract text from Word files using C#?
  • vba code to select only visible cells in specific column except heading
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • 0x202A in filename: Why?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • embed rChart in Markdown
  • How does Linux kernel interrupt the application?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal