Commit 1e6d0d76 by Gencer Sümbül

parent 7a84a587
# -*- coding: utf-8 -*-
# This script creates a csv file that contains the Sentinel-2 1C level tile
# names and their download links of the BigEarthNet patches.
# The script is capable of searching corresponding tiles of the BigEarthNet
# patches in Copernicus Open Access Hub via sentinelsat API.
# python --help can be used to learn
# how to use this script.
# Author: Gencer Sumbul,
# Email:
# Date: 14 May 2019
# Version: 1.0.1
from __future__ import print_function
from datetime import datetime, timedelta
import json
import os
import argparse
import csv
parser = argparse.ArgumentParser(
description='This script extracts the tile names and ' +
'their download links for the BigEarthNet image patches')
parser.add_argument('-u', '--username', dest='username', help='username ' +
'of a Copernicus Open Access Hub account')
parser.add_argument('-p', '--password', dest='password', help='password ' +
'of a Copernicus Open Access Hub account')
parser.add_argument('-r', '--root_folder', dest='root_folder',
help='root folder path contains multiple patch folders')
parser.add_argument('-o', '--out_path', dest='out_path',
help='file path of the output csv file whose each ' +
'row contains patch name, tile name and download link')
args = parser.parse_args()
if (not args.username) or (not args.password) or (not args.out_path):
print('ERROR: need to specify -u, -p, -u and -o arguments')
print('INFO: For help, python --help')
elif args.root_folder:
if not os.path.exists(args.root_folder):
print('ERROR: folder', args.root_folder, 'does not exist')
print('INFO: need to download archive first')
root_folder = os.path.realpath(args.root_folder)
# Checks the existence of required python packages
from sentinelsat import SentinelAPI
except ImportError:
print('ERROR: please install sentinelsat')
print('INFO: for how to install sentinelsat, see ' +
from os import scandir
except ImportError:
from scandir import scandir
except ImportError:
print('ERROR: Either use Python 3 or ' +
'install scandir package for Python 2')
# Creates SentinelAPI object with username and password
api = SentinelAPI(args.username, args.password)
dict_iter_items = (lambda d: d.iteritems()) if hasattr(
dict, 'iteritems') else (lambda d: iter(d.items()))
# Creates resulting csv file
with open(os.path.realpath(args.out_path), 'w') as fp:
csv_writer = csv.writer(fp)
# Iterates over patch folders
for folder in scandir(root_folder):
patch_folder_path = os.path.realpath(folder.path)
patch_name = os.path.basename(patch_folder_path)
labels_metadata_path = os.path.join(patch_folder_path,
patch_name + '_labels_metadata.json')
# Reads labels_metadata json file
with open(labels_metadata_path, 'r') as f:
labels_metadata = json.load(f)
tile_identifiers = labels_metadata['tile_source'].split('_')
MGRS_grid_id = tile_identifiers[5][1:]
acqusition_date = tile_identifiers[2].split('T')[0]
acqusition_datetime = datetime.strptime(acqusition_date, '%Y%m%d')
# Prepares search query
query_kwargs = {
'platformname': 'Sentinel-2',
'producttype': 'S2MSI1C',
'tileid': MGRS_grid_id,
'date' : (acqusition_date, (
acqusition_datetime + timedelta(days=1)).strftime('%Y%m%d'))
# Searches within Copernicus Open Access Hub
query_res = next(dict_iter_items(api.query(**query_kwargs)))
# Write resulting tile name and download link to csv file
patch_name, query_res[1]['filename'],
print('INFO: name and donwload link of the tile for patch',
patch_name, 'is ready')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment