Python scripts in Sentinel2-L1C

Hi to everyone,
I wanted ask you if there are any python script to calculate NDVI in Sentinel 2. Thanks in advance

Is it being a python script a necessity?

If not, I personally enjoy using the Orfeo Toolbox … namely the Band Math tool.

You can easily access it from the command-line and therefore from your preferred scripting environment.

if your input is a S2 multiband raster containing all bands, the syntax for the command-line looks like this:

otbcli_BandMath -il INPUT.tif -out OUTPUT.tif -exp ndvi(im1b8,im1b4)

However you could also input the bands seperately:

otbcli_BandMath -il RED.tif NIR.tif -out NDVI.tif -exp ndvi(im1b1,im2b1)


It normally comes with the basic OSGeo4W distribution …

It’s also easily doable on the command line with SNAP.
You can use gpt to do compute NDVI.

 gpt NdviOp -PnirSourceBand=B8 -PresSourceBand=B4 -t <path_to_target> <path_to_source>

However, a sample for doing it in python can be found in the snappy directory, if you have already installed it.
Or you can have a look in our source code repository. There you will find a file.

i tried to do this script in Python but it says: " To exit: use ‘exit’, ‘quit’, or Ctrl-D.
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1 ".

So i have a bit confusion with NDVI. If i want detect the vegetation of coast line with NDVI how i can do it? Thanks very much

and you have called it like the following?


yes i have called it but doesn’t work. can you help me about the second question? thank you.

Do other scripts work? It’s a strange message.

I think there are no or not many predefined building blocks which you can simply use for your use case.
First you need to detect the coastline then define the area around it and calculate the NDVI there.
You could use the ‘Fractional Land/Water Mask’. This way you can already decide if you are over water or land. If you use a sub-sampling, values between 0 and 100 are most likely coastal pixels.
And from this information ongoing you need to check the distance of each pixel if it is still in the coastal area you are interested in. And then compute the NDVI or not.
This is just a rough approach. But currently I can’t go into more detail.

Other simple scripts work. It’s very strange because the file " python " is saved in the folder snappy. However you have clarify a lot of exstraction coastal vegetation. Thank you