private static void copyData(
     ProductData sourceBuffer,
     int sourcePos,
     ProductData destBuffer,
     int destPos,
     int destLength) {
   System.arraycopy(
       sourceBuffer.getElems(), sourcePos, destBuffer.getElems(), destPos, destLength);
 }
  public void readDescendingRasterBand(
      final int sourceOffsetX,
      final int sourceOffsetY,
      final int sourceStepX,
      final int sourceStepY,
      final ProductData destBuffer,
      final int destOffsetX,
      final int destOffsetY,
      final int destWidth,
      final int destHeight,
      final int imageID,
      final ImageIOFile img,
      final int bandSampleOffset,
      final boolean isAntennaPointingRight)
      throws IOException {

    final Raster data;

    synchronized (dataDir) {
      final ImageReader reader = img.getReader();
      final ImageReadParam param = reader.getDefaultReadParam();
      param.setSourceSubsampling(
          sourceStepX, sourceStepY, sourceOffsetX % sourceStepX, sourceOffsetY % sourceStepY);

      final RenderedImage image = reader.readAsRenderedImage(0, param);
      if (flipToSARGeometry && isAntennaPointingRight) { // flip the image left to right
        data =
            image.getData(
                new Rectangle(
                    img.getSceneWidth() - destOffsetX - destWidth,
                    destOffsetY,
                    destWidth,
                    destHeight));
      } else {
        data = image.getData(new Rectangle(destOffsetX, destOffsetY, destWidth, destHeight));
      }
    }

    final DataBuffer dataBuffer = data.getDataBuffer();
    final SampleModel sampleModel = data.getSampleModel();
    final int sampleOffset = imageID + bandSampleOffset;

    if (flipToSARGeometry && isAntennaPointingRight) { // flip the image left to right
      final int[] dArray = new int[destWidth * destHeight];
      sampleModel.getSamples(0, 0, destWidth, destHeight, sampleOffset, dArray, dataBuffer);

      int srcStride, destStride;
      for (int r = 0; r < destHeight; r++) {
        srcStride = r * destWidth;
        destStride = r * destWidth + destWidth;
        for (int c = 0; c < destWidth; c++) {
          destBuffer.setElemIntAt(destStride - c - 1, dArray[srcStride + c]);
        }
      }
    } else { // no flipping is needed
      sampleModel.getSamples(
          0, 0, destWidth, destHeight, sampleOffset, (int[]) destBuffer.getElems(), dataBuffer);
    }
  }
Exemple #3
0
  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 {

    if (mustFlipY) {
      sourceOffsetY = destBand.getSceneRasterHeight() - (sourceOffsetY + sourceHeight);
    }
    if (mustFlipX) {
      sourceOffsetX = destBand.getSceneRasterWidth() - (sourceOffsetX + sourceWidth);
    }
    sourceOffsetY += leadLineSkip;
    start[0] = sourceOffsetY;
    start[1] = sourceOffsetX;
    stride[0] = sourceStepY;
    stride[1] = sourceStepX;
    count[0] = sourceHeight;
    count[1] = sourceWidth;
    Object buffer = destBuffer.getElems();
    Variable variable = variableMap.get(destBand);

    pm.beginTask("Reading band '" + variable.getShortName() + "'...", sourceHeight);
    try {
      Section section = new Section(start, count, stride);

      Array array;
      int[] newshape = {sourceHeight, sourceWidth};

      array = variable.read(section);
      if (array.getRank() == 3) {
        array = array.reshapeNoCopy(newshape);
      }
      Object storage;

      if (mustFlipX && !mustFlipY) {
        storage = array.flip(1).copyTo1DJavaArray();
      } else if (!mustFlipX && mustFlipY) {
        storage = array.flip(0).copyTo1DJavaArray();
      } else if (mustFlipX && mustFlipY) {
        storage = array.flip(0).flip(1).copyTo1DJavaArray();
      } else {
        storage = array.copyTo1DJavaArray();
      }

      arraycopy(storage, 0, buffer, 0, destBuffer.getNumElems());
    } finally {
      pm.done();
    }
  }
Exemple #4
0
  /**
   * Sets the data elements of this data node.
   *
   * @see ProductData#setElems(Object)
   */
  public void setDataElems(Object elems) {

    if (isReadOnly()) {
      throw new IllegalArgumentException("attribute is read-only");
    }

    checkState();
    if (data == null) {
      if (numElems > Integer.MAX_VALUE) {
        throw new IllegalStateException(
            "number of elements must be less than " + (long) Integer.MAX_VALUE + 1);
      }
      data = createCompatibleProductData((int) numElems);
    }
    Object oldData = data.getElems();
    if (!ObjectUtils.equalObjects(oldData, elems)) {
      data.setElems(elems);
      fireProductNodeDataChanged();
      setModified(true);
    }
  }
Exemple #5
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();
    }
  }
