Snappy + Numpy help

Dear colleagues,

I am trying to:

  • extract the band values of a complex SAR image
  • execute a math algorith with bands (numpy)
  • write the resulting band to a BEAM-DIMAP file (original geocoding and metadata)

Here is my basic code:


# read bands
bi_HH = product.getBand('i_HH')
bq_HH = product.getBand('q_HH')
bi_HV = product.getBand('i_HV')
bq_HV = product.getBand('q_HV')
bi_VH = product.getBand('i_VH')
bq_VH = product.getBand('q_VH')
bi_VV = product.getBand('i_VV')
bq_VV = product.getBand('q_VV')

#create product
FR_product = snappy.Product(product.getProductType(), product.getProductType(), width, height)

#create band
FR_band = FR_product.addBand('FR_angle', ProductData.TYPE_FLOAT32)

#set metedata
ProductUtils.copyGeoCoding(product, FR_product)
ProductUtils.copyProductNodes(product, FR_product)
ProductUtils.copyMetadata(product, FR_product)


#create variables numpy
i_HH = numpy.zeros((width, height), dtype=numpy.float32) 
q_HH = numpy.zeros((width, height), dtype=numpy.float32) 
i_HV = numpy.zeros((width, height), dtype=numpy.float32)
q_HV = numpy.zeros((width, height), dtype=numpy.float32) 
i_VH = numpy.zeros((width, height), dtype=numpy.float32) 
q_VH = numpy.zeros((width, height), dtype=numpy.float32)
i_VV = numpy.zeros((width, height), dtype=numpy.float32)
q_VV = numpy.zeros((width, height), dtype=numpy.float32)

#set variables
bi_HH.readPixels(0, 0, width-1, height-1, i_HH)
bq_HH.readPixels(0, 0, width-1, height-1, q_HH)
bi_HV.readPixels(0, 0, width-1, height-1, i_HV)
bq_HV.readPixels(0, 0, width-1, height-1, q_HV)
bi_VH.readPixels(0, 0, width-1, height-1, i_VH)
bq_VH.readPixels(0, 0, width-1, height-1, q_VH)
bi_VV.readPixels(0, 0, width-1, height-1, i_VV)
bq_VV.readPixels(0, 0, width-1, height-1, q_VV)

#Evaluate Faraday Rotation Angle
FR = 0.25 * numpy.arctan( (-(q_HH- i_VH+ i_HV- q_VV)*(i_HH+ q_VH- q_HV+ i_VV) + (i_HH- q_VH+ q_HV+ i_VV)* (-q_HH+ i_VH- i_HV- q_VV)) / ((q_HH- i_VH+ i_HV- q_VV)* (-q_HH+ i_VH- i_HV- q_VV)+ (i_HH- q_VH+ q_HV+ i_VV)* (i_HH+ q_VH- q_HV+ i_VV))  )

#Write band
FR_band.writePixels(0, 0, width-1, height-1, FR)

#Create file
ProductIO.writeProduct(FR_product, 'FR2.dim', "BEAM-DIMAP")

gives the error:

ProductIO.writeProduct(FR_product, 'FR2.dim', "BEAM-DIMAP")
RuntimeError: java.lang.IllegalStateException: no product reader for band 'FR_angle'

If I change to:
FR_band.setPixels(0, 0, width-1, height-1, FR)

The error becomes:

FR_band.setPixels(0, 0, width-1, height-1, FR) RuntimeError: java.lang.NullPointerException

Please, any idea for helping me?


If you want to implement an algorithm and compute things I would suggest to implement an operator.

However, regarding your problem I think you miss


Call it directly after setting the writer to the product.
At the end you don’t need the ProductIO.writeProduct anymore if you have used the writePixels method of the band.

Here you can find an example implementation

1 Like

Yes! It worked, @marpet. Thank you!
Now the sky is the limit :slight_smile:

PS: I will improve my python scripting skills to develop operators, classes etc…