@Override protected Product readProductNodesImpl() throws IOException { final String s = getInput().toString(); final File file0 = new File(s); final File dir = file0.getParentFile(); final S2FilenameInfo fni0 = S2FilenameInfo.create(file0.getName()); if (fni0 == null) { throw new IOException(); } Header metadataHeader = null; final Map<Integer, BandInfo> fileMap = new HashMap<Integer, BandInfo>(); if (dir != null) { File[] files = dir.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(Sentinel2ProductReaderPlugIn.JP2_EXT); } }); if (files != null) { for (File file : files) { int bandIndex = fni0.getBand(file.getName()); if (bandIndex >= 0 && bandIndex < WAVEBAND_INFOS.length) { final S2WavebandInfo wavebandInfo = WAVEBAND_INFOS[bandIndex]; BandInfo bandInfo = new BandInfo( file, bandIndex, wavebandInfo, imageLayouts[wavebandInfo.resolution.id]); fileMap.put(bandIndex, bandInfo); } } } File[] metadataFiles = dir.listFiles( new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith("MTD_") && name.endsWith(".xml"); } }); if (metadataFiles != null && metadataFiles.length > 0) { File metadataFile = metadataFiles[0]; try { metadataHeader = Header.parseHeader(metadataFile); } catch (JDOMException e) { BeamLogManager.getSystemLogger() .warning("Failed to parse metadata file: " + metadataFile); } } else { BeamLogManager.getSystemLogger().warning("No metadata file found"); } } final ArrayList<Integer> bandIndexes = new ArrayList<Integer>(fileMap.keySet()); Collections.sort(bandIndexes); if (bandIndexes.isEmpty()) { throw new IOException("No valid bands found."); } String prodType = "S2_MSI_" + fni0.procLevel; final Product product = new Product( String.format("%s_%s_%s", prodType, fni0.orbitNo, fni0.tileId), prodType, imageLayouts[S2Resolution.R10M.id].width, imageLayouts[S2Resolution.R10M.id].height); try { product.setStartTime(ProductData.UTC.parse(fni0.start, "yyyyMMddHHmmss")); } catch (ParseException e) { // warn } try { product.setEndTime(ProductData.UTC.parse(fni0.stop, "yyyyMMddHHmmss")); } catch (ParseException e) { // warn } if (metadataHeader != null) { SceneDescription sceneDescription = SceneDescription.create(metadataHeader); int tileIndex = sceneDescription.getTileIndex(fni0.tileId); Envelope2D tileEnvelope = sceneDescription.getTileEnvelope(tileIndex); Header.Tile tile = metadataHeader.getTileList().get(tileIndex); try { product.setGeoCoding( new CrsGeoCoding( tileEnvelope.getCoordinateReferenceSystem(), imageLayouts[S2Resolution.R10M.id].width, imageLayouts[S2Resolution.R10M.id].height, tile.tileGeometry10M.upperLeftX, tile.tileGeometry10M.upperLeftY, tile.tileGeometry10M.xDim, -tile.tileGeometry10M.yDim, 0.0, 0.0)); } catch (FactoryException e) { // todo - handle e } catch (TransformException e) { // todo - handle e } } for (Integer bandIndex : bandIndexes) { final BandInfo bandInfo = fileMap.get(bandIndex); final Band band = product.addBand(bandInfo.wavebandInfo.bandName, ProductData.TYPE_UINT16); band.setSpectralWavelength((float) bandInfo.wavebandInfo.centralWavelength); band.setSpectralBandwidth((float) bandInfo.wavebandInfo.bandWidth); band.setSpectralBandIndex(bandIndex); band.setSourceImage(new DefaultMultiLevelImage(new Jp2MultiLevelSource(bandInfo))); } product.setNumResolutionLevels(imageLayouts[0].numResolutions); return product; }
protected void addBandsToProduct(Product product) { Debug.assertNotNull(getSourceProduct()); Debug.assertNotNull(product); for (int i = 0; i < getSourceProduct().getNumBands(); i++) { Band sourceBand = getSourceProduct().getBandAt(i); String bandName = sourceBand.getName(); if (isNodeAccepted(bandName)) { Band destBand; boolean treatVirtualBandsAsRealBands = false; if (getSubsetDef() != null && getSubsetDef().getTreatVirtualBandsAsRealBands()) { treatVirtualBandsAsRealBands = true; } // @todo 1 se/se - extract copy of a band or virtual band to create deep clone of band and // virtual band if (!treatVirtualBandsAsRealBands && sourceBand instanceof VirtualBand) { VirtualBand virtualSource = (VirtualBand) sourceBand; destBand = new VirtualBand( bandName, sourceBand.getDataType(), getSceneRasterWidth(), getSceneRasterHeight(), virtualSource.getExpression()); } else { destBand = new Band( bandName, sourceBand.getDataType(), getSceneRasterWidth(), getSceneRasterHeight()); } if (sourceBand.getUnit() != null) { destBand.setUnit(sourceBand.getUnit()); } if (sourceBand.getDescription() != null) { destBand.setDescription(sourceBand.getDescription()); } destBand.setScalingFactor(sourceBand.getScalingFactor()); destBand.setScalingOffset(sourceBand.getScalingOffset()); destBand.setLog10Scaled(sourceBand.isLog10Scaled()); destBand.setSpectralBandIndex(sourceBand.getSpectralBandIndex()); destBand.setSpectralWavelength(sourceBand.getSpectralWavelength()); destBand.setSpectralBandwidth(sourceBand.getSpectralBandwidth()); destBand.setSolarFlux(sourceBand.getSolarFlux()); if (sourceBand.isNoDataValueSet()) { destBand.setNoDataValue(sourceBand.getNoDataValue()); } destBand.setNoDataValueUsed(sourceBand.isNoDataValueUsed()); destBand.setValidPixelExpression(sourceBand.getValidPixelExpression()); FlagCoding sourceFlagCoding = sourceBand.getFlagCoding(); IndexCoding sourceIndexCoding = sourceBand.getIndexCoding(); if (sourceFlagCoding != null) { String flagCodingName = sourceFlagCoding.getName(); FlagCoding destFlagCoding = product.getFlagCodingGroup().get(flagCodingName); Debug.assertNotNull( destFlagCoding); // should not happen because flag codings should be already in // product destBand.setSampleCoding(destFlagCoding); } else if (sourceIndexCoding != null) { String indexCodingName = sourceIndexCoding.getName(); IndexCoding destIndexCoding = product.getIndexCodingGroup().get(indexCodingName); Debug.assertNotNull( destIndexCoding); // should not happen because index codings should be already in // product destBand.setSampleCoding(destIndexCoding); } else { destBand.setSampleCoding(null); } if (isFullScene(getSubsetDef()) && sourceBand.isStxSet()) { copyStx(sourceBand, destBand); } product.addBand(destBand); bandMap.put(destBand, sourceBand); } } for (final Map.Entry<Band, RasterDataNode> entry : bandMap.entrySet()) { copyImageInfo(entry.getValue(), entry.getKey()); } }