Snappy problem and sen2cor

Hello, i am doing an algortihm for this:

  • Reading a .SAFE file
  • Run sen2cor and get the 2A level product
  • Read L2A. SAFE product

it works like this

import sys
import numpy
from snappy import Product
from snappy import ProductData
from snappy import ProductIO
from snappy import ProductUtils
from snappy import FlagCoding

import os

cmd = ‘L2A_Process --resolution 60 D:\S2A_MSIL1C_20170407T153621_N0204_R068_T18NUJ_20170407T154054.SAFE’
os.system(cmd)

file = (‘D:\S2A_MSIL1C_20170407T153621_N0204_R068_T18NUJ_20170407T154054.SAFE’)
print(“Reading…”)
product = ProductIO.readProduct(file)
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
name = product.getName()
description = product.getDescription()
band_names = product.getBandNames()

print(“Product: %s, %s” % (name, description))
print(“Raster size: %d x %d pixels” % (width, height))
print("Start time: " + str(product.getStartTime()))
print("End time: " + str(product.getEndTime()))
print(“Bands: %s” % (list(band_names)))

and the response is

Reading…
Product: MER_FRS_L1B_SUBSET, MERIS Full Resolution Geolocated and Calibrated TOA Radiance
Raster size: 221 x 184 pixels
Start time: 09-JUL-2007 11:14:55.493954
End time: 09-JUL-2007 11:15:03.545405
Bands: [‘radiance_1’, ‘radiance_2’, ‘radiance_3’, ‘radiance_4’, ‘radiance_5’, ‘radiance_6’, ‘radiance_7’, ‘radiance_8’, ‘radiance_9’, ‘radiance_10’, ‘radiance_11’, ‘radiance_12’, ‘radiance_13’, ‘radiance_14’, ‘radiance_15’, ‘l1_flags’, ‘detector_index’]

so this part is going well but now i have 2 problems i can’t solve
1- first one is cutting the L2A.safe file to my region of interest that is a square with this coordinates (Because the L2A is too big for processing):
‘POLYGON ((-74.911862 4.162501, -74.915142 4.162332, -74.914788 4.156032, -74.912351 4.156068))’

2 - the second one is that inside the polygon and obviously the image, i have to get the NDVI value of a specific pixel giving a coordinate

for example… i cut the image to my ROI , calculate NDVI of the image, give a coordinate and then i need the pixel value for that coordinate

thanks if anyone can guide me in this way because i have been trying for 2 weeks but i’m new in this

first question : why after processing a S-2 product your output is about a MERIS subset?

Sorry, my fault that is from the snappy examples directory, the corresponding response is :

Reading…
Product: S2A_MSIL2A_20170407T153621_N0204_R068_T18NUJ_20170407T154054, None
Raster size: 1830 x 1830 pixels
Start time: 07-APR-2017 15:36:21.026000
End time: 07-APR-2017 15:36:21.026000
Bands: [‘B1’, ‘B2’, ‘B3’, ‘B4’, ‘B5’, ‘B6’, ‘B7’, ‘B8A’, ‘B9’, ‘B11’, ‘B12’, ‘quality_aot’, ‘quality_wvp’, ‘quality_cloud_confidence’, ‘quality_snow_confidence’, ‘quality_scene_classification’, ‘view_zenith_mean’, ‘view_azimuth_mean’, ‘sun_zenith’, ‘sun_azimuth’, ‘view_zenith_B1’, ‘view_azimuth_B1’, ‘view_zenith_B2’, ‘view_azimuth_B2’, ‘view_zenith_B3’, ‘view_azimuth_B3’, ‘view_zenith_B4’, ‘view_azimuth_B4’, ‘view_zenith_B5’, ‘view_azimuth_B5’, ‘view_zenith_B6’, ‘view_azimuth_B6’, ‘view_zenith_B7’, ‘view_azimuth_B7’, ‘view_zenith_B8’, ‘view_azimuth_B8’, ‘view_zenith_B8A’, ‘view_azimuth_B8A’, ‘view_zenith_B9’, ‘view_azimuth_B9’, ‘view_zenith_B10’, ‘view_azimuth_B10’, ‘view_zenith_B11’, ‘view_azimuth_B11’, ‘view_zenith_B12’, ‘view_azimuth_B12’]

Your first problem is probably already solved.

For the second one. you can use the GeoCoding to retrieve the pixel coordinate for a geo-coordinate.

from snappy import (GeoPos, PixelPos)
gc = sourceProduct.getSceneGeoCoding()
#or in multi-resolution case use the geo-coding of the raster
#gc = sourceProduct.getRasterDataNode(“name”).getGeoCoding()
pixelPos = gc.getPixelPos(GeoPos(lat, lon), None)
x_pixel = geoPos.getX()
y_pixel = geoPos.getY()

There is also the PixEx operator. This can be invoked via GPF.createProduct(…). Just an alternative to get the values at a certain location.

the first one have been solved Thanks to your recomendation

but in the second one, i don’t need the Pixel position as X and Y , i need giving a coordinate point( e.g -74.911862 4.162501) and retrieve the NDVI value that is contained into the pixel Value after doing the band-math, i guess.

does this work in that case?
from snappy import (GeoPos, PixelPos)
gc = sourceProduct.getSceneGeoCoding()

Thank you for your help

When you have the pixel coordinates you can get the ndvi value at this location from the ndvi band.
You can find an example here:

well i have 2 .safe files:
1 for testing = 443MB
2 image i have to process = 8GB

so if i do my ROI algorithm with first file 1, it runs well

but if i run it with te file 2 shows this error:


RuntimeError Traceback (most recent call last)
in ()
21 geometry = WKTReader().read(wellKnownText)
22
—> 23 product = ProductIO.readProduct(file)
24
25 wktFeatureType = PlainFeatureFactory.createDefaultFeatureType(DefaultGeographicCRS.WGS84)

RuntimeError: java.lang.OutOfMemoryError: GC overhead limit exceeded

Have you adjusted the memory settings in the snappy.ini?

yes :confused: and i got this

RuntimeError: java.lang.OutOfMemoryError: Java heap space

To which value have you set it. Maybe it is not yet enough