Esempio n. 1
0
 private void readBandRasterDataSubSampling(
     Band sourceBand,
     int sourceOffsetX,
     int sourceOffsetY,
     int sourceWidth,
     int sourceHeight,
     int sourceStepX,
     int sourceStepY,
     ProductData destBuffer,
     int destWidth,
     ProgressMonitor pm)
     throws IOException {
   final int sourceMinY = sourceOffsetY;
   final int sourceMaxY = sourceOffsetY + sourceHeight - 1;
   ProductData lineBuffer = ProductData.createInstance(destBuffer.getType(), sourceWidth);
   int destPos = 0;
   try {
     pm.beginTask("Reading sub sampled raster data...", 2 * (sourceMaxY - sourceMinY));
     for (int sourceY = sourceMinY; sourceY <= sourceMaxY; sourceY += sourceStepY) {
       sourceBand.readRasterData(
           sourceOffsetX, sourceY, sourceWidth, 1, lineBuffer, SubProgressMonitor.create(pm, 1));
       if (sourceStepX == 1) {
         copyData(lineBuffer, 0, destBuffer, destPos, destWidth);
       } else {
         copyLine(lineBuffer, 0, sourceWidth, sourceStepX, destBuffer, destPos);
       }
       pm.worked(1);
       destPos += destWidth;
     }
   } finally {
     pm.done();
   }
 }
Esempio n. 2
0
  /**
   * Returns the UTC for a global time attribute.
   *
   * @param product the product.
   * @param timeAttrName the name of the time attribute.
   * @return the attribute value as UTC or {@code null} if the attribute vallue cannot be parsed.
   * @see MetadataAttribute
   * @see ProductData.UTC
   */
  public static ProductData.UTC getTimeAttrValue(final Product product, final String timeAttrName) {
    final MetadataElement pa = product.getMetadataRoot().getElement(GLOBAL_ATTRIBUTES);
    if (pa == null) {
      return null;
    }
    final MetadataAttribute timeAttr = pa.getAttribute(timeAttrName);
    if (timeAttr == null) {
      return null;
    }
    final ProductData timeAttrData = timeAttr.getData();
    if (timeAttrData == null) {
      return null;
    }

    final String timeString = timeAttrData.getElemString();
    if ("".equals(timeString.trim())) {
      return null;
    }

    ProductData.UTC utc = null;
    for (final String pattern : DATE_TIME_PATTERNS) {
      try {
        utc = ProductData.UTC.parse(timeString, pattern);
        break; // utc is never null here
      } catch (ParseException ignored) {
        // ignore
      } catch (IllegalArgumentException ignored) {
        // ignore
      }
    }

    return utc;
  }
  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);
    }
  }
Esempio n. 4
0
 private static void copyData(
     ProductData sourceBuffer,
     int sourcePos,
     ProductData destBuffer,
     int destPos,
     int destLength) {
   System.arraycopy(
       sourceBuffer.getElems(), sourcePos, destBuffer.getElems(), destPos, destLength);
 }
Esempio n. 5
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();
    }
  }
Esempio n. 6
0
 public static ProductData createProductData(int productDataType, Array values) {
   Object data = values.getStorage();
   if (data instanceof char[]) {
     data = new String((char[]) data);
   }
   return ProductData.createInstance(productDataType, data);
 }
Esempio n. 7
0
  public void addInputParamMetadata(Product product) throws ProductIOException {

    Variable inputParams = ncFile.findVariable("Input_Parameters");
    if (inputParams != null) {
      final MetadataElement inputParamsMeta = new MetadataElement("Input_Parameters");
      Array array;
      try {
        array = inputParams.read();
      } catch (IOException e) {
        throw new ProductIOException(e.getMessage());
      }

      String[] lines = array.toString().split("\n");
      for (String line : lines) {
        String[] parts = line.split("=");
        if (parts.length == 2) {
          final String name = parts[0].trim();
          final String value = parts[1].trim();
          final ProductData data = ProductData.createInstance(ProductData.TYPE_ASCII, value);
          final MetadataAttribute attribute = new MetadataAttribute(name, data, true);
          inputParamsMeta.addAttribute(attribute);
        }
      }

      final MetadataElement metadataRoot = product.getMetadataRoot();
      metadataRoot.addElement(inputParamsMeta);
    }
  }
