public void readShortMissing() throws Exception { Variable v = null; assert (null != (v = ncfileRead.findVariable("t4"))); assert (v.getDataType() == DataType.SHORT); // default use of missing_value assert (null != (v = dsRead.findVariable("t4"))); assert v instanceof VariableEnhanced; assert v instanceof VariableDS; VariableDS vs = (VariableDS) v; assert (vs.getDataType() == DataType.SHORT); Attribute att = vs.findAttribute(CDM.MISSING_VALUE); assert (null != att); assert (!att.isArray()); assert (1 == att.getLength()); System.out.println("missing_value = " + att.getNumericValue().shortValue()); assert (((short) -9999) == att.getNumericValue().shortValue()); assert (DataType.SHORT == att.getDataType()); assert (vs.hasMissing()); assert (vs.hasMissingValue()); assert (vs.isMissing((double) ((short) -9999))); assert (vs.isMissingValue((double) ((short) -9999))); Array A = vs.read(); Index ima = A.getIndex(); int[] shape = A.getShape(); int i, j; for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getFloat(ima.set(i, j)) == (i * 10 + j)); } } // turn off missing data vs.setMissingDataIsMissing(false); assert (vs.getDataType() == DataType.SHORT); assert (!vs.hasMissing()); assert (vs.hasMissingValue()); assert (!vs.isMissing((double) ((short) -9999))); assert (vs.isMissingValue((double) ((short) -9999))); vs.setMissingDataIsMissing(true); assert (vs.hasMissing()); assert (vs.isMissing((double) ((short) -9999))); System.out.println("**************TestStandardVar Read readShortMissing"); }
public void readByte2Short() throws Exception { Variable t2 = null; assert (null != (t2 = ncfileRead.findVariable("t2"))); assert (t2.getDataType() == DataType.BYTE); Attribute att = t2.findAttribute(CDM.SCALE_FACTOR); assert (null != att); assert (!att.isArray()); assert (1 == att.getLength()); assert (2 == att.getNumericValue().doubleValue()); assert (DataType.SHORT == att.getDataType()); assert (null != (t2 = dsRead.findVariable("t2"))); assert t2 instanceof VariableEnhanced; VariableDS vs = (VariableDS) t2; assert (vs.getDataType() == DataType.SHORT) : vs.getDataType(); assert (!vs.hasMissing()); Array A = vs.read(); assert (A.getElementType() == short.class) : A.getElementType(); Index ima = A.getIndex(); int[] shape = A.getShape(); int i, j; for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getShort(ima.set(i, j)) == (2 * (i * 10 + j) + 77)); } } System.out.println("**************TestStandardVar readByte2Short"); }
/** * Convert (in place) all values in the given array that are considered as "missing" to Float.NaN, * according to isMissingData(val). * * @param values input array * @return input array, with missing values converted to NaNs. */ public float[] setMissingToNaN(float[] values) { if (!vs.hasMissing()) return values; final int length = values.length; for (int i = 0; i < length; i++) { double value = values[i]; if (vs.isMissing(value)) values[i] = Float.NaN; } return values; }
public void readByte() throws Exception { Variable v = null; assert (null != (v = ncfileRead.findVariable("t3"))); assert (v.getDataType() == DataType.BYTE); assert (null != (v = dsRead.findVariable("t3"))); assert v instanceof VariableEnhanced; assert v instanceof VariableDS; VariableDS vs = (VariableDS) v; assert (vs.getDataType() == DataType.BYTE); Attribute att = vs.findAttribute("_FillValue"); assert (null != att); assert (!att.isArray()); assert (1 == att.getLength()); System.out.println("_FillValue = " + att.getNumericValue().byteValue()); assert (((byte) 255) == att.getNumericValue().byteValue()); assert (DataType.BYTE == att.getDataType()); assert (vs.hasMissing()); assert (vs.hasFillValue()); assert (vs.isMissing((double) ((byte) 255))); assert (vs.isFillValue((double) ((byte) 255))); Array A = vs.read(); assert (A.getElementType() == byte.class) : A.getElementType(); Index ima = A.getIndex(); int[] shape = A.getShape(); int i, j; for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getFloat(ima.set(i, j)) == (i * 10 + j)); } } System.out.println("**************TestStandardVar ReadByte"); }
public void readShort2FloatMissing() throws Exception { Variable v = null; assert (null != (v = ncfileRead.findVariable("t5"))); assert (v.getDataType() == DataType.SHORT); // standard convert with missing data assert (null != (v = dsRead.findVariable("t5"))); assert v instanceof VariableEnhanced; assert v instanceof VariableDS; VariableDS vs = (VariableDS) v; assert (vs.getDataType() == DataType.FLOAT); assert (vs.hasMissing()); assert (vs.hasMissingValue()); double mv = 2 * (-9999) + 77; assert (vs.isMissing((double) mv)); assert (vs.isMissingValue((double) mv)); Array A = vs.read(); Index ima = A.getIndex(); int[] shape = A.getShape(); int i, j; assert (vs.isMissing(A.getFloat(ima.set(0, 0)))); for (i = 0; i < shape[0]; i++) { for (j = 1; j < shape[1]; j++) { float val = A.getFloat(ima.set(i, j)); float want = 2 * (i * 10 + j) + 77; if (val != want) System.out.println(i + " " + j + " " + val + " " + want); assert (val == want); } } // useNaNs vs.setUseNaNs(true); assert (vs.getDataType() == DataType.FLOAT); assert (vs.hasMissing()); assert (vs.hasMissingValue()); double mv2 = 2 * (-9999) + 77; assert (vs.isMissing((double) mv2)); assert (vs.isMissingValue((double) mv2)); Array A2 = vs.read(); Index ima2 = A2.getIndex(); int[] shape2 = A2.getShape(); double mval = A2.getFloat(ima2.set(0, 0)); assert vs.isMissing(mval); assert Double.isNaN(mval); for (i = 0; i < shape2[0]; i++) { for (j = 1; j < shape2[1]; j++) { float val = A2.getFloat(ima2.set(i, j)); float want = 2 * (i * 10 + j) + 77; if (val != want) System.out.println(i + " " + j + " " + val + " " + want); assert (val == want) : val + " != " + want; } } assert (null == vs.findAttribute(CDM.SCALE_FACTOR)); assert (null == vs.findAttribute("add_offset")); assert (null == vs.findAttribute(CDM.MISSING_VALUE)); System.out.println("**************TestStandardVar Read readShort2FloatMissing"); }
/** true if there may be missing data, see VariableDS.hasMissing() */ public boolean hasMissingData() { return vs.hasMissing(); }