private static boolean isNearRangeOnLeft(final Product product) { final TiePointGrid incidenceAngle = product.getTiePointGrid("incident_angle"); if (incidenceAngle != null) { final double incidenceAngleToFirstPixel = incidenceAngle.getPixelDouble(0, 0); final double incidenceAngleToLastPixel = incidenceAngle.getPixelDouble(product.getSceneRasterWidth() - 1, 0); return (incidenceAngleToFirstPixel < incidenceAngleToLastPixel); } else { return true; } }
protected void addTiePointGridsToProduct(final Product product) { final GeoCoding geoCoding = getSourceProduct().getGeoCoding(); final String latGridName; final String lonGridName; if (geoCoding instanceof TiePointGeoCoding) { final TiePointGeoCoding tiePointGeoCoding = (TiePointGeoCoding) geoCoding; final TiePointGrid latGrid = tiePointGeoCoding.getLatGrid(); final TiePointGrid lonGrid = tiePointGeoCoding.getLonGrid(); latGridName = latGrid.getName(); lonGridName = lonGrid.getName(); } else { latGridName = null; lonGridName = null; } for (int i = 0; i < getSourceProduct().getNumTiePointGrids(); i++) { final TiePointGrid sourceTiePointGrid = getSourceProduct().getTiePointGridAt(i); final String gridName = sourceTiePointGrid.getName(); if (isNodeAccepted(gridName) || (gridName.equals(latGridName) || gridName.equals(lonGridName))) { final TiePointGrid tiePointGrid = TiePointGrid.createSubset(sourceTiePointGrid, getSubsetDef()); if (isFullScene(getSubsetDef()) && sourceTiePointGrid.isStxSet()) { copyStx(sourceTiePointGrid, tiePointGrid); } product.addTiePointGrid(tiePointGrid); copyImageInfo(sourceTiePointGrid, tiePointGrid); } } }
static void copyReferencedRasters( String validMaskExpression, Scene sourceScene, Scene targetScene, ProductSubsetDef subsetDef) throws ParseException { final Product targetProduct = targetScene.getProduct(); final RasterDataNode[] nodes = BandArithmetic.getRefRasters(validMaskExpression, sourceScene.getProduct()); for (RasterDataNode node : nodes) { if (!targetProduct.containsRasterDataNode(node.getName())) { if (node instanceof TiePointGrid) { TiePointGrid tpg = TiePointGrid.createSubset((TiePointGrid) node, subsetDef); targetProduct.addTiePointGrid(tpg); } if (node instanceof Band) { final Band sourceBand = (Band) node; final Band band = createSubset(sourceBand, targetScene, subsetDef); targetProduct.addBand(band); setFlagCoding(band, sourceBand.getFlagCoding()); } } } }
private void writeTiePointGrid(TiePointGrid grid, String path) throws IOException { final int w = grid.getRasterWidth(); final int h = grid.getRasterHeight(); long[] dims = new long[] {h, w}; int dataTypeID = -1; int dataSpaceID = -1; int datasetID = -1; try { dataTypeID = createH5TypeID(grid.getDataType()); dataSpaceID = H5.H5Screate_simple(2, dims, null); datasetID = H5.H5Dcreate( _fileID, path + "/" + grid.getName(), dataTypeID, dataSpaceID, HDF5Constants.H5P_DEFAULT); // Very important attributes createScalarAttribute(datasetID, "scene_raster_width", grid.getSceneRasterWidth()); createScalarAttribute(datasetID, "scene_raster_height", grid.getSceneRasterHeight()); createScalarAttribute(datasetID, "offset_x", grid.getOffsetX()); createScalarAttribute(datasetID, "offset_y", grid.getOffsetY()); createScalarAttribute(datasetID, "sub_sampling_x", grid.getSubSamplingX()); createScalarAttribute(datasetID, "sub_sampling_y", grid.getSubSamplingY()); // Less important attributes try { createScalarAttribute(datasetID, "raster_width", grid.getRasterWidth()); createScalarAttribute(datasetID, "raster_height", grid.getRasterHeight()); createScalarAttribute(datasetID, "unit", grid.getUnit()); createScalarAttribute(datasetID, "description", grid.getDescription()); createScalarAttribute(datasetID, "CLASS", "IMAGE"); createScalarAttribute(datasetID, "IMAGE_VERSION", 1.0F); } catch (IOException e) { /* ignore IOException because these attributes are not very essential... */ Debug.trace("failed to create attribute: " + e.getMessage()); } H5.H5Dwrite( datasetID, dataTypeID, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, grid.getData().getElems()); } catch (HDF5Exception e) { throw new ProductIOException(createErrorMessage(e)); } finally { closeH5D(datasetID); closeH5S(dataSpaceID); closeH5T(dataTypeID); } }
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); } }