Esempio n. 8
0
  private void handleMetadataGroup(Group group, MetadataElement metadataElement)
      throws ProductIOException {
    List<Variable> variables = group.getVariables();
    for (Variable variable : variables) {
      final String name = variable.getShortName();
      final int dataType = getProductDataType(variable);
      Array array;
      try {
        array = variable.read();
      } catch (IOException e) {
        throw new ProductIOException(e.getMessage());
      }
      final ProductData data = ProductData.createInstance(dataType, array.getStorage());
      final MetadataAttribute attribute = new MetadataAttribute("data", data, true);

      final MetadataElement sdsElement = new MetadataElement(name);
      sdsElement.addAttribute(attribute);
      metadataElement.addElement(sdsElement);

      final List<Attribute> list = variable.getAttributes();
      for (Attribute hdfAttribute : list) {
        final String attribName = hdfAttribute.getShortName();
        if ("units".equals(attribName)) {
          attribute.setUnit(hdfAttribute.getStringValue());
        } else if ("long_name".equals(attribName)) {
          attribute.setDescription(hdfAttribute.getStringValue());
        } else {
          addAttributeToElement(sdsElement, hdfAttribute);
        }
      }
    }
  }
Esempio n. 9
0
 /**
  * Constructs a new field for the given field info.
  *
  * @param info the field info
  * @throws java.lang.IllegalArgumentException if the field info is null
  */
 protected Field(FieldInfo info) {
   Guardian.assertNotNull("info", info);
   _info = info;
   _data = ProductData.createInstance(info.getDataType(), info.getNumDataElems());
   if (_data == null) {
     throw new IllegalArgumentException("field info has an unknown data type"); /*I18N*/
   }
 }
Esempio n. 10
0
  public ProductData readData(Variable variable) throws ProductIOException {
    final int dataType = getProductDataType(variable);
    Array array;
    try {
      array = variable.read();

    } catch (IOException e) {
      throw new ProductIOException(e.getMessage());
    }
    return ProductData.createInstance(dataType, array.getStorage());
  }
Esempio n. 11
0
  @Override
  public synchronized void readBandRasterData(
      int sourceOffsetX,
      int sourceOffsetY,
      int sourceWidth,
      int sourceHeight,
      int sourceStepX,
      int sourceStepY,
      ProductData destBuffer,
      ProgressMonitor pm)
      throws IOException {

    AvhrrFile.RawCoordinates rawCoord =
        noaaFile.getRawCoordinates(sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight);

    final float[] targetData = (float[]) destBuffer.getElems();

    int targetIdx = rawCoord.targetStart;
    pm.beginTask("Reading AVHRR band '" + getBandName() + "'...", rawCoord.maxY - rawCoord.minY);
    try {
      for (int rawY = rawCoord.minY; rawY <= rawCoord.maxY; rawY += sourceStepY) {
        if (pm.isCanceled()) {
          break;
        }

        boolean validData = hasData(rawY);
        if (validData) {
          if (calibrator.requiresCalibrationData()) {
            readCalibrationCoefficients(rawY, calibrationData);
            validData = calibrator.processCalibrationData(calibrationData);
          }
          if (validData) {
            readData(rawY);
            validData = containsValidCounts();
            if (validData) {
              for (int sourceX = rawCoord.minX; sourceX <= rawCoord.maxX; sourceX += sourceStepX) {
                targetData[targetIdx] = calibrator.calibrate(lineOfCounts[sourceX]);
                targetIdx += rawCoord.targetIncrement;
              }
            }
          }
        }
        if (!validData) {
          for (int sourceX = rawCoord.minX; sourceX <= rawCoord.maxX; sourceX += sourceStepX) {
            targetData[targetIdx] = AvhrrConstants.NO_DATA_VALUE;
            targetIdx += rawCoord.targetIncrement;
          }
        }
        pm.worked(1);
      }
    } finally {
      pm.done();
    }
  }
  @Override
  public void readBandRasterData(
      int sourceOffsetX,
      int sourceOffsetY,
      int sourceWidth,
      int sourceHeight,
      int sourceStepX,
      int sourceStepY,
      final ProductData destBuffer,
      final ProgressMonitor pm)
      throws IOException {

    AvhrrFile.RawCoordinates rawCoord =
        metopFile.getRawCoordinates(sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight);
    final float[] targetData = (float[]) destBuffer.getElems();
    final float scalingFactor = (float) super.getScalingFactor();

    pm.beginTask(
        MessageFormat.format("Reading AVHRR band ''{0}''...", getBandName()),
        rawCoord.maxY - rawCoord.minY);

    int targetIdx = rawCoord.targetStart;
    for (int sourceY = rawCoord.minY; sourceY <= rawCoord.maxY; sourceY += sourceStepY) {
      if (pm.isCanceled()) {
        break;
      }

      if (hasData(sourceY)) {
        final int dataOffset = getDataOffset(sourceOffsetX, sourceY);
        synchronized (inputStream) {
          final short[] radianceScanLine = new short[metopFile.getProductWidth()];
          inputStream.seek(dataOffset);
          inputStream.readFully(radianceScanLine, 0, sourceWidth);

          for (int sourceX = 0; sourceX <= sourceWidth - 1; sourceX++) {
            targetData[targetIdx] = calibrator.calibrate(radianceScanLine[sourceX] * scalingFactor);
            targetIdx += rawCoord.targetIncrement;
          }
        }
      } else {
        for (int sourceX = rawCoord.minX; sourceX <= rawCoord.maxX; sourceX += sourceStepX) {
          targetData[targetIdx] = AvhrrConstants.NO_DATA_VALUE;
          targetIdx += rawCoord.targetIncrement;
        }
      }
      pm.worked(1);
    }
    pm.done();
  }
  private Element createXmlElement(final String version) {
    final List<Element> contentList = new ArrayList<Element>(16);
    contentList.add(createElement(DimapProductConstants.TAG_BAND_INDEX, "1"));
    contentList.add(createElement(DimapProductConstants.TAG_BAND_NAME, "filtered_coffee"));
    contentList.add(createElement(DimapProductConstants.TAG_BAND_DESCRIPTION, "with milk & sugar"));
    final String typeString = ProductData.getTypeString(_source.getGeophysicalDataType());
    contentList.add(createElement(DimapProductConstants.TAG_DATA_TYPE, typeString));
    contentList.add(createElement(DimapProductConstants.TAG_PHYSICAL_UNIT, "l"));
    contentList.add(createElement(DimapProductConstants.TAG_SOLAR_FLUX, "0.0"));
    contentList.add(createElement(DimapProductConstants.TAG_BAND_WAVELEN, "0.0"));
    contentList.add(createElement(DimapProductConstants.TAG_BANDWIDTH, "0.0"));
    contentList.add(createElement(DimapProductConstants.TAG_SCALING_FACTOR, "1.0"));
    contentList.add(createElement(DimapProductConstants.TAG_SCALING_OFFSET, "0.0"));
    contentList.add(createElement(DimapProductConstants.TAG_SCALING_LOG_10, "false"));
    contentList.add(createElement(DimapProductConstants.TAG_NO_DATA_VALUE_USED, "true"));
    contentList.add(
        createElement(
            DimapProductConstants.TAG_NO_DATA_VALUE,
            String.valueOf(_source.getGeophysicalNoDataValue())));
    final List<Element> filterBandInfoList = new ArrayList<Element>(5);
    filterBandInfoList.add(createElement(DimapProductConstants.TAG_FILTER_SOURCE, "anyBand"));
    filterBandInfoList.add(
        createElement(
            DimapProductConstants.TAG_FILTER_OPERATOR_CLASS_NAME,
            "org.esa.beam.framework.datamodel.GeneralFilterBand$Mean"));
    final Element filterBandInfo = new Element(DimapProductConstants.TAG_FILTER_BAND_INFO);
    filterBandInfo.setAttribute(
        GeneralFilterBandPersistable.ATTRIBUTE_BAND_TYPE,
        GeneralFilterBandPersistable.GENERAL_FILTER_BAND_TYPE);
    if (GeneralFilterBandPersistable.VERSION_1_1.equals(version)) {
      filterBandInfoList.add(createElement(DimapProductConstants.TAG_FILTER_SUB_WINDOW_SIZE, "5"));
      filterBandInfo.setAttribute(
          GeneralFilterBandPersistable.ATTRIBUTE_VERSION, GeneralFilterBandPersistable.VERSION_1_1);
    } else {
      // Version 1.0
      filterBandInfoList.add(createElement(DimapProductConstants.TAG_FILTER_SUB_WINDOW_WIDTH, "5"));
      filterBandInfoList.add(
          createElement(DimapProductConstants.TAG_FILTER_SUB_WINDOW_HEIGHT, "2"));
    }
    filterBandInfo.addContent(filterBandInfoList);
    contentList.add(filterBandInfo);

    final Element root = new Element(DimapProductConstants.TAG_SPECTRAL_BAND_INFO);
    root.setContent(contentList);
    return root;
  }
