S2 NDVI batch processing with Python

ndvi_processor.py (3.6 KB)

I’m adding the Python program that does batch NDVI processing of Sentinel-2 products and masking them to an area.


  1. Create a directory and make sure all Sentinel-2 L2A products you want to be processed are in it.

  2. Create a GeoJSON mask enclosing the area of interest. Draw it with http://geojson.io and save it as field.json inside the same directory as the L2A products. What I’d do is copy-paste the JSON code to a text file and change the extension from .txt to .json.

  3. Make sure you have PyCharm CE installed together with Conda. The default package manager (pip) makes things very difficult.

  4. Create a new project and import the attached .py file.

  5. Open the file so that you can see the code. Make sure all frameworks at the top of the .py file are imported (installed).

  6. Scroll all the way to the bottom and you’ll see the line: ndvi_batch_processor('D:', 'field.json'). Replace 'D:' with the directory where the L2A zips and the field.json file are located.

  7. Run the entire thing.

What this program does is to search for zip files in the working directory, create a list of products to be processed, and then one-by-one unzip, create NDVI raster, mask to the GeoJSON, delete full-size NDVI raster and temporary folder with the unzipped product. You should be left with several NDVI files one for each of the source products.

Let me know how this works out.

ADDENDUM: As @cristianoLopes pointed out, I should improve my input; his points make good sense, at least to the extent I can understand, and I’ll try to do in due course.

In the meantime, there’s no license to this code, use it as you please.


Thanks for sharing this.
I moved it into the show room, which is the best location for sharing such results.

Interesting approach… several comments:

  • besides the academic nature, why not use something like Orfeo Toolbox to calculate this and other indices? Check this.
  • you should consider to use a requirements file than, from a python packaging/distribution perspective this is better than to ask people to install pycharm to resolve dependencies
  • instead of hard-coding your input directory, why not make this also a configurable parameter - in essence consider adding command line parameters (via a “name==main” block - note that I can’t add underlines without messing formatting: “_” )
  • please be explicit about which license you are providing this – this is very important

Thanks, Cristiano!

Regarding the 2nd bullet point, is this a good starting point?

Regarding the 3rd one, any examples / suggestions?

Actually this isn’t as simple as this… you own the copyright and must explain in what conditions you made this available and what people can expect. That’s what a license is for. In most of Europe the concept of public domain that exists in other countries doesn’t apply in the same way. My suggestion is to use one of the know open source licenses.

Perhaps you can have a look at this other thread:

Python S1-TOPS-SPLIT Analyzer

Most of the discussion there applies here.
For the license I would consider something simple like MIT or Free Public License

You can use that code as an example for command line parameters in you code.

With only two parameters for the command line, you can probably get away with something simpler.
Here are a couple of references for command line parsing in python:
Python 3 - Command Line Arguments - Tutorialspoint
Python Command Line Arguments – Real Python

Looks like it… anything related to managing requirements/environments should give you some good background.

1 Like