Ejemplo n.º 1
0
  private static void readCalibrationLUT(
      final File file, final String lutName, final MetadataElement root, final boolean flipLUT)
      throws IOException {
    if (!file.exists()) return;
    final org.jdom.Document xmlDoc = XMLSupport.LoadXML(file.getAbsolutePath());
    final Element rootElement = xmlDoc.getRootElement();

    final Element offsetElem = rootElement.getChild("offset");
    final double offset = Double.parseDouble(offsetElem.getValue());

    final Element gainsElem = rootElement.getChild("gains");
    double[] gainsArray = StringUtils.toDoubleArray(gainsElem.getValue().trim(), " ");
    if (flipLUT) {
      double tmp;
      for (int i = 0; i < gainsArray.length / 2; i++) {
        tmp = gainsArray[i];
        gainsArray[i] = gainsArray[gainsArray.length - i - 1];
        gainsArray[gainsArray.length - i - 1] = tmp;
      }
    }

    final MetadataElement lut = new MetadataElement(lutName);
    root.addElement(lut);

    final MetadataAttribute offsetAttrib =
        new MetadataAttribute("offset", ProductData.TYPE_FLOAT64);
    offsetAttrib.getData().setElemDouble(offset);
    lut.addAttribute(offsetAttrib);

    final MetadataAttribute gainsAttrib =
        new MetadataAttribute("gains", ProductData.TYPE_FLOAT64, gainsArray.length);
    gainsAttrib.getData().setElems(gainsArray);
    lut.addAttribute(gainsAttrib);
  }
Ejemplo n.º 2
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);
        }
      }
    }
  }
 public void testASCIIAttribute() {
   final MetadataAttribute attribute1 = new MetadataAttribute("name", ProductData.TYPE_ASCII, 1);
   attribute1.getData().setElems("new data");
   // attribute should be of type ASCII and not INT8
   assertEquals(attribute1.getDataType(), ProductData.TYPE_ASCII);
   final MetadataAttribute attribute2 =
       new MetadataAttribute("name", new ProductData.ASCII("my string"), false);
   // attribute should be of type ASCII and not INT8
   assertEquals(attribute2.getDataType(), ProductData.TYPE_ASCII);
 }
Ejemplo n.º 4
0
 /**
  * Adds a new coding value to this sample coding.
  *
  * @param name the coding name
  * @param values the values
  * @param description the description text
  * @throws IllegalArgumentException if <code>name</code> is null
  * @return A new attribute representing the coded sample.
  */
 public MetadataAttribute addSamples(String name, int[] values, String description) {
   Guardian.assertNotNull("name", name);
   final ProductData productData =
       ProductData.createInstance(ProductData.TYPE_INT32, values.length);
   MetadataAttribute attribute = new MetadataAttribute(name, productData, false);
   attribute.setDataElems(values);
   if (description != null) {
     attribute.setDescription(description);
   }
   addAttribute(attribute);
   return attribute;
 }
Ejemplo n.º 5
0
 /**
  * Adds an attribute to this node. If an attribute with the same name already exists, the method
  * does nothing.
  *
  * @param attribute the attribute to be added
  * @throws IllegalArgumentException if the attribute added is not an integer or does not have a
  *     scalar value
  */
 @Override
 public void addAttribute(MetadataAttribute attribute) {
   if (!attribute.getData().isInt()) {
     throw new IllegalArgumentException("attribute value is not a integer");
   }
   if (attribute.getData().getNumElems() == 0) {
     throw new IllegalArgumentException("attribute value is missing");
   }
   /*
   if (!attribute.getData().isScalar()) {
       throw new IllegalArgumentException("attribute value is not a scalar");
   }
   */
   super.addAttribute(attribute);
 }
 public void testSetValueWithWrongType() {
   // rejects illegal type?
   try {
     _attributeInt.setDataElems("5");
     fail("IllegalArgumentException expected");
   } catch (IllegalArgumentException e) {
   }
 }
Ejemplo n.º 7
0
  private static void setLatLongMetadata(
      final Product product,
      final MetadataElement absRoot,
      final String tagLat,
      final String tagLon,
      final float x,
      final float y) {
    final PixelPos pixelPos = new PixelPos(x, y);
    final GeoPos geoPos = new GeoPos();
    if (product.getGeoCoding() == null) return;
    product.getGeoCoding().getGeoPos(pixelPos, geoPos);

    final MetadataAttribute lat = absRoot.getAttribute(tagLat);
    if (lat != null) lat.getData().setElemDouble(geoPos.getLat());
    final MetadataAttribute lon = absRoot.getAttribute(tagLon);
    if (lon != null) lon.getData().setElemDouble(geoPos.getLon());
  }
  public void testSetData() {

    try {
      _attributeInt.setDataElems(null);
      fail("IllegalArgumentException expected because data is null");
    } catch (IllegalArgumentException e) {
    }

    // null --> new value: is modified ?
    _attributeInt.setDataElems(new int[] {1, 2, 3});
    assertEquals(true, Arrays.equals(new int[] {1, 2, 3}, (int[]) _attributeInt.getDataElems()));
    assertEquals(true, _attributeInt.isModified());

    // old value == new value?
    _attributeInt.setDataElems(new int[] {1, 2, 3});
    assertEquals(true, Arrays.equals(new int[] {1, 2, 3}, (int[]) _attributeInt.getDataElems()));
    assertEquals(true, _attributeInt.isModified());
  }