Esempio n. 14
0
  /** Creates Metadata nodes for all the necessary information. */
  protected MetadataElement[] getMetadata() {
    MetadataElement[] metadata = new MetadataElement[2];

    MetadataElement fileList = new MetadataElement("Input_Products");
    final String[] productList = context.getProcessedProducts();
    String keyString;

    for (int n = 0; n < productList.length; n++) {
      keyString = "Product." + n;
      fileList.addAttribute(
          new MetadataAttribute(keyString, ProductData.createInstance(productList[n]), true));
    }
    metadata[0] = fileList;

    MetadataElement binParams = new MetadataElement("Binning_Parameter");
    binParams.addAttribute(
        new MetadataAttribute(
            "Resampling_Type", ProductData.createInstance(context.getResamplingType()), true));
    final String cellSizeName;
    if (L3Constants.RESAMPLING_TYPE_VALUE_BINNING.equals(context.getResamplingType())) {
      cellSizeName = "Bin_Size_In_Km";
    } else {
      cellSizeName = "Bins_Per_Degree";
    }
    binParams.addAttribute(
        new MetadataAttribute(
            cellSizeName,
            ProductData.createInstance(new float[] {context.getGridCellSize()}),
            true));
    final L3Context.BandDefinition[] bandDefs = context.getBandDefinitions();
    for (int bandIndex = 0; bandIndex < bandDefs.length; bandIndex++) {
      final L3Context.BandDefinition bandDef = bandDefs[bandIndex];
      binParams.addAttribute(
          new MetadataAttribute(
              "Geophysical_Parameter_" + bandIndex,
              ProductData.createInstance(bandDef.getBandName()),
              true));
      binParams.addAttribute(
          new MetadataAttribute(
              "Bitmask_" + bandIndex, ProductData.createInstance(bandDef.getBitmaskExp()), true));
      binParams.addAttribute(
          new MetadataAttribute(
              "Algorithm_" + bandIndex,
              ProductData.createInstance(bandDef.getAlgorithm().getTypeString()),
              true));
    }
    metadata[1] = binParams;

    return metadata;
  }
Esempio n. 15
0
 MetadataAttribute attributeToMetadata(Attribute attribute) {
   final int productDataType = getProductDataType(attribute.getDataType(), false, false);
   if (productDataType != -1) {
     ProductData productData;
     if (attribute.isString()) {
       productData = ProductData.createInstance(attribute.getStringValue());
     } else if (attribute.isArray()) {
       productData = ProductData.createInstance(productDataType, attribute.getLength());
       productData.setElems(attribute.getValues().getStorage());
     } else {
       productData = ProductData.createInstance(productDataType, 1);
       productData.setElems(attribute.getValues().getStorage());
     }
     return new MetadataAttribute(attribute.getShortName(), productData, true);
   }
   return null;
 }
