Skip to content

Commit 69f4966

Browse files
committed
v2 arch support
1 parent 983d584 commit 69f4966

3 files changed

Lines changed: 76 additions & 14 deletions

File tree

src/main/java/org/hdf5javalib/datasource/TypedDataSource.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private T[][][] populateTensor(ByteBuffer buffer, int depth, int rows, int cols)
172172
*/
173173
public T readScalar() throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
174174
if (dimensions.length != 0) {
175-
throw new IllegalStateException("Dataset must be 0D(Scalar)");
175+
throw new IllegalStateException("Dataset is not 0D(Scalar)");
176176
}
177177
if (!dataset.hasData()) {
178178
throw new IllegalStateException("Dataset has no data");
@@ -190,7 +190,7 @@ public T readScalar() throws IOException, InvocationTargetException, Instantiati
190190
*/
191191
public Stream<T> streamScalar() throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
192192
if (dimensions.length != 0) {
193-
throw new IllegalStateException("Dataset must be 0D(Scalar)");
193+
throw new IllegalStateException("Dataset is not 0D(Scalar)");
194194
}
195195
if (!dataset.hasData()) {
196196
throw new IllegalStateException("Dataset has no data");
@@ -220,7 +220,7 @@ public Stream<T> parallelStreamScalar() throws IOException, InvocationTargetExce
220220
*/
221221
public T[] readVector() throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
222222
if (dimensions.length != 1) {
223-
throw new IllegalStateException("Dataset must be 1D(Vector)");
223+
throw new IllegalStateException("Dataset is not 1D(Vector)");
224224
}
225225
int size = dimensions[0];
226226
ByteBuffer buffer = readBytes(0, (long) elementSize * size);
@@ -235,7 +235,7 @@ public T[] readVector() throws IOException, InvocationTargetException, Instantia
235235
*/
236236
public Stream<T> streamVector() {
237237
if (dimensions.length != 1) {
238-
throw new IllegalStateException("Dataset must be 1D(Vector)");
238+
throw new IllegalStateException("Dataset is not 1D(Vector)");
239239
}
240240
return StreamSupport.stream(new VectorSpliterator(0, dimensions[0], elementSize), false);
241241
}
@@ -248,7 +248,7 @@ public Stream<T> streamVector() {
248248
*/
249249
public Stream<T> parallelStreamVector() {
250250
if (dimensions.length != 1) {
251-
throw new IllegalStateException("Dataset must be 1D(Vector)");
251+
throw new IllegalStateException("Dataset is not 1D(Vector)");
252252
}
253253
return StreamSupport.stream(new VectorSpliterator(0, dimensions[0], elementSize), true);
254254
}
@@ -264,7 +264,7 @@ public Stream<T> parallelStreamVector() {
264264
*/
265265
public T[][] readMatrix() throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
266266
if (dimensions.length != 2) {
267-
throw new IllegalStateException("Dataset must be 2D(Matrix)");
267+
throw new IllegalStateException("Dataset is not 2D(Matrix)");
268268
}
269269
int rows = dimensions[0];
270270
int cols = dimensions[1];
@@ -280,7 +280,7 @@ public T[][] readMatrix() throws IOException, InvocationTargetException, Instant
280280
*/
281281
public Stream<T[]> streamMatrix() {
282282
if (dimensions.length != 2) {
283-
throw new IllegalStateException("Dataset must be 2D(Matrix)");
283+
throw new IllegalStateException("Dataset is not 2D(Matrix)");
284284
}
285285
long rowSize = (long) elementSize * dimensions[1];
286286
return StreamSupport.stream(new MatrixSpliterator(0, dimensions[0], rowSize, dimensions[1]), false);
@@ -294,7 +294,7 @@ public Stream<T[]> streamMatrix() {
294294
*/
295295
public Stream<T[]> parallelStreamMatrix() {
296296
if (dimensions.length != 2) {
297-
throw new IllegalStateException("Dataset must be 2D(Matrix)");
297+
throw new IllegalStateException("Dataset is not 2D(Matrix)");
298298
}
299299
long rowSize = (long) elementSize * dimensions[1];
300300
return StreamSupport.stream(new MatrixSpliterator(0, dimensions[0], rowSize, dimensions[1]), true);
@@ -311,7 +311,7 @@ public Stream<T[]> parallelStreamMatrix() {
311311
*/
312312
public T[][][] readTensor() throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
313313
if (dimensions.length != 3) {
314-
throw new IllegalStateException("Dataset must be 3D(Tensor)");
314+
throw new IllegalStateException("Dataset is not 3D(Tensor)");
315315
}
316316
int depth = dimensions[0];
317317
int rows = dimensions[1];
@@ -328,7 +328,7 @@ public T[][][] readTensor() throws IOException, InvocationTargetException, Insta
328328
*/
329329
public Stream<T[][]> streamTensor() {
330330
if (dimensions.length != 3) {
331-
throw new IllegalStateException("Dataset must be 3D(Tensor)");
331+
throw new IllegalStateException("Dataset is not 3D(Tensor)");
332332
}
333333
long sliceSize = (long) elementSize * dimensions[1] * dimensions[2];
334334
return StreamSupport.stream(new TensorSpliterator(0, dimensions[0], sliceSize, dimensions[1], dimensions[2]), false);
@@ -342,7 +342,7 @@ public Stream<T[][]> streamTensor() {
342342
*/
343343
public Stream<T[][]> parallelStreamTensor() {
344344
if (dimensions.length != 3) {
345-
throw new IllegalStateException("Dataset must be 3D(Tensor)");
345+
throw new IllegalStateException("Dataset is not 3D(Tensor)");
346346
}
347347
long sliceSize = (long) elementSize * dimensions[1] * dimensions[2];
348348
return StreamSupport.stream(new TensorSpliterator(0, dimensions[0], sliceSize, dimensions[1], dimensions[2]), true);

src/main/java/org/hdf5javalib/examples/hdf5examples/HDF5Debug.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
package org.hdf5javalib.examples.hdf5examples;
22

3+
import org.hdf5javalib.dataclass.HdfFixedPoint;
4+
import org.hdf5javalib.dataclass.HdfFloatPoint;
35
import org.hdf5javalib.datasource.TypedDataSource;
6+
import org.hdf5javalib.hdfjava.HdfDataFile;
7+
import org.hdf5javalib.hdfjava.HdfDataset;
8+
import org.hdf5javalib.hdfjava.HdfFileReader;
9+
import org.hdf5javalib.utils.HdfDisplayUtils;
410
import org.slf4j.Logger;
511
import org.slf4j.LoggerFactory;
612

13+
import java.io.IOException;
14+
import java.lang.reflect.InvocationTargetException;
715
import java.net.URISyntaxException;
16+
import java.nio.channels.SeekableByteChannel;
17+
import java.nio.file.Files;
818
import java.nio.file.Path;
919
import java.nio.file.Paths;
20+
import java.nio.file.StandardOpenOption;
21+
import java.util.Arrays;
22+
import java.util.DoubleSummaryStatistics;
1023
import java.util.Objects;
24+
import java.util.OptionalDouble;
1125

1226
import static org.hdf5javalib.utils.HdfDisplayUtils.displayFile;
27+
import static org.hdf5javalib.utils.HdfDisplayUtils.displayValue;
1328

1429
/**
1530
* Demonstrates reading and processing compound data from an HDF5 file.
@@ -39,11 +54,58 @@ private void run() {
3954
// List all .h5 files in HDF5Examples resources directory
4055
// ATL03_20250302235544_11742607_006_01
4156
// Path dirPath = Paths.get(Objects.requireNonNull(HDF5Debug.class.getClassLoader().getResource("HDF5Examples/h5ex_g_compact2.h5")).toURI());
42-
Path dirPath = Paths.get("c:/users/karln/Downloads/ATL03_20250302235544_11742607_006_01.h5");
43-
displayFile(dirPath);
57+
Path dirPath = Paths.get("c:/users/karnicho/Downloads/ATL03_20250302235544_11742607_006_01.h5");
58+
processFile(dirPath);
4459
} catch (Exception e) {
4560
throw new IllegalStateException(e);
4661
}
4762
}
63+
// Generalized method to process the file and apply a custom action per dataset
64+
private static void processFile(Path filePath) {
65+
try (SeekableByteChannel channel = Files.newByteChannel(filePath, StandardOpenOption.READ)) {
66+
HdfFileReader reader = new HdfFileReader(channel).readFile();
67+
// for (HdfDataset dataSet : reader.getDatasets()) {
68+
// System.out.println("{} " + dataSet);
69+
//// log.info("{} ", dataSet);
70+
// displayScalarData(channel, dataSet, HdfFloatPoint.class, reader);
71+
// }
72+
HdfDataset dataSet = reader.getDataset("/gt1l/geolocation/altitude_sc").get();
73+
System.out.println("{} " + dataSet);
74+
// System.out.println("{} " + dataSet.getObjectPath());
75+
// log.info("{} ", dataSet);
76+
displayScalarData(channel, dataSet, HdfFloatPoint.class, reader);
77+
} catch (Exception e) {
78+
log.error("Exception in processFile: {}", filePath, e);
79+
}
80+
}
81+
82+
public static <T> void displayScalarData(SeekableByteChannel fileChannel, HdfDataset dataSet, Class<T> clazz, HdfDataFile hdfDataFile) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
83+
TypedDataSource<T> dataSource = new TypedDataSource<>(fileChannel, hdfDataFile, dataSet, clazz);
84+
85+
T[] resultR = dataSource.readFlattened();
86+
System.out.println(resultR.length);
87+
OptionalDouble max = Arrays.stream(resultR).mapToDouble(h -> {
88+
try {
89+
return ((HdfFloatPoint)h).getInstance(Double.class);
90+
} catch (Exception e) {
91+
throw new RuntimeException(e);
92+
}
93+
})
94+
.max();
95+
System.out.println("max = " + max.orElseThrow());
96+
97+
// T result = dataSource.streamFlattened().findFirst().orElseThrow();
98+
// log.info("{} stream = {}", dataSet.getObjectName(), displayValue(result));
99+
// dataSource.streamFlattened().limit(10).forEach(result->System.out.println(result));
100+
// OptionalDouble max = dataSource.streamFlattened().mapToDouble(h -> {
101+
// try {
102+
// return ((HdfFloatPoint)h).getInstance(Double.class);
103+
// } catch (Exception e) {
104+
// throw new RuntimeException(e);
105+
// }
106+
// })
107+
// .max();
108+
// System.out.println("max = " + max.orElseThrow());
109+
}
48110

49111
}

src/main/java/org/hdf5javalib/utils/HdfDisplayUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private static String displayType(Class<?> declaredType, Object actualValue) {
316316
* @param value the value to format
317317
* @return a string representation of the value, handling arrays appropriately
318318
*/
319-
private static String displayValue(Object value) {
319+
public static String displayValue(Object value) {
320320
if (value == null) return "null";
321321
Class<?> clazz = value.getClass();
322322
if (!clazz.isArray()) return value.toString();

0 commit comments

Comments
 (0)