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 testAggCoordVar(NetcdfFile ncfile) { Variable time = ncfile.findVariable("time"); assert null != time; assert time.getShortName().equals("time"); assert time.getRank() == 1 : time.getRank(); assert time.getShape()[0] == 3; assert time.getDataType() == DataType.INT; assert time.getDimension(0) == ncfile.findDimension("time"); try { Array data = time.read(); assert (data instanceof ArrayInt.D1) : data.getClass().getName(); ArrayInt.D1 dataI = (ArrayInt.D1) data; assert dataI.get(0) == 0; assert dataI.get(1) == 10; assert dataI.get(2) == 99; } catch (IOException io) { io.printStackTrace(); assert false; } }
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 testAggCoordVar2(NetcdfFile ncfile) { Variable time = ncfile.findVariable("time"); assert null != time; assert time.getShortName().equals("time"); assert time.getRank() == 1 : time.getRank(); assert time.getShape()[0] == 3; assert time.getDataType() == DataType.INT; assert time.getDimension(0) == ncfile.findDimension("time"); try { Array data = time.read(); assert (data instanceof ArrayInt); IndexIterator dataI = data.getIndexIterator(); assert dataI.getIntNext() == 0 : dataI.getIntCurrent(); assert dataI.getIntNext() == 1 : dataI.getIntCurrent(); assert dataI.getIntNext() == 2 : dataI.getIntCurrent(); } catch (IOException io) { io.printStackTrace(); assert false; } }
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); }
private int[] getWeights(Variable v) { int rank = v.getRank(); int[] w = new int[rank]; for (int n = 0; n < rank; n++) { Dimension dim = v.getDimension(n); String dimName = dim.getName(); if (dimName.equals("time")) w[n] = 1000; if (dimName.equals("z")) w[n] = 100; if (dimName.equals("y")) w[n] = 10; if (dimName.equals("x")) w[n] = 1; } return w; }
private double computeFitness(IGPProgram a_program, Variable vx) { double error = 0.0f; Object[] noargs = new Object[0]; // Initialize local stores. // ------------------------ a_program.getGPConfiguration().clearStack(); a_program.getGPConfiguration().clearMemory(); // Compute fitness for each program. // --------------------------------- for (int i = 2; i < 15; i++) { for (int j = 0; j < a_program.size(); j++) { vx.set(new Integer(i)); try { try { // Only evaluate after whole GP program was run. // --------------------------------------------- if (j == a_program.size() - 1) { double result = a_program.execute_int(j, noargs); error += Math.abs(result - fib_iter(i)); } else { a_program.execute_void(j, noargs); } } catch (IllegalStateException iex) { error = Double.MAX_VALUE / 2; break; } } catch (ArithmeticException ex) { System.out.println("Arithmetic Exception with x = " + i); System.out.println(a_program.getChromosome(j)); throw ex; } } } return error; }
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 testAggCoordVarScan(NetcdfFile ncfile) throws IOException { Variable time = ncfile.findVariable("time"); assert null != time; assert time.getShortName().equals("time"); assert time.getRank() == 1 : time.getRank(); assert time.getShape()[0] == 3; assert time.getDataType() == DataType.INT : time.getDataType(); assert time.getDimension(0) == ncfile.findDimension("time"); int count = 0; Array data = time.read(); assert (data instanceof ArrayInt); while (data.hasNext()) { int val = data.nextInt(); assert val == count * 10 : val + "!=" + count * 10; count++; } }
public void testAggCoordVar3(NetcdfFile ncfile) throws IOException { Variable time = ncfile.findVariable("time"); assert null != time; assert time.getShortName().equals("time"); assert time.getRank() == 1 : time.getRank(); assert time.getShape()[0] == 3; assert time.getDataType() == DataType.DOUBLE : time.getDataType(); assert time.getDimension(0) == ncfile.findDimension("time"); Array data = time.read(); assert (data instanceof ArrayDouble); IndexIterator dataI = data.getIndexIterator(); double val = dataI.getDoubleNext(); assert Misc.closeEnough(val, 0.0) : val; assert Misc.closeEnough(dataI.getDoubleNext(), 10.0) : dataI.getDoubleCurrent(); assert Misc.closeEnough(dataI.getDoubleNext(), 99.0) : dataI.getDoubleCurrent(); }
public void testCoordVar(NetcdfFile ncfile) { Variable lat = ncfile.findVariable("lat"); assert null != lat; assert lat.getShortName().equals("lat"); assert lat.getRank() == 1; assert lat.getSize() == 3; assert lat.getShape()[0] == 3; assert lat.getDataType() == DataType.FLOAT; assert !lat.isUnlimited(); assert lat.getDimension(0).equals(ncfile.findDimension("lat")); Attribute att = lat.findAttribute("units"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("degrees_north"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; try { Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == 3; assert data.getShape()[0] == 3; assert data.getElementType() == float.class; IndexIterator dataI = data.getIndexIterator(); assert TestUtils.close(dataI.getDoubleNext(), 41.0); assert TestUtils.close(dataI.getDoubleNext(), 40.0); assert TestUtils.close(dataI.getDoubleNext(), 39.0); } catch (IOException io) { } }
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 testAggCoordVarNoCoordsDir(NetcdfFile ncfile) throws IOException { Variable time = ncfile.findVariable("time"); assert null != time; assert time.getShortName().equals("time"); assert time.getRank() == 1 : time.getRank(); assert time.getShape()[0] == 3; assert time.getDataType() == DataType.STRING : time.getDataType(); assert time.getDimension(0) == ncfile.findDimension("time"); Array data = time.read(); assert (data instanceof ArrayObject); IndexIterator dataI = data.getIndexIterator(); String coordName = (String) dataI.getObjectNext(); assert coordName.equals("time0Dir.nc") : coordName; coordName = (String) dataI.getObjectNext(); assert coordName.equals("time1Dir.nc") : coordName; coordName = (String) dataI.getObjectNext(); assert coordName.equals("time2Dir.nc") : coordName; }
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 testRandomInitialize_0() throws Exception { Variable vx; Class[] types = {CommandGene.VoidClass, CommandGene.VoidClass, CommandGene.IntegerClass}; Class[][] argTypes = {{}, {}, {}}; int[] minDepths = new int[] {3, 4, 1}; int[] maxDepths = new int[] {3, 4, 1}; CommandGene[][] nodeSets = { { CMD_SUB_V_V, // 0 CMD_CONST1, // 1 new StoreTerminal(m_gpconf, "mem0", CommandGene.IntegerClass), // 2 new StoreTerminal(m_gpconf, "mem1", CommandGene.IntegerClass), // 3 }, { vx = Variable.create(m_gpconf, "X", CommandGene.IntegerClass), // 0 new AddAndStore(m_gpconf, CommandGene.IntegerClass, "mem2"), // 1 CMD_FOR, // 2 new TransferMemory(m_gpconf, "mem2", "mem1"), // 3 new TransferMemory(m_gpconf, "mem1", "mem0"), // 4 new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem0"), // 5 new ReadTerminal(m_gpconf, CommandGene.IntegerClass, "mem1"), // 6 CMD_SUB_V_V_V, // 7 new Increment(m_gpconf, CommandGene.IntegerClass, -1), // 8 }, {} }; // Add commands working with internal memory. // ------------------------------------------ nodeSets[2] = CommandFactory.createReadOnlyCommands( nodeSets[2], m_gpconf, CommandGene.IntegerClass, "mem", 1, 2, !true); // Execute the functionality to test. // ---------------------------------- rn.setNextIntSequence(new int[] {0, 2, 1, 3, 1, 2, 8, 0, 7, 1, 5, 6, 4, 3}); m_gpconf.setPopulationSize(1); GPGenotype gen = GPGenotype.randomInitialGenotype( m_gpconf, types, argTypes, nodeSets, minDepths, maxDepths, 200, new boolean[] {true, true, false}, false); GPPopulation pop = gen.getGPPopulation(); assertEquals(m_gpconf.getPopulationSize(), pop.size()); // Evaluate program 1 // ------------------ IGPProgram p = pop.getGPProgram(0); assertEquals(8, p.getChromosome(0).size()); assertEquals(CMD_SUB_V_V, p.getChromosome(0).getNode(0)); assertEquals(StoreTerminal.class, p.getChromosome(0).getNode(1).getClass()); assertSame(CMD_CONST1, p.getChromosome(0).getNode(2)); assertEquals(SubProgram.class, p.getChromosome(0).getNode(3).getClass()); // assertEquals(StoreTerminal.class, p.getChromosome(0).getNode(3).getClass()); assertSame(CMD_CONST1, p.getChromosome(0).getNode(4)); // Evaluate program 2 // ------------------ int node = 0; assertEquals(9, p.getChromosome(1).size()); assertEquals(CMD_FOR, p.getChromosome(1).getNode(node++)); assertEquals(Increment.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(Variable.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(CMD_SUB_V_V_V, p.getChromosome(1).getNode(node++)); assertEquals(AddAndStore.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(ReadTerminal.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(ReadTerminal.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(TransferMemory.class, p.getChromosome(1).getNode(node++).getClass()); assertEquals(TransferMemory.class, p.getChromosome(1).getNode(node++).getClass()); // Evaluate program 3 // ------------------ assertEquals(1, p.getChromosome(2).size()); assertEquals(ReadTerminal.class, p.getChromosome(2).getNode(0).getClass()); assertEquals(0.0, computeFitness(p, vx), DELTA); }
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 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"); }
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; } }