-
Notifications
You must be signed in to change notification settings - Fork 0
/
restructure.py
170 lines (130 loc) · 5.24 KB
/
restructure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#-------------------------------------------------------------------------------
# Name: Restructuring for SIAM and IQ.
# Purpose: This script takes the originally downloaded S2 file
# structure and accesses the individual tile directories
# located within the IMG_DATA folder. It then modifies the tile
# under the old naming convention (pre-06.12.16) to have the
# sensing/capture date in the title, rather than the day
# the data was processed, which can be days to months laterself.
# It then moves all tile directories to a given root folder,
# with the purpose of reducing overall path length for Windows.
# Finally, it renames tile folders under the new naming
# convention, adding "S2A" to the first part "L1C_*".
#
# Author: h.Augustin
#
# Created: 21.12.2016
# Updated: 09.01.2017
#
#-------------------------------------------------------------------------------
#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
import os
import fnmatch
import shutil
def tile_folders(root_folder):
'''This function delivers a list of all tile folders located within a
root directory, still within the original download file structure
from the S2 Data Hub. If tile folders are already located in the
root directory, there are not affected.'''
#
# Create and return list of tile folder paths.
#
tile_folders = []
for dirpath, dirnames, filenames in os.walk(root_folder, topdown=True):
for dirname in dirnames:
if (dirname == 'IMG_DATA'
and fnmatch.fnmatch(dirpath, '*GRANULE*')):
tile_folders.append(dirpath)
return tile_folders
def change_to_capture(tile_folders):
'''This function replaces the date of file creation with the date of
image capture in the tile folder name under the old naming
conventions (pre 06.12.16).'''
for folder in tile_folders:
#
# This needs to return capture date for old structure.
#
head, tile_file = os.path.split(folder)
#
# If it is the old version, then rename the tile package.
#
if tile_file.startswith("S2A_"):
#
# Return path and name of package folder, extracting date and time
# of image capture.
#
package = os.path.dirname(os.path.dirname(folder))
head, tail = os.path.split(package)
package_parts = tail.split("_")
capture_info = package_parts[7]
#
# Replace the creation date and time with the capture date and time.
# Note: capture date without time is capture_info[1:-7]
#
file_parts = tile_file.split("_")
file_parts[7] = capture_info[1:]
new_name = "_".join(file_parts)
new_path = os.path.join(os.path.dirname(folder), new_name)
os.rename(folder, new_path)
def move_and_meta(tile_folders, root_folder, delete=False):
'''This function moves tile folders to the root directory, and either
deletes the original file structure, or moves them into a folder
in the root directory called metadata.'''
for folder in tile_folders:
#
# Move tile folder to temp folder.
#
shutil.move(folder, root_folder)
if delete is True:
#
# Remove original file structure.
#
shutil.rmtree(os.path.dirname(os.path.dirname(folder)))
else:
#
# Move original file structure without images to metadata folder.
#
metadata_dir = os.path.join(root_folder, 'metadata')
if not(os.path.exists(metadata_dir)):
os.mkdir(metadata_dir)
shutil.move(os.path.dirname(os.path.dirname(folder)), metadata_dir)
def new_to_S2A(root_folder):
''' This function adds the prefix S2A to the first part of the tile
folder namescreated under the new naming conventions (post 06.12.16)'''
#
# Rename new L1C folders to start with S2A_
#
for fn in os.listdir(root_folder):
if not os.path.isdir(os.path.join(root_folder, fn)):
continue # Not a directory.
if fn.startswith('L1C'):
new_fn = 'S2A{}'.format(fn)
os.rename(os.path.join(root_folder, fn),
os.path.join(root_folder, new_fn))
if __name__ == "__main__":
cwd = os.getcwd()
root_folder = os.path.join(cwd, 'tempS2')
# root_folder = 'C:\\tempS2'
#
# List all original tile folders.
#
orig_folders = tile_folders(root_folder)
#
# Modify old tile name directories (pre-06.12.16) to capture date and time
# instead of creation date and time.
#
change_to_capture(orig_folders)
#
# List all tile folders after modification.
#
mod_folders = tile_folders(root_folder)
#
# Move folders to root directory, and either delete the rest, or move into
# a metadata folder in the root directory for later reference.
#
move_and_meta(mod_folders, root_folder)
#
# Modify new tile name directories (post-06.12.16) to start with S2A.
#
new_to_S2A(root_folder)