Translate filename of downloaded S2L2A data into dates

I have downloaded 2017-2019 Sentinel2 L2A data using WmsRequest() through the Python SDK, the folder name seems to be some encoding (see below). Is there a way to change the file name to dates while downloading? I did not find any parameters for this on the WmsRequest() documentation

1 Like

Folder names are hashes of request to the service, and are used by the sentinelhub-py as caches - if you’d make another (identical) request, by default it would be read from the cached data.

The information about what is in each response.tiff is also stored in the corresponding request.json. It holds the date/time parameter of the particular request.

1 Like

Here is a short script that will hopefully help you. It targets WmsRequest…

import pandas as pd
import glob
import json
import datetime as dt
from urllib.parse import unquote

def get_request_dt(request_file):
    with open(request_file, 'r') as req:
        request = json.load(req)
        url = unquote(request['url'])
        time_parameter = [t for t in url.split('&') if t.startswith('TIME=')][0]
        time = time_parameter.split('TIME=')[1].split('/')[0]
        return dt.datetime.strptime(time, "%Y-%m-%dT%H:%M:%S")

if __name__ == "__main__":
    folders = glob.glob('./data/*')
    dates = [get_request_dt(f'{folder}/request.json') for folder in folders]
    df = pd.DataFrame(zip(folders, dates), columns=['folder', 'datetime'])
    df.sort_values(by='datetime', ascending=True, inplace=True)
    print(df.head().to_markdown())
  

Such script would output something like

|    | folder                                  | datetime            |
|---:|:----------------------------------------|:--------------------|
|  2 | ./data/a986c7940bf6ed992b150b9e42ec13ae | 2017-12-15 07:12:03 |
|  9 | ./data/895642dd7e880aa150881f852409d454 | 2017-12-20 07:12:10 |
| 13 | ./data/d3eecd3d345e2bc98fd2966bd7d261a8 | 2017-12-25 07:12:04 |
| 41 | ./data/c259365d92977c1e0c347ec91d064dd5 | 2017-12-30 07:12:09 |
| 26 | ./data/886e355f522798ec8bbed6b03743f3b1 | 2018-01-04 07:12:06 |

You’ll have to adapt it to work with recursive folders etc, but I hope it will get you going.

2 Likes

Thank you very much for your response! I ended up writing a python script to read in request.json and scrape the date and then rename response.tiff with the date. But your code is pretty nice! i will save it for future documentation of the hashed file names!

Hi @batic
I am downloading weekly Sentinel-2 median mosaic using process-api and sh-py. I want the date range I gave for the median in the downloaded file name. Can you share an example script for this?