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
4 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 years ago
Hey Laura,
the package has seen some major improvements and changes since this blog post was published. This might yield different results than intended.
Check out the new specifications on how to download S2 in SAFE format here: http://sentinelhub-py.readthedocs.io/en/latest/examples/aws_request.html#Data-into-.SAFE-structure
Martin 5 years ago
Thanks a lot!
I will use some adaption of this script in my work.
Juanma 4 years ago
Hi Marin,
Thanks for your sharing.Now I want to download cloud-free S2 data. So how should I modify your script. Could you please give me some advice?Thank you very much.
Feng Gao 4 years ago
Post A Reply