Skip to content

Commit

Permalink
Merge pull request #1236 from jmasson-l3h/jmasson-netcdf-java-1235
Browse files Browse the repository at this point in the history
Added support to parse variable length char element from ncml
  • Loading branch information
mnlerman authored Sep 20, 2023
2 parents 8935ad7 + 0f8ad82 commit 7380a93
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/internal/ncml/NcmlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1306,9 +1306,32 @@ private void readValues(Variable.Builder v, DataType dtype, Element varElem, Ele

if (dtype == DataType.CHAR) {
int nhave = values.length();
char[] data = new char[nhave];
for (int i = 0; i < nhave; i++) {
data[i] = values.charAt(i);
int[] theDims = Dimensions.makeShape(v.getDimensions());
int totalSize = 1;
for (int i = 0; i < theDims.length; i++) {
totalSize *= theDims[i];
}

char[] data = new char[totalSize];
if (nhave == totalSize) {
for (int i = 0; i < totalSize; i++) {
data[i] = values.charAt(i);
}
}
// special case when when size of the input does not equal the number of elements * max size
// get the values as tokens and pad '0' as needed to reach the correct size
else {
// per specification the last dimension is the largest size an element can be
int maxSize = theDims[theDims.length - 1];
List<String> valList = getTokens(values, sep);
int startingIndex = 0;
for (String value : valList) {
for (int i = 0; i < value.length() && i < maxSize; i++) {
data[startingIndex + i] = value.charAt(i);
}
// move to the next word, all unset chars are left se to '0'
startingIndex += maxSize;
}
}
Array dataArray = Array.factory(DataType.CHAR, Dimensions.makeShape(v.getDimensions()), data);
v.setCachedData(dataArray, true);
Expand Down

0 comments on commit 7380a93

Please sign in to comment.