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
  /**
   * 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;
  }
Ejemplo n.º 3
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);
        }
      }
    }
  }
Ejemplo n.º 4
0
 private void addMask(
     MetadataAttribute metadataSample, String expression, Color color, Product product) {
   final ProductNodeGroup<Mask> maskGroup = product.getMaskGroup();
   final int width = product.getSceneRasterWidth();
   final int height = product.getSceneRasterHeight();
   Mask mask =
       Mask.BandMathsType.create(
           metadataSample.getName().toLowerCase(),
           metadataSample.getDescription(),
           width,
           height,
           expression,
           color,
           0.5);
   maskGroup.add(mask);
 }
Ejemplo n.º 5
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());
  }
Ejemplo n.º 6
0
  static boolean addDiagnosticSitePin(Product product) {
    final MetadataElement pa = product.getMetadataRoot().getElement(GLOBAL_ATTRIBUTES);
    if (pa == null || product.getGeoCoding() == null) {
      return false;
    }

    final MetadataAttribute siteIdAttr = pa.getAttribute(ProductAttributes.SITE_ID);
    final MetadataAttribute siteLonAttr = pa.getAttribute(ProductAttributes.SITE_LON);
    final MetadataAttribute siteLatAttr = pa.getAttribute(ProductAttributes.SITE_LAT);
    if (siteIdAttr == null || siteLonAttr == null || siteLatAttr == null) {
      return false;
    }

    final String siteId = siteIdAttr.getData().getElemString();
    final float siteLon = siteLonAttr.getData().getElemFloat();
    final float siteLat = siteLatAttr.getData().getElemFloat();

    final String pinName = new StringBuilder("SITE").append("_").append(siteId).toString();
    if (product.getPinGroup().contains(pinName)) {
      return false;
    }

    final String pinLabel = pa.getAttributeString(ProductAttributes.SITE_NAME, pinName);

    final Placemark pointPlacemark =
        Placemark.createPointPlacemark(
            PinDescriptor.getInstance(),
            pinName,
            pinLabel,
            "GlobColour diagnostic site",
            null,
            new GeoPos(siteLat, siteLon),
            product.getGeoCoding());
    product.getPinGroup().add(pointPlacemark);

    return true;
  }
Ejemplo n.º 7
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;
 }
Ejemplo n.º 8
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.º 9
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);
    }
  }