ClassCastException thrown when trying to read a ucar.nc2.Variable that is an empty ucar.nc2.Structure or a member of an empty Structure
elzbietaa opened this issue · 4 comments
Summary:
The following ClassCastException
is thrown when trying to read data from an empty Structure:
java.lang.ClassCastException: [B cannot be cast to [Ljava.lang.Object;
at ucar.ma2.Array.factory(Array.java:181)
at ucar.ma2.Array.factory(Array.java:160)
at ucar.nc2.iosp.hdf5.H5iosp.readData(H5iosp.java:155)
at ucar.nc2.iosp.hdf5.H5iosp.readData(H5iosp.java:141)
at ucar.nc2.NetcdfFile.readData(NetcdfFile.java:2020)
at ucar.nc2.Variable.reallyRead(Variable.java:874)
at ucar.nc2.Variable._read(Variable.java:845)
at ucar.nc2.Variable.read(Variable.java:723)
at ucar.nc2.dataset.StructureDS.reallyRead(StructureDS.java:232)
at ucar.nc2.Variable._read(Variable.java:845)
at ucar.nc2.Variable.read(Variable.java:723)
at ucar.nc2.NetcdfFile.readArrays(NetcdfFile.java:2142)
...
Notes:
- same happens when the Variable indicates a member of an empty Structure
- this problem can be reproduced given an .nc file generated from a simple .cdl example:
https://www.unidata.ucar.edu/software/netcdf/workshops/2010/groups-types/popup_groups-types_ObsDataExample_1.html viancgen -b odnc4_empty_structures.cdl
(see the attached .zip file for both the .cdl and the .nc file). - we're using edu.ucar:cdm-4.6.7 but also reproduced this issue with v.4.6.11
Example test cases highlighting the issue are:
- For an empty structure:
@Test
public void testReadEmptyStructure() throws IOException {
URL ncfile = getClass().getClassLoader()
.getResource("psenterprise/gsa/netcdf/odnc4_empty_structures.nc");
String variablePath = "/observations";
try (NetcdfDataset dataSet = NetcdfDataset.acquireDataset(null, ncfile.toString(),
EnumSet.noneOf(NetcdfDataset.Enhance.class), -1, null, null)) {
Variable var = dataSet.findVariable(variablePath);
assertThat(var, is(notNullValue()));
dataSet.readArrays(ImmutableList.of(var));
}
}
- For a member of an empty structure:
@Test
public void testReadEmptyStructure() throws IOException {
URL ncfile = getClass().getClassLoader()
.getResource("psenterprise/gsa/netcdf/odnc4_empty_structures.nc");
String variablePath = "/observations.temperature";
try (NetcdfDataset dataSet = NetcdfDataset.acquireDataset(null, ncfile.toString(),
EnumSet.noneOf(NetcdfDataset.Enhance.class), -1, null, null)) {
Variable var = dataSet.findVariable(variablePath);
assertThat(var, is(notNullValue()));
dataSet.readArrays(ImmutableList.of(var));
}
}
@DennisHeimbigner - can you take a look at this one?
ok
It turns out this issue is related to this one: #1211 (comment)
Apparently when an array is created whose size is zero, some piece of code
(probably in H5Iosp) decides that the Array needs a fill value, and creates one if not specified.
later, when H5Iop.readData is called, it attempts to return that fillvalue as the data.
This is probably because (as noted in the other issue) the size is one, not zero.
In any case, the created fillvalue is an empty byte[] object. When an attempt is made
to convert to Object[], it fails as noted on a ClassCastException.
Not sure yet, what the proper solution is.
Just note that the issue occurs only for empty Structure
s or members of an empty Structure
.
With empty ucar.nc2.dataset.VariableDS
no exception is thrown.
The following test passes without problems:
@Test
public void testReadAVariableDs() throws IOException {
URL ncfile = getClass().getClassLoader()
.getResource("psenterprise/gsa/netcdf/odnc4_empty_structures.nc");
String variablePath = "/elevation";
try (NetcdfDataset dataSet = NetcdfDataset.acquireDataset(null, ncfile.toString(),
EnumSet.noneOf(NetcdfDataset.Enhance.class), -1, null, null)) {
Variable var = dataSet.findVariable(variablePath);
assertThat(var, is(notNullValue()));
dataSet.readArrays(ImmutableList.of(var));
}
}