Esempio n. 16
0
 public MetadataElement getAsMetadataElement(CompoundData compoundData) throws IOException {
   CompoundType type = compoundData.getType();
   final int memberCount = type.getMemberCount();
   MetadataElement metadataElement = new MetadataElement(type.getName());
   for (int i = 0; i < memberCount; i++) {
     String typeName = type.getMemberName(i);
     CompoundMember member = type.getMember(i);
     FormatMetadata formatMetadata = (FormatMetadata) member.getMetadata();
     if (typeName.equals("fill")) {
       // ignore
     } else if (formatMetadata != null && formatMetadata.getType().equals("string")) {
       String stringValue = getAsString(compoundData.getSequence(i));
       Map<Object, String> map = getMetaData(member).getItemMap();
       if (map != null) {
         stringValue = map.get(stringValue);
       }
       ProductData data = ProductData.createInstance(stringValue);
       MetadataAttribute attribute = new MetadataAttribute(typeName, data, true);
       attribute.setDescription(getDescription(member));
       attribute.setUnit(getUnits(member));
       metadataElement.addAttribute(attribute);
     } else if (member.getType().getName().equals("DATE")) {
       CompoundData dateCompound = compoundData.getCompound(i);
       ProductData data = createDate(dateCompound);
       MetadataAttribute attribute = new MetadataAttribute(typeName, data, true);
       attribute.setDescription(getDescription(member));
       attribute.setUnit(getUnits(member));
       metadataElement.addAttribute(attribute);
     } else if (member.getType().isSequenceType()) {
       SequenceData sequence = compoundData.getSequence(i);
       for (int j = 0; j < sequence.getType().getElementCount(); j++) {
         CompoundData compound = sequence.getCompound(j);
         metadataElement.addElement(getAsMetadataElement(compound));
       }
     } else if (member.getType().isCompoundType()) {
       metadataElement.addElement(getAsMetadataElement(compoundData.getCompound(i)));
     } else if (member.getType().isSimpleType()) {
       int intValue = compoundData.getInt(i);
       Map<Object, String> map = getMetaData(member).getItemMap();
       ProductData data;
       if (map != null) {
         String stringValue = map.get(intValue);
         data = ProductData.createInstance(stringValue);
       } else {
         double scalingFactor = getMetaData(member).getScalingFactor();
         if (scalingFactor == 1.0) {
           data = ProductData.createInstance(new int[] {intValue});
         } else {
           data = ProductData.createInstance(new double[] {intValue * scalingFactor});
         }
       }
       MetadataAttribute attribute = new MetadataAttribute(typeName, data, true);
       attribute.setDescription(getDescription(member));
       attribute.setUnit(getUnits(member));
       metadataElement.addAttribute(attribute);
     } else {
       System.out.println("not handled: name=" + typeName);
       System.out.println("member = " + member.getType());
     }
   }
   return metadataElement;
 }
Esempio n. 17
0
 private void checkBufferSize(int sourceWidth, int sourceHeight, ProductData sourceBuffer) {
   int expectedBufferSize = sourceWidth * sourceHeight;
   int actualBufferSize = sourceBuffer.getNumElems();
   Guardian.assertEquals("wrong sourceBuffer size", actualBufferSize, expectedBufferSize);
 }
