Commit b07d6ecd by Gencer

performance improvement

parent 4a914fa3
......@@ -12,7 +12,7 @@
# Author: Gencer Sumbul, http://www.user.tu-berlin.de/gencersumbul/
# Email: gencer.suembuel@tu-berlin.de
# Date: 14 May 2019
# Version: 1.0.1
# Version: 1.0.2
# Usage: extract_tile_names_and_links.py -u USERNAME -p PASSWORD
# -r ROOT_FOLDER -o OUT_PATH
......@@ -71,12 +71,14 @@ api = SentinelAPI(args.username, args.password)
dict_iter_items = (lambda d: d.iteritems()) if hasattr(
dict, 'iteritems') else (lambda d: iter(d.items()))
search_res = {}
# 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):
send_query = False
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,
......@@ -88,21 +90,38 @@ with open(os.path.realpath(args.out_path), 'w') as fp:
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
csv_writer.writerow([
patch_name, query_res[1]['filename'],
query_res[1]['link']
])
print('INFO: name and download link of the tile for patch',
patch_name, 'is ready')
if not MGRS_grid_id in search_res.keys():
search_res[MGRS_grid_id] = {}
send_query = True
if not acqusition_date in [
k for k, v in dict_iter_items(search_res[MGRS_grid_id])
]:
search_res[MGRS_grid_id][acqusition_date] = {}
send_query = True
if send_query:
# 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)))
search_res[MGRS_grid_id][acqusition_date][
'filename'
] = query_res[1]['filename']
search_res[MGRS_grid_id][acqusition_date][
'link'
] = query_res[1]['link']
filename = search_res[MGRS_grid_id][acqusition_date]['filename']
link = search_res[MGRS_grid_id][acqusition_date]['link']
# Write resulting tile name and download link to csv file
csv_writer.writerow([patch_name, filename, link])
print('INFO: name and donwload link of the tile for patch',
patch_name, 'is ready')
\ No newline at end of file
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