Exemple #6
0
  /** {@inheritDoc} */
  public void writeBandRasterData(
      Band sourceBand,
      int sourceOffsetX,
      int sourceOffsetY,
      int sourceWidth,
      int sourceHeight,
      ProductData sourceBuffer,
      ProgressMonitor pm)
      throws IOException {
    checkBufferSize(sourceWidth, sourceHeight, sourceBuffer);
    final int sourceBandWidth = sourceBand.getSceneRasterWidth();
    final int sourceBandHeight = sourceBand.getSceneRasterHeight();
    checkSourceRegionInsideBandRegion(
        sourceWidth, sourceBandWidth, sourceHeight, sourceBandHeight, sourceOffsetX, sourceOffsetY);

    int memTypeID = -1;
    int memSpaceID = -1;

    pm.beginTask("Writing band '" + sourceBand.getName() + "'...", 1);
    try {
      final int datasetID = getOrCreateBandH5D(sourceBand);
      final int fileSpaceID = H5.H5Dget_space(datasetID);
      final long[] memDims = new long[] {sourceHeight, sourceWidth};
      final long[] memStart = new long[2];
      final long[] memCount = new long[2];
      final long[] fileStart = new long[2];
      final long[] fileCount = new long[2];

      memTypeID = createH5TypeID(sourceBuffer.getType());
      memSpaceID = H5.H5Screate_simple(2, memDims, null);

      memStart[0] = 0;
      memStart[1] = 0;
      memCount[0] = sourceHeight;
      memCount[1] = sourceWidth;
      H5.H5Sselect_hyperslab(
          memSpaceID, HDF5Constants.H5S_SELECT_SET, memStart, null, memCount, null);

      fileStart[0] = sourceOffsetY;
      fileStart[1] = sourceOffsetX;
      fileCount[0] = sourceHeight;
      fileCount[1] = sourceWidth;
      H5.H5Sselect_hyperslab(
          fileSpaceID, HDF5Constants.H5S_SELECT_SET, fileStart, null, fileCount, null);

      H5.H5Dwrite(
          datasetID,
          memTypeID,
          memSpaceID,
          fileSpaceID,
          HDF5Constants.H5P_DEFAULT,
          sourceBuffer.getElems());

      pm.worked(1);
    } catch (IOException e) {
      throw e;
    } catch (HDF5Exception e) {
      throw new ProductIOException(createErrorMessage(e));
    } finally {
      closeH5S(memSpaceID);
      closeH5T(memTypeID);
      pm.done();
    }
  }
 private static void copyLine(
     ProductData sourceBuffer,
     int sourceOffsetPos,
     int sourceWidth,
     int sourceStepX,
     ProductData destBuffer,
     int destOffsetPos) {
   final int sourceMinX = sourceOffsetPos;
   final int sourceMaxX = sourceOffsetPos + sourceWidth - 1;
   if (destBuffer.getElems() instanceof byte[]) {
     byte[] destArray = (byte[]) destBuffer.getElems();
     byte[] sourceArray = (byte[]) sourceBuffer.getElems();
     for (int sourceX = sourceMinX; sourceX <= sourceMaxX; sourceX += sourceStepX) {
       destArray[destOffsetPos] = sourceArray[sourceX];
       destOffsetPos++;
     }
   } else if (destBuffer.getElems() instanceof short[]) {
     short[] destArray = (short[]) destBuffer.getElems();
     short[] sourceArray = (short[]) sourceBuffer.getElems();
     for (int sourceX = sourceMinX; sourceX <= sourceMaxX; sourceX += sourceStepX) {
       destArray[destOffsetPos] = sourceArray[sourceX];
       destOffsetPos++;
     }
   } else if (destBuffer.getElems() instanceof int[]) {
     int[] destArray = (int[]) destBuffer.getElems();
     int[] sourceArray = (int[]) sourceBuffer.getElems();
     for (int sourceX = sourceMinX; sourceX <= sourceMaxX; sourceX += sourceStepX) {
       destArray[destOffsetPos] = sourceArray[sourceX];
       destOffsetPos++;
     }
   } else if (destBuffer.getElems() instanceof float[]) {
     float[] destArray = (float[]) destBuffer.getElems();
     float[] sourceArray = (float[]) sourceBuffer.getElems();
     for (int sourceX = sourceMinX; sourceX <= sourceMaxX; sourceX += sourceStepX) {
       destArray[destOffsetPos] = sourceArray[sourceX];
       destOffsetPos++;
     }
   } else if (destBuffer.getElems() instanceof double[]) {
     double[] destArray = (double[]) destBuffer.getElems();
     double[] sourceArray = (double[]) sourceBuffer.getElems();
     for (int sourceX = sourceMinX; sourceX <= sourceMaxX; sourceX += sourceStepX) {
       destArray[destOffsetPos] = sourceArray[sourceX];
       destOffsetPos++;
     }
   } else {
     Debug.assertTrue(false, "illegal product data type");
     throw new IllegalStateException("illegal product data type");
   }
 }