5

I know it's possible to query products for Sentinel 2 via their SciHub, or using a tool like http://sentinelsat.readthedocs.io/en/master/

However, I need to somehow, given a product ID, get a URL to a specific band in JPEG200, like the following URL example:

https://scihub.copernicus.eu/apihub/odata/v1/Products('22e2fbfe-0aa7-423d-b0b5-df46527f03f5')/Nodes('S2A_MSIL1C_20170316T112101_N0204_R037_T29SNC_20170316T112658.SAFE')/Nodes('GRANULE')/Nodes('L1C_T29SNC_A009043_20170316T112658')/Nodes('IMG_DATA')/Nodes('T29SNC_20170316T112101_B04.jp2')/$value

This is the Band 4 of a given product.

3
  • Your second link doesn't work - it requires a username/password.
    – Midavalo
    Commented Mar 27, 2017 at 14:52
  • @Midavalo The Copernicus Open Access Hub requires self-registration prior to downloading images. It is however free of charge.
    – Kersten
    Commented Mar 28, 2017 at 11:39
  • By 'product ID' do you mean 22e2fbfe-0aa7-423d-b0b5-df46527f03f5 or S2A_MSIL1C_20170316T112101_N0204_R037_T29SNC_20170316T112658.SAFE? The first is the 'Unique Universal Identifier (UUID)', the second is referred to as 'Product Name' by ESA.
    – Kersten
    Commented Mar 28, 2017 at 11:43

3 Answers 3

5

Assuming you have the product identifier (22e2fbfe-0aa7-423d-b0b5-df46527f03f5) you can construct the query you want with a little parsing of the scihub odata response.

The constructed img_url can then be downloaded with the tool/library of your choice.

import requests
from six.moves.urllib.parse import urljoin

# connect to the api
api_session = requests.Session()
api_session.auth = ("username", "password")
api_url = "https://scihub.copernicus.eu/apihub/odata/v1/"

# product UUID you want to download a single band for
prod_id = "22e2fbfe-0aa7-423d-b0b5-df46527f03f5"

# parse the product name
nodes = api_session.get(urljoin(api_url, "Products('%s')/Nodes?$format=json" % prod_id)).json()
prod_name = nodes["d"]["results"][0]["Id"]

# parse the granule id
granules = api_session.get(urljoin(api_url, "Products('%s')/Nodes('%s')/Nodes('GRANULE')/Nodes?$format=json" % (prod_id, prod_name))).json()
granules["d"]["results"][0].keys()
gran_id = granules["d"]["results"][0]["Id"]


# parse the band names
bands = api_session.get(urljoin(api_url, "Products('%s')/Nodes('%s')/Nodes('GRANULE')/Nodes('%s')/Nodes('IMG_DATA')/Nodes?$format=json" % (prod_id, prod_name, gran_id))).json()
band_id = bands["d"]["results"][3]["Id"]  # element 3 is band 4, element 0 band 1

# construct the final image url
img_url = urljoin(api_url, "Products('%s')/Nodes('%s')/Nodes('GRANULE')/Nodes('%s')/Nodes('IMG_DATA')/Nodes('%s')/$value" % (prod_id, prod_name, gran_id, band_id))
4
  • The problem here is that a product can have multiple granules, etc. Also, each query to the odata API is painfully slow, at least in my system... If there were an expedite way of doing this a priori..
    – manatttta
    Commented Apr 5, 2017 at 13:00
  • If you want fast queries - query the AWS Sentinel-2 bucket. Dealing with multiple granules in a single product (i.e. the old product format) is possible as well - but you need to know which granules you are after. Apart from querying there is no way to convert the UUID to a product name.
    – Kersten
    Commented Apr 5, 2017 at 13:27
  • Very nice code. Also, aws transitioned to "requester pays" so it is really not that open any more. Commented Aug 22, 2019 at 14:31
  • 2
    Also the path changed a bit since 2017, and now the band files are inside a folder indicating the resolution. So there are 3 folders R10m, R20m, R60m, after IMG_DATA. You need to change the last urljoin to account for that: /Nodes('IMG_DATA')/Nodes('R10m')/Nodes?$format=json Commented Aug 22, 2019 at 14:34
3

I would recommend accessing data via the Sentinel-2 AWS Public Bucket.

Users can search for files using the index pages and access individual or related sets of files through the Sentinel Public bucket.

This service is designed for applications such as yours, where automated downloads are required. The bucket contains L1C (top of atmosphere reflectance) products like SciHub.

enter image description here

3
  • The problem is that for this API I need a way to convert LatLon WGS84 coordinates to MGRS coordinates, which I cannot do easily
    – manatttta
    Commented Mar 28, 2017 at 13:53
  • You may want to update your question with this pertinent information.
    – Aaron
    Commented Mar 28, 2017 at 13:55
  • Please note that this bucket has been turned into a requester pays bucket: forum.sentinel-hub.com/t/…
    – Chris P
    Commented Feb 19, 2019 at 21:52
0

The following link may be useful: Download only some of the channels of a Sentinel-1 product

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.