Skip to content

Commit e7a698f

Browse files
committed
New test for h5ex_tutr.
1 parent dcaa80c commit e7a698f

7 files changed

Lines changed: 181 additions & 111 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.hdf5javalib.examples.h5ex_tutr.att;
2+
3+
import org.hdf5javalib.datasource.TypedDataSource;
4+
5+
import java.io.IOException;
6+
import java.net.URISyntaxException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.Objects;
11+
import java.util.stream.Stream;
12+
13+
import static org.hdf5javalib.utils.HdfDisplayUtils.*;
14+
15+
/**
16+
* Demonstrates reading and processing compound data from an HDF5 file.
17+
* <p>
18+
* The {@code CompoundRead} class serves as an example application that reads
19+
* a compound dataset from an HDF5 file, processes it using a {@link TypedDataSource},
20+
* and displays the results. It showcases filtering and mapping operations on the
21+
* dataset, as well as conversion to a custom Java class.
22+
* </p>
23+
*/
24+
public class h5ex_tutr_att_read {
25+
/**
26+
* Entry point for the application.
27+
*
28+
* @param args command-line arguments (not used)
29+
*/
30+
public static void main(String[] args) {
31+
new h5ex_tutr_att_read().run();
32+
}
33+
34+
/**
35+
* Executes the main logic of reading and displaying compound data from an HDF5 file.
36+
*/
37+
private void run() {
38+
try {
39+
// List all .h5 files in HDF5Examples resources directory
40+
Path dirPath = Paths.get(Objects.requireNonNull(h5ex_tutr_att_read.class.getClassLoader().getResource("h5ex_tutr/att")).toURI());
41+
try ( Stream<Path> streamList = Files.list(dirPath) ) {
42+
streamList.filter(p -> p.toString().endsWith(".h5"))
43+
.forEach(p -> {
44+
System.out.println("Running " + p.getFileName());
45+
displayFileAttr(p);
46+
});
47+
48+
}
49+
} catch (URISyntaxException | IOException e) {
50+
throw new RuntimeException(e);
51+
}
52+
}
53+
54+
}

src/main/java/org/hdf5javalib/examples/h5ex_tutr/h5ex_d/h5ex_tutr_read.java renamed to src/main/java/org/hdf5javalib/examples/h5ex_tutr/h5ex_tutr_read.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.hdf5javalib.examples.h5ex_tutr.h5ex_d;
1+
package org.hdf5javalib.examples.h5ex_tutr;
22

33
import org.hdf5javalib.datasource.TypedDataSource;
44

src/test/java/org/hdf5javalib/examples/h5ex_d/H5exDReadTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@
44
import org.hdf5javalib.datasource.TypedDataSource;
55
import org.hdf5javalib.examples.ResourceLoader;
66
import org.hdf5javalib.hdffile.dataobjects.messages.DataLayoutMessage;
7+
import org.hdf5javalib.hdffile.dataobjects.messages.ExternalDataFilesMessage;
78
import org.hdf5javalib.hdfjava.HdfDataset;
89
import org.hdf5javalib.hdfjava.HdfFileReader;
9-
import org.hdf5javalib.hdffile.dataobjects.messages.ExternalDataFilesMessage;
10+
import org.junit.jupiter.api.Test;
1011

1112
import java.math.BigDecimal;
1213
import java.nio.channels.SeekableByteChannel;
1314
import java.util.List;
1415

1516
import static org.junit.jupiter.api.Assertions.*;
1617

