Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.slf4j.LoggerFactory;

import ucar.ma2.Array;
import ucar.ma2.DataType;
import ucar.ma2.Index;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.Variable;
Expand Down Expand Up @@ -440,30 +441,13 @@ public Number get(int... coords) {
* Do the actual data read.
*/
Number val = null;
switch (arrLocal.getDataType()) {
case BYTE:
if (arrLocal.getDataType() == DataType.BYTE) {
val = arrLocal.getByte(index);
while (val.doubleValue() < var.getValidMin()) {
val = val.intValue() + 256;
}
break;
case DOUBLE:
val = arrLocal.getDouble(index);
break;
case FLOAT:
val = arrLocal.getFloat(index);
break;
case INT:
val = arrLocal.getInt(index);
break;
case LONG:
val = arrLocal.getLong(index);
break;
case SHORT:
val = arrLocal.getShort(index);
break;
default:
break;
} else if (arrLocal.getDataType().isNumeric()) {
val = (Number) arrLocal.getObject(index);
}

if (isMissing(val)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package uk.ac.rdg.resc.edal.dataset.cdm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -74,25 +74,40 @@ public void testReadVariableWithOffset() throws IOException, DataReadingExceptio
NetcdfDataset nc = NetcdfDatasetAggregator.getDataset(url.getPath());

try (CdmGridDataSource datasource = new CdmGridDataSource(nc)) {
int xmin = 0;
int xmax = 2;
int ymin = 0;
int ymax = 2;

Array4D<Number> variableWithOffset =
datasource.read("variableWithOffset", 0, 0, 0, 0, ymin, ymax, xmin, xmax);
Array4D<Number> variableWithoutOffset =
datasource.read("variableWithoutOffset", 0, 0, 0, 0, ymin, ymax, xmin, xmax);

int i = 0;
Iterator<Number> variableWithOffsetIterator = variableWithOffset.iterator();
Iterator<Number> variableWithoutOffsetIterator = variableWithoutOffset.iterator();

while (variableWithOffsetIterator.hasNext()) {
float expectedValue = i++ * 1.25f;
assertEquals(expectedValue, variableWithOffsetIterator.next().floatValue(), delta);
assertEquals(expectedValue, variableWithoutOffsetIterator.next().floatValue(), delta);
}
float[] expectedValues = new float[] {0f, 1.25f, 2.5f, 3.75f, 5f, 6.25f, 7.5f, 8.75f, 10f};

checkVariable(datasource, "variableWithOffset", expectedValues);
checkVariable(datasource, "variableWithoutOffset", expectedValues);
}
}

@Test
public void testReadUnisgnedVariables() throws IOException, DataReadingException {
URL url = this.getClass().getResource("/testUnsignedTypes.nc");
NetcdfDataset nc = NetcdfDatasetAggregator.getDataset(url.getPath());

try (CdmGridDataSource datasource = new CdmGridDataSource(nc)) {
float[] expectedValues = new float[]{0, 1, 2, 3, 4, 5, 6, 7, 8};

checkVariable(datasource, "variableUbyte", expectedValues);
checkVariable(datasource, "variableUshort", expectedValues);
checkVariable(datasource, "variableUint", expectedValues);
}
}

private void checkVariable(CdmGridDataSource datasource, String variableName, float[] expectedValues)
throws IOException {
int xmin = 0;
int xmax = 2;
int ymin = 0;
int ymax = 2;

Array4D<Number> variable = datasource.read(variableName, 0, 0, 0, 0, ymin, ymax, xmin, xmax);
int i = 0;

for (Number number : variable) {
assertNotNull(number);
assertEquals(expectedValues[i++], number.floatValue(), delta);
}
}
}
Binary file added cdm/src/test/resources/testUnsignedTypes.nc
Binary file not shown.