Skip to content

Commit b766604

Browse files
author
haileyajohnson
authored
Merge pull request #800 from lesserwhirls/nilstr-5
[5.x]: Handle NULL string attributes in HDF5.
2 parents 49aa047 + 785b618 commit b766604

4 files changed

Lines changed: 112 additions & 0 deletions

File tree

cdm/core/src/main/java/ucar/nc2/internal/iosp/hdf5/H5headerNew.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public class H5headerNew implements H5headerIF, HdfHeaderIF {
9393
private static boolean warnings = true, debugReference, debugRegionReference, debugCreationOrder, debugStructure;
9494
private static boolean debugDimensionScales;
9595

96+
// NULL string value, following netCDF-C, set to NIL
97+
private static final String NULL_STRING_VALUE = "NIL";
98+
9699
public static void setWarnings(boolean warn) {
97100
warnings = warn;
98101
}
@@ -2135,6 +2138,9 @@ Array getHeapDataArray(HeapIdentifier heapId, DataType dataType, int endian)
21352138
*/
21362139
String readHeapString(long heapIdAddress) throws IOException {
21372140
HeapIdentifier heapId = h5objects.readHeapIdentifier(heapIdAddress);
2141+
if (heapId.isEmpty()) {
2142+
return NULL_STRING_VALUE;
2143+
}
21382144
GlobalHeap.HeapObject ho = heapId.getHeapObject();
21392145
if (ho == null)
21402146
throw new IllegalStateException("Cant find Heap Object,heapId=" + heapId);
@@ -2154,6 +2160,9 @@ String readHeapString(long heapIdAddress) throws IOException {
21542160
*/
21552161
String readHeapString(ByteBuffer bb, int pos) throws IOException {
21562162
HeapIdentifier heapId = h5objects.readHeapIdentifier(bb, pos);
2163+
if (heapId.isEmpty()) {
2164+
return NULL_STRING_VALUE;
2165+
}
21572166
GlobalHeap.HeapObject ho = heapId.getHeapObject();
21582167
if (ho == null)
21592168
throw new IllegalStateException("Cant find Heap Object,heapId=" + heapId);

cdm/core/src/main/java/ucar/nc2/iosp/hdf5/H5header.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public class H5header extends NCheader implements H5headerIF {
6060
private static boolean warnings = true, debugReference, debugRegionReference, debugCreationOrder, debugStructure;
6161
private static boolean debugDimensionScales;
6262

63+
// NULL string value, following netCDF-C, set to NIL
64+
private static final String NULL_STRING_VALUE = "NIL";
65+
6366
public static void setWarnings(boolean warn) {
6467
warnings = warn;
6568
}
@@ -4711,6 +4714,9 @@ Array getHeapDataArray(HeapIdentifier heapId, DataType dataType, int endian)
47114714
*/
47124715
String readHeapString(long heapIdAddress) throws IOException {
47134716
H5header.HeapIdentifier heapId = new HeapIdentifier(heapIdAddress);
4717+
if (heapId.isEmpty()) {
4718+
return NULL_STRING_VALUE;
4719+
}
47144720
H5header.GlobalHeap.HeapObject ho = heapId.getHeapObject();
47154721
if (ho == null)
47164722
throw new IllegalStateException("Cant find Heap Object,heapId=" + heapId);
@@ -4730,6 +4736,9 @@ String readHeapString(long heapIdAddress) throws IOException {
47304736
*/
47314737
String readHeapString(ByteBuffer bb, int pos) throws IOException {
47324738
H5header.HeapIdentifier heapId = new HeapIdentifier(bb, pos);
4739+
if (heapId.isEmpty()) {
4740+
return NULL_STRING_VALUE;
4741+
}
47334742
H5header.GlobalHeap.HeapObject ho = heapId.getHeapObject();
47344743
if (ho == null)
47354744
throw new IllegalStateException("Cant find Heap Object,heapId=" + heapId);
6.09 KB
Binary file not shown.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2021 University Corporation for Atmospheric Research/Unidata
3+
* See LICENSE for license information.
4+
*/
5+
package ucar.nc2.iosp.hdf5;
6+
7+
import org.junit.AfterClass;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.junit.runners.Parameterized;
11+
import ucar.nc2.Attribute;
12+
import ucar.nc2.NetcdfFile;
13+
import ucar.nc2.NetcdfFiles;
14+
import ucar.nc2.Variable;
15+
import ucar.unidata.util.test.TestDir;
16+
17+
import java.io.IOException;
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
21+
import static com.google.common.truth.Truth.assertThat;
22+
23+
/** Test String Attributes */
24+
@RunWith(Parameterized.class)
25+
public class TestNullStringAttr {
26+
27+
private static NetcdfFile ncfOld, ncfNew;
28+
private static final String testFile = TestDir.cdmLocalTestDataDir + "hdf5/string_attrs.nc4";
29+
30+
private final NetcdfFile ncf;
31+
32+
public TestNullStringAttr(String apiType, NetcdfFile ncf) {
33+
this.ncf = ncf;
34+
}
35+
36+
@Parameterized.Parameters(name = "{0}")
37+
public static Collection<?> netcdfFileObjects() throws IOException {
38+
ncfOld = NetcdfFile.open(testFile);
39+
ncfNew = NetcdfFiles.open(testFile);
40+
return Arrays.asList(new Object[][] {{"Old API", ncfOld}, {"New API", ncfNew},});
41+
}
42+
43+
@Test
44+
public void testNullStringGlobalAttr() {
45+
Attribute attr = ncf.findGlobalAttribute("NULL_STR_GATTR");
46+
assertThat(attr).isNotNull();
47+
String value = attr.getStringValue();
48+
assertThat(value).isNotNull();
49+
assertThat(value).isNotEmpty();
50+
assertThat(value).isNotEqualTo("");
51+
assertThat(value).isEqualTo("NIL");
52+
}
53+
54+
@Test
55+
public void testEmptyStringGlobalAttr() {
56+
Attribute attr = ncf.findGlobalAttribute("EMPTY_STR_GATTR");
57+
assertThat(attr).isNotNull();
58+
String value = attr.getStringValue();
59+
assertThat(value).isNotNull();
60+
assertThat(value).isEmpty();
61+
assertThat(value).isEqualTo("");
62+
}
63+
64+
@Test
65+
public void testNullStringAttr() {
66+
Variable testVar = ncf.findVariable("/var");
67+
assert null != testVar;
68+
Attribute attr = testVar.findAttribute("NULL_STR_ATTR");
69+
assertThat(attr).isNotNull();
70+
String value = attr.getStringValue();
71+
assertThat(value).isNotNull();
72+
assertThat(value).isNotEmpty();
73+
assertThat(value).isNotEqualTo("");
74+
assertThat(value).isEqualTo("NIL");
75+
}
76+
77+
@Test
78+
public void testEmptyStringAttr() {
79+
Variable testVar = ncf.findVariable("/var");
80+
assert null != testVar;
81+
Attribute attr = testVar.findAttribute("EMPTY_STR_ATTR");
82+
assertThat(attr).isNotNull();
83+
String value = attr.getStringValue();
84+
assertThat(value).isNotNull();
85+
assertThat(value).isEmpty();
86+
assertThat(value).isEqualTo("");
87+
}
88+
89+
@AfterClass
90+
public static void closeFile() throws IOException {
91+
ncfOld.close();
92+
ncfNew.close();
93+
}
94+
}

0 commit comments

Comments
 (0)