Retrieving 1D arrays from NetCDF char array variables in HDF5 files using Java

Using the Java API for NetCDF, I have an HDF5 file with an array of type CHAR, which according to the documentation is similar to strings containing only ASCII characters: "The char type contains uninterpreted characters, one character per byte. Typically these contain 7-bit ASCII characters." In HDFView, an example of one of thee values in the array is "13".

I know that for an array of integers I can get them all as a Java array like this:

int[] data = (int[]) netCDFArray.get1DJavaArray(int.class);

But how do I get back an array of this CHAR type? Unfortunately the documentation I referenced is of no help.

The following cannot be correct, because some of the items are more than single characters:

char[] data = (char[]) netCDFArray.get1DJavaArray(char.class);

The following attempts all throw a ForbiddenConversionException:

char[] data = (char[]) netCDFArray.get1DJavaArray(char.class);
char[][] data = (char[][]) netCDFArray.get1DJavaArray(char[].class);
String[] data = (String[]) netCDFArray.get1DJavaArray(String.class);

If I use netCDFArray.toString() I see my array of strings, because ArrayChar uses a StringIterator. I too could use such an iterator and do something with each string, I suppose. But I don't need to get an int iterator to retrieve integers. How can I efficiently retrieve all strings of a CHAR type in one go, analogous to how I can retrieve integers (see above)? I would be content with retrieving a Java Strings[], CharSequence[], or char[][].


It seems that a NetCDF string of type CHAR is represented logically as a two-dimensional array of type char, but internally it is stored as a single array of type char. Therefore, the most efficient way to retrieve the data is the following:

char[] data = (char[]) netCDFArray.get1DJavaArray(char.class);

One must then extract the individual strings from this single array:

assert netCDFArray.getRank()==2 : "Expected a two-dimensional logical array of chars.";
int stringLength=netCDFArray.getShape()[1];
int stringCount=netCDFArray.getShape()[0];
//iterate through stringCount positions of stringLength length

The added twist is that apparently strings are stored as zero-terminated, that is, the supposedly fixed-length strings apparently may be variable-length strings of less than stringLength using ASCII 0 as a delimiter. I derived this from the code; I couldn't find it in the documentation.

Need Your Help

Using XPATH to compute the minimum attribute element and add it to another attribute

xslt attributes minimum

My question is similar to this one, in which the author asks how to find the minimum attribute of an set of elements. I'm a beginner at xpath and I'm having trouble taking that answer and adding i...

Why this protected member is not visible in the subclass

java inheritance protected

I have a problem understanding protected members inheritance and visibility.