Exemplo n.º 1
0
  private RowInfo[] createRowInfos() throws IOException {
    final ISINGrid grid = this.grid;
    final RowInfo[] binLines = new RowInfo[sceneHeight];
    final Variable idxVariable =
        ncFile.getRootGroup().findGroup("Level-3_Binned_Data").findVariable("BinList");
    final Structure idxStructure = (Structure) idxVariable;
    final Variable idx = idxStructure.findVariable("bin_num");
    final int[] idxValues;
    synchronized (ncFile) {
      idxValues = (int[]) idx.read().getStorage();
    }
    if (bins == null) {
      bins = idxValues; // (int[]) idxVariable.read().copyTo1DJavaArray();
    }
    final Point gridPoint = new Point();
    int lastBinIndex = -1;
    int lastRowIndex = -1;
    int lineOffset = 0;
    int lineLength = 0;
    for (int i = 0; i < idxValues.length; i++) {

      final int binIndex = idxValues[i];
      if (binIndex < lastBinIndex) {
        throw new IOException(
            "Unrecognized level-3 format. Bins numbers expected to appear in ascending order.");
      }
      lastBinIndex = binIndex;

      grid.getGridPoint(binIndex, gridPoint);
      final int rowIndex = gridPoint.y;

      if (rowIndex != lastRowIndex) {
        if (lineLength > 0) {
          binLines[lastRowIndex] = new RowInfo(lineOffset, lineLength);
        }
        lineOffset = i;
        lineLength = 0;
      }

      lineLength++;
      lastRowIndex = rowIndex;
    }

    if (lineLength > 0) {
      binLines[lastRowIndex] = new RowInfo(lineOffset, lineLength);
    }

    return binLines;
  }
Exemplo n.º 2
0
  @Override
  public synchronized void readBandData(
      Band destBand,
      int sourceOffsetX,
      int sourceOffsetY,
      int sourceWidth,
      int sourceHeight,
      int sourceStepX,
      int sourceStepY,
      ProductData destBuffer,
      ProgressMonitor pm)
      throws IOException, InvalidRangeException {

    final Variable variable = variableMap.get(destBand);
    DataType prodtype = variable.getDataType();
    float[] fbuffer;
    short[] sbuffer;
    int[] ibuffer;
    byte[] bbuffer;
    Object buffer;

    if (prodtype == DataType.FLOAT) {
      fbuffer = (float[]) destBuffer.getElems();
      Arrays.fill(fbuffer, Float.NaN);
      buffer = fbuffer;
    } else if (prodtype == DataType.SHORT) {
      sbuffer = (short[]) destBuffer.getElems();
      Arrays.fill(sbuffer, (short) -999);
      buffer = sbuffer;
    } else if (prodtype == DataType.BYTE) {
      bbuffer = (byte[]) destBuffer.getElems();
      Arrays.fill(bbuffer, (byte) 255);
      buffer = bbuffer;
    } else {
      ibuffer = (int[]) destBuffer.getElems();
      Arrays.fill(ibuffer, -999);
      buffer = ibuffer;
    }

    if (rowInfo == null) {
      rowInfo = createRowInfos();
    }

    final int height = sceneHeight;
    final int width = sceneWidth;
    final ISINGrid grid = this.grid;

    // loop over lines
    try {
      int[] lineOffsets = new int[1];
      int[] lineLengths = new int[1];
      int[] stride = new int[1];
      stride[0] = 1;

      //            for (int y = sourceOffsetY; y < sourceOffsetY + sourceHeight; y++) {
      for (int y = sourceOffsetY; y < sourceOffsetY + sourceHeight; y += sourceStepY) {
        if (pm.isCanceled()) {
          break;
        }
        final int rowIndex = (height - 1) - y;
        final RowInfo rowInfo = this.rowInfo[rowIndex];
        if (rowInfo != null) {

          final int lineOffset = rowInfo.offset;
          final int lineLength = rowInfo.length;

          lineOffsets[0] = lineOffset;
          lineLengths[0] = lineLength;
          final Object bindata;

          synchronized (ncFile) {
            bindata = variable.read().section(lineOffsets, lineLengths, stride).copyTo1DJavaArray();
          }
          int lineIndex0 = 0;
          for (int x = sourceOffsetX; x < sourceOffsetX + sourceWidth; x++) {
            final double lon = x * 360.0 / width;
            final int binIndex = grid.getBinIndex(rowIndex, lon);
            int lineIndex = -1;
            for (int i = lineIndex0; i < lineLength; i++) {
              int binidx = bins[lineOffset + i];
              if (binidx >= binIndex) {
                if (binidx == binIndex) {
                  lineIndex = i;
                }
                lineIndex0 = i;
                break;
              }
            }

            if (lineIndex >= 0) {
              final int rasterIndex = sourceWidth * (y - sourceOffsetY) + (x - sourceOffsetX);

              System.arraycopy(bindata, lineIndex, buffer, rasterIndex, 1);
            }
          }
          pm.worked(1);
        }
      }

    } finally {
      pm.done();
    }
  }
Exemplo n.º 3
0
  // Don't do this...it hurts.  Too much of a memory hog...
  private void addBandsBinMap(Product product) throws IOException, InvalidRangeException {
    String[] bandList = product.getBandNames();
    if (rowInfo == null) {
      rowInfo = createRowInfos();
    }

    final int height = sceneHeight;
    final int width = sceneWidth;
    final ISINGrid grid = this.grid;

    // loop over lines
    try {
      int[] lineOffsets = new int[1];
      int[] lineLengths = new int[1];
      int[] stride = new int[1];
      stride[0] = 1;

      //            for (int y = sourceOffsetY; y < sourceOffsetY + sourceHeight; y++) {
      for (String name : bandList) {
        if (name.endsWith("mean") || name.endsWith("stdev")) continue;
        Band band = product.getBand(name);
        ProductData buffer;
        final Variable variable = variableMap.get(band);
        DataType prodtype = variable.getDataType();
        float[] fbuffer = new float[width * height];
        short[] sbuffer = new short[width * height];
        int[] ibuffer = new int[width * height];
        byte[] bbuffer = new byte[width * height];

        if (prodtype == DataType.FLOAT) {
          Arrays.fill(fbuffer, Float.NaN);
          buffer = ProductData.createInstance(fbuffer);
        } else if (prodtype == DataType.SHORT) {
          Arrays.fill(sbuffer, (short) -999);
          buffer = ProductData.createInstance(sbuffer);
        } else if (prodtype == DataType.BYTE) {
          Arrays.fill(bbuffer, (byte) 255);
          buffer = ProductData.createInstance(bbuffer);
        } else {
          Arrays.fill(ibuffer, -999);
          buffer = ProductData.createInstance(ibuffer);
        }

        for (int y = 0; y < height; y++) {

          final int rowIndex = (height - 1) - y;
          final RowInfo rowInfo = this.rowInfo[rowIndex];
          if (rowInfo != null) {
            final Array bindata;

            final int lineOffset = rowInfo.offset;
            final int lineLength = rowInfo.length;

            lineOffsets[0] = lineOffset;
            lineLengths[0] = lineLength;

            synchronized (ncFile) {
              bindata =
                  variable
                      .read()
                      .section(lineOffsets, lineLengths, stride); // .copyTo1DJavaArray();
            }
            int lineIndex0 = 0;
            for (int x = 0; x < width; x++) {
              final double lon = x * 360.0 / width;
              final int binIndex = grid.getBinIndex(rowIndex, lon);
              int lineIndex = -1;
              for (int i = lineIndex0; i < lineLength; i++) {
                int binidx = bins[lineOffset + i];
                if (binidx >= binIndex) {
                  if (binidx == binIndex) {
                    lineIndex = i;
                  }
                  lineIndex0 = i;
                  break;
                }
              }

              if (lineIndex >= 0) {
                final int rasterIndex = width * y + x;
                final Array elem;
                elem = Array.factory(bindata.copyTo1DJavaArray());
                for (int i = 0; i < elem.getSize(); i++) {
                  if (prodtype == DataType.FLOAT) {

                    buffer.setElemFloatAt(rasterIndex, elem.getFloat(i));
                  } else {
                    buffer.setElemIntAt(rasterIndex, elem.getInt(i));
                  }
                  //                                System.arraycopy(bindata, lineIndex, buffer,
                  // rasterIndex, 1);
                }
              }
            }
          }
        }
        band.setDataElems(buffer);
      }
    } catch (IOException e) {
      throw new IOException("Could not map product " + product.getName(), e);
    }
  }