/**
  * Read data from a top level Variable and send data to a WritableByteChannel.
  *
  * @param v2 Variable
  * @param section wanted section of data of Variable. The section list is a list of ucar.ma2.Range
  *     which define the requested data subset.
  * @param channel WritableByteChannel object - channel that can write bytes.
  * @return the number of bytes written, possibly zero.
  */
 public long readToByteChannel11(
     ucar.nc2.Variable v2, Section section, WritableByteChannel channel)
     throws java.io.IOException, ucar.ma2.InvalidRangeException {
   Array data = readData(v2, section);
   float[] ftdata = new float[(int) data.getSize()];
   byte[] bytedata = new byte[(int) data.getSize() * 4];
   IndexIterator iter = data.getIndexIterator();
   int i = 0;
   ByteBuffer buffer = ByteBuffer.allocateDirect(bytedata.length);
   while (iter.hasNext()) {
     ftdata[i] = iter.getFloatNext();
     bytedata[i] = new Float(ftdata[i]).byteValue();
     buffer.put(bytedata[i]);
     i++;
   }
   buffer = ByteBuffer.wrap(bytedata);
   // write the bytes to the channel
   int count = channel.write(buffer);
   System.out.println("COUNT=" + count);
   // check if all bytes where written
   if (buffer.hasRemaining()) {
     // if not all bytes were written, move the unwritten bytes to the beginning and
     // set position just after the last unwritten byte
     buffer.compact();
   } else {
     buffer.clear();
   }
   return (long) count;
 }
 // @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);
 }
Exemple #3
0
 private float testReadScalar(Variable v) throws IOException {
   if (show) System.out.printf(" read %s%n", v.getNameAndDimensions());
   assert (null != v);
   Array a = v.read();
   assert (null != a);
   IndexIterator ii = a.getIndexIterator();
   return ii.getFloatNext();
 }
  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);

  }
  /**
   * Translate missing data to NaNs. Data must be DOUBLE or FLOAT
   *
   * @param in convert this array
   * @return same array, with missing values replaced by NaNs
   */
  private Array convertMissing(Array in) {
    if (debugRead) System.out.println("convertMissing ");

    IndexIterator iterIn = in.getIndexIterator();
    if (in.getElementType() == double.class) {
      while (iterIn.hasNext()) {
        double val = iterIn.getDoubleNext();
        if (isMissing_(val)) iterIn.setDoubleCurrent(Double.NaN);
      }
    } else if (in.getElementType() == float.class) {
      while (iterIn.hasNext()) {
        float val = iterIn.getFloatNext();
        if (isMissing_(val)) iterIn.setFloatCurrent(Float.NaN);
      }
    }
    return in;
  }
Exemple #6
0
  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;
  }