Reading .dim image in Python using snappy


#1

Hello,

I am trying to get an array of intensities of changes generated by Snap from 2 Sentinel1 images (called mosaic.dim). If I see an mosaic.dim image in Snap, I can see various values of intensities from -1 to 1, but if I try to read it in Python to an array of intensities, the only array I am receiving is an array full of 0.0 what is not correct. I have gone through this website and I have tried to read it similarly as I have seen it here but it does not work. Could you please tell me, how to read an array intensities from mosaic.dim image in Python correctly? I need to work with it afterwards.

Here is my code:

filename = "mosaic.dim"
# load picture and its proportions
product = ProductIO.readProduct(filename)
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()

#print(list(product.getBandNames()))
#get band and get array on intensity from band
intensity = product.getBand('ratio_VH')
rasterData = intensity.createCompatibleRasterData() #returns array of 0.0 instead of intensties
array = rasterData.getElems()
intensity_pixels = np.zeros(width * height, np.float32)
intensity_pixels.shape = height, width

#TODO:result = changeValuesinArray(array)
#TODO:save result to a new dim file

Furthermore, I would like to change some values in array of intensities and save it as a new .dim image, that I could work with it in Snap. Do you know, how to do that in Python as well?


#2

You can search for readPixels in this forum and you will find several examples

Especially the following post shows how to read, change and write data.
I think it wil be help you further.

However if you and to read, change and write, then you should think about implementing an operator. This can be done either in Python or Java.
How to write a processor in Python


#3

Hello, Thank you very much for the response. I have one more question. I have an intensity values in pixels (NOT rgb). And I have somehow change them. Now, I want to save this new pixels to new .dim file. I am trying to do it like that but it doesnt work. Can you please tell me, what is wrong?

filename = “img2.dim”
# load picture and its proportions
product = ProductIO.readProduct(filename)
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()

#print(list(product.getBandNames()))
#get band and get array on intensity from band
band = product.getBand('ratio_VH')
dataline=np.zeros(width*height)
intensitiesArray=band.readPixels(0,0,width,height,dataline) #this seems to works fine

#process image and save it to the output
threshold = computeTreshold(intensitiesArray, width, height)
print(threshold)
pixelsNew = createNewMaskAboveTreshold(intensitiesArray, width, height, threshold)

pixelsNew.shape = height, width
product.setProductWriter(ProductIO.getProductWriter('BEAM-DIMAP'))
#Write intensities to band
band.writePixels(0, 0, width, height, pixelsNew)
#create file
ProductIO.writeProduct(product, 'result.dim', "BEAM-DIMAP")

(last paragraph - saving, is probably not working)