MatchupFormatStrategy extends AbstractFormatStrategy { @Override public void writeMeasurements(Product product, PrintWriter writer, Measurement[] measurements) { for (Measurement measurement : measurements) { Measurement matchingMeasurement = findMatchingMeasurement(measurement, originalMeasurements); final boolean withExpression = expression != null && exportExpressionResult; if (expression == null || exportExpressionResult || measurement.isValid()) { for (int i = 0; i < getOriginalAttributeNames().size(); i++) { for (int j = 0; j < matchingMeasurement.getOriginalAttributeNames().length; j++) { String originalAttributeName = matchingMeasurement.getOriginalAttributeNames()[j]; Integer index = attributeIndices.get(originalAttributeName); if (index == i) { writeValue(writer, matchingMeasurement.getValues()[j]); } } writer.write("\t"); } writeLine(writer, measurement, withExpression); writer.write("\n"); } } } MatchupFormatStrategy(Measurement[] originalMeasurements,
                                 RasterNamesFactory rasterNamesFactory, int windowSize,
                                 String expression, boolean exportExpressionResult); @Override void writeHeader(PrintWriter writer, Product product); @Override void writeMeasurements(Product product, PrintWriter writer, Measurement[] measurements);  }
@Test public void testWriteMeasurements_oneMeasurement_withNaN() throws Exception { final Number[] values = {12.4, Double.NaN, 1.0345, 7}; final Measurement measurement = new Measurement(14, "name", 13, 1, 1, null, new GeoPos(10, 10), values, true); final Number[] originalValues = {13.4, Double.NaN, 2.0345, 12}; final Measurement originalMeasurement = new Measurement(14, null, -1, -1, -1, null, new GeoPos(10.1F, 10.01F), originalValues, new String[]{"sst", "tsm", "pigs", "cows"}, true); final FormatStrategy pixExFormat = new MatchupFormatStrategy( new Measurement[]{originalMeasurement}, null, 1, "expression", false); final StringWriter stringWriter = new StringWriter(); pixExFormat.writeMeasurements(null, new PrintWriter(stringWriter), new Measurement[]{measurement}); final BufferedReader reader = new BufferedReader(new StringReader(stringWriter.toString())); String originalMeasurementString = "13.4\t\t2.0345\t12"; String newMeasurementString = "\t13\t14\tname\t10.000000\t10.000000\t1.000\t1.000\t \t \t12.4\t\t1.0345\t7"; assertEquals(originalMeasurementString + newMeasurementString, reader.readLine()); assertNull(reader.readLine()); }
@Test public void testWriteTwoMeasurements() throws Exception { final Measurement originalMeasurement1 = createMeasurement(14, new String[]{"sst", "tsm"}, "_1"); final Measurement originalMeasurement2 = createMeasurement(23, new String[]{"pigs", "cows"}, "_2"); final Number[] values = {12.4, 7}; Measurement additionalMeasurement = new Measurement(14, "name", 13, 1, 1, null, new GeoPos(10, 10), values, true); final FormatStrategy pixExFormat = new MatchupFormatStrategy( new Measurement[]{ originalMeasurement1, originalMeasurement2 }, null, 1, "expression", false); StringWriter stringWriter = new StringWriter(); pixExFormat.writeMeasurements(null, new PrintWriter(stringWriter), new Measurement[]{additionalMeasurement}); String originalMeasurementString = "value1_1\tvalue2_1\t\t"; String newMeasurementString = "\t13\t14\tname\t10.000000\t10.000000\t1.000\t1.000\t \t \t12.4\t7\n"; assertEquals(originalMeasurementString + newMeasurementString, stringWriter.toString()); additionalMeasurement = new Measurement(23, "name", 13, 1, 1, null, new GeoPos(10, 10), values, true); stringWriter = new StringWriter(); pixExFormat.writeMeasurements(null, new PrintWriter(stringWriter), new Measurement[]{additionalMeasurement}); originalMeasurementString = "\t\tvalue1_2\tvalue2_2"; newMeasurementString = "\t13\t23\tname\t10.000000\t10.000000\t1.000\t1.000\t \t \t12.4\t7\n"; assertEquals(originalMeasurementString + newMeasurementString, stringWriter.toString()); }