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; } }
// @todo Make sure units are meters public Array getElevation(Range range) throws IOException, InvalidRangeException { List section = new ArrayList(1); section.add(range); Array a = elevVar.read(section); if (elevVarUnitsConversionFactor == 1.0) return (a); for (IndexIterator it = a.getIndexIterator(); it.hasNext(); ) { if (elevVar.getDataType() == DataType.DOUBLE) { double val = it.getDoubleNext(); it.setDoubleCurrent(val * elevVarUnitsConversionFactor); } else if (elevVar.getDataType() == DataType.FLOAT) { float val = it.getFloatNext(); it.setFloatCurrent((float) (val * elevVarUnitsConversionFactor)); } else if (elevVar.getDataType() == DataType.INT) { int val = it.getIntNext(); it.setIntCurrent((int) (val * elevVarUnitsConversionFactor)); } else if (elevVar.getDataType() == DataType.LONG) { long val = it.getLongNext(); it.setLongCurrent((long) (val * elevVarUnitsConversionFactor)); } else { throw new IllegalStateException( "Elevation variable type <" + elevVar.getDataType().toString() + "> not double, float, int, or long."); } } return (a); }
private void convertScaleOffsetUnsignedInt(IndexIterator iterIn, IndexIterator iterOut) { boolean checkMissing = useNaNs && hasMissing(); while (iterIn.hasNext()) { int valb = iterIn.getIntNext(); double val = scale * DataType.unsignedIntToLong(valb) + offset; iterOut.setDoubleNext(checkMissing && isMissing_(val) ? Double.NaN : val); } }
private void writeCoordinateVariable(VariableEntry variable, NetcdfFileWriteable ncFile) throws IOException, InvalidRangeException { int[] dimensions = {variable.getSize()}; Array array; // so far, everything is a float or an int // way too much code duplication but I'm done fightin java for now if (variable.getType() == DataType.INT) { array = new ArrayInt(dimensions); int tempInt = 0; IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { iter.getIntNext(); iter.setIntCurrent(tempInt); tempInt++; } ncFile.write(variable.getVariableName(), array); } else if (variable.getType() == DataType.FLOAT) { array = new ArrayFloat(dimensions); float tempFloat = 0; IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { iter.getFloatNext(); iter.setFloatCurrent(tempFloat); tempFloat++; } ncFile.write(variable.getVariableName(), array); } else if (variable.getType() == DataType.LONG) { array = new ArrayLong(dimensions); long tempLong = 0; IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { iter.getLongNext(); iter.setLongCurrent(tempLong); tempLong++; } ncFile.write(variable.getVariableName(), array); } // ncFile.write(variable.getVariableName(),array); }
private int populateFile2( NetcdfFileWriteable ncFile, String varName, DataType dataType, int[] dimensions, int valueCounter, Random generator) throws IOException, InvalidRangeException { long singleWriteSize = maxMemory / dataType.getSize(); int highestNonWriteDim = determineHighestNonWriteDimension(dimensions, singleWriteSize); int[] singleStep = createWriteStep(dimensions, singleWriteSize, highestNonWriteDim); int numStepWrites = determineNumberOfStepWrites(singleStep, singleWriteSize); System.out.println( "SingleWriteSize: " + singleWriteSize + " datatype size: " + dataType.getSize() + " singleStep: " + arrayToString(singleStep) + " numberStepsPerWrite: " + numStepWrites); int[] allOnes = new int[dimensions.length]; int[] allZeros = new int[dimensions.length]; for (int i = 0; i < allOnes.length; i++) { allOnes[i] = 1; allZeros[i] = 0; } Index origin = new Index(dimensions, allOnes); long totalSize = origin.getSize(); long writtenSoFar = 0; boolean done = false; while (!done) { for (int i = 0; i < numStepWrites; i++) { // this is a crack at an optimization if ((highestNonWriteDim >= 0) && ((origin.getCurrentCounter()[highestNonWriteDim] + numStepWrites) <= dimensions[highestNonWriteDim])) { singleStep[highestNonWriteDim] = numStepWrites; System.out.println("JUST OPTIMIZED. New write step: " + arrayToString(singleStep)); // keep 'i' right i += numStepWrites - 1; } else { singleStep[highestNonWriteDim] = 1; } Array array = new ArrayInt(singleStep); IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { iter.getIntNext(); // uncomment the following line for a random distribution // iter.setIntCurrent((int) (Math.abs(generator.nextGaussian()) * 40) ); // uncomment this line for an incrementing value iter.setIntCurrent(valueCounter); valueCounter++; // book keeping writtenSoFar++; // origin.incr(); } System.out.println( "Writing to file: " + ncFile.getLocation() + ". var_name: " + varName + " origin: " + arrayToString(origin.getCurrentCounter()) + " writeSize: " + array.getSize() + " write shape: " + arrayToString(singleStep)); // write to the actual file ncFile.write(varName, origin.getCurrentCounter(), array); // update origin accordingly for (int j = 0; j < Index.computeSize(singleStep); j++) { // writtenSoFar++; origin.incr(); } System.out.println( "\tcurrentIndex: " + arrayToString(origin.getCurrentCounter()) + " currentElement: " + origin.currentElement() + " totalsize: " + (totalSize) + " writtenSoFar: " + writtenSoFar); if (writtenSoFar >= totalSize) { done = true; break; } } } return valueCounter; }
private boolean compareData(String name, Array data1, Array data2, double tol, boolean justOne) { boolean ok = true; if (data1.getSize() != data2.getSize()) { f.format(" DIFF %s: size %d !== %d%n", name, data1.getSize(), data2.getSize()); ok = false; } if (data1.getElementType() != data2.getElementType()) { f.format( " DIFF %s: element type %s !== %s%n", name, data1.getElementType(), data2.getElementType()); ok = false; } if (!ok) return false; DataType dt = DataType.getType(data1.getElementType()); IndexIterator iter1 = data1.getIndexIterator(); IndexIterator iter2 = data2.getIndexIterator(); if (dt == DataType.DOUBLE) { while (iter1.hasNext() && iter2.hasNext()) { double v1 = iter1.getDoubleNext(); double v2 = iter2.getDoubleNext(); if (!Double.isNaN(v1) || !Double.isNaN(v2)) if (!closeEnough(v1, v2, tol)) { f.format( " DIFF %s: %f != %f count=%s diff = %f pdiff = %f %n", name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2)); ok = false; if (justOne) break; } } } else if (dt == DataType.FLOAT) { while (iter1.hasNext() && iter2.hasNext()) { float v1 = iter1.getFloatNext(); float v2 = iter2.getFloatNext(); if (!Float.isNaN(v1) || !Float.isNaN(v2)) if (!closeEnough(v1, v2, (float) tol)) { f.format( " DIFF %s: %f != %f count=%s diff = %f pdiff = %f %n", name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2)); ok = false; if (justOne) break; } } } else if (dt == DataType.INT) { while (iter1.hasNext() && iter2.hasNext()) { int v1 = iter1.getIntNext(); int v2 = iter2.getIntNext(); if (v1 != v2) { f.format( " DIFF %s: %d != %d count=%s diff = %f pdiff = %f %n", name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2)); ok = false; if (justOne) break; } } } else if (dt == DataType.SHORT) { while (iter1.hasNext() && iter2.hasNext()) { short v1 = iter1.getShortNext(); short v2 = iter2.getShortNext(); if (v1 != v2) { f.format( " DIFF %s: %d != %d count=%s diff = %f pdiff = %f %n", name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2)); ok = false; if (justOne) break; } } } else if (dt == DataType.BYTE) { while (iter1.hasNext() && iter2.hasNext()) { byte v1 = iter1.getByteNext(); byte v2 = iter2.getByteNext(); if (v1 != v2) { f.format( " DIFF %s: %d != %d count=%s diff = %f pdiff = %f %n", name, v1, v2, iter1, diff(v1, v2), pdiff(v1, v2)); ok = false; if (justOne) break; } } } else if (dt == DataType.STRUCTURE) { while (iter1.hasNext() && iter2.hasNext()) { compareStructureData( (StructureData) iter1.next(), (StructureData) iter2.next(), tol, justOne); } } return ok; }