Get numpy array using readPixels

Hi there,

(I’m a snappy newbie. ;-))
I am trying to get a numpy array from a S3A_SY_2_VGP product.
It works for one line, or a rectangle starting in the top left corner, but it won’t work for a rectangle somewhere in the middle.
This is (part of) my code:

import numpy as np
from snappy import ProductIO
testfile=’…/S3A_SY_2_VGP____20161101T061252_20161101T065709_20171209T023146_2657_010_248______LR1_R_NT_002.SEN3/xfdumanifest.xml’

product=ProductIO.readProduct(testfile)
width=product.getSceneRasterWidth()
height=product.getSceneRasterHeight()

b0=product.getBand(‘B0’)

dataline=np.zeros(width)
line=b0.readPixels(0,0,width,1,dataline) #this seems to works fine

databox=np.zeros(100*100)
box=b0.readPixels(0,0,100,100,databox) #this works too

databox2=np.zeros(100*100)
box2=b0.readPixels(100,100,200,200,databox2) #this won’t work!

I get an error message:

RuntimeError: java.lang.IllegalArgumentException: The length of the given array is less than 40000

What are the correct arguments for readPixels() ? I interpreted it as (startcolumn, startrow, endcolumn, endrow, outputarray), but this seems incorrect.

Thanks in advance for your help.

Hi cartot,
I’ve started to use snappy some months ago and this is a problem I also got stuck into at the beginning.
I solved this and many other issues by looking at the snappy API reference page --> (http://step.esa.int/docs/v5.0/apidoc/engine/)

By the way, the arguments of readPixels are not as you interpreted but are readPixels(x,y,w,h,Array) with:

  • x: x offset of upper left corner
  • y: y offset of upper left corner
  • w: width of the desired data array
  • h: height of the desired data array
  • Array: the Output Array to be filled with data

So in your case, if you want to get pixels data inside a m x n rectangle, you have to set w = m and h = n inside readPixels (no matter which offset you select), thus:

databox2=np.zeros(100*100)
box2=b0.readPixels(100,100,100,100,databox2) 

will do the job!

Hope this helps :wink:
Davide

2 Likes

Hi Davide,
It works! :clap:
Thanks a lot. This snappy API reference page will be very helpful…
Carolien

Hi, for further examples and applications of snappy you can refer to this exteranl resource in the STEP website : https://github.com/techforspace/sentinel

The repository is not rich at the moment, but it is still active and more elements will be added in the next months
Hope it can be useful!

1 Like