Ejemplo n.º 9
0
  private void writeMetadataAttribute(int locationID, MetadataAttribute attribute)
      throws IOException {
    int productDataType = attribute.getDataType();
    if (attribute.getData() instanceof ProductData.ASCII
        || attribute.getData() instanceof ProductData.UTC) {
      createScalarAttribute(locationID, attribute.getName(), attribute.getData().getElemString());
    } else if (attribute.getData().isScalar()) {
      createScalarAttribute(
          locationID,
          attribute.getName(),
          getH5DataType(productDataType),
          attribute.getData().getElems());
    } else {
      createArrayAttribute(
          locationID,
          attribute.getName(),
          getH5DataType(productDataType),
          attribute.getData().getNumElems(),
          attribute.getData().getElems());
    }
    if (_metadataAnnotated) {
      if (attribute.getUnit() != null) {
        createScalarAttribute(locationID, attribute.getName() + ".unit", attribute.getUnit());
      }

      if (attribute.getDescription() != null) {
        createScalarAttribute(
            locationID, attribute.getName() + ".descr", attribute.getDescription());
      }
    }
  }
Ejemplo n.º 10
0
  private static void updateMetadata(
      final Product sourceProduct, final Product targetProduct, ProductSubsetDef subsetDef)
      throws IOException {

    try {
      final MetadataElement root = targetProduct.getMetadataRoot();
      if (root == null) return;

      final MetadataElement absRoot = root.getElement("Abstracted_Metadata");
      if (absRoot == null) return;

      boolean nearRangeOnLeft = isNearRangeOnLeft(targetProduct);

      final MetadataAttribute firstLineTime = absRoot.getAttribute("first_line_time");
      if (firstLineTime != null) {
        final ProductData.UTC startTime = targetProduct.getStartTime();
        if (startTime != null) firstLineTime.getData().setElems(startTime.getArray());
      }
      final MetadataAttribute lastLineTime = absRoot.getAttribute("last_line_time");
      if (lastLineTime != null) {
        final ProductData.UTC endTime = targetProduct.getEndTime();
        if (endTime != null) lastLineTime.getData().setElems(endTime.getArray());
      }
      final MetadataAttribute totalSize = absRoot.getAttribute("total_size");
      if (totalSize != null) totalSize.getData().setElemUInt(targetProduct.getRawStorageSize());

      if (nearRangeOnLeft) {
        setLatLongMetadata(targetProduct, absRoot, "first_near_lat", "first_near_long", 0.5f, 0.5f);
        setLatLongMetadata(
            targetProduct,
            absRoot,
            "first_far_lat",
            "first_far_long",
            targetProduct.getSceneRasterWidth() - 1 + 0.5f,
            0.5f);

        setLatLongMetadata(
            targetProduct,
            absRoot,
            "last_near_lat",
            "last_near_long",
            0.5f,
            targetProduct.getSceneRasterHeight() - 1 + 0.5f);
        setLatLongMetadata(
            targetProduct,
            absRoot,
            "last_far_lat",
            "last_far_long",
            targetProduct.getSceneRasterWidth() - 1 + 0.5f,
            targetProduct.getSceneRasterHeight() - 1 + 0.5f);
      } else {
        setLatLongMetadata(
            targetProduct,
            absRoot,
            "first_near_lat",
            "first_near_long",
            targetProduct.getSceneRasterWidth() - 1 + 0.5f,
            0.5f);
        setLatLongMetadata(targetProduct, absRoot, "first_far_lat", "first_far_long", 0.5f, 0.5f);

        setLatLongMetadata(
            targetProduct,
            absRoot,
            "last_near_lat",
            "last_near_long",
            targetProduct.getSceneRasterWidth() - 1 + 0.5f,
            targetProduct.getSceneRasterHeight() - 1 + 0.5f);
        setLatLongMetadata(
            targetProduct,
            absRoot,
            "last_far_lat",
            "last_far_long",
            0.5f,
            targetProduct.getSceneRasterHeight() - 1 + 0.5f);
      }

      final MetadataAttribute height = absRoot.getAttribute("num_output_lines");
      if (height != null) height.getData().setElemUInt(targetProduct.getSceneRasterHeight());

      final MetadataAttribute width = absRoot.getAttribute("num_samples_per_line");
      if (width != null) width.getData().setElemUInt(targetProduct.getSceneRasterWidth());

      final MetadataAttribute offsetX = absRoot.getAttribute("subset_offset_x");
      if (offsetX != null && subsetDef.getRegion() != null)
        offsetX.getData().setElemUInt(subsetDef.getRegion().x);

      final MetadataAttribute offsetY = absRoot.getAttribute("subset_offset_y");
      if (offsetY != null && subsetDef.getRegion() != null)
        offsetY.getData().setElemUInt(subsetDef.getRegion().y);

      final MetadataAttribute slantRange = absRoot.getAttribute("slant_range_to_first_pixel");
      if (slantRange != null) {
        final TiePointGrid srTPG = targetProduct.getTiePointGrid("slant_range_time");
        if (srTPG != null) {
          final double slantRangeTime;
          if (nearRangeOnLeft) {
            slantRangeTime = srTPG.getPixelDouble(0, 0) / 1000000000.0; // ns to s
          } else {
            slantRangeTime =
                srTPG.getPixelDouble(targetProduct.getSceneRasterWidth() - 1, 0)
                    / 1000000000.0; // ns to s
          }
          final double halfLightSpeed = 299792458.0 / 2.0;
          final double slantRangeDist = slantRangeTime * halfLightSpeed;
          slantRange.getData().setElemDouble(slantRangeDist);
        }
      }

      setSubsetSRGRCoefficients(sourceProduct, targetProduct, subsetDef, absRoot, nearRangeOnLeft);
    } catch (Exception e) {
      throw new IOException(e);
    }
  }