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!”
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
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:
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, add
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.