Esempio n. 18
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();
    }
  }
  public void testCreateXmlFromObject() {
    final GeneralFilterBand gfb =
        new GeneralFilterBand("filteredBand", _source, 2, GeneralFilterBand.MAX);
    gfb.setDescription("somehow explainig");
    gfb.setUnit("someUnit");
    _product.addBand(gfb);

    final Element xmlElement = _generalFilterBandPersistable.createXmlFromObject(gfb);

    assertNotNull(xmlElement);
    assertEquals(DimapProductConstants.TAG_SPECTRAL_BAND_INFO, xmlElement.getName());
    assertEquals(14, xmlElement.getChildren().size());
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_BAND_INDEX) != null);
    assertEquals(
        gfb.getProduct().getBandIndex(gfb.getName()),
        Integer.parseInt(xmlElement.getChildTextTrim(DimapProductConstants.TAG_BAND_INDEX)));
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_BAND_NAME) != null);
    assertEquals(gfb.getName(), xmlElement.getChildTextTrim(DimapProductConstants.TAG_BAND_NAME));
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_BAND_DESCRIPTION) != null);
    assertEquals(
        gfb.getDescription(),
        xmlElement.getChildTextTrim(DimapProductConstants.TAG_BAND_DESCRIPTION));
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_DATA_TYPE) != null);
    assertEquals(
        ProductData.getTypeString(gfb.getDataType()),
        xmlElement.getChildTextTrim(DimapProductConstants.TAG_DATA_TYPE));
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_PHYSICAL_UNIT) != null);
    assertEquals(
        gfb.getUnit(), xmlElement.getChildTextTrim(DimapProductConstants.TAG_PHYSICAL_UNIT));
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_SOLAR_FLUX) != null);
    assertEquals(
        gfb.getSolarFlux(),
        Float.parseFloat(xmlElement.getChildTextTrim(DimapProductConstants.TAG_SOLAR_FLUX)),
        EPS);
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_BAND_WAVELEN) != null);
    assertEquals(
        gfb.getSpectralWavelength(),
        Float.parseFloat(xmlElement.getChildTextTrim(DimapProductConstants.TAG_BAND_WAVELEN)),
        EPS);
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_BANDWIDTH) != null);
    assertEquals(
        gfb.getSpectralBandwidth(),
        Float.parseFloat(xmlElement.getChildTextTrim(DimapProductConstants.TAG_BANDWIDTH)),
        EPS);
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_SCALING_FACTOR) != null);
    assertEquals(
        gfb.getScalingFactor(),
        Double.parseDouble(xmlElement.getChildTextTrim(DimapProductConstants.TAG_SCALING_FACTOR)),
        EPS);
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_SCALING_OFFSET) != null);
    assertEquals(
        gfb.getScalingOffset(),
        Double.parseDouble(xmlElement.getChildTextTrim(DimapProductConstants.TAG_SCALING_OFFSET)),
        EPS);
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_SCALING_LOG_10) != null);
    assertEquals(
        gfb.isLog10Scaled(),
        Boolean.parseBoolean(
            xmlElement.getChildTextTrim(DimapProductConstants.TAG_SCALING_LOG_10)));
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_NO_DATA_VALUE_USED) != null);
    assertEquals(
        gfb.isNoDataValueUsed(),
        Boolean.parseBoolean(
            xmlElement.getChildTextTrim(DimapProductConstants.TAG_NO_DATA_VALUE_USED)));
    assertTrue(xmlElement.getChild(DimapProductConstants.TAG_NO_DATA_VALUE) != null);
    assertEquals(
        gfb.getNoDataValue(),
        Double.parseDouble(xmlElement.getChildTextTrim(DimapProductConstants.TAG_NO_DATA_VALUE)),
        EPS);

    final Element filterInfo = xmlElement.getChild(DimapProductConstants.TAG_FILTER_BAND_INFO);
    assertNotNull(filterInfo);
    assertEquals(
        GeneralFilterBandPersistable.GENERAL_FILTER_BAND_TYPE,
        filterInfo.getAttributeValue(GeneralFilterBandPersistable.ATTRIBUTE_BAND_TYPE));
    assertEquals(
        GeneralFilterBandPersistable.VERSION_1_1,
        filterInfo.getAttributeValue(GeneralFilterBandPersistable.ATTRIBUTE_VERSION));
    assertEquals(3, filterInfo.getChildren().size());
    assertTrue(filterInfo.getChild(DimapProductConstants.TAG_FILTER_SOURCE) != null);
    assertEquals(
        gfb.getSource().getName(),
        filterInfo.getChildTextTrim(DimapProductConstants.TAG_FILTER_SOURCE));
    assertTrue(filterInfo.getChild(DimapProductConstants.TAG_FILTER_SUB_WINDOW_SIZE) != null);
    assertEquals(
        gfb.getSubWindowSize(),
        Integer.parseInt(
            filterInfo.getChildTextTrim(DimapProductConstants.TAG_FILTER_SUB_WINDOW_SIZE)));
    assertTrue(filterInfo.getChild(DimapProductConstants.TAG_FILTER_OPERATOR_CLASS_NAME) != null);
    assertEquals(
        gfb.getOperator().getClass().getName(),
        filterInfo.getChildTextTrim(DimapProductConstants.TAG_FILTER_OPERATOR_CLASS_NAME));
  }
Esempio n. 20
0
 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");
   }
 }
Esempio n. 21
0
 /**
  * Reads the data elements of this field from the given input stream. This method must be
  * overridden in order to provide an implementation suitable for the type of the underlying data
  * array of this field.
  *
  * @param dataInputStream a seekable data input stream
  * @throws java.io.IOException if an I/O error occurs
  */
 public void readFrom(ImageInputStream dataInputStream) throws IOException {
   _data.readFrom(dataInputStream);
 }
Esempio n. 22
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);
    }
  }
Esempio n. 23
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();
    }
  }
Esempio n. 24
0
 /**
  * Tests whether this field has an integer data type.
  *
  * @return true, if so
  */
 public final boolean isIntType() {
   return _data.isInt();
 }