17-
import org.junit.jupiter.api.Test;
18-
1918
public class H5exDReadTest {
2019

2120
private int[][] toIntMatrix(HdfData[][] data) {

src/test/java/org/hdf5javalib/examples/h5ex_tutr/H5exTutrReadTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22

33
import org.hdf5javalib.dataclass.HdfData;
44
import org.hdf5javalib.datasource.TypedDataSource;
5-
import org.hdf5javalib.datatype.FixedPointDatatype;
65
import org.hdf5javalib.examples.ResourceLoader;
76
import org.hdf5javalib.hdfjava.HdfDataset;
87
import org.hdf5javalib.hdfjava.HdfFileReader;
8+
import org.junit.jupiter.api.Test;
99

1010
import java.nio.channels.SeekableByteChannel;
1111
import java.util.Arrays;
1212
import java.util.function.IntBinaryOperator;
1313

1414
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1515

16-
import org.junit.jupiter.api.Test;
17-
1816
public class H5exTutrReadTest {
1917

2018
private int[][] toIntMatrix(HdfData[][] data) {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.hdf5javalib.examples.h5ex_tutr.att;
2+
3+
import org.hdf5javalib.dataclass.HdfData;
4+
import org.hdf5javalib.examples.ResourceLoader;
5+
import org.hdf5javalib.hdffile.dataobjects.messages.AttributeMessage;
6+
import org.hdf5javalib.hdfjava.HdfDataset;
7+
import org.hdf5javalib.hdfjava.HdfFileReader;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.nio.channels.SeekableByteChannel;
11+
import java.util.List;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
public class H5exTutrAttReadTest {
16+
17+
private double[][] toDoubleMatrix(HdfData[][] data) {
18+
double[][] res = new double[data.length][data[0].length];
19+
for (int i = 0; i < data.length; i++) {
20+
for (int j = 0; j < data[i].length; j++) {
21+
res[i][j] = data[i][j].getInstance(Double.class);
22+
}
23+
}
24+
return res;
25+
}
26+
27+
private int[] toIntArray(HdfData[] data) {
28+
int[] res = new int[data.length];
29+
for (int i = 0; i < data.length; i++) {
30+
res[i] = data[i].getInstance(Long.class).intValue();
31+
}
32+
return res;
33+
}
34+
35+
@Test
36+
void testAttributes() throws Exception {
37+
try (SeekableByteChannel channel = ResourceLoader.loadResourceAsChannel("h5ex_tutr/att/Attributes.h5")) {
38+
HdfFileReader reader = new HdfFileReader(channel).readFile();
39+
List<HdfDataset> datasets = reader.getDatasets();
40+
assertEquals(1, datasets.size());
41+
HdfDataset ds = datasets.get(0);
42+
assertEquals("Dataset", ds.getObjectName());
43+
List<AttributeMessage> attrs = ds.getAttributeMessages();
44+
assertEquals(3, attrs.size());
45+
46+
// Float attribute
47+
AttributeMessage floatAttr = attrs.stream().filter(a -> "Float attribute".equalsIgnoreCase(a.getName().toString())).findFirst().orElse(null);
48+
assertNotNull(floatAttr);
49+
assertEquals(2, floatAttr.getHdfDataHolder().getDimensionality());
50+
HdfData[][] floatData = floatAttr.getHdfDataHolder().getAll(HdfData[][].class);
51+
double[][] expectedFloat = {{-1.0, -1.0, -1.0}, {-1.0, -1.0, -1.0}};
52+
assertArrayEquals(expectedFloat, toDoubleMatrix(floatData));
53+
54+
// Integer attribute (scalar)
55+
AttributeMessage intAttr = attrs.stream().filter(a -> "Integer attribute".equalsIgnoreCase(a.getName().toString())).findFirst().orElse(null);
56+
assertNotNull(intAttr);
57+
assertEquals(0, intAttr.getHdfDataHolder().getDimensionality());
58+
HdfData intData = intAttr.getHdfDataHolder().getAll(HdfData.class);
59+
assertEquals(1, intData.getInstance(Long.class).intValue());
60+
61+
// Character attribute (scalar string)
62+
AttributeMessage charAttr = attrs.stream().filter(a -> "Character attribute".equalsIgnoreCase(a.getName().toString())).findFirst().orElse(null);
63+
assertNotNull(charAttr);
64+
assertEquals(0, charAttr.getHdfDataHolder().getDimensionality());
65+
HdfData charData = charAttr.getHdfDataHolder().getAll(HdfData.class);
66+
assertEquals("ABCD", charData.getInstance(String.class));
67+
}
68+
}
69+
70+
@Test
71+
void testDefaultFile() throws Exception {
72+
try (SeekableByteChannel channel = ResourceLoader.loadResourceAsChannel("h5ex_tutr/att/default_file.h5")) {
73+
HdfFileReader reader = new HdfFileReader(channel).readFile();
74+
List<HdfDataset> datasets = reader.getDatasets();
75+
assertFalse(datasets.isEmpty());
76+
for (HdfDataset ds : datasets) {
77+
List<AttributeMessage> attrs = ds.getAttributeMessages();
78+
assertEquals(1, attrs.size());
79+
AttributeMessage attr = attrs.get(0);
80+
assertEquals("attr_name", attr.getName().toString());
81+
assertEquals(1, attr.getHdfDataHolder().getDimensionality());
82+
HdfData[] data = attr.getHdfDataHolder().getAll(HdfData[].class);
83+
int[] actual = toIntArray(data);
84+
int[] expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
85+
assertArrayEquals(expected, actual);
86+
}
87+
}
88+
}
89+
}

src/test/java/org/hdf5javalib/examples/read/CompoundReadTest.java

Lines changed: 31 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -29,132 +29,80 @@ public class CompoundReadTest {
2929
new BigDecimal("10.2500000"));
3030

3131
public static class CompoundExample {
32-
private Long recordId;
33-
private String fixedStr;
34-
private String varStr;
35-
private Float floatVal;
36-
private Double doubleVal;
37-
private Byte int8_Val;
38-
private Short int16_Val;
39-
private Integer int32_Val;
40-
private Long int64_Val;
41-
private Short uint8_Val;
42-
private Integer uint16_Val;
43-
private Long uint32_Val;
44-
private BigInteger uint64_Val;
45-
private BigDecimal scaledUintVal;
32+
private final Long recordId;
33+
private final String fixedStr;
34+
private final String varStr;
35+
private final Float floatVal;
36+
private final Double doubleVal;
37+
private final Byte int8_Val;
38+
private final Short int16_Val;
39+
private final Integer int32_Val;
40+
private final Long int64_Val;
41+
private final Short uint8_Val;
42+
private final Integer uint16_Val;
43+
private final Long uint32_Val;
44+
private final BigInteger uint64_Val;
45+
private final BigDecimal scaledUintVal;
46+
47+
public CompoundExample(Long recordId, String fixedStr, String varStr, Float floatVal, Double doubleVal, Byte int8Val, Short int16Val, Integer int32Val, Long int64Val, Short uint8Val, Integer uint16Val, Long uint32Val, BigInteger uint64Val, BigDecimal scaledUintVal) {
48+
this.recordId = recordId;
49+
this.fixedStr = fixedStr;
50+
this.varStr = varStr;
51+
this.floatVal = floatVal;
52+
this.doubleVal = doubleVal;
53+
int8_Val = int8Val;
54+
int16_Val = int16Val;
55+
int32_Val = int32Val;
56+
int64_Val = int64Val;
57+
uint8_Val = uint8Val;
58+
uint16_Val = uint16Val;
59+
uint32_Val = uint32Val;
60+
uint64_Val = uint64Val;
61+
this.scaledUintVal = scaledUintVal;
62+
}
4663

4764
public Long getRecordId() {
4865
return recordId;
4966
}
50-
51-
public void setRecordId(Long recordId) {
52-
this.recordId = recordId;
53-
}
54-
5567
public String getFixedStr() {
5668
return fixedStr;
5769
}
58-
59-
public void setFixedStr(String fixedStr) {
60-
this.fixedStr = fixedStr;
61-
}
62-
6370
public String getVarStr() {
6471
return varStr;
6572
}
66-
67-
public void setVarStr(String varStr) {
68-
this.varStr = varStr;
69-
}
70-
7173
public Float getFloatVal() {
7274
return floatVal;
7375
}
74-
75-
public void setFloatVal(Float floatVal) {
76-
this.floatVal = floatVal;
77-
}
78-
7976
public Double getDoubleVal() {
8077
return doubleVal;
8178
}
82-
83-
public void setDoubleVal(Double doubleVal) {
84-
this.doubleVal = doubleVal;
85-
}
86-
8779
public Byte getInt8_Val() {
8880
return int8_Val;
8981
}
90-
91-
public void setInt8_Val(Byte int8_Val) {
92-
this.int8_Val = int8_Val;
93-
}
94-
9582
public Short getInt16_Val() {
9683
return int16_Val;
9784
}
98-
99-
public void setInt16_Val(Short int16_Val) {
100-
this.int16_Val = int16_Val;
101-
}
102-
10385
public Integer getInt32_Val() {
10486
return int32_Val;
10587
}
106-
107-
public void setInt32_Val(Integer int32_Val) {
108-
this.int32_Val = int32_Val;
109-
}
110-
11188
public Long getInt64_Val() {
11289
return int64_Val;
11390
}
114-
115-
public void setInt64_Val(Long int64_Val) {
116-
this.int64_Val = int64_Val;
117-
}
118-
11991
public Short getUint8_Val() {
12092
return uint8_Val;
12193
}
122-
123-
public void setUint8_Val(Short uint8_Val) {
124-
this.uint8_Val = uint8_Val;
125-
}
126-
12794
public Integer getUint16_Val() {
12895
return uint16_Val;
12996
}
130-
131-
public void setUint16_Val(Integer uint16_Val) {
132-
this.uint16_Val = uint16_Val;
133-
}
134-
13597
public Long getUint32_Val() {
13698
return uint32_Val;
13799
}
138-
139-
public void setUint32_Val(Long uint32_Val) {
140-
this.uint32_Val = uint32_Val;
141-
}
142-
143100
public BigInteger getUint64_Val() {
144101
return uint64_Val;
145102
}
146-
147-
public void setUint64_Val(BigInteger uint64_Val) {
148-
this.uint64_Val = uint64_Val;
149-
}
150-
151103
public BigDecimal getScaledUintVal() {
152104
return scaledUintVal;
153105
}
154-
155-
public void setScaledUintVal(BigDecimal scaledUintVal) {
156-
this.scaledUintVal = scaledUintVal;
157-
}
158106
}
159107

160108
public static class MonitoringData {

src/test/java/org/hdf5javalib/examples/read/SeparateTypesReadTest.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,7 @@
2020

2121
public class SeparateTypesReadTest {
2222

23-
public static class Compound {
24-
private Short a;
25-
private Double b;
26-
27-
public Short getA() {
28-
return a;
29-
}
30-
31-
public void setA(Short a) {
32-
this.a = a;
33-
}
34-
35-
public Double getB() {
36-
return b;
37-
}
38-
39-
public void setB(Double b) {
40-
this.b = b;
41-
}
23+
public record Compound(Short a, Double b) {
4224
}
4325

4426
public static class CustomCompound {
@@ -178,8 +160,8 @@ void testAllTypesSeparateH5() throws Exception {
178160
assertEquals("123, 9.81", compoundStrSource.readScalar());
179161
TypedDataSource<Compound> compoundJavaSource = new TypedDataSource<>(channel, reader, compoundDs, Compound.class);
180162
Compound compoundJava = compoundJavaSource.readScalar();
181-
assertEquals(Short.valueOf((short)123), compoundJava.getA());
182-
assertEquals(9.81, compoundJava.getB(), 0.001);
163+
assertEquals(Short.valueOf((short)123), compoundJava.a());
164+
assertEquals(9.81, compoundJava.b(), 0.001);
183165
CompoundDatatype.addConverter(CustomCompound.class, (bytes, compoundDataType) -> {
184166
Map<String, HdfCompoundMember> nameToMember = compoundDataType.getInstance(HdfCompound.class, bytes)
185167
.getMembers()

0 commit comments

Comments
 (0)