Hi,
I am trying to use numpy to stack bands and filter the result then write it to file. However I get the error “no product reader” or a “null pointer exception”. I hase seen these documented but no real solution offered. The source file is a stack built with SNAP. I have tried to create a file just with the band, add a band to the existing stack, following the different tutorials but nothing seems to work.
Here is the code.
import os, subprocess
sys.path.append(os.environ['HOME']+os.sep+'.snap'+os.sep+'snap-python')
import esa_snappy as es
import numpy as np
def process_array(p,band, X, Y):
'''
Subroutine to extract numpy array from band'''
print(f"converting band {band} to numpy array")
source=p.getBand(band)
width = p.getSceneRasterWidth()
height = p.getSceneRasterHeight()
print(f"Array dimensions {height} x {width}")
arr=np.zeros((height,width))
source.readPixels(0, 0, width, height, arr)
arr[arr!=0] -=arr[Y,X]
print("Array type: ",arr.dtype)
print("sum of data: ",arr.sum())
print("Array shape: ", arr.shape)
return arr
X,Y=2258,1214 #pixel coordinates for referencing
stack=es.ProductIO.readProduct('Path_to_stack.dim')
bands=list(stack.getBandNames())
for i in range(len(bands)):
arr = process_array(stack, bands[i], X, Y)
if i ==0:
host=np.zeros((len(bands),arr.shape[0],arr.shape[1]))
host[i] = arr
host_stack=np.sum(host,axis=0)
stack_m=es.Product('Stack_B6_m','Type', stack.getSceneRasterWidth(), stack.getSceneRasterHeight())
es.ProductUtils.copyMetadata(stack, stack_m)
stack_m.setDescription('Calibrated Stack')
stack_m.setProductWriter(es.ProductIO.getProductWriter("BEAM-DIMAP"))
stack_m.writeHeader(es.String('B6_Stack_m.dim'))
es.ProductUtils.copyGeoCoding(stack,stack_m)
targetband=stack_m.addBand('stacked_d', es.ProductData.TYPE_FLOAT32)
targetband.setPixels(0,0,stack.getSceneRasterWidth(),stack.getSceneRasterHeight(),host_stack)
es.ProductIO.writeProduct(stack_m, os.path.join(work_dir,'unwrapped','B6_Stack_m.dim')
,"BEAM-DIMAP")
I also tried to
targetband.writePixels(0,0,stack.getSceneRasterWidth(),stack.getSceneRasterHeight(),host_stack)
But it is not happy either. In the examples there are some confusions between row and column oriented arrays. So it is unclear if the array needs to be flat and in which order but I don’t really think that’s the issue, is it?
host_stack is correctly coded and contain data however the layer is only filled with 0s.
Thanks in advance for your answer