Cannot write a product after band renaming

So I am working with sentinel 1 GRD data, I want to preprocess an image and write it, the problem is that if I rename the only band in the product to something else (e.g. Sigma0_VV ----> DESCENDING_Sigma0_VV), the ProductIO.writeProduct function returns: RuntimeError: org.esa.snap.core.gpf.OperatorException: java.lang.NullPointerException

If I dont rename the band it writes the product with no problem

indent preformatted text by 4 spaces 
pol = 'VV' 

for folder in os.listdir(in_path):
# ids
   fid = in_path + folder + "//"  
   timestamp = folder.split("_")[4] 
   date = timestamp[:8]

print('input file: {}'.format(fid))
sentinel_1 = ProductIO.readProduct(fid + "//")   
PASS = sentinel_1.getMetadataRoot().getElement('Abstracted_Metadata').getAttributeString('PASS')

#Apply orbit file
print('apply orbit file...')
orbit = apply_orbit_file(sentinel_1)
del sentinel_1 

# Radiometric calibration
print('radiometric calibration file...')
calibration = radiometric_calibration(orbit, pol)  

# Doppler terrain correction with SRTM3 DEM
print('doppler terrain correction with SRTM3 DEM...')
terr_corrected = terrain_correction(calibration, pol)
del calibration

print('Old band names:')
for i in terr_corrected.getBandNames():
print('Band rename...')
for i in terr_corrected.getBandNames():
    terr_corrected.getBand(i).setName('{}_Sigma0_{}'.format(PASS, pol))
print('new band names:')
for i in terr_corrected.getBandNames():

# Write a TIF file with a meaningful name
print('writing to: {}.tiff'.format(out_path + timestamp + '_' + PASS + '_' + pol))
ProductIO.writeProduct(terr_corrected, out_path + timestamp + '_' + PASS + '_' + pol, 'GeoTIFF-BigTIFF')

The problem is probably that the terrain correction references some of the bands by name.
As you probably know the actual computation is only done when the result is written to disk. If you change the band names the operator can’t work anymore.

You can work around this issue by creating a copy of the ‘terr_corrected’ product.

The easiest way is to use the createSubset method of the Product class.
The 3 parameters are the subset definition, the new product name and the product description text.
In your case you can set all params to None, then everything will be taken from the terr_corrected product.

out_product = terr_corrected.createSubset(None, None, None)

Then do the renaming on the ‘out_product’ and write it.

1 Like

So I just tried that and got a Nullpointerexception, but now I rephrased it with:
parameters = HashMap()
parameters.put(‘copyMetadata’, True)
out_product = GPF.createProduct(‘Subset’, parameters, terr_corrected).

Update: It worked perfectly