示例#1
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);
 }
示例#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);
        }
      }
    }
  }
示例#3
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);
    }
  }
示例#4
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();
   }
 }
示例#5
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*/
   }
 }
示例#6
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;
 }
示例#7
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());
  }
示例#8
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;
  }
示例#9
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);
    }
  }
示例#10
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;
 }