Skip to content

Commit a1a34be

Browse files
committed
[SNAP-3493] SampleCoding of bands can get lost during collocation
1 parent 9634766 commit a1a34be

2 files changed

Lines changed: 114 additions & 43 deletions

File tree

snap-collocation/src/main/java/org/esa/snap/collocation/CollocateOp.java

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,24 @@ public void initialize() throws OperatorException {
346346
ProductUtils.copyTiePointGrids(masterProduct, targetProduct);
347347

348348
// Add master bands
349+
ProductNodeGroup<FlagCoding> flagCodingGroup = targetProduct.getFlagCodingGroup();
350+
ProductNodeGroup<IndexCoding> indexCodingGroup = targetProduct.getIndexCodingGroup();
349351
for (Band sourceBand : masterProduct.getBands()) {
350352
Band targetBand = ProductUtils.copyBand(sourceBand.getName(), masterProduct, targetProduct, true);
351-
handleSampleCodings(sourceBand, targetBand, renameMasterComponents, masterComponentPattern);
353+
FlagCoding flagCoding = targetBand.getFlagCoding();
354+
if (flagCoding != null) {
355+
// first remove FlagCoding potentially added by copyBand() then handle the FlagCoding
356+
flagCodingGroup.remove(flagCoding);
357+
targetBand.setSampleCoding(null);
358+
handleFlagCoding(sourceBand, targetBand, renameMasterComponents, masterComponentPattern);
359+
}
360+
IndexCoding indexCoding = targetBand.getIndexCoding();
361+
if (indexCoding != null) {
362+
// first remove IndexCoding potentially added by copyBand() then handle the IndexCoding
363+
indexCodingGroup.remove(indexCoding);
364+
targetBand.setSampleCoding(null);
365+
handleIndexCoding(sourceBand, targetBand, renameMasterComponents, masterComponentPattern);
366+
}
352367
sourceRasterMap.put(targetBand, sourceBand);
353368
if (renameMasterComponents) {
354369
targetBand.setName(masterComponentPattern.replace(SOURCE_NAME_REFERENCE, sourceBand.getName()));
@@ -383,8 +398,12 @@ public void initialize() throws OperatorException {
383398
// Add slave bands
384399
for (Band sourceBand : slaveProduct.getBands()) {
385400
String targetBandName = getTargetBandName(sourceBand, i);
386-
Band targetBand = targetProduct.addBand(targetBandName, sourceBand.getDataType());
401+
// first creating the band, then copying the properties and then adding it to the target product
402+
// if the
403+
Band targetBand = new Band(targetBandName, sourceBand.getDataType(),
404+
targetProduct.getSceneRasterWidth(), targetProduct.getSceneRasterHeight());
387405
ProductUtils.copyRasterDataNodeProperties(sourceBand, targetBand);
406+
targetProduct.addBand(targetBand);
388407
handleSampleCodings(sourceBand, targetBand, renameSlaveComponents, slaveComponentPattern);
389408
sourceRasterMap.put(targetBand, sourceBand);
390409
originalSlaveNames.put(targetBand.getName(), sourceBand.getName());
@@ -449,6 +468,7 @@ public void initialize() throws OperatorException {
449468
}
450469
}
451470

471+
452472
private String getTargetBandName(RasterDataNode rasterDataNode, int productIndex) {
453473
String rasterDataNodeName = rasterDataNode.getName();
454474
if (renameSlaveComponents) {
@@ -457,7 +477,7 @@ private String getTargetBandName(RasterDataNode rasterDataNode, int productIndex
457477
if (StringUtils.isNullOrEmpty(slaveComponentPattern)) {
458478
throw new OperatorException(format(
459479
"Target product already contains a raster data node with name ''{0}''. " +
460-
"Parameter 'slaveComponentPattern' must be set.",
480+
"Parameter 'slaveComponentPattern' must be set.",
461481
rasterDataNodeName));
462482
}
463483
return rename(rasterDataNodeName, productIndex);
@@ -654,7 +674,7 @@ private void collocateSourceBand(RasterDataNode sourceBand, Rectangle sourceRect
654674

655675
if (sourcePixelPos != null) {
656676
resampling.computeIndex(sourcePixelPos.x, sourcePixelPos.y,
657-
sourceRasterWidth, sourceRasterHeight, resamplingIndex);
677+
sourceRasterWidth, sourceRasterHeight, resamplingIndex);
658678
double sample;
659679
if (resampling == Resampling.NEAREST_NEIGHBOUR) {
660680
sample = sourceTile.getSampleDouble((int) resamplingIndex.i0, (int) resamplingIndex.j0);
@@ -717,9 +737,9 @@ private void copyMasks(Product sourceProduct, boolean rename, String pattern, Ma
717737
if (!targetProduct.getMaskGroup().contains(sourceMask.getName())) {
718738
Mask.ImageType imageType = sourceMask.getImageType();
719739
Mask targetMask = new Mask(sourceMask.getName(),
720-
targetProduct.getSceneRasterWidth(),
721-
targetProduct.getSceneRasterHeight(),
722-
imageType);
740+
targetProduct.getSceneRasterWidth(),
741+
targetProduct.getSceneRasterHeight(),
742+
imageType);
723743
targetMask.setDescription(sourceMask.getDescription());
724744
for (Property property : sourceMask.getImageConfig().getProperties()) {
725745
targetMask.getImageConfig().setValue(property.getDescriptor().getName(), property.getValue());
@@ -746,53 +766,46 @@ private void copyMasks(Product sourceProduct, boolean rename, String pattern, Ma
746766
}
747767

748768
private void handleSampleCodings(Band sourceBand, Band targetBand, boolean renameComponents, String renamePattern) {
749-
handleFlagCoding(sourceBand, targetBand, renameComponents, renamePattern);
750-
handleIndexCoding(sourceBand, targetBand, renameComponents, renamePattern);
769+
if (sourceBand.getFlagCoding() != null) {
770+
handleFlagCoding(sourceBand, targetBand, renameComponents, renamePattern);
771+
}
772+
if (sourceBand.getIndexCoding() != null) {
773+
handleIndexCoding(sourceBand, targetBand, renameComponents, renamePattern);
774+
}
751775
}
752776

753777
private void handleFlagCoding(Band sourceBand, Band targetBand, boolean renameComponents, String renamePattern) {
754-
if (sourceBand.getFlagCoding() != null) {
755-
targetBand.getProduct().getFlagCodingGroup().remove(targetBand.getFlagCoding());
756-
targetBand.setSampleCoding(null);
757-
}
758778
setFlagCoding(targetBand, sourceBand.getFlagCoding(), renameComponents, renamePattern);
759779
}
760780

761781
private void handleIndexCoding(Band sourceBand, Band targetBand, boolean renameComponents, String renamePattern) {
762-
if (sourceBand.getIndexCoding() != null) {
763-
targetBand.getProduct().getIndexCodingGroup().remove(targetBand.getIndexCoding());
764-
targetBand.setSampleCoding(null);
765-
}
766782
setIndexCoding(targetBand, sourceBand.getIndexCoding(), renameComponents, renamePattern);
767783
}
768784

769785
private void setFlagCoding(Band band, FlagCoding flagCoding, boolean rename, String pattern) {
770-
if (flagCoding != null) {
771-
String flagCodingName = flagCoding.getName();
772-
if (rename) {
773-
if (slaveProducts.length == 1) {
774-
flagCodingName = pattern.replace(SOURCE_NAME_REFERENCE, flagCodingName).replace(SLAVE_NUMBER_ID_REFERENCE, "");
775-
} else {
776-
int id = -1;
777-
for (int i = 0; i < slaveProducts.length; i++) {
778-
if (band.getProduct().getName().equals(slaveProducts[i].getName())) {
779-
id = i;
780-
break;
781-
}
786+
String flagCodingName = flagCoding.getName();
787+
if (rename) {
788+
if (slaveProducts.length == 1) {
789+
flagCodingName = pattern.replace(SOURCE_NAME_REFERENCE, flagCodingName).replace(SLAVE_NUMBER_ID_REFERENCE, "");
790+
} else {
791+
int id = -1;
792+
for (int i = 0; i < slaveProducts.length; i++) {
793+
if (band.getProduct().getName().equals(slaveProducts[i].getName())) {
794+
id = i;
795+
break;
782796
}
783-
flagCodingName = pattern.replace(SOURCE_NAME_REFERENCE, flagCodingName).replace(SLAVE_NUMBER_ID_REFERENCE, String.valueOf(id));
784797
}
798+
flagCodingName = pattern.replace(SOURCE_NAME_REFERENCE, flagCodingName).replace(SLAVE_NUMBER_ID_REFERENCE, String.valueOf(id));
785799
}
786-
final Product product = band.getProduct();
787-
if (!product.getFlagCodingGroup().contains(flagCodingName)) {
788-
addFlagCoding(product, flagCoding, flagCodingName);
789-
}
790-
band.setSampleCoding(product.getFlagCodingGroup().get(flagCodingName));
791800
}
801+
final Product product = band.getProduct();
802+
if (!product.getFlagCodingGroup().contains(flagCodingName)) {
803+
addFlagCoding(product, flagCoding, flagCodingName);
804+
}
805+
band.setSampleCoding(product.getFlagCodingGroup().get(flagCodingName));
792806
}
793807

794808
private void setIndexCoding(Band band, IndexCoding indexCoding, boolean rename, String pattern) {
795-
if (indexCoding != null) {
796809
String indexCodingName = indexCoding.getName();
797810
if (rename) {
798811
if (slaveProducts.length == 1) {
@@ -813,7 +826,6 @@ private void setIndexCoding(Band band, IndexCoding indexCoding, boolean rename,
813826
addIndexCoding(product, indexCoding, indexCodingName);
814827
}
815828
band.setSampleCoding(product.getIndexCodingGroup().get(indexCodingName));
816-
}
817829
}
818830

819831
private static void addFlagCoding(Product product, FlagCoding flagCoding, String flagCodingName) {

snap-collocation/src/test/java/org/esa/snap/collocation/CollocateOpTest.java

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.esa.snap.core.datamodel.MetadataElement;
2525
import org.esa.snap.core.datamodel.Product;
2626
import org.esa.snap.core.datamodel.ProductData;
27+
import org.esa.snap.core.datamodel.ProductNodeGroup;
2728
import org.esa.snap.core.datamodel.TiePointGrid;
2829
import org.esa.snap.core.datamodel.VirtualBand;
2930
import org.esa.snap.core.gpf.OperatorException;
@@ -95,9 +96,9 @@ public void testCollocate1Type() {
9596
assertEquals("!l1_flags_M.INVALID && radiance_1_M > 10", targetProduct.getBandAt(1).getValidMaskExpression());
9697

9798
assertEquals("!l1_flags_S.INVALID && radiance_1_S > 10",
98-
targetProduct.getBandAt(16 + 1).getValidMaskExpression());
99+
targetProduct.getBandAt(16 + 1).getValidMaskExpression());
99100
assertEquals("!l1_flags_S.INVALID && radiance_1_S > 10",
100-
targetProduct.getBandAt(16 + 2).getValidMaskExpression());
101+
targetProduct.getBandAt(16 + 2).getValidMaskExpression());
101102

102103
assertEquals(4, targetProduct.getMaskGroup().getNodeCount());
103104
Mask mask1 = targetProduct.getMaskGroup().get(0);
@@ -193,9 +194,9 @@ public void testCollocate2Types() {
193194
assertEquals("!l1_flags_M.INVALID && radiance_1_M > 10", targetProduct.getBandAt(1).getValidMaskExpression());
194195

195196
assertEquals("!l2_flags_S.INVALID && reflec_1_S > 0.1",
196-
targetProduct.getBandAt(16 + 1).getValidMaskExpression());
197+
targetProduct.getBandAt(16 + 1).getValidMaskExpression());
197198
assertEquals("!l2_flags_S.INVALID && reflec_1_S > 0.1",
198-
targetProduct.getBandAt(16 + 2).getValidMaskExpression());
199+
targetProduct.getBandAt(16 + 2).getValidMaskExpression());
199200

200201
assertEquals(3, targetProduct.getMaskGroup().getNodeCount());
201202
Mask mask1 = targetProduct.getMaskGroup().get(0);
@@ -271,11 +272,69 @@ public void testCollocate_failsWhenSlaveRenamingPatternIsMissing() {
271272
fail("Exception expected");
272273
} catch (OperatorException oe) {
273274
assertEquals("Target product already contains a raster data node with name 'latitude'. " +
274-
"Parameter slaveComponentPattern must be set.",
275-
oe.getMessage());
275+
"Parameter slaveComponentPattern must be set.",
276+
oe.getMessage());
276277
}
277278
}
278279

280+
@Test
281+
public void testCollocate_SampleCodingSOfMasterWhenNotRenamingMaster() {
282+
final Product masterProduct = createTestProduct1();
283+
final Product slaveProduct = createTestProduct1();
284+
285+
CollocateOp op = new CollocateOp();
286+
op.setParameterDefaultValues();
287+
op.setRenameMasterComponents(false);
288+
289+
op.setMasterProduct(masterProduct);
290+
op.setSlaveProduct(slaveProduct);
291+
292+
Product targetProduct = op.getTargetProduct();
293+
ProductNodeGroup<FlagCoding> flagCodingGroup = targetProduct.getFlagCodingGroup();
294+
assertEquals(3, flagCodingGroup.getNodeCount());
295+
assertTrue(flagCodingGroup.contains("l1_flags"));
296+
assertTrue(flagCodingGroup.contains("l1_flags_S"));
297+
assertTrue(flagCodingGroup.contains("collocationFlags"));
298+
assertTrue(flagCodingGroup.contains(targetProduct.getBand("l1_flags").getFlagCoding()));
299+
assertTrue(flagCodingGroup.contains(targetProduct.getBand("l1_flags_S").getFlagCoding()));
300+
301+
ProductNodeGroup<IndexCoding> indexCodingGroup = targetProduct.getIndexCodingGroup();
302+
assertEquals(2, indexCodingGroup.getNodeCount());
303+
assertTrue(indexCodingGroup.contains("l1_class"));
304+
assertTrue(indexCodingGroup.contains("l1_class_S"));
305+
assertTrue(indexCodingGroup.contains(targetProduct.getBand("l1_class").getIndexCoding()));
306+
assertTrue(indexCodingGroup.contains(targetProduct.getBand("l1_class_S").getIndexCoding()));
307+
}
308+
309+
@Test
310+
public void testCollocate_SampleCodingOfMasterWhenRenamingMaster() {
311+
final Product masterProduct = createTestProduct1();
312+
final Product slaveProduct = createTestProduct1();
313+
314+
CollocateOp op = new CollocateOp();
315+
op.setParameterDefaultValues();
316+
op.setRenameMasterComponents(true);
317+
318+
op.setMasterProduct(masterProduct);
319+
op.setSlaveProduct(slaveProduct);
320+
321+
Product targetProduct = op.getTargetProduct();
322+
ProductNodeGroup<FlagCoding> flagCodingGroup = targetProduct.getFlagCodingGroup();
323+
assertEquals(3, flagCodingGroup.getNodeCount());
324+
assertTrue(flagCodingGroup.contains("l1_flags_M"));
325+
assertTrue(flagCodingGroup.contains("l1_flags_S"));
326+
assertTrue(flagCodingGroup.contains("collocationFlags"));
327+
assertTrue(flagCodingGroup.contains(targetProduct.getBand("l1_flags_M").getFlagCoding()));
328+
assertTrue(flagCodingGroup.contains(targetProduct.getBand("l1_flags_S").getFlagCoding()));
329+
330+
ProductNodeGroup<IndexCoding> indexCodingGroup = targetProduct.getIndexCodingGroup();
331+
assertEquals(2, indexCodingGroup.getNodeCount());
332+
assertTrue(indexCodingGroup.contains("l1_class_M"));
333+
assertTrue(indexCodingGroup.contains("l1_class_S"));
334+
assertTrue(indexCodingGroup.contains(targetProduct.getBand("l1_class_M").getIndexCoding()));
335+
assertTrue(indexCodingGroup.contains(targetProduct.getBand("l1_class_S").getIndexCoding()));
336+
}
337+
279338
private static final float[] wl = new float[]{
280339
412.6395569f,
281340
442.5160217f,

0 commit comments

Comments
 (0)