Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to parse variable length char element from ncml #1236

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading