Download Sentinel-2 data using Python or Command Line

Sentinel-2 by ESA - CopernicusEU

Dear Sentinel-2 enthusiasts!
First of all, I would like to wish you merry christmas and happy holidays. I thought I will use my time free time inbetween family gatherings first of all for digesting delicious christmas cookies and secondly for writing a short post on how to download Sentinel-2 satellite imagery using Python or the Command Line.

Why is this helpful?

Well, it depends on your application, but generally speaking¬†it allows you a higher degree of automatisation of your program/code because you won’t be forced to log in to a data providers website / portal (for example: scihub.copernicus.eu) anymore and look for images manually. From now on you will be able to download the newest Sentinel-2 images directly from your Python console or Command Line and hopefully be able safe some time. “The force is strong with this one!”

Prerequisites

Before we can start, we need to install the python library called “sentinelhub“. So how can we get it? It’s as easy as

pip install sentinelhub

If you don’t have access to pip, please follow these instructions for Linux or macOS.

Let’s start

Python

In order to download Sentinel-2 imagery via Python, all we need is the function:download_safe_format. Simply specify the tileID and the date you want to download as follows:

sentinelhub.download_safe_format(tile=('T33UXP', '2017-05-01'))

And voila! All bands will be downloaded into the current working directory. The nice thing about this function is that the data is downladed in the .SAFE format which is needed in order to execute sen2cor. If you want to download the entire product of the tile, adddownload_safe_format(..., entire_product=True).

Command Line

From the command line, the function works very similar. You can use the following line to get to the same result:

$ sentinelhub.aws --tile T33UXP 2017-05-01 -e

The flag -e again enables us to download the entire product corresponding to the selected tile.

Wrapping it up

Now let’s say we want to download all images from May 2017 for two tiles. The function above makes this fairly easy right? Here is an approch you can use:

#First import all needed modules
import sentinelhub
import pandas as pd 
import datetime

#specify tiles, for example two in the eastern part of Austria:
tiles = ["T33UXP","T33UVP"]

#specify start and end date (here we use May 2017)
dateStart = "2017-05-01"
dateEnd = "2017-05-31"

#create date range
dates = pd.date_range(start=dateStart, end=dateEnd)

#loop over tiles
for tile in tiles:
    print("Downloading tile: " + tile)
#loop over dates
    for date in dates:
        print(str(date.date()) + " ...")

        #try if there is a product available for the selected date
        try:
            sentinelhub.download_safe_format(tile=(tile, str(date.date())), entire_product=True)
        except Exception as ex:
            template = "No image for the specified tile / date combination could be found."
            message = template.format(type(ex).__name__)
            print(message)

Hope you found this helpful. If you need more information on the sentinelhub module, please have a look here or leave us a comment.

Martin

About This Author

Martin was born in Czech Republic and studied at the University of Natural Resources and Life Sciences, Vienna. He is currently working at GeoVille - an Earth Observation Company based in Austria, specialised in Land Monitoring. His main interests are: Open-source applications like R, (geospatial) statistics and data-management, web-mapping and visualization. He loves travelling, geocaching, photography and sports.

2 Comments

You can post comments in this post.


  • Hi Martin!
    I found very helpful your code! I tried to use it for the S2 in 2016 and I was able to download all the available .SAFE folders. But now I find an issue.. I am not able to open these files with SNAP. It seems that these folder are empty. What am I doing wrong?

    Laura 5 months ago Reply


Post A Reply

*