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"); }
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"); }
private void doWrite2(NetcdfFileWriteable ncfile, String varName) throws Exception { Variable v = ncfile.findVariable(varName); int[] w = getWeights(v); int[] shape = v.getShape(); Array aa = Array.factory(v.getDataType().getPrimitiveClassType(), shape); Index ima = aa.getIndex(); for (int i = 0; i < shape[0]; i++) { for (int j = 0; j < shape[1]; j++) { aa.setDouble(ima.set(i, j), (double) (i * w[0] + j * w[1])); } } ncfile.write(varName, aa); }
public void readDouble() throws Exception { Variable t1 = null; assert (null != (t1 = ncfileRead.findVariable("t1"))); assert (t1.getDataType() == DataType.DOUBLE); Attribute att = t1.findAttribute(CDM.SCALE_FACTOR); assert (null != att); assert (!att.isArray()); assert (1 == att.getLength()); assert (2.0 == att.getNumericValue().doubleValue()); assert (DataType.DOUBLE == att.getDataType()); // read Array A = t1.read(); int i, j; Index ima = A.getIndex(); int[] shape = A.getShape(); for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getDouble(ima.set(i, j)) == (double) (i * 10.0 + j)); } } assert (null != (t1 = dsRead.findVariable("t1"))); assert t1 instanceof VariableEnhanced; VariableEnhanced dsVar = (VariableEnhanced) t1; assert (dsVar.getDataType() == DataType.DOUBLE); A = dsVar.read(); ima = A.getIndex(); shape = A.getShape(); for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { assert (A.getDouble(ima.set(i, j)) == (2.0 * (i * 10.0 + j) + 77.0)); } } assert (null == t1.findAttribute(CDM.SCALE_FACTOR)); assert (null == t1.findAttribute("add_offset")); System.out.println("**************TestStandardVar ReadDouble"); }
public void readDoubleMissing() throws Exception { VariableDS v = null; assert (null != (v = (VariableDS) dsRead.findVariable("m1"))); assert (v.getDataType() == DataType.DOUBLE); Array A = v.read(); Index ima = A.getIndex(); double val = A.getFloat(ima.set(1, 1)); assert Double.isNaN(val); assert v.isMissing(val); // reread with useNans off v.setUseNaNs(false); v.createNewCache(); A = v.read(); ima = A.getIndex(); val = A.getFloat(ima.set(1, 1)); assert TestAll.closeEnough(val, -999.99) : val; assert v.isMissing(val); }
public void readSlice(NetcdfFile ncfile, int[] origin, int[] shape, String name) throws IOException, InvalidRangeException { Variable v = ncfile.findVariable(name); Array data = v.read(origin, shape); assert data.getRank() == 3; assert data.getSize() == shape[0] * shape[1] * shape[2]; assert data.getShape()[0] == shape[0] : data.getShape()[0] + " " + shape[0]; assert data.getShape()[1] == shape[1]; assert data.getShape()[2] == shape[2]; assert data.getElementType() == double.class; Index tIndex = data.getIndex(); for (int i = 0; i < shape[0]; i++) for (int j = 0; j < shape[1]; j++) for (int k = 0; k < shape[2]; k++) { double val = data.getDouble(tIndex.set(i, j, k)); // System.out.println(" "+val); assert TestUtils.close(val, 100 * (i + origin[0]) + 10 * j + k) : val; } }
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 testReadData(NetcdfFile ncfile, String name) throws IOException { Variable v = ncfile.findVariable(name); assert null != v; assert v.getShortName().equals(name); assert v.getRank() == 3; assert v.getSize() == 36 : v.getSize(); assert v.getShape()[0] == 3; assert v.getShape()[1] == 3; assert v.getShape()[2] == 4; assert v.getDataType() == DataType.DOUBLE; assert !v.isCoordinateVariable(); assert v.getDimension(0) == ncfile.findDimension("time"); assert v.getDimension(1) == ncfile.findDimension("lat"); assert v.getDimension(2) == ncfile.findDimension("lon"); Array data = v.read(); assert data.getRank() == 3; assert data.getSize() == 36; assert data.getShape()[0] == 3; assert data.getShape()[1] == 3; assert data.getShape()[2] == 4; assert data.getElementType() == double.class; int[] shape = data.getShape(); Index tIndex = data.getIndex(); for (int i = 0; i < shape[0]; i++) for (int j = 0; j < shape[1]; j++) for (int k = 0; k < shape[2]; k++) { double val = data.getDouble(tIndex.set(i, j, k)); // System.out.println(" "+val); assert TestUtils.close(val, 100 * i + 10 * j + k) : val; } }
public void testWritePermute() throws Exception { NetcdfFileWriteable ncfile = new NetcdfFileWriteable(); ncfile.setName(TestLocal.cdmTestDataDir + "permuteTest.nc"); // define dimensions Dimension xDim = ncfile.addDimension("x", 3); Dimension yDim = ncfile.addDimension("y", 5); Dimension zDim = ncfile.addDimension("z", 4); Dimension tDim = ncfile.addDimension("time", 2); // define Variables ncfile.addVariable("time", double.class, new Dimension[] {tDim}); ncfile.addVariableAttribute("time", "units", "secs since 1-1-1 00:00"); ncfile.addVariable("z", double.class, new Dimension[] {zDim}); ncfile.addVariableAttribute("z", "units", "meters"); ncfile.addVariableAttribute("z", "positive", "up"); ncfile.addVariable("y", double.class, new Dimension[] {yDim}); ncfile.addVariableAttribute("y", "units", "degrees_north"); ncfile.addVariable("x", double.class, new Dimension[] {xDim}); ncfile.addVariableAttribute("x", "units", "degrees_east"); ncfile.addVariable("tzyx", double.class, new Dimension[] {tDim, zDim, yDim, xDim}); ncfile.addVariableAttribute("tzyx", "units", "K"); ncfile.addVariable("tzxy", double.class, new Dimension[] {tDim, zDim, xDim, yDim}); ncfile.addVariableAttribute("tzxy", "units", "K"); ncfile.addVariable("tyxz", double.class, new Dimension[] {tDim, yDim, xDim, zDim}); ncfile.addVariableAttribute("tyxz", "units", "K"); ncfile.addVariable("txyz", double.class, new Dimension[] {tDim, xDim, yDim, zDim}); ncfile.addVariableAttribute("txyz", "units", "K"); ncfile.addVariable("zyxt", double.class, new Dimension[] {zDim, yDim, xDim, tDim}); ncfile.addVariableAttribute("zyxt", "units", "K"); ncfile.addVariable("zxyt", double.class, new Dimension[] {zDim, xDim, yDim, tDim}); ncfile.addVariableAttribute("zxyt", "units", "K"); ncfile.addVariable("yxzt", double.class, new Dimension[] {yDim, xDim, zDim, tDim}); ncfile.addVariableAttribute("yxzt", "units", "K"); ncfile.addVariable("xyzt", double.class, new Dimension[] {xDim, yDim, zDim, tDim}); ncfile.addVariableAttribute("xyzt", "units", "K"); // missing one dimension ncfile.addVariable("zyx", double.class, new Dimension[] {zDim, yDim, xDim}); ncfile.addVariable("txy", double.class, new Dimension[] {tDim, xDim, yDim}); ncfile.addVariable("yxz", double.class, new Dimension[] {yDim, xDim, zDim}); ncfile.addVariable("xzy", double.class, new Dimension[] {xDim, zDim, yDim}); ncfile.addVariable("yxt", double.class, new Dimension[] {yDim, xDim, tDim}); ncfile.addVariable("xyt", double.class, new Dimension[] {xDim, yDim, tDim}); ncfile.addVariable("xyz", double.class, new Dimension[] {xDim, yDim, zDim}); // missing two dimension ncfile.addVariable("yx", double.class, new Dimension[] {yDim, xDim}); ncfile.addVariable("xy", double.class, new Dimension[] {xDim, yDim}); ncfile.addVariable("yz", double.class, new Dimension[] {yDim, zDim}); ncfile.addVariable("xz", double.class, new Dimension[] {xDim, zDim}); ncfile.addVariable("yt", double.class, new Dimension[] {yDim, tDim}); ncfile.addVariable("xt", double.class, new Dimension[] {xDim, tDim}); ncfile.addVariable("ty", double.class, new Dimension[] {tDim, yDim}); ncfile.addVariable("tx", double.class, new Dimension[] {tDim, xDim}); // add global attributes ncfile.addGlobalAttribute("Convention", "COARDS"); // create the file try { ncfile.create(); } catch (IOException e) { System.err.println("ERROR creating file"); assert (false); } // write time data int len = tDim.getLength(); ArrayDouble A = new ArrayDouble.D1(len); Index ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3600)); int[] origin = new int[1]; try { ncfile.write("time", origin, A); } catch (IOException e) { System.err.println("ERROR writing time"); assert (false); } // write z data len = zDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 10)); try { ncfile.write("z", origin, A); } catch (IOException e) { System.err.println("ERROR writing z"); assert (false); } // write y data len = yDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3)); try { ncfile.write("y", origin, A); } catch (IOException e) { System.err.println("ERROR writing y"); assert (false); } // write x data len = xDim.getLength(); A = new ArrayDouble.D1(len); ima = A.getIndex(); for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 5)); try { ncfile.write("x", origin, A); } catch (IOException e) { System.err.println("ERROR writing x"); assert (false); } // write tzyx data doWrite4(ncfile, "tzyx"); doWrite4(ncfile, "tzxy"); doWrite4(ncfile, "txyz"); doWrite4(ncfile, "tyxz"); doWrite4(ncfile, "zyxt"); doWrite4(ncfile, "zxyt"); doWrite4(ncfile, "xyzt"); doWrite4(ncfile, "yxzt"); doWrite3(ncfile, "zyx"); doWrite3(ncfile, "txy"); doWrite3(ncfile, "yxz"); doWrite3(ncfile, "xzy"); doWrite3(ncfile, "yxt"); doWrite3(ncfile, "xyt"); doWrite3(ncfile, "yxt"); doWrite3(ncfile, "xyz"); doWrite2(ncfile, "yx"); doWrite2(ncfile, "xy"); doWrite2(ncfile, "yz"); doWrite2(ncfile, "xz"); doWrite2(ncfile, "yt"); doWrite2(ncfile, "xt"); doWrite2(ncfile, "ty"); doWrite2(ncfile, "tx"); if (show) System.out.println("ncfile = " + ncfile); // all done try { ncfile.close(); } catch (IOException e) { System.err.println("ERROR writing file"); assert (false); } System.out.println("*****************Test Write done"); }
public void testNC3Read() throws IOException { NetcdfFile ncfile = TestDir.openFileLocal("testWrite.nc"); assert (null != ncfile.findDimension("lat")); assert (null != ncfile.findDimension("lon")); Variable temp = null; assert (null != (temp = ncfile.findVariable("temperature"))); // read entire array Array A; try { A = temp.read(); } catch (IOException e) { System.err.println("ERROR reading file"); assert (false); return; } assert (A.getRank() == 2); int i, j; Index ima = A.getIndex(); int[] shape = A.getShape(); assert shape[0] == 64; assert shape[1] == 128; for (i = 0; i < shape[0]; i++) { for (j = 0; j < shape[1]; j++) { double dval = A.getDouble(ima.set(i, j)); assert (dval == (double) (i * 1000000 + j * 1000)) : dval; } } // read part of array int[] origin2 = new int[2]; int[] shape2 = new int[2]; shape2[0] = 1; shape2[1] = temp.getShape()[1]; try { A = temp.read(origin2, shape2); } catch (InvalidRangeException e) { System.err.println("ERROR reading file " + e); assert (false); return; } catch (IOException e) { System.err.println("ERROR reading file"); assert (false); return; } assert (A.getRank() == 2); for (j = 0; j < shape2[1]; j++) { assert (A.getDouble(ima.set(0, j)) == (double) (j * 1000)); } // rank reduction Array Areduce = A.reduce(); Index ima2 = Areduce.getIndex(); assert (Areduce.getRank() == 1); for (j = 0; j < shape2[1]; j++) { assert (Areduce.getDouble(ima2.set(j)) == (double) (j * 1000)); } // read char variable Variable c = null; assert (null != (c = ncfile.findVariable("svar"))); try { A = c.read(); } catch (IOException e) { assert (false); } assert (A instanceof ArrayChar); ArrayChar ac = (ArrayChar) A; String val = ac.getString(ac.getIndex()); assert val.equals("Testing 1-2-3") : val; // System.out.println( "val = "+ val); // read char variable 2 Variable c2 = null; assert (null != (c2 = ncfile.findVariable("svar2"))); try { A = c2.read(); } catch (IOException e) { assert (false); } assert (A instanceof ArrayChar); ArrayChar ac2 = (ArrayChar) A; assert (ac2.getString().equals("Two pairs of ladies stockings!")); // read String Array Variable c3 = null; assert (null != (c3 = ncfile.findVariable("names"))); try { A = c3.read(); } catch (IOException e) { assert (false); } assert (A instanceof ArrayChar); ArrayChar ac3 = (ArrayChar) A; ima = ac3.getIndex(); assert (ac3.getString(ima.set(0)).equals("No pairs of ladies stockings!")); assert (ac3.getString(ima.set(1)).equals("One pair of ladies stockings!")); assert (ac3.getString(ima.set(2)).equals("Two pairs of ladies stockings!")); // read String Array - 2 Variable c4 = null; assert (null != (c4 = ncfile.findVariable("names2"))); try { A = c4.read(); } catch (IOException e) { assert (false); } assert (A instanceof ArrayChar); ArrayChar ac4 = (ArrayChar) A; ima = ac4.getIndex(); assert (ac4.getString(0).equals("0 pairs of ladies stockings!")); assert (ac4.getString(1).equals("1 pair of ladies stockings!")); assert (ac4.getString(2).equals("2 pairs of ladies stockings!")); // System.out.println( "ncfile = "+ ncfile); ncfile.close(); System.out.println("**************TestRead done"); }
public void testWriteStandardVar() throws Exception { NetcdfFileWriteable ncfile = new NetcdfFileWriteable(filename, false); // define dimensions Dimension latDim = ncfile.addDimension("lat", 2); Dimension lonDim = ncfile.addDimension("lon", 3); ArrayList dims = new ArrayList(); dims.add(latDim); dims.add(lonDim); // case 1 ncfile.addVariable("t1", DataType.DOUBLE, dims); ncfile.addVariableAttribute("t1", CDM.SCALE_FACTOR, new Double(2.0)); ncfile.addVariableAttribute("t1", "add_offset", new Double(77.0)); // case 2 ncfile.addVariable("t2", DataType.BYTE, dims); ncfile.addVariableAttribute("t2", CDM.SCALE_FACTOR, new Short((short) 2)); ncfile.addVariableAttribute("t2", "add_offset", new Short((short) 77)); // case 3 ncfile.addVariable("t3", DataType.BYTE, dims); ncfile.addVariableAttribute("t3", "_FillValue", new Byte((byte) 255)); // case 4 ncfile.addVariable("t4", DataType.SHORT, dims); ncfile.addVariableAttribute("t4", CDM.MISSING_VALUE, new Short((short) -9999)); // case 5 ncfile.addVariable("t5", DataType.SHORT, dims); ncfile.addVariableAttribute("t5", CDM.MISSING_VALUE, new Short((short) -9999)); ncfile.addVariableAttribute("t5", CDM.SCALE_FACTOR, new Short((short) 2)); ncfile.addVariableAttribute("t5", "add_offset", new Short((short) 77)); // case 1 ncfile.addVariable("m1", DataType.DOUBLE, dims); ncfile.addVariableAttribute("m1", CDM.MISSING_VALUE, -999.99); // create the file ncfile.create(); // write t1 ArrayDouble A = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength()); int i, j; Index ima = A.getIndex(); // write for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) A.setDouble(ima.set(i, j), (double) (i * 10.0 + j)); int[] origin = new int[2]; ncfile.write("t1", origin, A); // write t2 ArrayByte Ab = new ArrayByte.D2(latDim.getLength(), lonDim.getLength()); ima = Ab.getIndex(); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) Ab.setByte(ima.set(i, j), (byte) (i * 10 + j)); ncfile.write("t2", origin, Ab); // write t3 ncfile.write("t3", origin, Ab); // write t4 Array As = new ArrayShort.D2(latDim.getLength(), lonDim.getLength()); ima = As.getIndex(); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) As.setShort(ima.set(i, j), (short) (i * 10 + j)); ncfile.write("t4", origin, As); As.setShort(ima.set(0, 0), (short) -9999); ncfile.write("t5", origin, As); // write m1 ArrayDouble.D2 Ad = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength()); for (i = 0; i < latDim.getLength(); i++) for (j = 0; j < lonDim.getLength(); j++) Ad.setDouble(ima.set(i, j), (double) (i * 10.0 + j)); Ad.set(1, 1, -999.99); ncfile.write("m1", new int[2], Ad); // all done ncfile.close(); System.out.println("**************TestStandardVar Write done"); }
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"); }