CsvProductWriter extends AbstractProductWriter { @Override public void writeBandRasterData(Band sourceBand, int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, ProductData sourceBuffer, ProgressMonitor pm) throws IOException { if (productWritten) { return; } final Band[] bands = getSourceProduct().getBands(); final DataBuffer[] bandDataBuffers = new DataBuffer[bands.length]; final int[] bandTypes = new int[bands.length]; for (int i = 0; i < bands.length; i++) { final Band band = bands[i]; bandDataBuffers[i] = band.getGeophysicalImage().getData().getDataBuffer(); bandTypes[i] = bandDataBuffers[i].getDataType(); } final TiePointGrid[] tiePointGrids = getSourceProduct().getTiePointGrids(); final DataBuffer[] tpgDataBuffers = new DataBuffer[tiePointGrids.length]; final int[] tpgTypes = new int[tiePointGrids.length]; for (int i = 0; i < tiePointGrids.length; i++) { final TiePointGrid tpg = tiePointGrids[i]; tpgDataBuffers[i] = tpg.getGeophysicalImage().getData().getDataBuffer(); tpgTypes[i] = tpgDataBuffers[i].getDataType(); } for (int j = 0; j < getSourceProduct().getSceneRasterHeight(); j++) { for (int i = 0; i < getSourceProduct().getSceneRasterWidth(); i++) { StringBuilder line = new StringBuilder(); final int index = j * getSourceProduct().getSceneRasterWidth() + i; line.append(getFeatureIdFromMetadata(index)); for (int k = 0; k < bandDataBuffers.length; k++) { final DataBuffer buffer = bandDataBuffers[k]; line.append(separator); final Number elem; final int type = bandTypes[k]; if (type == DataBuffer.TYPE_INT || type == DataBuffer.TYPE_SHORT || type == DataBuffer.TYPE_USHORT || type == DataBuffer.TYPE_BYTE) { elem = buffer.getElem(index); } else if (type == DataBuffer.TYPE_FLOAT) { elem = buffer.getElemFloat(index); } else if (type == DataBuffer.TYPE_DOUBLE) { elem = buffer.getElemDouble(index); } else { throw new IllegalArgumentException("Undefined band data type '" + type + "' in source product."); } line.append(elem); } for (int k = 0; k < tpgDataBuffers.length; k++) { final DataBuffer buffer = tpgDataBuffers[k]; line.append(separator); final Number elem; final int type = tpgTypes[k]; if (type == DataBuffer.TYPE_INT || type == DataBuffer.TYPE_SHORT || type == DataBuffer.TYPE_USHORT || type == DataBuffer.TYPE_BYTE) { elem = buffer.getElem(index); } else if (type == DataBuffer.TYPE_FLOAT) { elem = buffer.getElemFloat(index); } else if (type == DataBuffer.TYPE_DOUBLE) { elem = buffer.getElemDouble(index); } else { throw new IllegalArgumentException("Undefined tie point grid data type '" + type + "' in source product."); } line.append(elem); } writeLine(line.toString()); } } productWritten = true; } CsvProductWriter(ProductWriterPlugIn plugIn, int config, Writer writer); @Override void writeBandRasterData(Band sourceBand, int sourceOffsetX, int sourceOffsetY,
                                    int sourceWidth, int sourceHeight, ProductData sourceBuffer,
                                    ProgressMonitor pm); @Override void flush(); @Override void close(); @Override void deleteOutput(); static final int WRITE_PROPERTIES; static final int WRITE_FEATURES; }
@Test public void testWriteFeatures() throws Exception { writer.writeBandRasterData(null, -1, -1, -1, -1, null, ProgressMonitor.NULL); final StringBuilder expected = new StringBuilder("featureId\tradiance_1:float\tradiance_2:double\tradiance_3:int\n"); expected.append("0\t0.0\t10.0\t100\n"); expected.append("1\t1.0\t11.0\t101\n"); expected.append("2\t2.0\t12.0\t102\n"); expected.append("3\t3.0\t13.0\t103\n"); expected.append("4\t4.0\t14.0\t104\n"); expected.append("5\t5.0\t15.0\t105"); assertEquals(expected.toString(), result.toString().trim()); }