I am struggling with two very basic Java questions:
I have a dataset of rank 2 with dimensions (100,3), all of entries are double.
(Q1)
I want to read it and treat it as a matrix:
H5File fh5 = new H5File(filename, FileFormat.READ);
H5ScalarDS dset = (H5ScalarDS) fh5.get("/table1");
dset.init();
dset.open(;
double[] data= (double[]) dset.getData();
this way works but I have to deal with a one dimensional array. I
would like to have something like:
double[][] data= (double[][]) dset.getData(); and being able to write
System.out.println(data[2][4]);
(Q2)
Same example as before but I want to read only one column. I did
long[] strides=dset.getStride();
strides[0]= 3;
double[] data= (double[]) dset.getData();
but at this point data[1] and data[2] are giving me two consecutive
values ( I am not jumping along column 0, i.e. 3 by 3).
Thanks for your help.
Jacopo
Hi Jacopo,
Jacopo Pecci wrote:
I am struggling with two very basic Java questions:
I have a dataset of rank 2 with dimensions (100,3), all of entries are double.
(Q1)
I want to read it and treat it as a matrix:
H5File fh5 = new H5File(filename, FileFormat.READ);
H5ScalarDS dset = (H5ScalarDS) fh5.get("/table1");
dset.init();
dset.open(;
double[] data= (double[]) dset.getData();
this way works but I have to deal with a one dimensional array. I
would like to have something like:
double[][] data= (double[][]) dset.getData(); and being able to write
System.out.println(data[2][4]);
We use 1D array for performance reason. There is a direct use of memory between 1D Java
array and 1D C array. There is little overhead to pass data from C to Java. While 2D Java array
2D C array are completely different things, the performance of reading a 2D array is a problem.
Therefore, we leave it to applications to convert the 1D array to 2D array.
(Q2)
Same example as before but I want to read only one column. I did
long[] strides=dset.getStride();
strides[0]= 3;
double[] data= (double[]) dset.getData();
but at this point data[1] and data[2] are giving me two consecutive
values ( I am not jumping along column 0, i.e. 3 by 3).
you need to set the size of the subset accordingly. See the example code below:
···
===
private static void testStride( final String fname ) throws Exception
{
final long[] dims2D = {100, 3};
final String dname = "int2d";
final int[] dataInt = new int[(int)dims2D[0]*(int)dims2D[1]];
final FileFormat fileFormat = new H5File();
for (int i=0; i<dataInt.length; i++) {
dataInt[i] = i;
}
// create a test file and add a dataset to the test file.
final H5File testFile = (H5File)fileFormat.create(fname);
testFile.open();
Datatype dtype = testFile.createDatatype( Datatype.CLASS_INTEGER, 4, Datatype.NATIVE, Datatype.NATIVE);
Dataset dataset = testFile.createScalarDS (dname, null, dtype, dims2D, null, null, 0, dataInt);
testFile.close();
// read the dataset
testFile.open();
dataset = (Dataset)testFile.get(dname);
dataset.init();
long[] dims = dataset.getDims();
long[] stride = dataset.getStride();
long[] select = dataset.getSelectedDims();
stride[0] = 3;
select[0] = dims[0]/stride[0];
dataset.clearData();
int[] dataRead = (int[]) dataset.getData();
for (int i=0; i<10; i++)
System.out.print(dataRead[i]+",\t");
// close file resource
testFile.close();
}
Thanks for your help.
Jacopo
_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org