Snappy - copy bands from source product and write to a new band

Hi, I’m using snappy, and I want to write a product that contains

  • some bands copied from a source product
  • metadata and tie-point-grids copied from the same source product
  • a new custom band from data in a numpy array

I have been trying with ProductUtils.copyBand() and targetBand.WritePixels() - I can get one or the other to work separately, but not both.

It feels like this shouldn’t be difficult to implement - is anybody able to provide a simplest case working example?

To give you an idea - here is roughly what I’m working with (going round in circles trying permutations of).

W = source.getBandAt(0).getRasterWidth()
H = source.getBandAt(0).getRasterHeight()
targetP = snappy.Product('new_product','new_type',W,H)
targetP.setProductReader(ProductIO.getProductReader('BEAM-DIMAP'))
targetP.setProductWriter(ProductIO.getProductWriter('BEAM-DIMAP'))
ProductUtils.copyMetadata(source,targetP)
ProductUtils.copyTiePointGrids(source,targetP)
ProductUtils.copyBand(bandname,source,bandname,targetP,True)
targetB = targetP.addBand('new_band', snappy.ProductData.TYPE_FLOAT32)
targetB.setUnit(new_band_unit)
targetP.writeHeader('new_product_file.dim')
targetB.writePixels(0,0,W,H,data_array)
targetP.closeIO()

I now have a working example, but would still be great if anyone could come up with a cleaner solution or help me to understand what is going on behind the scenes.

Here is my solution:

W = source.getBandAt(0).getRasterWidth()
H = image.getBandAt(0).getRasterHeight()
targetP = snappy.Product(‘new_product’,‘new_type’,W,H)
ProductUtils.copyMetadata(source,targetP)
ProductUtils.copyTiePointGrids(source,targetP)
targetP.setProductWriter(ProductIO.getProductWriter(‘BEAM-DIMAP’))
ProductUtils.copyBand(bandname,source,bandname,targetP,True)
targetP.setProductReader(ProductIO.getProductReader(‘BEAM-DIMAP’))
ProductIO.writeProduct(targetP,‘OUTFILE.dim’,‘BEAM-DIMAP’)

targetB = targetP.addBand(‘newband’, snappy.ProductData.TYPE_FLOAT32)
targetB.setUnit(new_band_unit)
targetP.setProductWriter(ProductIO.getProductWriter(‘BEAM-DIMAP’))
targetP.writeHeader(‘OUTFILE.dim’)
targetB.writePixels(0,0,W,H,data_array)
targetP.closeIO()