// @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);
 }
Ejemplo n.º 2
0
  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);

  }
Ejemplo n.º 3
0
  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;
  }