Is it possible to read whole band data as an array (as a raw data) from Python?

Once I obtained the band (org.esa.snap.core.datamodel.Band) from product:

import snappy
prod = snappy.ProductIO.readProduct(file1)
Band = prod.getBands()[0]

Am I able to read the whole content of a band as a raw data (as a numerical array), so that I can use those raw data in further computations?

According to documentation:
http://step.esa.int/docs/v2.0/apidoc/engine/org/esa/snap/core/datamodel/RasterDataNode.html#readRasterDataFully--
after executing:

Band.readRasterDataFully()

I should be able to get product data by executing:
readRasterData(int offsetX,int offsetY, int width, int height,ProductData rasterData)
but I am not sure which parameters should I provide to read whole band (and how to declare rasterData variable)

do you mean to read the whole band and for example to put it in a numpy matrix?

After doing readRasterDataFully() you can do:

array = band.getRasterData().getElems()

This will return an array of type float or integer. Depending on the band.

You can also use readRasterData(…) with the following parametrs:

rasterData = band.createCompatibleRasterData()
readRasterData(0, 0, band.getRasterWidth(), band.getRasterHeight(), rasterData)
array = rasterData.getElems()
2 Likes

I have read the band as array and perform some operations on this array now i want to save this array as tiff do you have any idea ?

to perform this i need to convert numpy.array to org.esa.snap.core.datamodel.Band
but i dont know how to do can you please let me know the idea?

@marpet @kedziorm

You can find several examples in in the example directory of your snappy installation
(e.g. ..\site-packages\snappy\examples).
The files snappy_flh.py and snappy_ndvi.py can be of interest for you.

@marpet in this code ;

targetProduct = Product('FLH_Product', 'FLH_Type', width, height)
targetBand = targetProduct.addBand('FLH', ProductData.TYPE_FLOAT32)
ProductUtils.copyGeoCoding(sourceProduct, targetProduct)
targetProduct.setProductWriter(ProductIO.getProductWriter('GeoTIFF'))

targetProduct.writeHeader(String('snappy_flh_output.tif'))

i don’t know what to put insted of ‘FLH_Product’, ‘FLH_Type’ ,‘FLH’
and
how to pass full path of image which i have to save.

my code is here

dataline=np.zeros(width*height)

VH = final.getBand('Sigma0_VH_db')
VV = final.getBand('Sigma0_VV_db')

arr_VH = VH.readPixels(0,0,width,height,dataline)
arr_VV = VV.readPixels(0,0,width,height,dataline)

sm = (img_VH + (img_VV - 15) * 1.1397) #this is final array i want to save as tiff

I want to save final array as tiff can you please see my code.

You can exchange ‘FLH_Product’, ‘FLH_Type’, ‘FLH’ with whatever you want. These are just names. Best would be if the names describe your data.

The absolute path can be provided as usual.
targetProduct.writeHeader(‘c:/temp/data/output/image.tif’)
or as it is done here: