Trouble propagating geocoding information with Snappy

Hello,

I’m developing a pre-processing workflow for S1 GRD data, for which I’m using Snappy/python (3.6). There’s a stage which requires me to step out of SNAP to do some operations, then back into .dim format to do terrain correction.

I’ve been trying to get snappy to produce a new product with bands reflecting the data that have been operated on elsewhere, but can’t get the new product to function in subsequent terrain correction because it lacks geocoding information. In this toy example I’m just copying bands from an original .dim to a new product. the bands and metadata display the same as the original in SNAP desktop, but I get a lack of geocoding error when I do RDTC.

How would I go about transferring geocoding information to the new product? I’ve tried
gc = p.getSceneGeoCoding()
target_product.setSceneGeoCoding(gc)
where p is the original .dim

Here’s the example of what I’ve been trying, I’ve put this together from a few days of trawling through the forums trying various things, but I can’t seem to make anything work. Any help appreciated.

import gdal
import numpy as np
import snappy
import sys, os

drive = os.path.normpath('Products')
productname = 'S1A_EW_GRDM_1SDH_20191003T042808_20191003T042912_029289_03540B_5F90'

product = productname + '.SAFE'
extension = '.tif'
filename = productname + '_Orb_TNR_Cal'

HashMap = snappy.jpy.get_type('java.util.HashMap')    
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

src = gdal.Open( os.path.join(drive, product,filename + extension) )

#Manipulate image data (or not)
band1 = src.GetRasterBand(1)
nda1=band1.ReadAsArray()
band2 = src.GetRasterBand(2)
nda2=band2.ReadAsArray()


#construct output band definitions
output = snappy.Product('titleI', 'title', src.RasterXSize, src.RasterYSize)
band1 = output.addBand('HH', snappy.ProductData.TYPE_FLOAT32)
band2 = output.addBand('HV', snappy.ProductData.TYPE_FLOAT32)

#copy metadata from the .dim version of the scene processed to Orb_TNR_Cal stage
p = snappy.ProductIO.readProduct(os.path.join(drive, product,filename + '.dim'))
snappy.ProductUtils.copyMetadata(p, output)

#set product writer
writer = snappy.ProductIO.getProductWriter('BEAM-DIMAP')
output.setProductWriter(writer)
#write header
output.writeHeader('snappy_output.dim')

#Write data into bands
band1.writePixels(0, 0, src.RasterXSize, src.RasterYSize, nda1)
band2.writePixels(0, 0, src.RasterXSize, src.RasterYSize, nda2)

How to attach geocoding information from another file has an (outdated?) example using snappy to copy geocoding from one product to a new product, and an example using gpt Merge that should work now (since SNAP 6). Since you are using snappy the old method might be appropriate.

Thanks. As simple as

ProductUtils.copyGeoCoding

I hadn’t realised that it wasn’t included in the metadata.